• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

llnl-asr / dftracer-utils / 38033748054

10 Oct 2026 07:14AM UTC coverage: 62.608% (+5.2%) from 57.391%
38033748054

push

github

rayandrew
ci: shard the test job

The coverage jobs ran 64 minutes of C++ tests in one process. Each shard
runs every n-th test with ctest -I, the Python tests and the type check
run in shard 1 only, and each shard uploads its own coverage report to
Coveralls, which merges the parallel uploads.

117733 of 235605 branches covered (49.97%)

Branch coverage included in aggregate %.

102379 of 115969 relevant lines covered (88.28%)

3014114.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.29
/src/dftracer/utils/dataframe/kernels/string_ops.cpp
1
#include <dftracer/utils/core/common/hash/fnv1a.h>
2
#include <dftracer/utils/core/common/hash/hex64.h>
3
#include <dftracer/utils/dataframe/abi.h>
4
#include <dftracer/utils/dataframe/internal/column_data.h>
5
#include <dftracer/utils/dataframe/internal/string_reader.h>
6
#include <dftracer/utils/dataframe/internal/string_simd.h>
7
#include <dftracer/utils/dataframe/internal/varwidth_offsets.h>
8
#include <dftracer/utils/dataframe/kernels/string_ops.h>
9
#include <dftracer/utils/dataframe/parallel.h>
10
#include <dftracer/utils/duql/pattern_engine.h>
11
#include <dftracer/utils/duql/substr_simd.h>
12

13
#include <algorithm>
14
#include <bit>
15
#include <cstdint>
16
#include <cstring>
17
#include <limits>
18
#include <mutex>
19
#include <optional>
20
#include <string>
21
#include <string_view>
22
#include <type_traits>
23
#include <unordered_set>
24
#include <vector>
25

26
// Highway static dispatch (baseline target): only the two
27
// naturally-vectorizable string ops use it - str_len_bytes (adjacent int32
28
// offset difference) and the ASCII case folds (byte compare + conditional add).
29
// Every other op is variable-length per row and is legitimately scalar.
30
#include <hwy/highway.h>
31
namespace hn = hwy::HWY_NAMESPACE;
32

33
namespace duql = dftracer::utils::duql;
34
using duql::detail::substr_find;
35

36
namespace dftracer::utils::dataframe {
37

38
Series str_eq(const Series& v, std::string_view rhs) {
6 ✔
39
    return Series{dftu_series_str_eq(v.handle(), rhs.data(),
9 ✔
40
                                     static_cast<std::int32_t>(rhs.size()))};
9 ✔
41
}
42
Series str_contains(const Series& v, std::string_view needle) {
2 ✔
43
    return Series{dftu_series_str_contains(
1 ✔
44
        v.handle(), needle.data(), static_cast<std::int32_t>(needle.size()))};
2 ✔
45
}
46
Series str_starts_with(const Series& v, std::string_view prefix) {
2 ✔
47
    return Series{dftu_series_str_starts_with(
1 ✔
48
        v.handle(), prefix.data(), static_cast<std::int32_t>(prefix.size()))};
2 ✔
49
}
50

51
}  // namespace dftracer::utils::dataframe
52

53
namespace {
54
using dftracer::utils::dataframe::Buffer;
55
using dftracer::utils::dataframe::buffer_bytes;
56
using dftracer::utils::dataframe::Encoding;
57
using dftracer::utils::dataframe::is_wide_offset_type;
58
using dftracer::utils::dataframe::narrow_varwidth_type;
59
using dftracer::utils::dataframe::offsets_of;
60
using dftracer::utils::dataframe::parallel_backend_installed;
61
using dftracer::utils::dataframe::parallel_for;
62
using dftracer::utils::dataframe::string_int64;
63
using dftracer::utils::dataframe::string_mask;
64
using dftracer::utils::dataframe::TypeId;
65

66
// String or Binary, at either offset width (narrow_varwidth_type folds the
67
// Large variants into the same case).
68
bool is_string_kind(TypeId t) {
13,062 ✔
69
    const TypeId n = narrow_varwidth_type(t);
13,062 ✔
70
    return n == TypeId::String || n == TypeId::Binary;
13,062 ✔
71
}
72

73
// A VIEW or CHUNKED column, or a DICTIONARY over a non-FLAT base, is read
74
// through its FLAT copy.
75
bool needs_flat_copy(const dftu_series& v) {
1,229 ✔
76
    return v.encoding == Encoding::View || v.is_chunked() ||
2,458 !
77
           (v.encoding == Encoding::Dictionary && v.child() &&
1,229 !
78
            v.child()->encoding != Encoding::Flat);
821 !
79
}
80

81
// Per-row byte reader over a String/Binary(/Large) column that transparently
82
// resolves a DICTIONARY input (row i -> dictionary entry codes[i]). Reads its
83
// offsets at width `Off`. Nullness comes from the top-level validity bitmap
84
// in both encodings.
85
template <class Off>
86
class RowReader {
87
   public:
88
    explicit RowReader(const dftu_series* v) : v_(v) {
1,924 ✔
89
        if (!is_string_kind(v->type)) return;
1,516 !
90
        if (needs_flat_copy(*v)) {
1,229 !
91
            own_.reset(dftu_series_materialize(v));
×
92
            if (!own_) return;
×
93
            v_ = v = own_.get();
×
94
        }
95
        if (v->encoding == Encoding::Flat && offsets_of<Off>(*v) && v->data) {
1,229 !
96
            off_ = reinterpret_cast<const Off*>(offsets_of<Off>(*v)->data());
1,229 !
97
            data_ = reinterpret_cast<const char*>(v->data->data());
1,229 ✔
98
            ok_ = true;
1,229 ✔
99
        } else if (v->encoding == Encoding::Dictionary && v->child() &&
408 !
100
                   offsets_of<Off>(*v->child()) && v->child()->data &&
×
101
                   v->data) {
102
            off_ = reinterpret_cast<const Off*>(
×
103
                offsets_of<Off>(*v->child())->data());
×
104
            data_ = reinterpret_cast<const char*>(v->child()->data->data());
×
105
            codes_ = reinterpret_cast<const std::int32_t*>(v->data->data());
×
106
            ok_ = true;
×
107
        }
108
    }
816 ✔
109
    bool ok() const { return ok_; }
1,516 ✔
110
    bool is_null(std::int64_t i) const {
5,847 ✔
111
        if (!v_->validity) return false;
5,847 ✔
112
        const std::uint8_t* bm = v_->validity->data();
5,191 ✔
113
        return ((bm[i >> 3] >> (i & 7)) & 1) == 0;
5,191 ✔
114
    }
263 ✔
115
    std::string_view at(std::int64_t i) const {
5,012 ✔
116
        std::int64_t k = codes_ ? codes_[i] : i;
5,012 !
117
        return std::string_view(
9,764 ✔
118
            data_ + off_[k], static_cast<std::size_t>(off_[k + 1] - off_[k]));
5,012 ✔
119
    }
120

121
   private:
122
    std::shared_ptr<dftu_series> own_;
123
    const dftu_series* v_;
124
    const Off* off_ = nullptr;
408 ✔
125
    const char* data_ = nullptr;
408 ✔
126
    const std::int32_t* codes_ = nullptr;
408 ✔
127
    bool ok_ = false;
408 ✔
128
};
129

130
// Int64 output column with the same length/validity as `v` (nulls preserved).
131
dftu_series* make_i64(const dftu_series* v,
161 ✔
132
                      const std::vector<std::int64_t>& vals) {
133
    auto* out = new dftu_series();
161 ✔
134
    out->type = TypeId::Int64;
160 ✔
135
    out->encoding = Encoding::Flat;
160 ✔
136
    out->length = v->length;
160 ✔
137
    out->null_count = v->null_count;
160 ✔
138
    out->validity = v->validity;
160 ✔
139
    std::size_t bytes = buffer_bytes(TypeId::Int64, v->length);
160 ✔
140
    out->data = Buffer::allocate(bytes);
160 ✔
141
    if (bytes != 0) std::memcpy(out->data->data(), vals.data(), bytes);
161 ✔
142
    return out;
160 ✔
143
}
144

145
// UInt64 output column with the same length as `v`. `validity` is a fresh
146
// Arrow-layout bitmap the caller owns, or NULL to share `v`'s nulls.
147
dftu_series* make_u64(const dftu_series* v,
59 ✔
148
                      const std::vector<std::uint64_t>& vals,
149
                      const std::uint8_t* validity) {
150
    auto* out = new dftu_series();
59 ✔
151
    out->type = TypeId::Uint64;
59 ✔
152
    out->encoding = Encoding::Flat;
59 ✔
153
    out->length = v->length;
59 ✔
154
    std::size_t bytes = buffer_bytes(TypeId::Uint64, v->length);
59 ✔
155
    out->data = Buffer::allocate(bytes);
59 ✔
156
    if (bytes != 0) std::memcpy(out->data->data(), vals.data(), bytes);
59 ✔
157
    if (validity) {
59 ✔
158
        std::size_t vb = static_cast<std::size_t>((v->length + 7) / 8);
26 ✔
159
        out->validity = Buffer::allocate(vb);
26 ✔
160
        if (vb != 0) std::memcpy(out->validity->data(), validity, vb);
26 ✔
161
    } else {
4 ✔
162
        out->null_count = v->null_count;
33 ✔
163
        out->validity = v->validity;
33 ✔
164
    }
165
    return out;
59 ✔
166
}
167

168
// Flat String output column from one string per row (size == v->length); shares
169
// v's validity so null rows stay null (their bytes are ignored). Always
170
// narrow (int32 offsets): a derived per-row transform's total size is its own
171
// fresh sizing question, not a width the source column forces on it. nullptr
172
// when the bytes pass INT32_MAX.
173
dftu_series* make_string(const dftu_series* v,
146 ✔
174
                         const std::vector<std::string>& parts) {
175
    std::size_t total = 0;
146 ✔
176
    for (const std::string& p : parts) total += p.size();
729 ✔
177
    if (!fits_int32_offsets(total)) return nullptr;
146 ✔
178
    auto* out = new dftu_series();
146 ✔
179
    out->type = TypeId::String;
146 ✔
180
    out->encoding = Encoding::Flat;
146 ✔
181
    out->length = v->length;
146 ✔
182
    out->null_count = v->null_count;
146 ✔
183
    out->validity = v->validity;
146 ✔
184
    std::size_t off_bytes =
146 ✔
185
        static_cast<std::size_t>(v->length + 1) * sizeof(std::int32_t);
146 ✔
186
    out->offsets = Buffer::allocate(off_bytes);
146 ✔
187
    out->data = Buffer::allocate(total);
146 ✔
188
    std::int32_t* od = reinterpret_cast<std::int32_t*>(out->offsets->data());
146 ✔
189
    char* bd =
13 ✔
190
        total != 0 ? reinterpret_cast<char*>(out->data->data()) : nullptr;
146 ✔
191
    std::int32_t pos = 0;
146 ✔
192
    od[0] = 0;
146 ✔
193
    for (std::int64_t i = 0; i < v->length; ++i) {
729 ✔
194
        const std::string& p = parts[static_cast<std::size_t>(i)];
583 ✔
195
        if (!p.empty()) std::memcpy(bd + pos, p.data(), p.size());
583 ✔
196
        pos += static_cast<std::int32_t>(p.size());
583 ✔
197
        od[i + 1] = pos;
583 ✔
198
    }
29 ✔
199
    return out;
146 ✔
200
}
13 ✔
201

202
// ---- SIMD helpers --------------------------------------------------------
203

204
// out[i] = off[i+1] - off[i], widened to int64. Highway over the int32 offsets.
205
void len_bytes_simd(const std::int32_t* off, std::int64_t* out, std::size_t n) {
67 ✔
206
    const hn::ScalableTag<std::int64_t> d64;
207
    const hn::Rebind<std::int32_t, decltype(d64)> d32;
208
    const std::size_t lanes = hn::Lanes(d64);
67 ✔
209
    std::size_t i = 0;
67 ✔
210
    for (; i + lanes <= n; i += lanes) {
2,171 ✔
211
        const auto a = hn::LoadU(d32, off + i + 1);
2,104 !
212
        const auto b = hn::LoadU(d32, off + i);
2,104 !
213
        hn::StoreU(hn::PromoteTo(d64, hn::Sub(a, b)), d64, out + i);
2,104 !
214
    }
494 ✔
215
    for (; i < n; ++i) out[i] = static_cast<std::int64_t>(off[i + 1] - off[i]);
84 ✔
216
}
67 ✔
217

218
// UTF-8 character count of a byte range: every byte that is not a continuation
219
// byte ((c & 0xC0) != 0x80) starts a character. Highway compares a block and
220
// folds the mask with CountTrue.
221
std::int64_t count_char_starts(const std::uint8_t* p, std::size_t len) {
548,718 ✔
222
    const hn::ScalableTag<std::uint8_t> d;
223
    const auto vc0 = hn::Set(d, 0xC0);
548,718 ✔
224
    const auto v80 = hn::Set(d, 0x80);
548,750 ✔
225
    const std::size_t lanes = hn::Lanes(d);
548,748 ✔
226
    std::int64_t count = 0;
548,724 ✔
227
    std::size_t i = 0;
548,724 ✔
228
    for (; i + lanes <= len; i += lanes) {
830,987 ✔
229
        const auto m = hn::Ne(hn::And(hn::LoadU(d, p + i), vc0), v80);
282,263 !
230
        count += static_cast<std::int64_t>(hn::CountTrue(d, m));
282,263 !
231
    }
435 ✔
232
    for (; i < len; ++i)
2,714,323 ✔
233
        if ((p[i] & 0xC0) != 0x80) ++count;
2,165,599 ✔
234
    return count;
548,724 ✔
235
}
236

237
// ASCII case fold of a byte buffer: `add` is +32 (upper->lower) or -32
238
// (lower->upper); `lo`/`hi` bound the source case. Non-matching bytes pass
239
// through unchanged (ASCII-only fold).
240
void ascii_fold_simd(const std::uint8_t* in, std::uint8_t* out, std::size_t n,
896 ✔
241
                     std::uint8_t lo, std::uint8_t hi, int add) {
242
    const hn::ScalableTag<std::uint8_t> d;
243
    const auto vlo = hn::Set(d, lo);
896 ✔
244
    const auto vhi = hn::Set(d, hi);
896 ✔
245
    const auto vadd = hn::Set(d, static_cast<std::uint8_t>(add));
896 ✔
246
    const std::size_t lanes = hn::Lanes(d);
896 ✔
247
    std::size_t i = 0;
896 ✔
248
    for (; i + lanes <= n; i += lanes) {
6,674,990 ✔
249
        const auto v = hn::LoadU(d, in + i);
6,674,094 ✔
250
        const auto m = hn::And(hn::Ge(v, vlo), hn::Le(v, vhi));
6,674,094 !
251
        hn::StoreU(hn::IfThenElse(m, hn::Add(v, vadd), v), d, out + i);
6,674,094 !
252
    }
3,327,119 ✔
253
    for (; i < n; ++i) {
6,471 ✔
254
        const std::uint8_t c = in[i];
5,575 ✔
255
        out[i] = (c >= lo && c <= hi) ? static_cast<std::uint8_t>(c + add) : c;
5,575 ✔
256
    }
2,350 ✔
257
}
896 ✔
258

259
// Fold that keeps offsets/validity and rewrites only the (byte-length
260
// preserving) data buffer. Fast path SIMD for a FLAT input, preserving the
261
// source's own offset width and buffer (no copy of the offsets); a
262
// DICTIONARY input falls back through make_string (always narrow) per row.
263
template <class Off>
264
dftu_series* ascii_fold_w(const dftu_series* v, std::uint8_t lo,
960 ✔
265
                          std::uint8_t hi, int add) {
266
    RowReader<Off> r(v);
960 !
267
    if (!r.ok()) return nullptr;
960 !
268
    if (v->encoding == Encoding::Flat) {
904 !
269
        auto* out = new dftu_series();
904 !
270
        out->type = v->type;
904 ✔
271
        out->encoding = Encoding::Flat;
904 ✔
272
        out->length = v->length;
904 ✔
273
        out->null_count = v->null_count;
904 ✔
274
        out->validity = v->validity;
904 ✔
275
        offsets_of<Off>(*out) = offsets_of<Off>(*v);  // byte lengths unchanged
904 ✔
276
        const std::size_t data_len = v->data ? v->data->size() : 0;
904 !
277
        out->data = Buffer::allocate(data_len);
904 !
278
        if (data_len != 0)
904 !
279
            ascii_fold_simd(v->data->data(), out->data->data(), data_len, lo,
1,270 !
280
                            hi, add);
374 ✔
281
        return out;
904 ✔
282
    }
283
    std::vector<std::string> parts(static_cast<std::size_t>(v->length));
×
284
    for (std::int64_t i = 0; i < v->length; ++i) {
×
285
        if (r.is_null(i)) continue;
×
286
        std::string s(r.at(i));
×
287
        for (char& c : s)
×
288
            if (static_cast<std::uint8_t>(c) >= lo &&
×
289
                static_cast<std::uint8_t>(c) <= hi)
×
290
                c = static_cast<char>(c + add);
×
291
        parts[static_cast<std::size_t>(i)] = std::move(s);
×
292
    }
293
    return make_string(v, parts);
×
294
}
960 ✔
295

296
dftu_series* ascii_fold(const dftu_series* v, std::uint8_t lo, std::uint8_t hi,
960 ✔
297
                        int add) {
298
    return is_wide_offset_type(v->type)
960 !
299
               ? ascii_fold_w<std::int64_t>(v, lo, hi, add)
586 !
300
               : ascii_fold_w<std::int32_t>(v, lo, hi, add);
960 ✔
301
}
302

303
// Per-row string transform (scalar) at offset width `Off`; null rows pass
304
// through as null. Output is always narrow (see make_string).
305
template <class Off, class Fn>
306
dftu_series* string_transform_w(const dftu_series* v, Fn fn) {
286 ✔
307
    RowReader<Off> r(v);
286 !
308
    if (!r.ok()) return nullptr;
286 !
309
    std::vector<std::string> parts(static_cast<std::size_t>(v->length));
146 !
310
    for (std::int64_t i = 0; i < v->length; ++i) {
729 !
311
        if (r.is_null(i)) continue;
583 !
312
        parts[static_cast<std::size_t>(i)] = fn(r.at(i));
488 !
313
    }
29 ✔
314
    return make_string(v, parts);
146 !
315
}
286 ✔
316

317
template <class Fn>
318
dftu_series* string_transform(const dftu_series* v, Fn fn) {
286 ✔
319
    return is_wide_offset_type(v->type)
286 !
320
               ? string_transform_w<std::int64_t>(v, fn)
273 !
321
               : string_transform_w<std::int32_t>(v, fn);
286 ✔
322
}
323

324
bool is_ascii_ws(char c) {
426 ✔
325
    return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
426 !
326
           c == '\v';
410 ✔
327
}
328

329
std::string_view lstrip_ws(std::string_view s) {
182 ✔
330
    std::size_t b = 0;
182 ✔
331
    while (b < s.size() && is_ascii_ws(s[b])) ++b;
217 ✔
332
    return s.substr(b);
182 ✔
333
}
334
std::string_view rstrip_ws(std::string_view s) {
182 ✔
335
    std::size_t e = s.size();
182 ✔
336
    while (e > 0 && is_ascii_ws(s[e - 1])) --e;
211 ✔
337
    return s.substr(0, e);
182 ✔
338
}
339

340
std::string replace_literal(std::string_view s, std::string_view pat,
228 ✔
341
                            std::string_view repl, bool all) {
342
    // An empty pattern has no well-defined occurrence; leave the row unchanged
343
    // (also avoids a zero-advance loop in the all-occurrences case).
344
    if (pat.empty()) return std::string(s);
228 !
345
    std::string out;
228 ✔
346
    std::size_t pos = 0;
228 ✔
347
    while (true) {
26 ✔
348
        std::size_t hit = s.find(pat, pos);
309 ✔
349
        if (hit == std::string_view::npos) break;
309 ✔
350
        out.append(s.substr(pos, hit - pos));
112 !
351
        out.append(repl);
112 !
352
        pos = hit + pat.size();
112 ✔
353
        if (!all) break;
112 ✔
354
    }
70 ✔
355
    out.append(s.substr(pos));
228 !
356
    return out;
228 ✔
357
}
243 !
358

359
// The strings whose regex match reached its work limit; their rows turn null.
360
class LimitedRows {
361
   public:
362
    void add(std::string_view s) {
×
363
        std::lock_guard<std::mutex> lock(mu_);
×
364
        rows_.emplace(s);
×
365
    }
×
366

367
    // `out` with every row of `v` whose string is listed made null.
368
    dftu_series* apply(dftu_series* out, const dftu_series* v) const {
529 ✔
369
        if (!out || rows_.empty()) return out;
529 !
370
        dftu_series* ok = string_mask(v, [this](std::string_view s) {
×
371
            return !rows_.contains(std::string(s));
×
372
        });
373
        if (!ok) return out;
×
374
        const std::size_t bytes = buffer_bytes(TypeId::Bool, v->length);
×
375
        auto valid = Buffer::allocate(bytes);
×
376
        for (std::size_t k = 0; k < bytes; ++k) {
×
377
            const std::uint8_t have =
378
                out->validity ? out->validity->data()[k] : std::uint8_t{0xFF};
×
379
            valid->data()[k] =
×
380
                static_cast<std::uint8_t>(ok->data->data()[k] & have);
×
381
        }
382
        std::int64_t nulls = 0;
×
383
        for (std::int64_t i = 0; i < v->length; ++i)
×
384
            if (!((valid->data()[i >> 3] >> (i & 7)) & 1)) ++nulls;
×
385
        delete ok;
×
386
        out->validity = std::move(valid);
×
387
        out->null_count = nulls;
×
388
        return out;
×
389
    }
172 ✔
390

391
   private:
392
    std::mutex mu_;
393
    std::unordered_set<std::string> rows_;
394
};
395

396
// Bool mask of `p` over `v`; a row whose match reached the work limit is null.
397
dftu_series* pattern_predicate(const dftu_series* v,
843 ✔
398
                               const duql::CompiledPattern& p) {
399
    DFTU_PER_CHUNK(v, pattern_predicate, p);
1,080 !
400
    // A literal pattern runs inline, with no call per row.
401
    if (const auto t = duql::literal_test(p); t && !t->icase) {
781 ✔
402
        const std::string_view lit = t->text;
267 ✔
403
        switch (t->op) {
267 ✔
404
            case duql::LiteralTest::Op::EQUALS:
63 ✔
405
                return string_mask(
68 !
406
                    v, [lit](std::string_view s) { return s == lit; });
306 ✔
407
            case duql::LiteralTest::Op::STARTS_WITH:
67 ✔
408
                return string_mask(v, [lit](std::string_view s) {
1,718 !
409
                    return s.starts_with(lit);
9,187 ✔
410
                });
67 ✔
411
            case duql::LiteralTest::Op::ENDS_WITH:
4 ✔
412
                return string_mask(
7 !
413
                    v, [lit](std::string_view s) { return s.ends_with(lit); });
202 ✔
414
            case duql::LiteralTest::Op::CONTAINS:
33 ✔
415
                return string_mask(v, [lit](std::string_view s) {
1,351 !
416
                    return substr_find(
5,190 ✔
417
                               s.data(), static_cast<std::int64_t>(s.size()),
2,595 ✔
418
                               lit.data(),
1,290 ✔
419
                               static_cast<std::int64_t>(lit.size())) >= 0;
5,190 ✔
420
                });
33 ✔
421
        }
422
    }
423
    // Case-sensitive globs run inline too; SEGMENTS search long strings.
424
    if (!p.icase && p.kind == duql::detail::Kind::GLOB)
514 ✔
425
        return string_mask(v, [toks = p.glob](std::string_view s) {
84 !
426
            return duql::detail::glob_match_t<false, true>(toks, s);
139 ✔
427
        });
6 ✔
428
    if (!p.icase && p.kind == duql::detail::Kind::SEGMENTS)
503 ✔
429
        return string_mask(v, [&p, toks = p.glob](std::string_view s) {
7 !
430
            return s.size() < duql::detail::SIMD_MIN_HAY
8 !
431
                       ? duql::detail::glob_match_t<false, false>(toks, s)
8 !
432
                       : duql::detail::segments_match(p, s);
4 ✔
433
        });
1 ✔
434
    LimitedRows limited;
832 ✔
435
    dftu_series* out = string_mask(v, [&](std::string_view s) {
4,523 !
436
        const auto r = duql::match(p, s);
19,653 ✔
437
        if (r == duql::MatchResult::LIMIT) limited.add(s);
19,652 ✔
438
        return r == duql::MatchResult::YES;
19,652 ✔
439
    });
440
    return limited.apply(out, v);
500 !
441
}
300 ✔
442

443
}  // namespace
444

445
namespace dftracer::utils::dataframe {
446

447
Series str_pattern(const Series& v, const duql::CompiledPattern& p) {
16 ✔
448
    if (!v.valid()) return {};
16 !
449
    return Series{pattern_predicate(v.handle(), p)};
16 ✔
450
}
2 ✔
451

452
}  // namespace dftracer::utils::dataframe
453

454
namespace {
455

456
template <class Off>
457
dftu_series* regex_replace_w(const dftu_series* v,
104 ✔
458
                             const duql::CompiledPattern& re,
459
                             const duql::Substitution& sub) {
460
    RowReader<Off> r(v);
104 !
461
    if (!r.ok()) return nullptr;
104 !
462
    const std::size_t n = static_cast<std::size_t>(v->length);
76 ✔
463
    std::vector<std::int32_t> offsets(n + 1, 0);
76 !
464
    std::string data;
76 ✔
465
    std::string buf;
76 ✔
466
    std::vector<std::uint8_t> valid((n + 7) / 8, 0);
76 !
467
    bool any_null = false;
76 ✔
468
    for (std::size_t i = 0; i < n; ++i) {
592 ✔
469
        bool hit = false;
516 ✔
470
        if (!r.is_null(static_cast<std::int64_t>(i))) {
516 !
471
            const std::string_view s = r.at(static_cast<std::int64_t>(i));
456 !
472
            if (duql::regex_replace(re, sub, s, buf) !=
456 !
473
                duql::MatchResult::LIMIT) {
474
                data.append(buf);
456 !
475
                hit = true;
456 ✔
476
                if (!fits_int32_offsets(data.size())) return nullptr;
456 !
477
            }
157 ✔
478
        }
157 ✔
479
        if (hit)
516 !
480
            valid[i >> 3] |= static_cast<std::uint8_t>(1u << (i & 7));
456 ✔
481
        else
482
            any_null = true;
60 ✔
483
        offsets[i + 1] = static_cast<std::int32_t>(data.size());
516 ✔
484
    }
157 ✔
485
    return dftu_series_new_string(DFTU_TYPE_STRING, offsets.data(), data.data(),
139 !
486
                                  static_cast<int64_t>(n),
7 ✔
487
                                  any_null ? valid.data() : nullptr);
132 !
488
}
104 ✔
489

490
dftu_series* regex_replace_flat(const dftu_series* v,
104 ✔
491
                                const duql::CompiledPattern& re,
492
                                const duql::Substitution& sub) {
493
    return is_wide_offset_type(v->type)
104 !
494
               ? regex_replace_w<std::int64_t>(v, re, sub)
97 !
495
               : regex_replace_w<std::int32_t>(v, re, sub);
104 ✔
496
}
497

498
}  // namespace
499

500
namespace dftracer::utils::dataframe {
501

502
Series str_regex_replace(const Series& v, const duql::CompiledPattern& p,
13 ✔
503
                         const duql::Substitution& sub) {
504
    if (!v.valid()) return {};
13 !
505
    const Series flat = v.materialize();
13 !
506
    return Series{regex_replace_flat(flat.handle(), p, sub)};
13 !
507
}
13 ✔
508

509
}  // namespace dftracer::utils::dataframe
510

511
namespace {
512

513
duql::PatternPtr compiled(duql::PatternResult r) {
789 ✔
514
    return r ? std::move(*r) : nullptr;
789 ✔
515
}
516

517
std::string_view pattern_text(const char* pattern, int32_t len) {
789 ✔
518
    return {pattern, static_cast<std::size_t>(len)};
789 ✔
519
}
520

521
}  // namespace
522

523
dftu_series* dftu_series_str_eq(const dftu_series* v, const char* rhs,
205 ✔
524
                                int32_t rhs_len) {
525
    std::string_view r(rhs, static_cast<std::size_t>(rhs_len));
205 ✔
526
    return string_mask(v, [r](std::string_view s) { return s == r; });
5,650 !
527
}
528

529
dftu_series* dftu_series_str_contains(const dftu_series* v, const char* needle,
272 ✔
530
                                      int32_t needle_len) {
531
    std::string_view n(needle, static_cast<std::size_t>(needle_len));
272 ✔
532
    return string_mask(v, [n](std::string_view s) {
268,944 !
533
        return substr_find(s.data(), static_cast<std::int64_t>(s.size()),
1,680,426 ✔
534
                           n.data(), static_cast<std::int64_t>(n.size())) >= 0;
2,286,154 ✔
535
    });
426 ✔
536
}
537

538
dftu_series* dftu_series_str_starts_with(const dftu_series* v,
146 ✔
539
                                         const char* prefix,
540
                                         int32_t prefix_len) {
541
    std::string_view p(prefix, static_cast<std::size_t>(prefix_len));
146 ✔
542
    return string_mask(v, [p](std::string_view s) { return s.starts_with(p); });
5,793 !
543
}
544

545
dftu_series* dftu_series_str_ends_with(const dftu_series* v, const char* suffix,
122 ✔
546
                                       int32_t suffix_len) {
547
    std::string_view p(suffix, static_cast<std::size_t>(suffix_len));
122 ✔
548
    return string_mask(v, [p](std::string_view s) { return s.ends_with(p); });
2,894 !
549
}
550

551
dftu_series* dftu_series_str_matches(const dftu_series* v, const char* pattern,
150 ✔
552
                                     int32_t pattern_len) {
553
    const auto p = compiled(duql::compile_regex(
150 !
554
        pattern_text(pattern, pattern_len), false, /*whole=*/true));
150 !
555
    if (!p) return nullptr;
150 ✔
556
    return pattern_predicate(v, *p);
150 !
557
}
150 ✔
558

559
namespace {
560

561
// The capture `group` of the first `re` match in each row, null where the row
562
// is null or does not match: a String column with its own validity.
563
template <class Off>
564
dftu_series* str_extract_w(const dftu_series* v,
16 ✔
565
                           const duql::CompiledPattern& re, std::size_t group) {
566
    RowReader<Off> r(v);
16 !
567
    if (!r.ok()) return nullptr;
16 !
568
    const std::size_t n = static_cast<std::size_t>(v->length);
16 ✔
569
    std::vector<std::int32_t> offsets(n + 1, 0);
16 !
570
    std::string data;
16 ✔
571
    std::vector<std::uint8_t> valid((n + 7) / 8, 0);
16 !
572
    bool any_null = false;
16 ✔
573
    for (std::size_t i = 0; i < n; ++i) {
2,213 ✔
574
        bool hit = false;
2,197 ✔
575
        if (!r.is_null(static_cast<std::int64_t>(i))) {
2,197 ✔
576
            const std::string_view s = r.at(static_cast<std::int64_t>(i));
1,882 ✔
577
            std::string_view m;
1,882 ✔
578
            if (duql::extract(re, s, group, m) == duql::MatchResult::YES) {
1,882 !
579
                data.append(m);
1,580 !
580
                hit = true;
1,580 ✔
581
                if (!fits_int32_offsets(data.size())) return nullptr;
1,580 !
582
            }
7 ✔
583
        }
14 ✔
584
        if (hit)
2,197 ✔
585
            valid[i >> 3] |= static_cast<std::uint8_t>(1u << (i & 7));
1,580 ✔
586
        else
587
            any_null = true;
617 ✔
588
        offsets[i + 1] = static_cast<std::int32_t>(data.size());
2,197 ✔
589
    }
15 ✔
590
    return dftu_series_new_string(DFTU_TYPE_STRING, offsets.data(), data.data(),
31 !
591
                                  static_cast<int64_t>(n),
4 ✔
592
                                  any_null ? valid.data() : nullptr);
27 !
593
}
16 ✔
594

595
}  // namespace
596

597
dftu_series* dftu_series_str_extract(const dftu_series* v, const char* pattern,
77 ✔
598
                                     int32_t pattern_len, int64_t group) {
599
    if (!v || !pattern || group < 0) return nullptr;
77 !
600
    DFTU_FLAT_OPERAND(
77 !
601
        v, flat_v,
602
        dftu_series_str_extract(flat_v, pattern, pattern_len, group));
603
    const auto re = compiled(
4 !
604
        duql::compile_regex(pattern_text(pattern, pattern_len), false));
63 !
605
    if (!re) return nullptr;
63 ✔
606
    const auto g = static_cast<std::size_t>(group);
62 ✔
607
    if (g > duql::capture_count(*re)) return nullptr;
62 !
608
    return is_wide_offset_type(v->type)
20 !
609
               ? str_extract_w<std::int64_t>(v, *re, g)
12 !
610
               : str_extract_w<std::int32_t>(v, *re, g);
16 !
611
}
63 ✔
612

613
dftu_series* dftu_series_str_regex_replace(const dftu_series* v,
116 ✔
614
                                           const char* pattern,
615
                                           int32_t pattern_len, const char* to,
616
                                           int32_t to_len) {
617
    if (!v || !pattern || !to) return nullptr;
116 !
618
    DFTU_FLAT_OPERAND(v, flat_v,
116 !
619
                      dftu_series_str_regex_replace(flat_v, pattern,
620
                                                    pattern_len, to, to_len));
621
    const auto re = compiled(
6 !
622
        duql::compile_regex(pattern_text(pattern, pattern_len), false));
102 !
623
    if (!re) return nullptr;
102 ✔
624
    auto sub = duql::compile_substitution(
5 !
625
        *re, std::string_view(to, static_cast<std::size_t>(to_len)));
98 !
626
    if (!sub) return nullptr;
98 ✔
627
    return regex_replace_flat(v, *re, *sub);
91 !
628
}
102 ✔
629

630
dftu_series* dftu_series_str_search(const dftu_series* v, const char* pattern,
227 ✔
631
                                    int32_t pattern_len) {
632
    const auto p = compiled(
72 !
633
        duql::compile_regex(pattern_text(pattern, pattern_len), false));
227 !
634
    if (!p) return nullptr;
227 ✔
635
    return pattern_predicate(v, *p);
227 !
636
}
227 ✔
637

638
dftu_series* dftu_series_str_like(const dftu_series* v, const char* pattern,
216 ✔
639
                                  int32_t pattern_len) {
640
    const auto p = compiled(
81 !
641
        duql::compile_like(pattern_text(pattern, pattern_len), false, '\\'));
216 !
642
    if (!p) return nullptr;
216 ✔
643
    return pattern_predicate(v, *p);
214 !
644
}
216 ✔
645

646
dftu_series* dftu_series_str_len_bytes(const dftu_series* v) {
163 ✔
647
    // FLAT, narrow offsets: adjacent int32 offset difference, vectorized.
648
    // Every other case (DICTIONARY, or a wide-offset FLAT column) is scalar;
649
    // a Large* column's row count is bounded by int32 either way, only its
650
    // byte offsets are not, so the SIMD int32 path stays narrow-only.
651
    if (v->encoding == Encoding::Flat && v->offsets && !v->wide_offsets()) {
163 ✔
652
        std::vector<std::int64_t> vals(static_cast<std::size_t>(v->length), 0);
69 !
653
        if (v->length > 0)
69 ✔
654
            len_bytes_simd(
67 !
655
                reinterpret_cast<const std::int32_t*>(v->offsets->data()),
67 ✔
656
                vals.data(), static_cast<std::size_t>(v->length));
67 ✔
657
        // Null rows keep 0 length regardless; validity marks them null.
658
        return make_i64(v, vals);
69 !
659
    }
69 ✔
660
    return string_int64(v, [](std::string_view s) {
855 ✔
661
        return static_cast<std::int64_t>(s.size());
1,549 ✔
662
    });
64 ✔
663
}
51 ✔
664

665
dftu_series* dftu_series_str_len_chars(const dftu_series* v) {
230 ✔
666
    // FLAT, narrow offsets: count non-continuation bytes per row with the
667
    // vectorized scan. DICTIONARY and wide-offset columns take the scalar
668
    // per-row path.
669
    if (v->encoding == Encoding::Flat && v->offsets && !v->wide_offsets() &&
304 !
670
        v->data) {
91 ✔
671
        const std::int32_t* off =
17 ✔
672
            reinterpret_cast<const std::int32_t*>(v->offsets->data());
91 ✔
673
        const std::uint8_t* data = v->data->data();
92 ✔
674
        std::vector<std::int64_t> vals(static_cast<std::size_t>(v->length), 0);
91 !
675
        for (std::int64_t i = 0; i < v->length; ++i)
548,814 ✔
676
            vals[static_cast<std::size_t>(i)] = count_char_starts(
548,720 !
677
                data + off[i], static_cast<std::size_t>(off[i + 1] - off[i]));
548,723 !
678
        return make_i64(v, vals);
91 !
679
    }
91 ✔
680
    return string_int64(v, [](std::string_view s) {
862 ✔
681
        std::int64_t count = 0;
59,953 ✔
682
        for (unsigned char c : s)
903,699 ✔
683
            if ((c & 0xC0) != 0x80) ++count;  // non-continuation byte
843,746 ✔
684
        return count;
59,953 ✔
685
    });
91 ✔
686
}
65 ✔
687

688
dftu_series* dftu_series_str_find(const dftu_series* v, const char* needle,
137 ✔
689
                                  int32_t needle_len) {
690
    std::string_view n(needle, static_cast<std::size_t>(needle_len));
137 ✔
691
    return string_int64(v, [n](std::string_view s) {
1,316 !
692
        return substr_find(s.data(), static_cast<std::int64_t>(s.size()),
71,725 ✔
693
                           n.data(), static_cast<std::int64_t>(n.size()));
139,659 ✔
694
    });
194 ✔
695
}
696

697
namespace {
698
template <class Off>
699
dftu_series* fnv1a_w(const dftu_series* v) {
66 ✔
700
    RowReader<Off> r(v);
66 !
701
    if (!r.ok()) return nullptr;
66 !
702
    std::vector<std::uint64_t> vals(static_cast<std::size_t>(v->length), 0);
31 !
703
    for (std::int64_t i = 0; i < v->length; ++i) {
201 ✔
704
        if (r.is_null(i)) continue;
170 !
705
        vals[static_cast<std::size_t>(i)] =
154 ✔
706
            dftracer::utils::hash::fnv1a_hash(r.at(i));
154 !
707
    }
38 ✔
708
    return make_u64(v, vals, nullptr);
31 !
709
}
66 ✔
710
}  // namespace
711

712
dftu_series* dftu_series_fnv1a(const dftu_series* v) {
84 ✔
713
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_fnv1a(flat_v));
84 ✔
714

715
    return is_wide_offset_type(v->type) ? fnv1a_w<std::int64_t>(v)
66 ✔
716
                                        : fnv1a_w<std::int32_t>(v);
66 ✔
717
}
6 ✔
718

719
namespace {
720
template <class Off>
721
dftu_series* hex64_parse_w(const dftu_series* v) {
56 ✔
722
    RowReader<Off> r(v);
56 !
723
    if (!r.ok()) return nullptr;
56 !
724
    std::vector<std::uint64_t> vals(static_cast<std::size_t>(v->length), 0);
28 !
725
    std::size_t bitmap_bytes = static_cast<std::size_t>((v->length + 7) / 8);
28 ✔
726
    std::vector<std::uint8_t> valid(bitmap_bytes, 0);
28 !
727
    std::int64_t nulls = 0;
28 ✔
728
    for (std::int64_t i = 0; i < v->length; ++i) {
138 ✔
729
        std::optional<std::uint64_t> parsed;
110 ✔
730
        if (!r.is_null(i)) parsed = dftracer::utils::hash::parse_hex64(r.at(i));
110 !
731
        if (!parsed) {
110 ✔
732
            ++nulls;
96 ✔
733
            continue;
96 ✔
734
        }
735
        vals[static_cast<std::size_t>(i)] = *parsed;
14 ✔
736
        valid[static_cast<std::size_t>(i >> 3)] |=
14 ✔
737
            static_cast<std::uint8_t>(1u << (i & 7));
14 ✔
738
    }
6 ✔
739
    dftu_series* out = make_u64(v, vals, valid.data());
28 !
740
    out->null_count = nulls;
28 ✔
741
    return out;
28 ✔
742
}
56 ✔
743
}  // namespace
744

745
dftu_series* dftu_series_hex64_parse(const dftu_series* v) {
70 ✔
746
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_hex64_parse(flat_v));
70 !
747

748
    return is_wide_offset_type(v->type) ? hex64_parse_w<std::int64_t>(v)
56 !
749
                                        : hex64_parse_w<std::int32_t>(v);
56 ✔
750
}
4 ✔
751

752
dftu_series* dftu_series_hex64_format(const dftu_series* v) {
65 ✔
753
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_hex64_format(flat_v));
65 !
754

755
    using dftracer::utils::hash::format_hex64;
756
    using dftracer::utils::hash::HEX64_DIGITS;
757
    if (!v || (v->type != TypeId::Uint64 && v->type != TypeId::Int64)) {
51 !
758
        return nullptr;
32 ✔
759
    }
760
    if (v->encoding != Encoding::Flat || !v->data) return nullptr;
19 !
761

762
    const std::size_t n = static_cast<std::size_t>(v->length);
19 ✔
763
    const std::size_t total = n * HEX64_DIGITS;
19 ✔
764
    // Every row is exactly HEX64_DIGITS bytes, so the byte total is known up
765
    // front; refuse rather than overflow the int32 offsets Arrow uses here.
766
    if (total > static_cast<std::size_t>(INT32_MAX)) return nullptr;
19 !
767

768
    const std::uint64_t* src =
2 ✔
769
        reinterpret_cast<const std::uint64_t*>(v->data->data());
19 ✔
770
    auto* out = new dftu_series();
19 ✔
771
    out->type = TypeId::String;
19 ✔
772
    out->encoding = Encoding::Flat;
19 ✔
773
    out->length = v->length;
19 ✔
774
    out->null_count = v->null_count;
19 ✔
775
    out->validity = v->validity;
19 ✔
776
    out->offsets = Buffer::allocate((n + 1) * sizeof(std::int32_t));
19 ✔
777
    out->data = Buffer::allocate(total);
19 ✔
778
    std::int32_t* od = reinterpret_cast<std::int32_t*>(out->offsets->data());
19 ✔
779
    char* bd =
2 ✔
780
        total != 0 ? reinterpret_cast<char*>(out->data->data()) : nullptr;
19 ✔
781
    for (std::size_t i = 0; i < n; ++i) {
88 ✔
782
        od[i] = static_cast<std::int32_t>(i * HEX64_DIGITS);
69 ✔
783
        format_hex64(src[i], bd + i * HEX64_DIGITS);
69 ✔
784
    }
4 ✔
785
    od[n] = static_cast<std::int32_t>(total);
19 ✔
786
    return out;
19 ✔
787
}
2 ✔
788

789
dftu_series* dftu_series_to_lowercase(const dftu_series* v) {
1,610 ✔
790
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_to_lowercase(flat_v));
1,610 ✔
791

792
    return ascii_fold(v, 'A', 'Z', 32);
836 ✔
793
}
674 ✔
794

795
dftu_series* dftu_series_to_uppercase(const dftu_series* v) {
198 ✔
796
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_to_uppercase(flat_v));
198 ✔
797

798
    return ascii_fold(v, 'a', 'z', -32);
124 ✔
799
}
64 ✔
800

801
dftu_series* dftu_series_str_strip(const dftu_series* v) {
73 ✔
802
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_strip(flat_v));
73 !
803

804
    return string_transform(v, [](std::string_view s) {
65 ✔
805
        return std::string(rstrip_ws(lstrip_ws(s)));
202 !
806
    });
57 ✔
807
}
2 ✔
808
dftu_series* dftu_series_str_lstrip(const dftu_series* v) {
66 ✔
809
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_lstrip(flat_v));
66 !
810

811
    return string_transform(
52 ✔
812
        v, [](std::string_view s) { return std::string(lstrip_ws(s)); });
204 !
813
}
2 ✔
814
dftu_series* dftu_series_str_rstrip(const dftu_series* v) {
66 ✔
815
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_rstrip(flat_v));
66 !
816

817
    return string_transform(
52 ✔
818
        v, [](std::string_view s) { return std::string(rstrip_ws(s)); });
204 !
819
}
2 ✔
820

821
dftu_series* dftu_series_str_replace(const dftu_series* v, const char* pat,
70 ✔
822
                                     int32_t pat_len, const char* repl,
823
                                     int32_t repl_len) {
824
    DFTU_FLAT_OPERAND(
70 !
825
        v, flat_v,
826
        dftu_series_str_replace(flat_v, pat, pat_len, repl, repl_len));
827

828
    std::string_view p(pat, static_cast<std::size_t>(pat_len));
56 ✔
829
    std::string_view r(repl, static_cast<std::size_t>(repl_len));
56 ✔
830
    return string_transform(v, [p, r](std::string_view s) {
61 !
831
        return replace_literal(s, p, r, false);
85 ✔
832
    });
53 ✔
833
}
3 ✔
834
dftu_series* dftu_series_str_replace_all(const dftu_series* v, const char* pat,
81 ✔
835
                                         int32_t pat_len, const char* repl,
836
                                         int32_t repl_len) {
837
    DFTU_FLAT_OPERAND(
81 !
838
        v, flat_v,
839
        dftu_series_str_replace_all(flat_v, pat, pat_len, repl, repl_len));
840

841
    std::string_view p(pat, static_cast<std::size_t>(pat_len));
67 ✔
842
    std::string_view r(repl, static_cast<std::size_t>(repl_len));
67 ✔
843
    return string_transform(v, [p, r](std::string_view s) {
77 !
844
        return replace_literal(s, p, r, true);
143 ✔
845
    });
63 ✔
846
}
4 ✔
847

848
namespace {
849
// Splits every row of `v` on `s_sep`, producing a List<String> (always
850
// narrow, both levels: the per-row part count and total byte count are the
851
// source column's own data, already known to fit an int32 row count).
852
// A List<String> column over `v`'s rows (always narrow, both levels): row i
853
// holds the parts `fn(row)` yields, a null input row a null list.
854
template <class Off, class Fn>
855
dftu_series* list_transform_w(const dftu_series* v, Fn fn) {
28 ✔
856
    RowReader<Off> r(v);
28 !
857
    if (!r.ok()) return nullptr;
28 !
858

859
    std::vector<std::string> all_parts;
28 ✔
860
    std::vector<std::int32_t> list_off(static_cast<std::size_t>(v->length + 1),
30 !
861
                                       0);
28 !
862
    for (std::int64_t i = 0; i < v->length; ++i) {
2,299 ✔
863
        if (!r.is_null(i)) fn(r.at(i), all_parts);
2,271 !
864
        if (!fits_int32_offsets(all_parts.size())) return nullptr;
2,271 !
865
        list_off[static_cast<std::size_t>(i + 1)] =
2,271 ✔
866
            static_cast<std::int32_t>(all_parts.size());
2,271 ✔
867
    }
13 ✔
868
    std::size_t ctotal = 0;
28 ✔
869
    for (const std::string& p : all_parts) ctotal += p.size();
27,770 ✔
870
    if (!fits_int32_offsets(ctotal)) return nullptr;
28 !
871

872
    auto* child = new dftu_series();
28 !
873
    child->type = TypeId::String;
28 ✔
874
    child->encoding = Encoding::Flat;
28 ✔
875
    child->length = static_cast<std::int64_t>(all_parts.size());
28 ✔
876
    std::size_t coff_bytes =
28 ✔
877
        static_cast<std::size_t>(all_parts.size() + 1) * sizeof(std::int32_t);
28 ✔
878
    child->offsets = Buffer::allocate(coff_bytes);
28 !
879
    child->data = Buffer::allocate(ctotal);
28 !
880
    std::int32_t* cod = reinterpret_cast<std::int32_t*>(child->offsets->data());
28 ✔
881
    char* cbd =
28 ✔
882
        ctotal != 0 ? reinterpret_cast<char*>(child->data->data()) : nullptr;
28 !
883
    std::int32_t cpos = 0;
28 ✔
884
    cod[0] = 0;
28 ✔
885
    for (std::size_t k = 0; k < all_parts.size(); ++k) {
27,770 ✔
886
        const std::string& p = all_parts[k];
27,742 ✔
887
        if (!p.empty()) std::memcpy(cbd + cpos, p.data(), p.size());
27,742 !
888
        cpos += static_cast<std::int32_t>(p.size());
27,742 ✔
889
        cod[k + 1] = cpos;
27,742 ✔
890
    }
19 ✔
891

892
    auto* out = new dftu_series();
28 !
893
    out->type = TypeId::List;
28 ✔
894
    out->encoding = Encoding::Flat;
28 ✔
895
    out->length = v->length;
28 ✔
896
    out->null_count = v->null_count;
28 ✔
897
    out->validity = v->validity;  // a null input row stays a null list
28 ✔
898
    std::size_t loff_bytes =
28 ✔
899
        static_cast<std::size_t>(v->length + 1) * sizeof(std::int32_t);
28 ✔
900
    out->offsets = Buffer::allocate(loff_bytes);
28 !
901
    std::memcpy(out->offsets->data(), list_off.data(), loff_bytes);
28 ✔
902
    out->set_child(std::shared_ptr<dftu_series>(child));
28 !
903
    return out;
28 ✔
904
}
28 ✔
905

906
template <class Fn>
907
dftu_series* list_transform(const dftu_series* v, Fn fn) {
28 ✔
908
    if (!v || !is_string_kind(v->type)) return nullptr;
28 !
909
    return is_wide_offset_type(v->type) ? list_transform_w<std::int64_t>(v, fn)
28 !
910
                                        : list_transform_w<std::int32_t>(v, fn);
28 ✔
911
}
2 ✔
912

913
}  // namespace
914

915
dftu_series* dftu_series_list_len(const dftu_series* v) {
3,251 ✔
916
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_list_len(flat_v));
3,251 !
917

918
    if (!v || v->type != TypeId::List || v->encoding != Encoding::Flat)
3,237 !
919
        return nullptr;
46 ✔
920
    const std::int32_t* off = dftu_series_offsets(v);
3,191 !
921
    if (!off) return nullptr;
3,191 ✔
922
    const std::size_t n = static_cast<std::size_t>(v->length);
3,191 ✔
923
    std::vector<std::int64_t> len(n);
3,191 !
924
    for (std::size_t i = 0; i < n; ++i) len[i] = off[i + 1] - off[i];
91,541 ✔
925
    auto* out = new dftu_series();
3,191 !
926
    out->type = TypeId::Int64;
3,191 ✔
927
    out->encoding = Encoding::Flat;
3,191 ✔
928
    out->length = v->length;
3,191 ✔
929
    out->null_count = v->null_count;
3,191 ✔
930
    out->validity = v->validity;
3,191 ✔
931
    out->data = Buffer::allocate(n * sizeof(std::int64_t));
3,191 !
932
    if (n != 0)
3,191 ✔
933
        std::memcpy(out->data->data(), len.data(), n * sizeof(std::int64_t));
3,191 ✔
934
    return out;
3,191 ✔
935
}
3,191 ✔
936

937
dftu_series* dftu_series_list_get(const dftu_series* v, int64_t index) {
116 ✔
938
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_list_get(flat_v, index));
116 !
939

940
    if (!v || v->type != TypeId::List || v->encoding != Encoding::Flat)
102 !
941
        return nullptr;
46 ✔
942
    const std::int32_t* off = dftu_series_offsets(v);
56 !
943
    if (!off || dftu_series_num_children(v) != 1) return nullptr;
56 !
944
    const std::size_t n = static_cast<std::size_t>(v->length);
56 ✔
945
    // A gather of the element at `index` (from the end when negative) per
946
    // row; -1 marks a null row or one whose list is too short.
947
    std::vector<std::int64_t> idx(n, -1);
56 !
948
    for (std::size_t i = 0; i < n; ++i) {
369 ✔
949
        if (v->validity && !((v->validity->data()[i >> 3] >> (i & 7)) & 1))
313 ✔
950
            continue;
41 ✔
951
        const std::int64_t len = off[i + 1] - off[i];
272 ✔
952
        const std::int64_t k = index < 0 ? len + index : index;
272 ✔
953
        if (k >= 0 && k < len) idx[i] = off[i] + k;
272 ✔
954
    }
49 ✔
955
    dftu_series* child = dftu_series_child(v, 0);
56 !
956
    if (!child) return nullptr;
56 ✔
957
    dftu_series* out =
12 ✔
958
        dftu_series_take(child, idx.data(), static_cast<int64_t>(n));
56 !
959
    dftu_series_free(child);
56 !
960
    return out;
56 ✔
961
}
56 ✔
962

963
// The pandas `.str` batch. Every op below works on the column's buffers
964
// directly (no per-row std::string): the SIMD primitives of strsimd scan the
965
// whole data buffer, the builders size the output in one pass and fill it in a
966
// second.
967
namespace {
968

969
namespace simd = dftracer::utils::dataframe::strsimd;
970

971
// A FLAT String/Binary column read at offset width `Off`. Offsets are
972
// absolute positions in `data`; a column cut from a larger one starts past 0.
973
template <class Off>
974
struct FlatStr {
975
    const Off* off = nullptr;
1,371 ✔
976
    const std::uint8_t* data = nullptr;
1,371 ✔
977
    const std::uint8_t* validity = nullptr;
1,371 ✔
978
    std::int64_t n = 0;
1,371 ✔
979
    bool ok = false;
1,371 ✔
980

981
    explicit FlatStr(const dftu_series* v) {
6,182 ✔
982
        if (!v || v->encoding != Encoding::Flat || !is_string_kind(v->type) ||
8,251 !
983
            is_wide_offset_type(v->type) != std::is_same_v<Off, std::int64_t>)
4,811 !
984
            return;
×
985
        const auto& ob = offsets_of<Off>(*v);
4,811 ✔
986
        if (!ob) return;
4,811 !
987
        off = reinterpret_cast<const Off*>(ob->data());
4,811 ✔
988
        data = v->data ? v->data->data() : nullptr;
4,811 !
989
        validity = v->validity ? v->validity->data() : nullptr;
4,811 ✔
990
        n = v->length;
4,811 ✔
991
        ok = true;
4,811 ✔
992
    }
2,742 ✔
993
    bool is_null(std::int64_t i) const {
1,299,808 ✔
994
        return validity != nullptr && ((validity[i >> 3] >> (i & 7)) & 1) == 0;
1,299,808 ✔
995
    }
996
    std::int64_t lo() const {
668 ✔
997
        return n != 0 ? static_cast<std::int64_t>(off[0]) : 0;
668 !
998
    }
999
    std::int64_t hi() const {
668 ✔
1000
        return n != 0 ? static_cast<std::int64_t>(off[n]) : 0;
668 !
1001
    }
1002
    std::int64_t len(std::int64_t i) const {
1,260,106 ✔
1003
        return static_cast<std::int64_t>(off[i + 1]) - off[i];
1,260,106 ✔
1004
    }
1005
    const std::uint8_t* row(std::int64_t i) const { return data + off[i]; }
490,373 ✔
1006
};
1007

1008
template <class Fn>
1009
dftu_series* with_flat(const dftu_series* v, Fn&& fn) {
1,464 ✔
1010
    if (!v || !is_string_kind(v->type) || v->encoding != Encoding::Flat)
1,464 !
1011
        return nullptr;
28 ✔
1012
    if (is_wide_offset_type(v->type)) {
1,436 !
1013
        FlatStr<std::int64_t> f(v);
×
1014
        return f.ok ? fn(f) : nullptr;
×
1015
    }
1016
    FlatStr<std::int32_t> f(v);
1,436 !
1017
    return f.ok ? fn(f) : nullptr;
1,436 !
1018
}
73 ✔
1019

1020
void share_nulls(dftu_series* out, const dftu_series* v) {
898 ✔
1021
    if (!out) return;
898 ✔
1022
    out->validity = v->validity;
897 ✔
1023
    out->null_count = v->null_count;
897 ✔
1024
}
38 ✔
1025

1026
void copy_bytes(std::uint8_t* dst, const std::uint8_t* src, std::int64_t n) {
963,847 ✔
1027
    if (n > 0) std::memcpy(dst, src, static_cast<std::size_t>(n));
963,847 ✔
1028
}
963,847 ✔
1029

1030
constexpr std::int64_t BUILD_GRAIN = 1 << 14;
1031

1032
// A narrow String column of `n` rows: row i is `len_of(i)` bytes that
1033
// `fill_of(i, dst)` writes (a null row is empty and never asked). Sizes the
1034
// output in one pass and fills it in a second, across the pool for a large
1035
// column. nullptr when the bytes pass what an int32 offset reaches.
1036
template <class IsNull, class LenFn, class FillFn>
1037
dftu_series* build_flat_string(std::int64_t n, IsNull is_null, LenFn len_of,
2,458 ✔
1038
                               FillFn fill_of) {
1039
    auto offb = Buffer::allocate(static_cast<std::size_t>(n + 1) *
2,458 !
1040
                                 sizeof(std::int32_t));
1041
    auto* od = reinterpret_cast<std::int32_t*>(offb->data());
2,458 ✔
1042
    std::int64_t total = 0;
2,458 ✔
1043
    od[0] = 0;
2,458 ✔
1044
    for (std::int64_t i = 0; i < n; ++i) {
394,600 !
1045
        if (!is_null(i)) {
392,143 !
1046
            const std::int64_t l = len_of(i);
365,757 !
1047
            if (l < 0 || __builtin_add_overflow(total, l, &total) ||
688,057 !
1048
                total > std::numeric_limits<std::int32_t>::max())
365,757 ✔
1049
                return nullptr;
1 ✔
1050
        }
43,457 ✔
1051
        od[i + 1] = static_cast<std::int32_t>(total);
392,142 ✔
1052
    }
43,484 ✔
1053
    auto datab = Buffer::allocate(static_cast<std::size_t>(total));
2,457 !
1054
    std::uint8_t* d = datab->data();
2,457 ✔
1055
    auto body = [&](std::int64_t b, std::int64_t e) {
609,746 ✔
1056
        for (std::int64_t i = b; i < e; ++i)
359,393 !
1057
            if (!is_null(i)) fill_of(i, d + od[i]);
357,047 !
1058
    };
681 ✔
1059
    if (n > BUILD_GRAIN && parallel_backend_installed())
2,457 !
1060
        parallel_for(n, BUILD_GRAIN, body);
2 !
1061
    else
1062
        body(0, n);
2,455 !
1063
    auto* out = new dftu_series();
2,457 !
1064
    out->type = TypeId::String;
2,457 ✔
1065
    out->encoding = Encoding::Flat;
2,457 ✔
1066
    out->length = n;
2,457 ✔
1067
    out->offsets = std::move(offb);
2,457 ✔
1068
    out->data = std::move(datab);
2,457 ✔
1069
    return out;
2,457 ✔
1070
}
2,458 ✔
1071

1072
// One string op over a single column: the output shares its nulls.
1073
template <class LenFn, class FillFn>
1074
dftu_series* map_strings(const dftu_series* v, LenFn len_of, FillFn fill_of) {
768 ✔
1075
    dftu_series* out = with_flat(v, [&](const auto& f) {
1,536 !
1076
        return build_flat_string(
768 ✔
1077
            f.n, [&](std::int64_t i) { return f.is_null(i); },
469,514 ✔
1078
            [&](std::int64_t i) { return len_of(f, i); },
233,655 ✔
1079
            [&](std::int64_t i, std::uint8_t* dst) { fill_of(f, i, dst); });
200,553 ✔
1080
    });
1081
    share_nulls(out, v);
768 ✔
1082
    return out;
768 ✔
1083
}
1084

1085
// The bit arrays of one kind over p[0, n), across the pool for a large buffer.
1086
struct Bits {
1087
    std::vector<std::uint64_t> bad;
1088
    std::vector<std::uint64_t> aux;
1089
};
1090

1091
Bits compute_bits(simd::BitKind kind, const std::uint8_t* p, std::size_t n,
527 ✔
1092
                  std::uint8_t eq, bool want_aux) {
1093
    const std::size_t words = (n + 63) / 64;
527 ✔
1094
    Bits b;
527 ✔
1095
    b.bad.assign(words + 1, 0);
527 !
1096
    if (want_aux) b.aux.assign(words + 1, 0);
527 !
1097
    std::uint64_t* bp = b.bad.data();
527 ✔
1098
    std::uint64_t* ap = want_aux ? b.aux.data() : nullptr;
527 ✔
1099
    constexpr std::int64_t CHUNK = 1 << 14;  // words: 1 MiB of bytes
527 ✔
1100
    if (static_cast<std::int64_t>(words) > CHUNK &&
527 !
1101
        parallel_backend_installed()) {
×
1102
        parallel_for(static_cast<std::int64_t>(words), CHUNK,
×
1103
                     [&](std::int64_t b0, std::int64_t e0) {
×
1104
                         simd::class_bits(
×
1105
                             kind, p, n, static_cast<std::size_t>(b0),
×
1106
                             static_cast<std::size_t>(e0), eq, bp, ap);
×
1107
                     });
×
1108
    } else if (words != 0) {
527 ✔
1109
        simd::class_bits(kind, p, n, 0, words, eq, bp, ap);
352 !
1110
    }
37 ✔
1111
    return b;
1,017 ✔
1112
}
37 !
1113

1114
bool lower_byte(std::uint8_t c) { return c >= 'a' && c <= 'z'; }
143,154 ✔
1115

1116
// Narrow String out of a List<String> shape: row i of the list holds
1117
// `list_off[i + 1] - list_off[i]` strings.
1118
dftu_series* make_str_list(const dftu_series* v,
272 ✔
1119
                           const std::vector<std::int32_t>& list_off,
1120
                           std::int64_t parts, std::shared_ptr<Buffer> coff,
1121
                           std::shared_ptr<Buffer> cdata) {
1122
    auto* child = new dftu_series();
272 ✔
1123
    child->type = TypeId::String;
272 ✔
1124
    child->encoding = Encoding::Flat;
272 ✔
1125
    child->length = parts;
272 ✔
1126
    child->offsets = std::move(coff);
272 ✔
1127
    child->data = std::move(cdata);
272 ✔
1128
    auto* out = new dftu_series();
272 ✔
1129
    out->type = TypeId::List;
272 ✔
1130
    out->encoding = Encoding::Flat;
272 ✔
1131
    out->length = v->length;
272 ✔
1132
    out->null_count = v->null_count;
272 ✔
1133
    out->validity = v->validity;  // a null input row stays a null list
272 ✔
1134
    const std::size_t loff_bytes = list_off.size() * sizeof(std::int32_t);
272 ✔
1135
    out->offsets = Buffer::allocate(loff_bytes);
272 ✔
1136
    std::memcpy(out->offsets->data(), list_off.data(), loff_bytes);
272 ✔
1137
    out->set_child(std::shared_ptr<dftu_series>(child));
272 !
1138
    return out;
272 ✔
1139
}
1140

1141
// The first (last, when `from_right`) place in row [s, e) of p where `sep`
1142
// occurs, or NPOS: candidate bits of sep's first byte, each verified.
1143
std::size_t find_sep(const std::uint64_t* cand, const std::uint8_t* p,
431,950 ✔
1144
                     std::size_t s, std::size_t e, std::string_view sep,
1145
                     bool from_right) {
1146
    const std::size_t sl = sep.size();
431,950 ✔
1147
    if (e - s < sl) return simd::NPOS;
431,950 ✔
1148
    const std::size_t limit = e - sl + 1;  // one past the last start
431,539 ✔
1149
    auto verify = [&](std::size_t q) {
381,341 ✔
1150
        return sl == 1 || std::memcmp(p + q + 1, sep.data() + 1, sl - 1) == 0;
381,235 ✔
1151
    };
431,433 ✔
1152
    if (!from_right) {
431,539 ✔
1153
        std::size_t pos = s;
411,692 ✔
1154
        while (pos < limit) {
454,022 ✔
1155
            const std::size_t q = simd::bits_next(cand, pos, limit);
453,696 ✔
1156
            if (q >= limit) return simd::NPOS;
453,696 ✔
1157
            if (verify(q)) return q;
322,781 ✔
1158
            pos = q + 1;
42,330 ✔
1159
        }
1160
        return simd::NPOS;
326 ✔
1161
    }
1162
    std::size_t lim = limit;
19,847 ✔
1163
    while (lim > s) {
71,232 ✔
1164
        const std::size_t q = simd::bits_prev(cand, s, lim);
70,872 ✔
1165
        if (q == simd::NPOS) return simd::NPOS;
70,872 ✔
1166
        if (verify(q)) return q;
58,454 ✔
1167
        lim = q;
51,385 ✔
1168
    }
1169
    return simd::NPOS;
360 ✔
1170
}
110 ✔
1171

1172
dftu_series* case_impl(const dftu_series* v, simd::CaseKind kind,
125 ✔
1173
                       bool capitalize) {
1174
    if (!v || !is_string_kind(v->type)) return nullptr;
125 !
1175
    const bool wide = is_wide_offset_type(v->type);
125 ✔
1176
    auto run = [&](const auto& f) -> dftu_series* {
135 ✔
1177
        auto* out = new dftu_series();
125 ✔
1178
        out->type = wide ? TypeId::LargeString : TypeId::String;
125 !
1179
        out->encoding = Encoding::Flat;
125 ✔
1180
        out->length = v->length;
562 ✔
1181
        out->null_count = v->null_count;
125 ✔
1182
        out->validity = v->validity;
125 ✔
1183
        using Off = std::remove_const_t<std::remove_pointer_t<decltype(f.off)>>;
1184
        offsets_of<Off>(*out) = offsets_of<Off>(*v);  // byte lengths unchanged
125 ✔
1185
        const std::size_t data_len = v->data ? v->data->size() : 0;
125 !
1186
        out->data = Buffer::allocate(data_len);
125 ✔
1187
        if (data_len == 0) return out;
125 !
1188
        const std::uint8_t* in = v->data->data();
102 ✔
1189
        std::uint8_t* o = out->data->data();
102 ✔
1190
        simd::case_map(capitalize ? simd::CaseKind::Lower : kind, in, o,
102 ✔
1191
                       data_len);
10 ✔
1192
        if (capitalize || kind == simd::CaseKind::Title) {
102 ✔
1193
            // A row's first byte has no previous byte: it is upper-cased when
1194
            // lower, whatever sat before it in the buffer.
1195
            for (std::int64_t i = 0; i < f.n; ++i) {
149,169 ✔
1196
                if (f.len(i) <= 0) continue;
149,089 ✔
1197
                const std::int64_t s = f.off[i];
140,710 ✔
1198
                o[s] = lower_byte(in[s]) ? static_cast<std::uint8_t>(in[s] - 32)
222,523 ✔
1199
                                         : in[s];
81,831 ✔
1200
            }
39 ✔
1201
        }
7 ✔
1202
        return out;
102 ✔
1203
    };
125 ✔
1204
    if (wide) {
125 !
1205
        FlatStr<std::int64_t> f(v);
×
1206
        return f.ok ? run(f) : nullptr;
×
1207
    }
1208
    FlatStr<std::int32_t> f(v);
125 !
1209
    return f.ok ? run(f) : nullptr;
125 !
1210
}
10 ✔
1211

1212
dftu_series* is_impl(const dftu_series* v, int cls) {
314 ✔
1213
    simd::BitKind kind = simd::BitKind::Alnum;
314 ✔
1214
    bool want_aux = false;
314 ✔
1215
    switch (cls) {
314 ✔
1216
        case DFTU_STR_ALNUM:
26 ✔
1217
            kind = simd::BitKind::Alnum;
28 ✔
1218
            break;
28 ✔
1219
        case DFTU_STR_ALPHA:
79 ✔
1220
            kind = simd::BitKind::Alpha;
82 ✔
1221
            break;
82 ✔
1222
        case DFTU_STR_DIGIT:
78 ✔
1223
        case DFTU_STR_DECIMAL:
1224
        case DFTU_STR_NUMERIC:
1225
            kind = simd::BitKind::Digit;
84 ✔
1226
            break;
84 ✔
1227
        case DFTU_STR_SPACE:
26 ✔
1228
            kind = simd::BitKind::Space;
28 ✔
1229
            break;
28 ✔
1230
        case DFTU_STR_LOWER:
27 ✔
1231
            kind = simd::BitKind::Lower;
30 ✔
1232
            want_aux = true;
30 ✔
1233
            break;
30 ✔
1234
        case DFTU_STR_UPPER:
28 ✔
1235
            kind = simd::BitKind::Upper;
30 ✔
1236
            want_aux = true;
30 ✔
1237
            break;
30 ✔
1238
        case DFTU_STR_TITLE:
27 ✔
1239
            kind = simd::BitKind::Title;
30 ✔
1240
            want_aux = true;
30 ✔
1241
            break;
30 ✔
1242
        default:
1 ✔
1243
            return nullptr;
2 ✔
1244
    }
1245
    const bool title = kind == simd::BitKind::Title;
312 ✔
1246
    return with_flat(v, [&](const auto& f) -> dftu_series* {
887 !
1247
        const std::int64_t lo = f.lo();
284 ✔
1248
        const std::size_t nbytes = static_cast<std::size_t>(f.hi() - lo);
284 ✔
1249
        const std::uint8_t* p = f.data != nullptr ? f.data + lo : nullptr;
284 !
1250
        // Short strings: one vector pass over the whole buffer, then a bit
1251
        // range per row. Long strings: scan each row and stop at its first
1252
        // bad vector (no bit arrays at all).
1253
        const std::int64_t avg =
284 ✔
1254
            f.n > 0 ? static_cast<std::int64_t>(nbytes) / f.n : 0;
284 !
1255
        const simd::PredStrategy strat = simd::pred_strategy();
284 !
1256
        const bool by_rows = strat == simd::PredStrategy::Rows ||
532 ✔
1257
                             (strat == simd::PredStrategy::Auto &&
248 !
1258
                              avg >= simd::PRED_ROWS_MIN_AVG);
21 ✔
1259
        Bits bits;
284 ✔
1260
        if (!by_rows) bits = compute_bits(kind, p, nbytes, 0, want_aux);
284 !
1261
        const std::uint64_t* bad = bits.bad.data();
284 ✔
1262
        const std::uint64_t* aux =
284 ✔
1263
            want_aux && !by_rows ? bits.aux.data() : nullptr;
284 !
1264

1265
        auto* out = new dftu_series();
284 !
1266
        out->type = TypeId::Bool;
284 ✔
1267
        out->encoding = Encoding::Flat;
284 ✔
1268
        out->length = f.n;
284 ✔
1269
        out->null_count = v->null_count;
284 ✔
1270
        out->validity = v->validity;
284 ✔
1271
        const std::size_t bytes = buffer_bytes(TypeId::Bool, f.n);
284 ✔
1272
        out->data = Buffer::allocate(bytes);
284 !
1273
        std::memset(out->data->data(), 0, bytes);
284 ✔
1274
        std::uint8_t* res = out->data->data();
284 ✔
1275
        auto rows = [&](std::int64_t b, std::int64_t e) {
87,999 ✔
1276
            if (by_rows) {
284 !
1277
                simd::class_rows(kind, f.data, f.off, lo, b, e, res);
117 ✔
1278
                return;
117 ✔
1279
            }
1280
            for (std::int64_t i = b; i < e; ++i) {
23,092 ✔
1281
                const std::size_t s = static_cast<std::size_t>(f.off[i] - lo);
22,925 ✔
1282
                const std::size_t t =
22,925 ✔
1283
                    static_cast<std::size_t>(f.off[i + 1] - lo);
22,925 ✔
1284
                if (s >= t) continue;  // an empty string is in no class
22,925 ✔
1285
                bool yes;
1286
                if (title) {
22,305 ✔
1287
                    // The first byte has no previous byte, so it breaks the
1288
                    // rule when lower; the rest read the bit array.
1289
                    yes = !lower_byte(p[s]) && !simd::bits_any(bad, s + 1, t) &&
2,496 !
1290
                          simd::bits_any(aux, s, t);
52 ✔
1291
                } else {
18 ✔
1292
                    yes = !simd::bits_any(bad, s, t) &&
26,961 ✔
1293
                          (aux == nullptr || simd::bits_any(aux, s, t));
7,363 !
1294
                }
1295
                if (yes)
22,042 ✔
1296
                    res[i >> 3] |= static_cast<std::uint8_t>(1u << (i & 7));
7,354 ✔
1297
            }
138 ✔
1298
        };
21 ✔
1299
        constexpr std::int64_t ROW_GRAIN = 1 << 15;  // a multiple of 8
284 ✔
1300
        if (f.n > ROW_GRAIN && parallel_backend_installed())
284 !
1301
            parallel_for(f.n, ROW_GRAIN, rows);
×
1302
        else
1303
            rows(0, f.n);
284 !
1304
        return out;
284 ✔
1305
    });
575 ✔
1306
}
22 ✔
1307

1308
// Pad / center: `mode` 0 pads the start, 1 the end, 2 centers.
1309
dftu_series* pad_impl(const dftu_series* v, std::int64_t width, char fill,
307 ✔
1310
                      int mode) {
1311
    return map_strings(
307 ✔
1312
        v,
11 ✔
1313
        [width](const auto& f, std::int64_t i) {
178,606 ✔
1314
            return std::max<std::int64_t>(f.len(i), width);
178,595 ✔
1315
        },
1316
        [width, fill, mode](const auto& f, std::int64_t i, std::uint8_t* dst) {
850,319 ✔
1317
            const std::int64_t len = f.len(i);
146,030 ✔
1318
            if (len >= width) {
146,415 ✔
1319
                copy_bytes(dst, f.row(i), len);
33,332 ✔
1320
                return;
33,220 ✔
1321
            }
1322
            const std::int64_t gap = width - len;
113,083 ✔
1323
            // CPython's center puts an odd gap's extra fill on the left when
1324
            // the width is odd, else on the right.
1325
            const std::int64_t left = mode == 0   ? gap
224,381 ✔
1326
                                      : mode == 1 ? 0
111,298 ✔
1327
                                                  : gap / 2 + (gap & width & 1);
110,097 ✔
1328
            std::memset(dst, fill, static_cast<std::size_t>(left));
113,083 ✔
1329
            copy_bytes(dst + left, f.row(i), len);
113,083 ✔
1330
            std::memset(dst + left + len, fill,
110,623 ✔
1331
                        static_cast<std::size_t>(gap - left));
110,572 ✔
1332
        });
356 ✔
1333
}
1334

1335
dftu_series* zfill_impl(const dftu_series* v, std::int64_t width) {
93 ✔
1336
    return map_strings(
93 ✔
1337
        v,
3 ✔
1338
        [width](const auto& f, std::int64_t i) {
10,934 ✔
1339
            return std::max<std::int64_t>(f.len(i), width);
10,931 ✔
1340
        },
1341
        [width](const auto& f, std::int64_t i, std::uint8_t* dst) {
23,486 ✔
1342
            const std::int64_t len = f.len(i);
10,931 ✔
1343
            const std::uint8_t* r = f.row(i);
10,931 ✔
1344
            if (len >= width) {
10,931 ✔
1345
                copy_bytes(dst, r, len);
9,284 ✔
1346
                return;
9,284 ✔
1347
            }
1348
            const std::int64_t gap = width - len;
1,647 ✔
1349
            // A leading sign stays first; the zeros go after it.
1350
            const bool sign = len > 0 && (r[0] == '+' || r[0] == '-');
1,657 !
1351
            std::int64_t at = 0;
1,647 ✔
1352
            if (sign) dst[at++] = r[0];
1,647 ✔
1353
            std::memset(dst + at, '0', static_cast<std::size_t>(gap));
1,647 ✔
1354
            at += gap;
1,647 ✔
1355
            copy_bytes(dst + at, r + (sign ? 1 : 0), len - (sign ? 1 : 0));
1,647 ✔
1356
        });
106 ✔
1357
}
1358

1359
dftu_series* remove_affix_impl(const dftu_series* v, std::string_view a,
160 ✔
1360
                               bool prefix) {
1361
    const auto plen = static_cast<std::int64_t>(a.size());
160 ✔
1362
    auto cut = [a, plen, prefix](const auto& f,
36,595 ✔
1363
                                 std::int64_t i) -> std::int64_t {
138,264 ✔
1364
        const std::int64_t len = f.len(i);
36,588 ✔
1365
        if (plen == 0 || len < plen) return 0;
36,588 !
1366
        const std::uint8_t* at = f.row(i) + (prefix ? 0 : len - plen);
29,060 ✔
1367
        return std::memcmp(at, a.data(), static_cast<std::size_t>(plen)) == 0
29,060 !
1368
                   ? plen
28,994 ✔
1369
                   : 0;
28,980 ✔
1370
    };
239 ✔
1371
    return map_strings(
160 !
1372
        v,
7 ✔
1373
        [&cut](const auto& f, std::int64_t i) { return f.len(i) - cut(f, i); },
18,301 ✔
1374
        [&cut, prefix](const auto& f, std::int64_t i, std::uint8_t* dst) {
36,552 ✔
1375
            const std::int64_t c = cut(f, i);
18,294 ✔
1376
            copy_bytes(dst, f.row(i) + (prefix ? c : 0), f.len(i) - c);
18,294 ✔
1377
        });
18,600 ✔
1378
}
1379

1380
dftu_series* repeat_impl(const dftu_series* v, std::int64_t n) {
70 ✔
1381
    return map_strings(
70 ✔
1382
        v,
4 ✔
1383
        [n](const auto& f, std::int64_t i) {
14,704 ✔
1384
            std::int64_t total;
1385
            if (__builtin_mul_overflow(f.len(i), n, &total))
7,362 !
1386
                return std::numeric_limits<std::int64_t>::max();
×
1387
            return total;
7,362 ✔
1388
        },
24 ✔
1389
        [n](const auto& f, std::int64_t i, std::uint8_t* dst) {
14,702 ✔
1390
            const std::int64_t len = f.len(i);
7,361 ✔
1391
            const std::int64_t total = len * n;
7,361 ✔
1392
            if (total == 0) return;
7,361 ✔
1393
            copy_bytes(dst, f.row(i), len);
5,507 ✔
1394
            // Double the filled prefix until the row is full.
1395
            std::int64_t filled = len;
5,507 ✔
1396
            while (filled < total) {
12,811 ✔
1397
                const std::int64_t c = std::min(filled, total - filled);
7,304 ✔
1398
                std::memcpy(dst + filled, dst, static_cast<std::size_t>(c));
7,304 ✔
1399
                filled += c;
7,304 ✔
1400
            }
1401
        });
90 ✔
1402
}
1403

1404
dftu_series* slice_impl(const dftu_series* v, std::int64_t start,
138 ✔
1405
                        std::int64_t length) {
1406
    // The byte range of a row: a negative start counts from the end, a
1407
    // negative length runs to the end (polars slice with no length).
1408
    auto range = [start, length](std::int64_t len) {
110,413 ✔
1409
        std::int64_t b = start < 0 ? start + len : start;
36,874 ✔
1410
        b = std::clamp<std::int64_t>(b, 0, len);
36,874 ✔
1411
        std::int64_t e = length < 0 ? len : b + length;
36,874 ✔
1412
        e = std::clamp<std::int64_t>(e, b, len);
36,874 ✔
1413
        return std::pair<std::int64_t, std::int64_t>(b, e);
36,874 ✔
1414
    };
127 ✔
1415
    return map_strings(
138 !
1416
        v,
11 ✔
1417
        [&range](const auto& f, std::int64_t i) {
18,448 ✔
1418
            const auto [b, e] = range(f.len(i));
18,437 !
1419
            return e - b;
18,437 ✔
1420
        },
1421
        [&range](const auto& f, std::int64_t i, std::uint8_t* dst) {
18,448 ✔
1422
            const auto [b, e] = range(f.len(i));
18,437 !
1423
            copy_bytes(dst, f.row(i) + b, e - b);
18,437 ✔
1424
        });
18,691 ✔
1425
}
1426

1427
template <class OffA, class OffB>
1428
dftu_series* cat_impl(const dftu_series* a, const dftu_series* b) {
1,560 ✔
1429
    FlatStr<OffA> fa(a);
1,560 !
1430
    FlatStr<OffB> fb(b);
1,560 !
1431
    if (!fa.ok || !fb.ok) return nullptr;
1,560 !
1432
    dftu_series* out = build_flat_string(
1,560 !
1433
        fa.n, [&](std::int64_t i) { return fa.is_null(i) || fb.is_null(i); },
229,703 !
1434
        [&](std::int64_t i) { return fa.len(i) + fb.len(i); },
112,945 ✔
1435
        [&](std::int64_t i, std::uint8_t* dst) {
458,220 ✔
1436
            copy_bytes(dst, fa.row(i), fa.len(i));
112,302 ✔
1437
            copy_bytes(dst + fa.len(i), fb.row(i), fb.len(i));
112,302 ✔
1438
        });
43,247 ✔
1439
    if (!out) return nullptr;
1,560 !
1440
    if (fa.validity == nullptr && fb.validity == nullptr) return out;
1,560 !
1441
    // A null on either side is null: the validity is the two ANDed.
1442
    const std::size_t vbytes = buffer_bytes(TypeId::Bool, fa.n);
305 ✔
1443
    auto valid = Buffer::allocate(vbytes);
305 !
1444
    std::int64_t valid_rows = 0;
305 ✔
1445
    for (std::size_t k = 0; k < vbytes; ++k) {
5,262 !
1446
        std::uint8_t w =
4,957 ✔
1447
            static_cast<std::uint8_t>((fa.validity ? fa.validity[k] : 0xFF) &
7,003 !
1448
                                      (fb.validity ? fb.validity[k] : 0xFF));
4,957 !
1449
        const std::int64_t rows_here =
4,957 ✔
1450
            std::min<std::int64_t>(8, fa.n - static_cast<std::int64_t>(k) * 8);
4,957 !
1451
        if (rows_here < 8)
4,957 !
1452
            w &= static_cast<std::uint8_t>((1u << rows_here) - 1);
214 ✔
1453
        valid->data()[k] = w;
4,957 ✔
1454
        valid_rows += std::popcount(static_cast<unsigned>(w));
4,957 ✔
1455
    }
1,392 ✔
1456
    out->null_count = fa.n - valid_rows;
305 ✔
1457
    out->validity = out->null_count != 0 ? std::move(valid) : nullptr;
305 !
1458
    return out;
305 ✔
1459
}
840 ✔
1460

1461
template <class ChildOff>
1462
dftu_series* join_impl(const dftu_series* v, const dftu_series& child,
130 ✔
1463
                       std::string_view sep) {
1464
    FlatStr<ChildOff> c(&child);
130 !
1465
    if (!c.ok) return nullptr;
130 !
1466
    const std::int32_t* off = dftu_series_offsets(v);
130 !
1467
    const auto sl = static_cast<std::int64_t>(sep.size());
130 ✔
1468
    auto is_null = [&](std::int64_t i) {
97,050 ✔
1469
        return v->validity && !((v->validity->data()[i >> 3] >> (i & 7)) & 1);
48,538 !
1470
    };
1471
    dftu_series* out = build_flat_string(
258 ✔
1472
        v->length, is_null,
130 !
1473
        [&](std::int64_t i) {
39,662 ✔
1474
            const std::int64_t cnt = off[i + 1] - off[i];
19,836 ✔
1475
            const std::int64_t bytes =
19,836 ✔
1476
                static_cast<std::int64_t>(c.off[off[i + 1]]) - c.off[off[i]];
19,836 ✔
1477
            return bytes + (cnt > 0 ? (cnt - 1) * sl : 0);
19,836 !
1478
        },
1479
        [&](std::int64_t i, std::uint8_t* dst) {
336,653 ✔
1480
            const std::int64_t cnt = off[i + 1] - off[i];
19,836 ✔
1481
            if (cnt == 0) return;
19,836 !
1482
            if (sl == 0 || cnt == 1) {
19,836 !
1483
                // The elements are back to back: one copy.
1484
                copy_bytes(dst, c.data + c.off[off[i]],
16,029 ✔
1485
                           static_cast<std::int64_t>(c.off[off[i + 1]]) -
16,029 ✔
1486
                               c.off[off[i]]);
16,020 ✔
1487
                return;
16,020 ✔
1488
            }
1489
            for (std::int32_t k = off[i]; k < off[i + 1]; ++k) {
43,964 ✔
1490
                if (k != off[i]) {
40,148 ✔
1491
                    std::memcpy(dst, sep.data(), static_cast<std::size_t>(sl));
36,332 ✔
1492
                    dst += sl;
36,332 ✔
1493
                }
5 ✔
1494
                copy_bytes(dst, c.row(k), c.len(k));
40,148 ✔
1495
                dst += c.len(k);
40,148 ✔
1496
            }
8 ✔
1497
        });
12 ✔
1498
    share_nulls(out, v);
130 ✔
1499
    return out;
130 ✔
1500
}
2 ✔
1501

1502
template <class F>
1503
dftu_series* split_impl(const dftu_series* v, const F& f,
133 ✔
1504
                        std::string_view sep) {
1505
    const std::int64_t n = f.n;
133 ✔
1506
    const std::int64_t lo = f.lo();
133 ✔
1507
    const std::size_t nbytes = static_cast<std::size_t>(f.hi() - lo);
133 ✔
1508
    const std::uint8_t* p = f.data != nullptr ? f.data + lo : nullptr;
133 !
1509
    const std::size_t sl = sep.size();
133 ✔
1510
    Bits cand;
133 ✔
1511
    if (sl != 0)
133 !
1512
        cand = compute_bits(simd::BitKind::Eq, p, nbytes,
128 !
1513
                            static_cast<std::uint8_t>(sep[0]), false);
121 !
1514

1515
    // Pass 1: where the separator occurs in each row, the part counts and
1516
    // the bytes the parts hold.
1517
    std::vector<std::size_t> hits;
133 ✔
1518
    std::vector<std::int32_t> list_off(static_cast<std::size_t>(n + 1), 0);
133 !
1519
    std::int64_t parts = 0, bytes = 0;
133 ✔
1520
    for (std::int64_t i = 0; i < n; ++i) {
158,144 ✔
1521
        if (!f.is_null(i)) {
158,011 ✔
1522
            const std::size_t s = static_cast<std::size_t>(f.off[i] - lo);
154,935 ✔
1523
            const std::size_t e = static_cast<std::size_t>(f.off[i + 1] - lo);
154,935 ✔
1524
            const std::size_t before = hits.size();
154,935 ✔
1525
            std::size_t pos = s;
154,935 ✔
1526
            while (sl != 0 && pos < e) {
431,567 !
1527
                const std::size_t q =
402,769 ✔
1528
                    find_sep(cand.bad.data(), p, pos, e, sep, false);
402,769 !
1529
                if (q == simd::NPOS) break;
402,769 ✔
1530
                hits.push_back(q);
276,632 !
1531
                pos = q + sl;
276,632 ✔
1532
            }
1533
            const std::int64_t h =
154,935 ✔
1534
                static_cast<std::int64_t>(hits.size() - before);
154,935 ✔
1535
            parts += h + 1;
154,935 ✔
1536
            bytes += static_cast<std::int64_t>(e - s) -
154,977 ✔
1537
                     h * static_cast<std::int64_t>(sl);
154,935 ✔
1538
            if (parts > std::numeric_limits<std::int32_t>::max())
154,935 !
1539
                return nullptr;
×
1540
        }
42 ✔
1541
        list_off[static_cast<std::size_t>(i + 1)] =
158,011 ✔
1542
            static_cast<std::int32_t>(parts);
47 ✔
1543
    }
47 ✔
1544
    if (bytes > std::numeric_limits<std::int32_t>::max()) return nullptr;
133 !
1545

1546
    // Pass 2: copy the parts.
1547
    auto coff = Buffer::allocate(static_cast<std::size_t>(parts + 1) *
133 !
1548
                                 sizeof(std::int32_t));
1549
    auto cdata = Buffer::allocate(static_cast<std::size_t>(bytes));
133 !
1550
    auto* co = reinterpret_cast<std::int32_t*>(coff->data());
133 ✔
1551
    std::uint8_t* cd = cdata->data();
133 ✔
1552
    std::int64_t at = 0, k = 0;
133 ✔
1553
    co[0] = 0;
133 ✔
1554
    std::size_t h = 0;
133 ✔
1555
    for (std::int64_t i = 0; i < n; ++i) {
158,144 ✔
1556
        if (f.is_null(i)) continue;
158,011 ✔
1557
        const std::size_t e = static_cast<std::size_t>(f.off[i + 1] - lo);
154,935 ✔
1558
        std::size_t start = static_cast<std::size_t>(f.off[i] - lo);
154,935 ✔
1559
        auto emit = [&](std::size_t from, std::size_t to) {
1,018,008 ✔
1560
            copy_bytes(cd + at, p + from, static_cast<std::int64_t>(to - from));
431,567 ✔
1561
            at += static_cast<std::int64_t>(to - from);
431,567 ✔
1562
            co[++k] = static_cast<std::int32_t>(at);
431,567 ✔
1563
        };
61 ✔
1564
        while (h < hits.size() && hits[h] < e) {
431,567 ✔
1565
            emit(start, hits[h]);
276,632 !
1566
            start = hits[h] + sl;
276,632 ✔
1567
            ++h;
276,632 ✔
1568
        }
1569
        emit(start, e);
154,935 !
1570
    }
42 ✔
1571
    return make_str_list(v, list_off, parts, std::move(coff), std::move(cdata));
133 !
1572
}
133 ✔
1573

1574
template <class F>
1575
dftu_series* partition_impl(const dftu_series* v, const F& f,
139 ✔
1576
                            std::string_view sep, bool right) {
1577
    const std::int64_t n = f.n;
139 ✔
1578
    const std::int64_t lo = f.lo();
139 ✔
1579
    const std::size_t nbytes = static_cast<std::size_t>(f.hi() - lo);
139 ✔
1580
    const std::uint8_t* p = f.data != nullptr ? f.data + lo : nullptr;
139 !
1581
    const std::size_t sl = sep.size();
139 ✔
1582
    const Bits cand = compute_bits(simd::BitKind::Eq, p, nbytes,
143 ✔
1583
                                   static_cast<std::uint8_t>(sep[0]), false);
139 !
1584
    std::vector<std::size_t> at(static_cast<std::size_t>(n), simd::NPOS);
139 !
1585
    std::vector<std::int32_t> list_off(static_cast<std::size_t>(n + 1), 0);
139 !
1586
    std::int64_t parts = 0, bytes = 0;
139 ✔
1587
    for (std::int64_t i = 0; i < n; ++i) {
22,303 ✔
1588
        if (!f.is_null(i)) {
22,164 ✔
1589
            const std::size_t s = static_cast<std::size_t>(f.off[i] - lo);
18,221 ✔
1590
            const std::size_t e = static_cast<std::size_t>(f.off[i + 1] - lo);
18,221 ✔
1591
            at[static_cast<std::size_t>(i)] =
18,221 ✔
1592
                find_sep(cand.bad.data(), p, s, e, sep, right);
18,221 !
1593
            parts += 3;
18,221 ✔
1594
            bytes += static_cast<std::int64_t>(e - s);
18,221 ✔
1595
            if (parts > std::numeric_limits<std::int32_t>::max())
18,221 !
1596
                return nullptr;
×
1597
        }
24 ✔
1598
        list_off[static_cast<std::size_t>(i + 1)] =
22,164 ✔
1599
            static_cast<std::int32_t>(parts);
26 ✔
1600
    }
26 ✔
1601
    if (bytes > std::numeric_limits<std::int32_t>::max()) return nullptr;
139 !
1602
    auto coff = Buffer::allocate(static_cast<std::size_t>(parts + 1) *
139 !
1603
                                 sizeof(std::int32_t));
1604
    auto cdata = Buffer::allocate(static_cast<std::size_t>(bytes));
139 !
1605
    auto* co = reinterpret_cast<std::int32_t*>(coff->data());
139 ✔
1606
    std::uint8_t* cd = cdata->data();
139 ✔
1607
    std::int64_t w = 0, k = 0;
139 ✔
1608
    co[0] = 0;
139 ✔
1609
    auto put = [&](const std::uint8_t* src, std::int64_t len) {
109,393 ✔
1610
        copy_bytes(cd + w, src, len);
54,663 ✔
1611
        w += len;
54,663 ✔
1612
        co[++k] = static_cast<std::int32_t>(w);
54,663 ✔
1613
    };
72 ✔
1614
    for (std::int64_t i = 0; i < n; ++i) {
22,303 ✔
1615
        if (f.is_null(i)) continue;
22,164 ✔
1616
        const std::size_t s = static_cast<std::size_t>(f.off[i] - lo);
18,221 ✔
1617
        const std::size_t e = static_cast<std::size_t>(f.off[i + 1] - lo);
18,221 ✔
1618
        const std::size_t q = at[static_cast<std::size_t>(i)];
18,221 ✔
1619
        if (q == simd::NPOS) {
18,221 ✔
1620
            // Python: (row, "", "") for partition, ("", "", row) for
1621
            // rpartition.
1622
            if (!right) {
11,583 ✔
1623
                put(p + s, static_cast<std::int64_t>(e - s));
5,262 !
1624
                put(p, 0);
5,262 !
1625
                put(p, 0);
5,262 !
1626
            } else {
9 ✔
1627
                put(p, 0);
6,321 !
1628
                put(p, 0);
6,321 !
1629
                put(p + s, static_cast<std::int64_t>(e - s));
6,321 !
1630
            }
1631
            continue;
11,583 ✔
1632
        }
1633
        put(p + s, static_cast<std::int64_t>(q - s));
6,638 !
1634
        put(p + q, static_cast<std::int64_t>(sl));
6,638 !
1635
        put(p + q + sl, static_cast<std::int64_t>(e - q - sl));
6,638 !
1636
    }
6 ✔
1637
    return make_str_list(v, list_off, parts, std::move(coff), std::move(cdata));
139 !
1638
}
139 ✔
1639

1640
template <class F>
1641
dftu_series* rfind_impl(const dftu_series* v, const F& f,
112 ✔
1642
                        std::string_view needle) {
1643
    const std::int64_t lo = f.lo();
112 ✔
1644
    const std::size_t nbytes = static_cast<std::size_t>(f.hi() - lo);
112 ✔
1645
    const std::uint8_t* p = f.data != nullptr ? f.data + lo : nullptr;
112 !
1646
    Bits cand;
112 ✔
1647
    if (!needle.empty())
112 !
1648
        cand = compute_bits(simd::BitKind::Eq, p, nbytes,
105 !
1649
                            static_cast<std::uint8_t>(needle[0]), false);
100 !
1650
    auto* out = new dftu_series();
112 !
1651
    out->type = TypeId::Int64;
112 ✔
1652
    out->encoding = Encoding::Flat;
112 ✔
1653
    out->length = f.n;
112 ✔
1654
    out->null_count = v->null_count;
112 ✔
1655
    out->validity = v->validity;
112 ✔
1656
    out->data = Buffer::allocate(buffer_bytes(TypeId::Int64, f.n));
112 !
1657
    auto* r = reinterpret_cast<std::int64_t*>(out->data->data());
112 ✔
1658
    for (std::int64_t i = 0; i < f.n; ++i) {
15,680 ✔
1659
        if (f.is_null(i)) {
15,568 ✔
1660
            r[i] = 0;
2,812 ✔
1661
            continue;
2,812 ✔
1662
        }
1663
        const std::size_t s = static_cast<std::size_t>(f.off[i] - lo);
12,756 ✔
1664
        const std::size_t e = static_cast<std::size_t>(f.off[i + 1] - lo);
12,756 ✔
1665
        if (needle.empty()) {
12,756 !
1666
            r[i] = static_cast<std::int64_t>(e - s);
1,796 ✔
1667
            continue;
1,796 ✔
1668
        }
1669
        const std::size_t q = find_sep(cand.bad.data(), p, s, e, needle, true);
10,960 !
1670
        r[i] = q == simd::NPOS ? -1 : static_cast<std::int64_t>(q - s);
10,960 ✔
1671
    }
26 ✔
1672
    return out;
112 ✔
1673
}
112 ✔
1674

1675
std::int64_t count_literal(std::string_view s, std::string_view pat) {
1,479 ✔
1676
    if (pat.empty()) return static_cast<std::int64_t>(s.size()) + 1;
1,479 ✔
1677
    std::int64_t n = 0;
1,462 ✔
1678
    std::size_t pos = 0;
1,462 ✔
1679
    while (true) {
1,811 ✔
1680
        const std::size_t hit = s.find(pat, pos);
3,714 ✔
1681
        if (hit == std::string_view::npos) break;
3,715 ✔
1682
        ++n;
2,252 ✔
1683
        pos = hit + pat.size();
2,252 ✔
1684
    }
1,136 ✔
1685
    return n;
1,463 ✔
1686
}
700 ✔
1687

1688
}  // namespace
1689

1690
dftu_series* dftu_series_str_slice(const dftu_series* v, int64_t start,
172 ✔
1691
                                   int64_t length) {
1692
    if (!v || !is_string_kind(v->type)) return nullptr;
172 !
1693
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_slice(flat_v, start, length));
144 !
1694
    return slice_impl(v, start, length);
138 ✔
1695
}
11 ✔
1696

1697
dftu_series* dftu_series_str_pad_start(const dftu_series* v, int64_t width,
139 ✔
1698
                                       char fill) {
1699
    if (!v || !is_string_kind(v->type)) return nullptr;
139 !
1700
    DFTU_FLAT_OPERAND(v, flat_v,
111 !
1701
                      dftu_series_str_pad_start(flat_v, width, fill));
1702
    return pad_impl(v, width, fill, 0);
105 ✔
1703
}
3 ✔
1704
dftu_series* dftu_series_str_pad_end(const dftu_series* v, int64_t width,
137 ✔
1705
                                     char fill) {
1706
    if (!v || !is_string_kind(v->type)) return nullptr;
137 !
1707
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_pad_end(flat_v, width, fill));
109 !
1708
    return pad_impl(v, width, fill, 1);
103 ✔
1709
}
3 ✔
1710
dftu_series* dftu_series_str_center(const dftu_series* v, int64_t width,
133 ✔
1711
                                    char fill) {
1712
    if (!v || !is_string_kind(v->type)) return nullptr;
133 !
1713
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_center(flat_v, width, fill));
105 !
1714
    return pad_impl(v, width, fill, 2);
99 ✔
1715
}
5 ✔
1716
dftu_series* dftu_series_str_zfill(const dftu_series* v, int64_t width) {
127 ✔
1717
    if (!v || !is_string_kind(v->type)) return nullptr;
127 !
1718
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_zfill(flat_v, width));
99 !
1719
    return zfill_impl(v, width);
93 ✔
1720
}
3 ✔
1721

1722
dftu_series* dftu_series_str_split(const dftu_series* v, const char* sep,
167 ✔
1723
                                   int32_t sep_len) {
1724
    if (!v || !is_string_kind(v->type)) return nullptr;
167 !
1725
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_split(flat_v, sep, sep_len));
139 !
1726
    // An empty separator is no split point: one part, the whole row.
1727
    const std::string_view s_sep(sep, static_cast<std::size_t>(sep_len));
133 ✔
1728
    return with_flat(v, [&](const auto& f) { return split_impl(v, f, s_sep); });
266 !
1729
}
7 ✔
1730

1731
dftu_series* dftu_series_str_partition(const dftu_series* v, const char* sep,
173 ✔
1732
                                       int32_t sep_len, int32_t from_right) {
1733
    if (!v || !is_string_kind(v->type) || sep_len <= 0) return nullptr;
173 !
1734
    DFTU_FLAT_OPERAND(
145 !
1735
        v, flat_v, dftu_series_str_partition(flat_v, sep, sep_len, from_right));
1736
    const std::string_view s_sep(sep, static_cast<std::size_t>(sep_len));
139 ✔
1737
    return with_flat(v, [&](const auto& f) {
413 !
1738
        return partition_impl(v, f, s_sep, from_right != 0);
139 ✔
1739
    });
135 ✔
1740
}
4 ✔
1741

1742
dftu_series* dftu_series_str_rfind(const dftu_series* v, const char* needle,
146 ✔
1743
                                   int32_t needle_len) {
1744
    if (!v || !is_string_kind(v->type)) return nullptr;
146 !
1745
    DFTU_FLAT_OPERAND(v, flat_v,
118 !
1746
                      dftu_series_str_rfind(flat_v, needle, needle_len));
1747
    const std::string_view n(needle, static_cast<std::size_t>(needle_len));
112 ✔
1748
    return with_flat(v, [&](const auto& f) { return rfind_impl(v, f, n); });
224 !
1749
}
5 ✔
1750

1751
dftu_series* dftu_series_str_case(const dftu_series* v, int32_t op) {
162 ✔
1752
    if (!v || !is_string_kind(v->type)) return nullptr;
162 !
1753
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_case(flat_v, op));
134 !
1754
    switch (static_cast<dftu_str_case>(op)) {
127 ✔
1755
        case DFTU_STR_CAPITALIZE:
38 ✔
1756
            return case_impl(v, simd::CaseKind::Lower, true);
42 ✔
1757
        case DFTU_STR_TITLE_CASE:
52 ✔
1758
            return case_impl(v, simd::CaseKind::Title, false);
55 ✔
1759
        case DFTU_STR_SWAPCASE:
25 ✔
1760
            return case_impl(v, simd::CaseKind::Swapcase, false);
28 ✔
1761
    }
1762
    return nullptr;
2 ✔
1763
}
11 ✔
1764

1765
dftu_series* dftu_series_str_is(const dftu_series* v, int32_t cls) {
328 ✔
1766
    if (!v) return nullptr;
328 ✔
1767
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_is(flat_v, cls));
328 !
1768
    return is_impl(v, cls);
314 ✔
1769
}
22 ✔
1770

1771
dftu_series* dftu_series_str_count(const dftu_series* v, const char* pat,
81 ✔
1772
                                   int32_t pat_len) {
1773
    if (!v || !is_string_kind(v->type)) return nullptr;
81 !
1774
    std::string_view p(pat, static_cast<std::size_t>(pat_len));
52 ✔
1775
    return string_int64(
52 !
1776
        v, [p](std::string_view s) { return count_literal(s, p); });
1,531 ✔
1777
}
16 ✔
1778

1779
dftu_series* dftu_series_str_remove_prefix(const dftu_series* v,
116 ✔
1780
                                           const char* prefix, int32_t len) {
1781
    if (!v || !is_string_kind(v->type)) return nullptr;
116 !
1782
    DFTU_FLAT_OPERAND(v, flat_v,
88 !
1783
                      dftu_series_str_remove_prefix(flat_v, prefix, len));
1784
    return remove_affix_impl(
160 !
1785
        v, std::string_view(prefix, static_cast<std::size_t>(len)), true);
82 ✔
1786
}
4 ✔
1787

1788
dftu_series* dftu_series_str_remove_suffix(const dftu_series* v,
112 ✔
1789
                                           const char* suffix, int32_t len) {
1790
    if (!v || !is_string_kind(v->type)) return nullptr;
112 !
1791
    DFTU_FLAT_OPERAND(v, flat_v,
84 !
1792
                      dftu_series_str_remove_suffix(flat_v, suffix, len));
1793
    return remove_affix_impl(
153 !
1794
        v, std::string_view(suffix, static_cast<std::size_t>(len)), false);
78 ✔
1795
}
3 ✔
1796

1797
dftu_series* dftu_series_str_repeat(const dftu_series* v, int64_t n) {
106 ✔
1798
    if (!v || !is_string_kind(v->type) || n < 0) return nullptr;
106 !
1799
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_str_repeat(flat_v, n));
76 !
1800
    return repeat_impl(v, n);
70 ✔
1801
}
5 ✔
1802

1803
dftu_series* dftu_series_str_cat(const dftu_series* a, const dftu_series* b) {
1,618 ✔
1804
    if (!a || !b) return nullptr;
1,618 !
1805
    DFTU_FLAT_OPERAND(a, flat_a, dftu_series_str_cat(flat_a, b));
1,618 !
1806
    DFTU_FLAT_OPERAND(b, flat_b, dftu_series_str_cat(a, flat_b));
1,604 !
1807
    if (!is_string_kind(a->type) || !is_string_kind(b->type) ||
2,508 !
1808
        a->length != b->length)
1,562 ✔
1809
        return nullptr;
30 ✔
1810
    const bool wa = is_wide_offset_type(a->type);
1,560 ✔
1811
    const bool wb = is_wide_offset_type(b->type);
1,560 ✔
1812
    if (wa && wb) return cat_impl<std::int64_t, std::int64_t>(a, b);
1,560 !
1813
    if (wa) return cat_impl<std::int64_t, std::int32_t>(a, b);
1,560 !
1814
    if (wb) return cat_impl<std::int32_t, std::int64_t>(a, b);
1,560 !
1815
    return cat_impl<std::int32_t, std::int32_t>(a, b);
1,560 ✔
1816
}
644 ✔
1817

1818
dftu_series* dftu_series_list_join(const dftu_series* v, const char* sep,
192 ✔
1819
                                   int32_t sep_len) {
1820
    DFTU_FLAT_OPERAND(v, flat_v, dftu_series_list_join(flat_v, sep, sep_len));
192 !
1821
    if (!v || v->type != TypeId::List || v->encoding != Encoding::Flat)
178 !
1822
        return nullptr;
48 ✔
1823
    if (!dftu_series_offsets(v) || !v->child() ||
258 !
1824
        !is_string_kind(v->child()->type))
130 !
1825
        return nullptr;
×
1826
    const dftu_series& child = *v->child();
130 ✔
1827
    const std::string_view s(sep, static_cast<std::size_t>(sep_len));
130 ✔
1828
    return is_wide_offset_type(child.type)
130 !
1829
               ? join_impl<std::int64_t>(v, child, s)
128 !
1830
               : join_impl<std::int32_t>(v, child, s);
130 !
1831
}
3 ✔
1832

1833
dftu_series* dftu_series_str_findall(const dftu_series* v, const char* pattern,
65 ✔
1834
                                     int32_t pattern_len) {
1835
    if (!v || !is_string_kind(v->type)) return nullptr;
65 !
1836
    DFTU_FLAT_OPERAND(v, flat_v,
37 !
1837
                      dftu_series_str_findall(flat_v, pattern, pattern_len));
1838
    const auto re = compiled(
3 !
1839
        duql::compile_regex(pattern_text(pattern, pattern_len), false));
31 !
1840
    if (!re) return nullptr;
31 ✔
1841
    LimitedRows limited;
28 ✔
1842
    dftu_series* out = list_transform(
28 !
1843
        v, [&](std::string_view row, std::vector<std::string>& parts) {
1,944 ✔
1844
            const auto r = duql::findall(
3,872 !
1845
                *re, row, [&](std::string_view m) { parts.emplace_back(m); });
29,684 ✔
1846
            if (r == duql::MatchResult::LIMIT) limited.add(row);
1,942 ✔
1847
        });
1,942 ✔
1848
    return limited.apply(out, v);
28 !
1849
}
31 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc