Coverage Report

Created: 2026-09-21 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zeek/build/src/strings.bif.func_def
Line
Count
Source
1
// This file was automatically generated by bifcl from strings.bif.
2
3
4
#line 7 "/src/zeek/src/strings.bif"
5
 // C segment
6
#include <vector>
7
#include <algorithm>
8
#include <cctype>
9
10
#include "zeek/SmithWaterman.h"
11
12
using namespace std;
13
zeek_uint_t zeek::BifFunc::levenshtein_distance_native(zeek::StringVal* s1, zeek::StringVal* s2)
14
#line 26 "/src/zeek/src/strings.bif"
15
16
  
17
#line 27 "/src/zeek/src/strings.bif"
18
0
{
19
0
#line 27 "/src/zeek/src/strings.bif"
20
21
0
  unsigned int n = s1->Len();
22
0
  unsigned int m = s2->Len();
23
24
0
  if ( ! n )
25
0
    return m;
26
27
0
  if ( ! m )
28
0
    return n;
29
30
0
  vector<vector<unsigned int> > d(n + 1, vector<unsigned int>(m + 1));
31
32
0
  d[0][0] = 0;
33
34
0
  for ( unsigned int i = 1; i <= n; ++i )
35
0
    d[i][0] = i;
36
37
0
  for ( unsigned int i = 1; i <= m; ++i )
38
0
    d[0][i] = i;
39
40
0
  for ( unsigned int i = 1; i <= n; ++i )
41
0
    {
42
0
    for ( unsigned int j = 1; j <= m; ++j )
43
0
      d[i][j] = std::min({d[i-1][j] + 1, d[i][j-1] + 1,
44
0
                        d[i-1][j-1] + (s1->Bytes()[i-1] == s2->Bytes()[j-1] ? 0 : 1)});
45
0
    }
46
47
0
  return d[n][m];
48
0
  } // end of BifFunc::levenshtein_distance
49
50
#line 55 "/src/zeek/src/strings.bif"
51
52
#line 26 "/src/zeek/src/strings.bif"
53
zeek::ValPtr zeek::BifFunc::levenshtein_distance_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
54
#line 26 "/src/zeek/src/strings.bif"
55
0
  {
56
0
#line 26 "/src/zeek/src/strings.bif"
57
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
58
0
#line 26 "/src/zeek/src/strings.bif"
59
0
    {
60
0
#line 26 "/src/zeek/src/strings.bif"
61
0
    zeek::emit_builtin_error(zeek::util::fmt("levenshtein_distance() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
62
0
#line 26 "/src/zeek/src/strings.bif"
63
0
    return nullptr;
64
0
#line 26 "/src/zeek/src/strings.bif"
65
0
    }
66
0
#line 26 "/src/zeek/src/strings.bif"
67
0
  zeek::StringVal* s1 = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
68
0
#line 26 "/src/zeek/src/strings.bif"
69
0
  zeek::StringVal* s2 = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
70
0
#line 55 "/src/zeek/src/strings.bif"
71
0
  return zeek::val_mgr->Count(zeek::BifFunc::levenshtein_distance_native(s1, s2));
72
0
#line 55 "/src/zeek/src/strings.bif"
73
0
  } // end of shim for BifFunc::levenshtein_distance
74
zeek::ValPtr zeek::BifFunc::string_cat_native(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
75
76
#line 65 "/src/zeek/src/strings.bif"
77
78
  
79
#line 66 "/src/zeek/src/strings.bif"
80
0
{
81
0
#line 66 "/src/zeek/src/strings.bif"
82
83
0
  int n = 0;
84
0
  for ( const auto& a : (*BiF_ARGS) )
85
0
    {
86
0
    if ( a->GetType()->Tag() != TYPE_STRING )
87
0
      {
88
0
      zeek::reporter->Error("string_cat() requires string arguments");
89
0
      return val_mgr->EmptyString();
90
0
      }
91
92
0
    n += a->AsString()->Len();
93
0
    }
94
95
0
  u_char* b = new u_char[n+1];
96
0
  zeek::String* s = new zeek::String(true, b, n);
97
98
0
  for ( const auto& a : (*BiF_ARGS) )
99
0
    {
100
0
    const zeek::String* s = a->AsString();
101
0
    memcpy(b, s->Bytes(), s->Len());
102
0
    b += s->Len();
103
0
    }
104
0
  *b = 0;
105
106
0
  return zeek::make_intrusive<zeek::StringVal>(s);
107
0
  } // end of BifFunc::string_cat
108
109
#line 91 "/src/zeek/src/strings.bif"
110
111
#line 65 "/src/zeek/src/strings.bif"
112
zeek::ValPtr zeek::BifFunc::string_cat_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
113
#line 65 "/src/zeek/src/strings.bif"
114
0
  {
115
0
#line 91 "/src/zeek/src/strings.bif"
116
0
  return zeek::BifFunc::string_cat_native(frame, BiF_ARGS);
117
0
#line 91 "/src/zeek/src/strings.bif"
118
0
  } // end of shim for BifFunc::string_cat
119
zeek::ValPtr zeek::BifFunc::join_string_vec_native(zeek::Val* vec, zeek::StringVal* sep)
120
#line 105 "/src/zeek/src/strings.bif"
121
122
  
123
#line 106 "/src/zeek/src/strings.bif"
124
2.59k
{
125
2.59k
#line 106 "/src/zeek/src/strings.bif"
126
127
2.59k
  ODesc d;
128
2.59k
  d.SetStyle(RAW_STYLE);
129
130
2.59k
  zeek::VectorVal *v = vec->AsVectorVal();
131
132
40.9k
  for ( unsigned i = 0; i < v->Size(); ++i )
133
38.3k
    {
134
38.3k
    if ( i > 0 )
135
35.7k
      d.AddN(reinterpret_cast<const char*>(sep->Bytes()), sep->Len());
136
137
38.3k
    auto e = v->ValAt(i);
138
139
    // If the element is empty, skip it.
140
38.3k
    if ( ! e )
141
18.1k
      continue;
142
143
20.2k
    e->Describe(&d);
144
20.2k
    }
145
146
2.59k
  zeek::String* s = new zeek::String(true, d.TakeBytes(), d.Size());
147
2.59k
  s->SetUseFreeToDelete(true);
148
149
2.59k
  return zeek::make_intrusive<zeek::StringVal>(s);
150
2.59k
  } // end of BifFunc::join_string_vec
151
152
#line 130 "/src/zeek/src/strings.bif"
153
154
#line 105 "/src/zeek/src/strings.bif"
155
zeek::ValPtr zeek::BifFunc::join_string_vec_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
156
#line 105 "/src/zeek/src/strings.bif"
157
2.59k
  {
158
2.59k
#line 105 "/src/zeek/src/strings.bif"
159
2.59k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
160
0
#line 105 "/src/zeek/src/strings.bif"
161
0
    {
162
0
#line 105 "/src/zeek/src/strings.bif"
163
0
    zeek::emit_builtin_error(zeek::util::fmt("join_string_vec() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
164
0
#line 105 "/src/zeek/src/strings.bif"
165
0
    return nullptr;
166
0
#line 105 "/src/zeek/src/strings.bif"
167
0
    }
168
2.59k
#line 105 "/src/zeek/src/strings.bif"
169
2.59k
  zeek::Val* vec = (zeek::Val*) ((*BiF_ARGS)[0].get()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
170
2.59k
#line 105 "/src/zeek/src/strings.bif"
171
2.59k
  zeek::StringVal* sep = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
172
2.59k
#line 130 "/src/zeek/src/strings.bif"
173
2.59k
  return zeek::BifFunc::join_string_vec_native(vec, sep);
174
2.59k
#line 130 "/src/zeek/src/strings.bif"
175
2.59k
  } // end of shim for BifFunc::join_string_vec
176
zeek::ValPtr zeek::BifFunc::join_string_set_native(zeek::Val* ss, zeek::StringVal* sep)
177
#line 145 "/src/zeek/src/strings.bif"
178
179
  
180
#line 146 "/src/zeek/src/strings.bif"
181
122k
{
182
122k
#line 146 "/src/zeek/src/strings.bif"
183
184
122k
  ODesc d;
185
122k
  d.SetStyle(RAW_STYLE);
186
187
122k
  if ( ! ss->GetType()->IsSet () )
188
0
    {
189
0
    zeek::emit_builtin_error("join_string_set() requires a string set argument");
190
0
    return val_mgr->EmptyString();
191
0
    }
192
193
122k
  const auto& it = ss->GetType()->AsTableType()->GetIndexTypes();
194
122k
  if ( it.size() != 1  || it[0]->Tag() != TYPE_STRING )
195
0
    {
196
0
    zeek::emit_builtin_error("join_string_set() requires a string set argument");
197
0
    return val_mgr->EmptyString();
198
0
    }
199
200
122k
  int i = 0;
201
122k
  TableVal* tv = ss->AsTableVal();
202
122k
  const PDict<TableEntryVal>* loop_vals = tv->AsTable();
203
204
122k
  if ( ! loop_vals->Length() )
205
0
    return val_mgr->EmptyString();
206
207
122k
  for ( const auto& iter : *loop_vals )
208
139k
    {
209
139k
    if ( i > 0 )
210
16.9k
      d.AddN(reinterpret_cast<const char*>(sep->Bytes()), sep->Len());
211
212
    // Not sure this is fast - I guess we don't have access to the
213
    // values used for the keys directly anymore.
214
139k
    auto k = iter.GetHashKey();
215
139k
    auto ind_lv = tv->RecreateIndex(*k);
216
139k
    ind_lv->Describe(&d);
217
218
139k
    ++i;
219
139k
    }
220
221
122k
  zeek::String* str = new zeek::String(true, d.TakeBytes(), d.Size());
222
122k
  str->SetUseFreeToDelete(true);
223
224
122k
  return zeek::make_intrusive<zeek::StringVal>(str);
225
122k
  } // end of BifFunc::join_string_set
226
227
#line 188 "/src/zeek/src/strings.bif"
228
229
#line 145 "/src/zeek/src/strings.bif"
230
zeek::ValPtr zeek::BifFunc::join_string_set_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
231
#line 145 "/src/zeek/src/strings.bif"
232
122k
  {
233
122k
#line 145 "/src/zeek/src/strings.bif"
234
122k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
235
0
#line 145 "/src/zeek/src/strings.bif"
236
0
    {
237
0
#line 145 "/src/zeek/src/strings.bif"
238
0
    zeek::emit_builtin_error(zeek::util::fmt("join_string_set() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
239
0
#line 145 "/src/zeek/src/strings.bif"
240
0
    return nullptr;
241
0
#line 145 "/src/zeek/src/strings.bif"
242
0
    }
243
122k
#line 145 "/src/zeek/src/strings.bif"
244
122k
  zeek::Val* ss = (zeek::Val*) ((*BiF_ARGS)[0].get()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
245
122k
#line 145 "/src/zeek/src/strings.bif"
246
122k
  zeek::StringVal* sep = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
247
122k
#line 188 "/src/zeek/src/strings.bif"
248
122k
  return zeek::BifFunc::join_string_set_native(ss, sep);
249
122k
#line 188 "/src/zeek/src/strings.bif"
250
122k
  } // end of shim for BifFunc::join_string_set
251
zeek::ValPtr zeek::BifFunc::edit_native(zeek::StringVal* arg_s, zeek::StringVal* arg_edit_char)
252
#line 208 "/src/zeek/src/strings.bif"
253
254
  
255
#line 209 "/src/zeek/src/strings.bif"
256
0
{
257
0
#line 209 "/src/zeek/src/strings.bif"
258
259
0
  if ( arg_edit_char->Len() != 1 )
260
0
    zeek::emit_builtin_error("not exactly one edit character", arg_edit_char);
261
262
0
  const u_char* s = arg_s->Bytes();
263
0
  const u_char* edit_s = arg_edit_char->Bytes();
264
265
0
  u_char edit_c = *edit_s;
266
267
0
  int n = arg_s->Len();
268
0
  u_char* new_s = new u_char[n+1];
269
0
  int ind = 0;
270
271
0
  for ( int i = 0; i < n; ++i )
272
0
    {
273
0
    if ( s[i] == edit_c )
274
0
      { // Delete last character
275
0
      if ( --ind < 0 )
276
0
        ind = 0;
277
0
      }
278
0
    else
279
0
      new_s[ind++] = s[i];
280
0
    }
281
282
0
  new_s[ind] = '\0';
283
284
0
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, reinterpret_cast<byte_vec>(new_s), ind));
285
0
  } // end of BifFunc::edit
286
287
#line 236 "/src/zeek/src/strings.bif"
288
289
#line 208 "/src/zeek/src/strings.bif"
290
zeek::ValPtr zeek::BifFunc::edit_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
291
#line 208 "/src/zeek/src/strings.bif"
292
0
  {
293
0
#line 208 "/src/zeek/src/strings.bif"
294
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
295
0
#line 208 "/src/zeek/src/strings.bif"
296
0
    {
297
0
#line 208 "/src/zeek/src/strings.bif"
298
0
    zeek::emit_builtin_error(zeek::util::fmt("edit() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
299
0
#line 208 "/src/zeek/src/strings.bif"
300
0
    return nullptr;
301
0
#line 208 "/src/zeek/src/strings.bif"
302
0
    }
303
0
#line 208 "/src/zeek/src/strings.bif"
304
0
  zeek::StringVal* arg_s = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
305
0
#line 208 "/src/zeek/src/strings.bif"
306
0
  zeek::StringVal* arg_edit_char = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
307
0
#line 236 "/src/zeek/src/strings.bif"
308
0
  return zeek::BifFunc::edit_native(arg_s, arg_edit_char);
309
0
#line 236 "/src/zeek/src/strings.bif"
310
0
  } // end of shim for BifFunc::edit
311
zeek::ValPtr zeek::BifFunc::sub_bytes_native(zeek::StringVal* s, zeek_uint_t start, zeek_int_t n)
312
#line 248 "/src/zeek/src/strings.bif"
313
314
  
315
#line 249 "/src/zeek/src/strings.bif"
316
111
{
317
111
#line 249 "/src/zeek/src/strings.bif"
318
319
111
  if ( start > 0 )
320
0
    --start; // make it 0-based
321
322
111
  zeek::String* ss = s->AsString()->GetSubstring(start, n);
323
324
111
  if ( ! ss )
325
0
    ss = new zeek::String("");
326
327
111
  return zeek::make_intrusive<zeek::StringVal>(ss);
328
111
  } // end of BifFunc::sub_bytes
329
330
#line 259 "/src/zeek/src/strings.bif"
331
332
#line 248 "/src/zeek/src/strings.bif"
333
zeek::ValPtr zeek::BifFunc::sub_bytes_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
334
#line 248 "/src/zeek/src/strings.bif"
335
111
  {
336
111
#line 248 "/src/zeek/src/strings.bif"
337
111
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
338
0
#line 248 "/src/zeek/src/strings.bif"
339
0
    {
340
0
#line 248 "/src/zeek/src/strings.bif"
341
0
    zeek::emit_builtin_error(zeek::util::fmt("sub_bytes() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
342
0
#line 248 "/src/zeek/src/strings.bif"
343
0
    return nullptr;
344
0
#line 248 "/src/zeek/src/strings.bif"
345
0
    }
346
111
#line 248 "/src/zeek/src/strings.bif"
347
111
  zeek::StringVal* s = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
348
111
#line 248 "/src/zeek/src/strings.bif"
349
111
  zeek_uint_t start = (zeek_uint_t) ((*BiF_ARGS)[1].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
350
111
#line 248 "/src/zeek/src/strings.bif"
351
111
  zeek_int_t n = (zeek_int_t) ((*BiF_ARGS)[2].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
352
111
#line 259 "/src/zeek/src/strings.bif"
353
111
  return zeek::BifFunc::sub_bytes_native(s, start, n);
354
111
#line 259 "/src/zeek/src/strings.bif"
355
111
  } // end of shim for BifFunc::sub_bytes
356
357
#line 261 "/src/zeek/src/strings.bif"
358
359
static int match_prefix(int s_len, const char* s, int t_len, const char* t)
360
0
  {
361
0
  for ( int i = 0; i < t_len; ++i )
362
0
    {
363
0
    if ( i >= s_len || s[i] != t[i] )
364
0
      return 0;
365
0
    }
366
0
  return 1;
367
0
  }
368
369
static zeek::VectorValPtr do_split_string(zeek::StringVal* str_val,
370
                                          zeek::RE_Matcher* re, int incl_sep,
371
                                          int max_num_sep)
372
557k
  {
373
  // string_vec is used early in the version script - do not use the NetVar.
374
557k
  auto rval = zeek::make_intrusive<zeek::VectorVal>(zeek::id::find_type<zeek::VectorType>("string_vec"));
375
557k
  const u_char* s = str_val->Bytes();
376
557k
  int n = str_val->Len();
377
557k
  const u_char* end_of_s = s + n;
378
557k
  int num = 0;
379
557k
  int num_sep = 0;
380
381
557k
  int offset = 0;
382
557k
  bool bol = true;
383
557k
  const bool eol = true;
384
385
2.90M
  while ( n >= 0 )
386
2.90M
    {
387
2.90M
    offset = 0;
388
    // Find next match offset.
389
2.90M
    int end_of_match = 0;
390
485M
    while ( n > 0 )
391
485M
      {
392
485M
      end_of_match = re->MatchPrefix(s + offset, n, bol, eol);
393
485M
      if ( end_of_match > 0 )
394
2.34M
        break;
395
396
      // Move on to next byte, use BOL only on the byte such that
397
      // a BOL anchored pattern won't be matched anywhere else.
398
483M
      bol = false;
399
483M
      ++offset;
400
483M
      --n;
401
483M
      }
402
403
2.90M
    if ( max_num_sep && num_sep >= max_num_sep )
404
4.28k
      {
405
4.28k
      offset = end_of_s - s;
406
4.28k
      n=0;
407
4.28k
      }
408
409
2.90M
    rval->Assign(num++, zeek::make_intrusive<zeek::StringVal>(offset, reinterpret_cast<const char*>(s)));
410
411
    // No more separators will be needed if this is the end of string.
412
2.90M
    if ( n <= 0 )
413
557k
      break;
414
415
2.34M
    if ( incl_sep )
416
2.26M
      { // including the part that matches the pattern
417
2.26M
      rval->Assign(num++, zeek::make_intrusive<zeek::StringVal>(end_of_match, reinterpret_cast<const char*>(s+offset)));
418
2.26M
      }
419
420
2.34M
    if ( max_num_sep && num_sep >= max_num_sep )
421
0
      break;
422
423
2.34M
    ++num_sep;
424
425
2.34M
    n -= end_of_match;
426
2.34M
    s += offset + end_of_match;;
427
428
2.34M
    if ( s > end_of_s )
429
0
      zeek::reporter->InternalError("RegMatch in split goes beyond the string");
430
2.34M
    }
431
432
557k
  return rval;
433
557k
  }
434
435
zeek::Val* do_split(zeek::StringVal* str_val, zeek::RE_Matcher* re, int incl_sep, int max_num_sep)
436
0
  {
437
0
  auto* a = new zeek::TableVal(zeek::id::string_array);
438
0
  const u_char* s = str_val->Bytes();
439
0
  int n = str_val->Len();
440
0
  const u_char* end_of_s = s + n;
441
0
  int num = 0;
442
0
  int num_sep = 0;
443
444
0
  int offset = 0;
445
0
  while ( n >= 0 )
446
0
    {
447
0
    offset = 0;
448
    // Find next match offset.
449
0
    int end_of_match = 0;
450
0
    while ( n > 0 &&
451
0
            (end_of_match = re->MatchPrefix(s + offset, n)) <= 0 )
452
0
      {
453
      // Move on to next byte.
454
0
      ++offset;
455
0
      --n;
456
0
      }
457
458
0
    if ( max_num_sep && num_sep >= max_num_sep )
459
0
      {
460
0
      offset = end_of_s - s;
461
0
      n=0;
462
0
      }
463
464
0
    auto ind = zeek::val_mgr->Count(++num);
465
0
    a->Assign(std::move(ind), zeek::make_intrusive<zeek::StringVal>(offset, reinterpret_cast<const char*>(s)));
466
467
    // No more separators will be needed if this is the end of string.
468
0
    if ( n <= 0 )
469
0
      break;
470
471
0
    if ( incl_sep )
472
0
      { // including the part that matches the pattern
473
0
      ind = zeek::val_mgr->Count(++num);
474
0
      a->Assign(std::move(ind), zeek::make_intrusive<zeek::StringVal>(end_of_match, reinterpret_cast<const char*>(s+offset)));
475
0
      }
476
477
0
    if ( max_num_sep && num_sep >= max_num_sep )
478
0
      break;
479
480
0
    ++num_sep;
481
482
0
    n -= end_of_match;
483
0
    s += offset + end_of_match;;
484
485
0
    if ( s > end_of_s )
486
0
      zeek::reporter->InternalError("RegMatch in split goes beyond the string");
487
0
    }
488
489
0
  return a;
490
0
  }
491
zeek::ValPtr zeek::BifFunc::split_string_native(zeek::StringVal* str, RE_Matcher* re)
492
#line 407 "/src/zeek/src/strings.bif"
493
494
  
495
#line 408 "/src/zeek/src/strings.bif"
496
128k
{
497
128k
#line 408 "/src/zeek/src/strings.bif"
498
499
128k
  return do_split_string(str, re, 0, 0);
500
128k
  } // end of BifFunc::split_string
501
502
#line 410 "/src/zeek/src/strings.bif"
503
504
#line 407 "/src/zeek/src/strings.bif"
505
zeek::ValPtr zeek::BifFunc::split_string_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
506
#line 407 "/src/zeek/src/strings.bif"
507
128k
  {
508
128k
#line 407 "/src/zeek/src/strings.bif"
509
128k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
510
0
#line 407 "/src/zeek/src/strings.bif"
511
0
    {
512
0
#line 407 "/src/zeek/src/strings.bif"
513
0
    zeek::emit_builtin_error(zeek::util::fmt("split_string() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
514
0
#line 407 "/src/zeek/src/strings.bif"
515
0
    return nullptr;
516
0
#line 407 "/src/zeek/src/strings.bif"
517
0
    }
518
128k
#line 407 "/src/zeek/src/strings.bif"
519
128k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
520
128k
#line 407 "/src/zeek/src/strings.bif"
521
128k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
522
128k
#line 410 "/src/zeek/src/strings.bif"
523
128k
  return zeek::BifFunc::split_string_native(str, re);
524
128k
#line 410 "/src/zeek/src/strings.bif"
525
128k
  } // end of shim for BifFunc::split_string
526
zeek::ValPtr zeek::BifFunc::split_string1_native(zeek::StringVal* str, RE_Matcher* re)
527
#line 427 "/src/zeek/src/strings.bif"
528
529
  
530
#line 428 "/src/zeek/src/strings.bif"
531
383k
{
532
383k
#line 428 "/src/zeek/src/strings.bif"
533
534
383k
  return do_split_string(str, re, 0, 1);
535
383k
  } // end of BifFunc::split_string1
536
537
#line 430 "/src/zeek/src/strings.bif"
538
539
#line 427 "/src/zeek/src/strings.bif"
540
zeek::ValPtr zeek::BifFunc::split_string1_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
541
#line 427 "/src/zeek/src/strings.bif"
542
383k
  {
543
383k
#line 427 "/src/zeek/src/strings.bif"
544
383k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
545
0
#line 427 "/src/zeek/src/strings.bif"
546
0
    {
547
0
#line 427 "/src/zeek/src/strings.bif"
548
0
    zeek::emit_builtin_error(zeek::util::fmt("split_string1() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
549
0
#line 427 "/src/zeek/src/strings.bif"
550
0
    return nullptr;
551
0
#line 427 "/src/zeek/src/strings.bif"
552
0
    }
553
383k
#line 427 "/src/zeek/src/strings.bif"
554
383k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
555
383k
#line 427 "/src/zeek/src/strings.bif"
556
383k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
557
383k
#line 430 "/src/zeek/src/strings.bif"
558
383k
  return zeek::BifFunc::split_string1_native(str, re);
559
383k
#line 430 "/src/zeek/src/strings.bif"
560
383k
  } // end of shim for BifFunc::split_string1
561
zeek::ValPtr zeek::BifFunc::split_string_all_native(zeek::StringVal* str, RE_Matcher* re)
562
#line 447 "/src/zeek/src/strings.bif"
563
564
  
565
#line 448 "/src/zeek/src/strings.bif"
566
42.2k
{
567
42.2k
#line 448 "/src/zeek/src/strings.bif"
568
569
42.2k
  return do_split_string(str, re, 1, 0);
570
42.2k
  } // end of BifFunc::split_string_all
571
572
#line 450 "/src/zeek/src/strings.bif"
573
574
#line 447 "/src/zeek/src/strings.bif"
575
zeek::ValPtr zeek::BifFunc::split_string_all_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
576
#line 447 "/src/zeek/src/strings.bif"
577
42.2k
  {
578
42.2k
#line 447 "/src/zeek/src/strings.bif"
579
42.2k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
580
0
#line 447 "/src/zeek/src/strings.bif"
581
0
    {
582
0
#line 447 "/src/zeek/src/strings.bif"
583
0
    zeek::emit_builtin_error(zeek::util::fmt("split_string_all() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
584
0
#line 447 "/src/zeek/src/strings.bif"
585
0
    return nullptr;
586
0
#line 447 "/src/zeek/src/strings.bif"
587
0
    }
588
42.2k
#line 447 "/src/zeek/src/strings.bif"
589
42.2k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
590
42.2k
#line 447 "/src/zeek/src/strings.bif"
591
42.2k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
592
42.2k
#line 450 "/src/zeek/src/strings.bif"
593
42.2k
  return zeek::BifFunc::split_string_all_native(str, re);
594
42.2k
#line 450 "/src/zeek/src/strings.bif"
595
42.2k
  } // end of shim for BifFunc::split_string_all
596
zeek::ValPtr zeek::BifFunc::split_string_n_native(zeek::StringVal* str, RE_Matcher* re, int incl_sep, zeek_uint_t max_num_sep)
597
#line 472 "/src/zeek/src/strings.bif"
598
599
  
600
#line 474 "/src/zeek/src/strings.bif"
601
3.95k
{
602
3.95k
#line 474 "/src/zeek/src/strings.bif"
603
604
3.95k
  return do_split_string(str, re, incl_sep, max_num_sep);
605
3.95k
  } // end of BifFunc::split_string_n
606
607
#line 476 "/src/zeek/src/strings.bif"
608
609
#line 472 "/src/zeek/src/strings.bif"
610
zeek::ValPtr zeek::BifFunc::split_string_n_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
611
#line 472 "/src/zeek/src/strings.bif"
612
3.95k
  {
613
3.95k
#line 472 "/src/zeek/src/strings.bif"
614
3.95k
  if ( BiF_ARGS->size() != 4 ) // NOLINT(readability-container-size-empty)
615
0
#line 472 "/src/zeek/src/strings.bif"
616
0
    {
617
0
#line 472 "/src/zeek/src/strings.bif"
618
0
    zeek::emit_builtin_error(zeek::util::fmt("split_string_n() takes exactly 4 argument(s), got %lu", BiF_ARGS->size()));
619
0
#line 472 "/src/zeek/src/strings.bif"
620
0
    return nullptr;
621
0
#line 472 "/src/zeek/src/strings.bif"
622
0
    }
623
3.95k
#line 472 "/src/zeek/src/strings.bif"
624
3.95k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
625
3.95k
#line 472 "/src/zeek/src/strings.bif"
626
3.95k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
627
3.95k
#line 472 "/src/zeek/src/strings.bif"
628
3.95k
  int incl_sep = (int) ((*BiF_ARGS)[2].get()->AsBool()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
629
3.95k
#line 472 "/src/zeek/src/strings.bif"
630
3.95k
  zeek_uint_t max_num_sep = (zeek_uint_t) ((*BiF_ARGS)[3].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
631
3.95k
#line 476 "/src/zeek/src/strings.bif"
632
3.95k
  return zeek::BifFunc::split_string_n_native(str, re, incl_sep, max_num_sep);
633
3.95k
#line 476 "/src/zeek/src/strings.bif"
634
3.95k
  } // end of shim for BifFunc::split_string_n
635
zeek::ValPtr zeek::BifFunc::sub_native(zeek::StringVal* str, RE_Matcher* re, zeek::StringVal* repl)
636
#line 491 "/src/zeek/src/strings.bif"
637
638
  
639
#line 492 "/src/zeek/src/strings.bif"
640
2.04M
{
641
2.04M
#line 492 "/src/zeek/src/strings.bif"
642
643
2.04M
  return str->Replace(re, *repl->AsString(), false);
644
2.04M
  } // end of BifFunc::sub
645
646
#line 494 "/src/zeek/src/strings.bif"
647
648
#line 491 "/src/zeek/src/strings.bif"
649
zeek::ValPtr zeek::BifFunc::sub_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
650
#line 491 "/src/zeek/src/strings.bif"
651
2.04M
  {
652
2.04M
#line 491 "/src/zeek/src/strings.bif"
653
2.04M
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
654
0
#line 491 "/src/zeek/src/strings.bif"
655
0
    {
656
0
#line 491 "/src/zeek/src/strings.bif"
657
0
    zeek::emit_builtin_error(zeek::util::fmt("sub() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
658
0
#line 491 "/src/zeek/src/strings.bif"
659
0
    return nullptr;
660
0
#line 491 "/src/zeek/src/strings.bif"
661
0
    }
662
2.04M
#line 491 "/src/zeek/src/strings.bif"
663
2.04M
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
664
2.04M
#line 491 "/src/zeek/src/strings.bif"
665
2.04M
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
666
2.04M
#line 491 "/src/zeek/src/strings.bif"
667
2.04M
  zeek::StringVal* repl = (zeek::StringVal*) ((*BiF_ARGS)[2].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
668
2.04M
#line 494 "/src/zeek/src/strings.bif"
669
2.04M
  return zeek::BifFunc::sub_native(str, re, repl);
670
2.04M
#line 494 "/src/zeek/src/strings.bif"
671
2.04M
  } // end of shim for BifFunc::sub
672
zeek::ValPtr zeek::BifFunc::gsub_native(zeek::StringVal* str, RE_Matcher* re, zeek::StringVal* repl)
673
#line 508 "/src/zeek/src/strings.bif"
674
675
  
676
#line 509 "/src/zeek/src/strings.bif"
677
29.8k
{
678
29.8k
#line 509 "/src/zeek/src/strings.bif"
679
680
29.8k
  return str->Replace(re, *repl->AsString(), true);
681
29.8k
  } // end of BifFunc::gsub
682
683
#line 511 "/src/zeek/src/strings.bif"
684
685
#line 508 "/src/zeek/src/strings.bif"
686
zeek::ValPtr zeek::BifFunc::gsub_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
687
#line 508 "/src/zeek/src/strings.bif"
688
29.8k
  {
689
29.8k
#line 508 "/src/zeek/src/strings.bif"
690
29.8k
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
691
0
#line 508 "/src/zeek/src/strings.bif"
692
0
    {
693
0
#line 508 "/src/zeek/src/strings.bif"
694
0
    zeek::emit_builtin_error(zeek::util::fmt("gsub() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
695
0
#line 508 "/src/zeek/src/strings.bif"
696
0
    return nullptr;
697
0
#line 508 "/src/zeek/src/strings.bif"
698
0
    }
699
29.8k
#line 508 "/src/zeek/src/strings.bif"
700
29.8k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
701
29.8k
#line 508 "/src/zeek/src/strings.bif"
702
29.8k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
703
29.8k
#line 508 "/src/zeek/src/strings.bif"
704
29.8k
  zeek::StringVal* repl = (zeek::StringVal*) ((*BiF_ARGS)[2].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
705
29.8k
#line 511 "/src/zeek/src/strings.bif"
706
29.8k
  return zeek::BifFunc::gsub_native(str, re, repl);
707
29.8k
#line 511 "/src/zeek/src/strings.bif"
708
29.8k
  } // end of shim for BifFunc::gsub
709
zeek_int_t zeek::BifFunc::strcmp_native(zeek::StringVal* s1, zeek::StringVal* s2)
710
#line 522 "/src/zeek/src/strings.bif"
711
712
  
713
#line 523 "/src/zeek/src/strings.bif"
714
0
{
715
0
#line 523 "/src/zeek/src/strings.bif"
716
717
0
  return Bstr_cmp(s1->AsString(), s2->AsString());
718
0
  } // end of BifFunc::strcmp
719
720
#line 525 "/src/zeek/src/strings.bif"
721
722
#line 522 "/src/zeek/src/strings.bif"
723
zeek::ValPtr zeek::BifFunc::strcmp_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
724
#line 522 "/src/zeek/src/strings.bif"
725
0
  {
726
0
#line 522 "/src/zeek/src/strings.bif"
727
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
728
0
#line 522 "/src/zeek/src/strings.bif"
729
0
    {
730
0
#line 522 "/src/zeek/src/strings.bif"
731
0
    zeek::emit_builtin_error(zeek::util::fmt("strcmp() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
732
0
#line 522 "/src/zeek/src/strings.bif"
733
0
    return nullptr;
734
0
#line 522 "/src/zeek/src/strings.bif"
735
0
    }
736
0
#line 522 "/src/zeek/src/strings.bif"
737
0
  zeek::StringVal* s1 = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
738
0
#line 522 "/src/zeek/src/strings.bif"
739
0
  zeek::StringVal* s2 = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
740
0
#line 525 "/src/zeek/src/strings.bif"
741
0
  return zeek::val_mgr->Int(zeek::BifFunc::strcmp_native(s1, s2));
742
0
#line 525 "/src/zeek/src/strings.bif"
743
0
  } // end of shim for BifFunc::strcmp
744
zeek_uint_t zeek::BifFunc::strstr_native(zeek::StringVal* big, zeek::StringVal* little)
745
#line 537 "/src/zeek/src/strings.bif"
746
747
  
748
#line 538 "/src/zeek/src/strings.bif"
749
925k
{
750
925k
#line 538 "/src/zeek/src/strings.bif"
751
752
925k
  return 1 + big->AsString()->FindSubstring(little->AsString());
753
925k
  } // end of BifFunc::strstr
754
755
#line 540 "/src/zeek/src/strings.bif"
756
757
#line 537 "/src/zeek/src/strings.bif"
758
zeek::ValPtr zeek::BifFunc::strstr_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
759
#line 537 "/src/zeek/src/strings.bif"
760
925k
  {
761
925k
#line 537 "/src/zeek/src/strings.bif"
762
925k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
763
0
#line 537 "/src/zeek/src/strings.bif"
764
0
    {
765
0
#line 537 "/src/zeek/src/strings.bif"
766
0
    zeek::emit_builtin_error(zeek::util::fmt("strstr() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
767
0
#line 537 "/src/zeek/src/strings.bif"
768
0
    return nullptr;
769
0
#line 537 "/src/zeek/src/strings.bif"
770
0
    }
771
925k
#line 537 "/src/zeek/src/strings.bif"
772
925k
  zeek::StringVal* big = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
773
925k
#line 537 "/src/zeek/src/strings.bif"
774
925k
  zeek::StringVal* little = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
775
925k
#line 540 "/src/zeek/src/strings.bif"
776
925k
  return zeek::val_mgr->Count(zeek::BifFunc::strstr_native(big, little));
777
925k
#line 540 "/src/zeek/src/strings.bif"
778
925k
  } // end of shim for BifFunc::strstr
779
zeek::ValPtr zeek::BifFunc::subst_string_native(zeek::StringVal* s, zeek::StringVal* from, zeek::StringVal* to)
780
#line 553 "/src/zeek/src/strings.bif"
781
782
  
783
#line 554 "/src/zeek/src/strings.bif"
784
0
{
785
0
#line 554 "/src/zeek/src/strings.bif"
786
787
0
  const int little_len = from->Len();
788
0
  if ( little_len == 0 )
789
0
    return IntrusivePtr{zeek::NewRef{}, s};
790
791
0
  int big_len = s->Len();
792
0
  const u_char* big = s->Bytes();
793
0
  data_chunk_t dc;
794
0
  vector<data_chunk_t> vs;
795
796
0
  while ( big_len >= little_len )
797
0
    {
798
0
    int j = zeek::util::strstr_n(big_len, big, little_len, from->Bytes());
799
800
0
    if ( j < 0 )
801
0
      break;
802
803
0
    if ( j > 0 )
804
0
      {
805
0
      dc.length = j;
806
0
      dc.data = reinterpret_cast<const char*>(big);
807
0
      vs.push_back(dc);
808
0
      }
809
810
0
    dc.length = to->Len();
811
0
    dc.data = reinterpret_cast<const char*>(to->Bytes());
812
0
    vs.push_back(dc);
813
814
0
    j += little_len;
815
0
    big += j;
816
0
    big_len -= j;
817
0
    }
818
819
0
  if ( big_len > 0 )
820
0
    {
821
0
    dc.length = big_len;
822
0
    dc.data = reinterpret_cast<const char*>(big);
823
0
    vs.push_back(dc);
824
0
    }
825
826
0
  return zeek::make_intrusive<zeek::StringVal>(concatenate(vs));
827
0
  } // end of BifFunc::subst_string
828
829
#line 595 "/src/zeek/src/strings.bif"
830
831
#line 553 "/src/zeek/src/strings.bif"
832
zeek::ValPtr zeek::BifFunc::subst_string_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
833
#line 553 "/src/zeek/src/strings.bif"
834
0
  {
835
0
#line 553 "/src/zeek/src/strings.bif"
836
0
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
837
0
#line 553 "/src/zeek/src/strings.bif"
838
0
    {
839
0
#line 553 "/src/zeek/src/strings.bif"
840
0
    zeek::emit_builtin_error(zeek::util::fmt("subst_string() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
841
0
#line 553 "/src/zeek/src/strings.bif"
842
0
    return nullptr;
843
0
#line 553 "/src/zeek/src/strings.bif"
844
0
    }
845
0
#line 553 "/src/zeek/src/strings.bif"
846
0
  zeek::StringVal* s = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
847
0
#line 553 "/src/zeek/src/strings.bif"
848
0
  zeek::StringVal* from = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
849
0
#line 553 "/src/zeek/src/strings.bif"
850
0
  zeek::StringVal* to = (zeek::StringVal*) ((*BiF_ARGS)[2].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
851
0
#line 595 "/src/zeek/src/strings.bif"
852
0
  return zeek::BifFunc::subst_string_native(s, from, to);
853
0
#line 595 "/src/zeek/src/strings.bif"
854
0
  } // end of shim for BifFunc::subst_string
855
zeek::ValPtr zeek::BifFunc::to_lower_native(zeek::StringVal* str)
856
#line 606 "/src/zeek/src/strings.bif"
857
858
  
859
#line 607 "/src/zeek/src/strings.bif"
860
2.86M
{
861
2.86M
#line 607 "/src/zeek/src/strings.bif"
862
863
2.86M
  const u_char* s = str->Bytes();
864
2.86M
  int n = str->Len();
865
2.86M
  u_char* lower_s = new u_char[n + 1];
866
2.86M
  u_char* ls = lower_s;
867
868
461M
  for ( int i = 0; i < n; ++i)
869
458M
    {
870
458M
    if ( isascii(s[i]) && isupper(s[i]) )
871
22.5M
      *ls++ = tolower(s[i]);
872
436M
    else
873
436M
      *ls++ = s[i];
874
458M
    }
875
876
2.86M
  *ls++ = '\0';
877
878
2.86M
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, lower_s, n));
879
2.86M
  } // end of BifFunc::to_lower
880
881
#line 624 "/src/zeek/src/strings.bif"
882
883
#line 606 "/src/zeek/src/strings.bif"
884
zeek::ValPtr zeek::BifFunc::to_lower_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
885
#line 606 "/src/zeek/src/strings.bif"
886
2.86M
  {
887
2.86M
#line 606 "/src/zeek/src/strings.bif"
888
2.86M
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
889
0
#line 606 "/src/zeek/src/strings.bif"
890
0
    {
891
0
#line 606 "/src/zeek/src/strings.bif"
892
0
    zeek::emit_builtin_error(zeek::util::fmt("to_lower() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
893
0
#line 606 "/src/zeek/src/strings.bif"
894
0
    return nullptr;
895
0
#line 606 "/src/zeek/src/strings.bif"
896
0
    }
897
2.86M
#line 606 "/src/zeek/src/strings.bif"
898
2.86M
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
899
2.86M
#line 624 "/src/zeek/src/strings.bif"
900
2.86M
  return zeek::BifFunc::to_lower_native(str);
901
2.86M
#line 624 "/src/zeek/src/strings.bif"
902
2.86M
  } // end of shim for BifFunc::to_lower
903
zeek::ValPtr zeek::BifFunc::to_upper_native(zeek::StringVal* str)
904
#line 635 "/src/zeek/src/strings.bif"
905
906
  
907
#line 636 "/src/zeek/src/strings.bif"
908
4.17M
{
909
4.17M
#line 636 "/src/zeek/src/strings.bif"
910
911
4.17M
  const u_char* s = str->Bytes();
912
4.17M
  int n = str->Len();
913
4.17M
  u_char* upper_s = new u_char[n + 1];
914
4.17M
  u_char* us = upper_s;
915
916
13.2M
  for ( int i = 0; i < n; ++i)
917
9.11M
    {
918
9.11M
    if ( isascii(s[i]) && islower(s[i]) )
919
2.06M
      *us++ = toupper(s[i]);
920
7.04M
    else
921
7.04M
      *us++ = s[i];
922
9.11M
    }
923
924
4.17M
  *us++ = '\0';
925
926
4.17M
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, upper_s, n));
927
4.17M
  } // end of BifFunc::to_upper
928
929
#line 653 "/src/zeek/src/strings.bif"
930
931
#line 635 "/src/zeek/src/strings.bif"
932
zeek::ValPtr zeek::BifFunc::to_upper_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
933
#line 635 "/src/zeek/src/strings.bif"
934
4.17M
  {
935
4.17M
#line 635 "/src/zeek/src/strings.bif"
936
4.17M
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
937
0
#line 635 "/src/zeek/src/strings.bif"
938
0
    {
939
0
#line 635 "/src/zeek/src/strings.bif"
940
0
    zeek::emit_builtin_error(zeek::util::fmt("to_upper() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
941
0
#line 635 "/src/zeek/src/strings.bif"
942
0
    return nullptr;
943
0
#line 635 "/src/zeek/src/strings.bif"
944
0
    }
945
4.17M
#line 635 "/src/zeek/src/strings.bif"
946
4.17M
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
947
4.17M
#line 653 "/src/zeek/src/strings.bif"
948
4.17M
  return zeek::BifFunc::to_upper_native(str);
949
4.17M
#line 653 "/src/zeek/src/strings.bif"
950
4.17M
  } // end of shim for BifFunc::to_upper
951
zeek::ValPtr zeek::BifFunc::clean_native(zeek::StringVal* str)
952
#line 669 "/src/zeek/src/strings.bif"
953
954
  
955
#line 670 "/src/zeek/src/strings.bif"
956
116k
{
957
116k
#line 670 "/src/zeek/src/strings.bif"
958
959
116k
  char* s = str->AsString()->Render();
960
116k
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, reinterpret_cast<byte_vec>(s), strlen(s)));
961
116k
  } // end of BifFunc::clean
962
963
#line 673 "/src/zeek/src/strings.bif"
964
965
#line 669 "/src/zeek/src/strings.bif"
966
zeek::ValPtr zeek::BifFunc::clean_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
967
#line 669 "/src/zeek/src/strings.bif"
968
116k
  {
969
116k
#line 669 "/src/zeek/src/strings.bif"
970
116k
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
971
0
#line 669 "/src/zeek/src/strings.bif"
972
0
    {
973
0
#line 669 "/src/zeek/src/strings.bif"
974
0
    zeek::emit_builtin_error(zeek::util::fmt("clean() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
975
0
#line 669 "/src/zeek/src/strings.bif"
976
0
    return nullptr;
977
0
#line 669 "/src/zeek/src/strings.bif"
978
0
    }
979
116k
#line 669 "/src/zeek/src/strings.bif"
980
116k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
981
116k
#line 673 "/src/zeek/src/strings.bif"
982
116k
  return zeek::BifFunc::clean_native(str);
983
116k
#line 673 "/src/zeek/src/strings.bif"
984
116k
  } // end of shim for BifFunc::clean
985
zeek::ValPtr zeek::BifFunc::to_string_literal_native(zeek::StringVal* str)
986
#line 687 "/src/zeek/src/strings.bif"
987
988
  
989
#line 688 "/src/zeek/src/strings.bif"
990
0
{
991
0
#line 688 "/src/zeek/src/strings.bif"
992
993
0
  char* s = str->AsString()->Render(zeek::String::ZEEK_STRING_LITERAL);
994
0
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, reinterpret_cast<byte_vec>(s), strlen(s)));
995
0
  } // end of BifFunc::to_string_literal
996
997
#line 691 "/src/zeek/src/strings.bif"
998
999
#line 687 "/src/zeek/src/strings.bif"
1000
zeek::ValPtr zeek::BifFunc::to_string_literal_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1001
#line 687 "/src/zeek/src/strings.bif"
1002
0
  {
1003
0
#line 687 "/src/zeek/src/strings.bif"
1004
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1005
0
#line 687 "/src/zeek/src/strings.bif"
1006
0
    {
1007
0
#line 687 "/src/zeek/src/strings.bif"
1008
0
    zeek::emit_builtin_error(zeek::util::fmt("to_string_literal() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1009
0
#line 687 "/src/zeek/src/strings.bif"
1010
0
    return nullptr;
1011
0
#line 687 "/src/zeek/src/strings.bif"
1012
0
    }
1013
0
#line 687 "/src/zeek/src/strings.bif"
1014
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1015
0
#line 691 "/src/zeek/src/strings.bif"
1016
0
  return zeek::BifFunc::to_string_literal_native(str);
1017
0
#line 691 "/src/zeek/src/strings.bif"
1018
0
  } // end of shim for BifFunc::to_string_literal
1019
bool zeek::BifFunc::is_ascii_native(zeek::StringVal* str)
1020
#line 702 "/src/zeek/src/strings.bif"
1021
1022
  
1023
#line 703 "/src/zeek/src/strings.bif"
1024
0
{
1025
0
#line 703 "/src/zeek/src/strings.bif"
1026
1027
0
  int n = str->Len();
1028
0
  const u_char* s = str->Bytes();
1029
1030
0
  for ( int i = 0; i < n; ++i )
1031
0
    if ( s[i] > 127 )
1032
0
      return false;
1033
1034
0
  return true;
1035
0
  } // end of BifFunc::is_ascii
1036
1037
#line 712 "/src/zeek/src/strings.bif"
1038
1039
#line 702 "/src/zeek/src/strings.bif"
1040
zeek::ValPtr zeek::BifFunc::is_ascii_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1041
#line 702 "/src/zeek/src/strings.bif"
1042
0
  {
1043
0
#line 702 "/src/zeek/src/strings.bif"
1044
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1045
0
#line 702 "/src/zeek/src/strings.bif"
1046
0
    {
1047
0
#line 702 "/src/zeek/src/strings.bif"
1048
0
    zeek::emit_builtin_error(zeek::util::fmt("is_ascii() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1049
0
#line 702 "/src/zeek/src/strings.bif"
1050
0
    return nullptr;
1051
0
#line 702 "/src/zeek/src/strings.bif"
1052
0
    }
1053
0
#line 702 "/src/zeek/src/strings.bif"
1054
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1055
0
#line 712 "/src/zeek/src/strings.bif"
1056
0
  return zeek::val_mgr->Bool(zeek::BifFunc::is_ascii_native(str));
1057
0
#line 712 "/src/zeek/src/strings.bif"
1058
0
  } // end of shim for BifFunc::is_ascii
1059
zeek::ValPtr zeek::BifFunc::escape_string_native(zeek::StringVal* s)
1060
#line 727 "/src/zeek/src/strings.bif"
1061
1062
  
1063
#line 728 "/src/zeek/src/strings.bif"
1064
0
{
1065
0
#line 728 "/src/zeek/src/strings.bif"
1066
1067
0
  char* escstr = s->AsString()->Render(zeek::String::ESC_HEX | zeek::String::ESC_ESC);
1068
0
  auto val = zeek::make_intrusive<zeek::StringVal>(escstr);
1069
0
  delete [] escstr;
1070
0
  return val;
1071
0
  } // end of BifFunc::escape_string
1072
1073
#line 733 "/src/zeek/src/strings.bif"
1074
1075
#line 727 "/src/zeek/src/strings.bif"
1076
zeek::ValPtr zeek::BifFunc::escape_string_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1077
#line 727 "/src/zeek/src/strings.bif"
1078
0
  {
1079
0
#line 727 "/src/zeek/src/strings.bif"
1080
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1081
0
#line 727 "/src/zeek/src/strings.bif"
1082
0
    {
1083
0
#line 727 "/src/zeek/src/strings.bif"
1084
0
    zeek::emit_builtin_error(zeek::util::fmt("escape_string() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1085
0
#line 727 "/src/zeek/src/strings.bif"
1086
0
    return nullptr;
1087
0
#line 727 "/src/zeek/src/strings.bif"
1088
0
    }
1089
0
#line 727 "/src/zeek/src/strings.bif"
1090
0
  zeek::StringVal* s = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1091
0
#line 733 "/src/zeek/src/strings.bif"
1092
0
  return zeek::BifFunc::escape_string_native(s);
1093
0
#line 733 "/src/zeek/src/strings.bif"
1094
0
  } // end of shim for BifFunc::escape_string
1095
zeek::ValPtr zeek::BifFunc::string_to_ascii_hex_native(zeek::StringVal* s)
1096
#line 741 "/src/zeek/src/strings.bif"
1097
1098
  
1099
#line 742 "/src/zeek/src/strings.bif"
1100
0
{
1101
0
#line 742 "/src/zeek/src/strings.bif"
1102
1103
0
  char* x = new char[s->Len() * 2 + 1];
1104
0
  const u_char* sp = s->Bytes();
1105
1106
0
  size_t length = s->Len();
1107
0
  for ( size_t i = 0; i < length; ++i )
1108
0
    snprintf(&x[i*2], 3, "%02x", sp[i]);
1109
1110
0
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, reinterpret_cast<u_char*>(x), s->Len() * 2));
1111
0
  } // end of BifFunc::string_to_ascii_hex
1112
1113
#line 751 "/src/zeek/src/strings.bif"
1114
1115
#line 741 "/src/zeek/src/strings.bif"
1116
zeek::ValPtr zeek::BifFunc::string_to_ascii_hex_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1117
#line 741 "/src/zeek/src/strings.bif"
1118
0
  {
1119
0
#line 741 "/src/zeek/src/strings.bif"
1120
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1121
0
#line 741 "/src/zeek/src/strings.bif"
1122
0
    {
1123
0
#line 741 "/src/zeek/src/strings.bif"
1124
0
    zeek::emit_builtin_error(zeek::util::fmt("string_to_ascii_hex() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1125
0
#line 741 "/src/zeek/src/strings.bif"
1126
0
    return nullptr;
1127
0
#line 741 "/src/zeek/src/strings.bif"
1128
0
    }
1129
0
#line 741 "/src/zeek/src/strings.bif"
1130
0
  zeek::StringVal* s = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1131
0
#line 751 "/src/zeek/src/strings.bif"
1132
0
  return zeek::BifFunc::string_to_ascii_hex_native(s);
1133
0
#line 751 "/src/zeek/src/strings.bif"
1134
0
  } // end of shim for BifFunc::string_to_ascii_hex
1135
zeek::ValPtr zeek::BifFunc::str_smith_waterman_native(zeek::StringVal* s1, zeek::StringVal* s2, zeek::Val* params)
1136
#line 763 "/src/zeek/src/strings.bif"
1137
1138
  
1139
#line 764 "/src/zeek/src/strings.bif"
1140
0
{
1141
0
#line 764 "/src/zeek/src/strings.bif"
1142
1143
0
  zeek::detail::SWParams sw_params(
1144
0
            params->AsRecordVal()->GetFieldAs<zeek::CountVal>(0),
1145
0
      zeek::detail::SWVariant(params->AsRecordVal()->GetFieldAs<zeek::CountVal>(1)));
1146
1147
0
  auto* subseq = zeek::detail::smith_waterman(s1->AsString(), s2->AsString(), sw_params);
1148
0
  auto result = zeek::VectorValPtr{zeek::AdoptRef{}, zeek::detail::Substring::VecToPolicy(subseq)};
1149
0
  zeek::util::delete_each(subseq);
1150
0
  delete subseq;
1151
1152
0
  return result;
1153
0
  } // end of BifFunc::str_smith_waterman
1154
1155
#line 775 "/src/zeek/src/strings.bif"
1156
1157
#line 763 "/src/zeek/src/strings.bif"
1158
zeek::ValPtr zeek::BifFunc::str_smith_waterman_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1159
#line 763 "/src/zeek/src/strings.bif"
1160
0
  {
1161
0
#line 763 "/src/zeek/src/strings.bif"
1162
0
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
1163
0
#line 763 "/src/zeek/src/strings.bif"
1164
0
    {
1165
0
#line 763 "/src/zeek/src/strings.bif"
1166
0
    zeek::emit_builtin_error(zeek::util::fmt("str_smith_waterman() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
1167
0
#line 763 "/src/zeek/src/strings.bif"
1168
0
    return nullptr;
1169
0
#line 763 "/src/zeek/src/strings.bif"
1170
0
    }
1171
0
#line 763 "/src/zeek/src/strings.bif"
1172
0
  zeek::StringVal* s1 = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1173
0
#line 763 "/src/zeek/src/strings.bif"
1174
0
  zeek::StringVal* s2 = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1175
0
#line 763 "/src/zeek/src/strings.bif"
1176
0
  zeek::Val* params = (zeek::Val*) ((*BiF_ARGS)[2].get()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1177
0
#line 775 "/src/zeek/src/strings.bif"
1178
0
  return zeek::BifFunc::str_smith_waterman_native(s1, s2, params);
1179
0
#line 775 "/src/zeek/src/strings.bif"
1180
0
  } // end of shim for BifFunc::str_smith_waterman
1181
zeek::ValPtr zeek::BifFunc::str_split_indices_native(zeek::StringVal* s, zeek::Val* idx)
1182
#line 787 "/src/zeek/src/strings.bif"
1183
1184
  
1185
#line 788 "/src/zeek/src/strings.bif"
1186
800
{
1187
800
#line 788 "/src/zeek/src/strings.bif"
1188
1189
800
  auto idx_v = idx->As<VectorVal*>();
1190
800
  auto n = idx_v->Size();
1191
800
  zeek::String::IdxVec indices(n);
1192
800
  unsigned int i;
1193
1194
6.17k
  for ( i = 0; i < n; i++ )
1195
5.37k
    indices[i] = idx_v->CountAt(i);
1196
1197
800
  zeek::String::Vec* result = s->AsString()->Split(indices);
1198
800
  auto result_v = zeek::make_intrusive<zeek::VectorVal>(zeek::id::string_vec);
1199
1200
800
  if ( result )
1201
800
    {
1202
800
    i = 0;
1203
1204
800
    for ( zeek::String::VecIt it = result->begin();
1205
6.97k
          it != result->end(); ++it, ++i )
1206
6.17k
      result_v->Assign(i, zeek::make_intrusive<zeek::StringVal>(*it));
1207
      // StringVal now possesses string.
1208
1209
800
    delete result;
1210
800
    }
1211
1212
800
  return result_v;
1213
800
  } // end of BifFunc::str_split_indices
1214
1215
#line 813 "/src/zeek/src/strings.bif"
1216
1217
#line 787 "/src/zeek/src/strings.bif"
1218
zeek::ValPtr zeek::BifFunc::str_split_indices_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1219
#line 787 "/src/zeek/src/strings.bif"
1220
800
  {
1221
800
#line 787 "/src/zeek/src/strings.bif"
1222
800
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
1223
0
#line 787 "/src/zeek/src/strings.bif"
1224
0
    {
1225
0
#line 787 "/src/zeek/src/strings.bif"
1226
0
    zeek::emit_builtin_error(zeek::util::fmt("str_split_indices() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
1227
0
#line 787 "/src/zeek/src/strings.bif"
1228
0
    return nullptr;
1229
0
#line 787 "/src/zeek/src/strings.bif"
1230
0
    }
1231
800
#line 787 "/src/zeek/src/strings.bif"
1232
800
  zeek::StringVal* s = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1233
800
#line 787 "/src/zeek/src/strings.bif"
1234
800
  zeek::Val* idx = (zeek::Val*) ((*BiF_ARGS)[1].get()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1235
800
#line 813 "/src/zeek/src/strings.bif"
1236
800
  return zeek::BifFunc::str_split_indices_native(s, idx);
1237
800
#line 813 "/src/zeek/src/strings.bif"
1238
800
  } // end of shim for BifFunc::str_split_indices
1239
zeek::ValPtr zeek::BifFunc::strip_native(zeek::StringVal* str)
1240
#line 822 "/src/zeek/src/strings.bif"
1241
1242
  
1243
#line 823 "/src/zeek/src/strings.bif"
1244
1.71k
{
1245
1.71k
#line 823 "/src/zeek/src/strings.bif"
1246
1247
1.71k
  const u_char* s = str->Bytes();
1248
1.71k
  int n = str->Len();
1249
1250
1.71k
  if ( n == 0 )
1251
    // Empty string.
1252
40
    return zeek::make_intrusive<zeek::StringVal>(new zeek::String(s, n, true));
1253
1254
1.67k
  const u_char* sp = s;
1255
1256
  // Move a pointer from the end of the string.
1257
1.67k
  const u_char* e = sp + n - 1;
1258
2.69k
  while ( e > sp && isspace(*e) )
1259
1.01k
    --e;
1260
1261
  // Move the pointer for the beginning of the string.
1262
2.43k
  while ( isspace(*sp) && sp <= e )
1263
757
    ++sp;
1264
1265
1.67k
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(sp, (e - sp + 1), true));
1266
1.71k
  } // end of BifFunc::strip
1267
1268
#line 843 "/src/zeek/src/strings.bif"
1269
1270
#line 822 "/src/zeek/src/strings.bif"
1271
zeek::ValPtr zeek::BifFunc::strip_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1272
#line 822 "/src/zeek/src/strings.bif"
1273
1.71k
  {
1274
1.71k
#line 822 "/src/zeek/src/strings.bif"
1275
1.71k
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1276
0
#line 822 "/src/zeek/src/strings.bif"
1277
0
    {
1278
0
#line 822 "/src/zeek/src/strings.bif"
1279
0
    zeek::emit_builtin_error(zeek::util::fmt("strip() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1280
0
#line 822 "/src/zeek/src/strings.bif"
1281
0
    return nullptr;
1282
0
#line 822 "/src/zeek/src/strings.bif"
1283
0
    }
1284
1.71k
#line 822 "/src/zeek/src/strings.bif"
1285
1.71k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1286
1.71k
#line 843 "/src/zeek/src/strings.bif"
1287
1.71k
  return zeek::BifFunc::strip_native(str);
1288
1.71k
#line 843 "/src/zeek/src/strings.bif"
1289
1.71k
  } // end of shim for BifFunc::strip
1290
1291
#line 845 "/src/zeek/src/strings.bif"
1292
1293
static bool should_strip(u_char c, const zeek::String* strip_chars)
1294
792
  {
1295
792
  auto strip_bytes = strip_chars->Bytes();
1296
1297
4.09k
  for ( auto i = 0; i < strip_chars->Len(); ++i )
1298
3.69k
    if ( c == strip_bytes[i] )
1299
396
      return true;
1300
1301
396
  return false;
1302
792
  }
1303
zeek::ValPtr zeek::BifFunc::lstrip_native(zeek::StringVal* str, zeek::StringVal* chars)
1304
#line 870 "/src/zeek/src/strings.bif"
1305
1306
  
1307
#line 871 "/src/zeek/src/strings.bif"
1308
396
{
1309
396
#line 871 "/src/zeek/src/strings.bif"
1310
1311
396
  const u_char* s = str->Bytes();
1312
396
  int n = str->Len();
1313
1314
  // empty input string
1315
396
  if ( n == 0 )
1316
0
    return zeek::make_intrusive<zeek::StringVal>(new zeek::String(s, n, true));
1317
1318
396
  int i;
1319
396
  auto bs_chars = chars->AsString();
1320
1321
792
  for ( i = 0; i < n; ++i )
1322
792
    if ( ! should_strip(s[i], bs_chars) )
1323
396
      break;
1324
1325
396
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(s + i, n - i, true));
1326
396
  } // end of BifFunc::lstrip
1327
1328
#line 887 "/src/zeek/src/strings.bif"
1329
1330
#line 870 "/src/zeek/src/strings.bif"
1331
zeek::ValPtr zeek::BifFunc::lstrip_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1332
#line 870 "/src/zeek/src/strings.bif"
1333
396
  {
1334
396
#line 870 "/src/zeek/src/strings.bif"
1335
396
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
1336
0
#line 870 "/src/zeek/src/strings.bif"
1337
0
    {
1338
0
#line 870 "/src/zeek/src/strings.bif"
1339
0
    zeek::emit_builtin_error(zeek::util::fmt("lstrip() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
1340
0
#line 870 "/src/zeek/src/strings.bif"
1341
0
    return nullptr;
1342
0
#line 870 "/src/zeek/src/strings.bif"
1343
0
    }
1344
396
#line 870 "/src/zeek/src/strings.bif"
1345
396
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1346
396
#line 870 "/src/zeek/src/strings.bif"
1347
396
  zeek::StringVal* chars = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1348
396
#line 887 "/src/zeek/src/strings.bif"
1349
396
  return zeek::BifFunc::lstrip_native(str, chars);
1350
396
#line 887 "/src/zeek/src/strings.bif"
1351
396
  } // end of shim for BifFunc::lstrip
1352
zeek::ValPtr zeek::BifFunc::rstrip_native(zeek::StringVal* str, zeek::StringVal* chars)
1353
#line 901 "/src/zeek/src/strings.bif"
1354
1355
  
1356
#line 902 "/src/zeek/src/strings.bif"
1357
0
{
1358
0
#line 902 "/src/zeek/src/strings.bif"
1359
1360
0
  const u_char* s = str->Bytes();
1361
0
  int n = str->Len();
1362
1363
  // empty input string
1364
0
  if ( n == 0 )
1365
0
    return zeek::make_intrusive<zeek::StringVal>(new zeek::String(s, n, true));
1366
1367
0
  int n_to_remove;
1368
0
  auto bs_chars = chars->AsString();
1369
1370
0
  for ( n_to_remove = 0; n_to_remove < n; ++n_to_remove )
1371
0
    if ( ! should_strip(s[n - n_to_remove - 1], bs_chars) )
1372
0
      break;
1373
1374
0
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(s, n - n_to_remove, true));
1375
0
  } // end of BifFunc::rstrip
1376
1377
#line 918 "/src/zeek/src/strings.bif"
1378
1379
#line 901 "/src/zeek/src/strings.bif"
1380
zeek::ValPtr zeek::BifFunc::rstrip_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1381
#line 901 "/src/zeek/src/strings.bif"
1382
0
  {
1383
0
#line 901 "/src/zeek/src/strings.bif"
1384
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
1385
0
#line 901 "/src/zeek/src/strings.bif"
1386
0
    {
1387
0
#line 901 "/src/zeek/src/strings.bif"
1388
0
    zeek::emit_builtin_error(zeek::util::fmt("rstrip() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
1389
0
#line 901 "/src/zeek/src/strings.bif"
1390
0
    return nullptr;
1391
0
#line 901 "/src/zeek/src/strings.bif"
1392
0
    }
1393
0
#line 901 "/src/zeek/src/strings.bif"
1394
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1395
0
#line 901 "/src/zeek/src/strings.bif"
1396
0
  zeek::StringVal* chars = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1397
0
#line 918 "/src/zeek/src/strings.bif"
1398
0
  return zeek::BifFunc::rstrip_native(str, chars);
1399
0
#line 918 "/src/zeek/src/strings.bif"
1400
0
  } // end of shim for BifFunc::rstrip
1401
zeek::ValPtr zeek::BifFunc::string_fill_native(zeek_int_t len, zeek::StringVal* source)
1402
#line 928 "/src/zeek/src/strings.bif"
1403
1404
  
1405
#line 929 "/src/zeek/src/strings.bif"
1406
0
{
1407
0
#line 929 "/src/zeek/src/strings.bif"
1408
1409
0
  const u_char* src = source->Bytes();
1410
0
  int64_t n = source->Len();
1411
0
  char* dst = new char[len];
1412
1413
0
  for ( int i = 0; i < len; i += n )
1414
0
    ::memcpy((dst + i), src, min(n, len - i));
1415
1416
0
  dst[len - 1] = 0;
1417
1418
0
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, reinterpret_cast<byte_vec>(dst), len));
1419
0
  } // end of BifFunc::string_fill
1420
1421
#line 940 "/src/zeek/src/strings.bif"
1422
1423
#line 928 "/src/zeek/src/strings.bif"
1424
zeek::ValPtr zeek::BifFunc::string_fill_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1425
#line 928 "/src/zeek/src/strings.bif"
1426
0
  {
1427
0
#line 928 "/src/zeek/src/strings.bif"
1428
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
1429
0
#line 928 "/src/zeek/src/strings.bif"
1430
0
    {
1431
0
#line 928 "/src/zeek/src/strings.bif"
1432
0
    zeek::emit_builtin_error(zeek::util::fmt("string_fill() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
1433
0
#line 928 "/src/zeek/src/strings.bif"
1434
0
    return nullptr;
1435
0
#line 928 "/src/zeek/src/strings.bif"
1436
0
    }
1437
0
#line 928 "/src/zeek/src/strings.bif"
1438
0
  zeek_int_t len = (zeek_int_t) ((*BiF_ARGS)[0].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1439
0
#line 928 "/src/zeek/src/strings.bif"
1440
0
  zeek::StringVal* source = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1441
0
#line 940 "/src/zeek/src/strings.bif"
1442
0
  return zeek::BifFunc::string_fill_native(len, source);
1443
0
#line 940 "/src/zeek/src/strings.bif"
1444
0
  } // end of shim for BifFunc::string_fill
1445
zeek::ValPtr zeek::BifFunc::safe_shell_quote_native(zeek::StringVal* source)
1446
#line 956 "/src/zeek/src/strings.bif"
1447
1448
  
1449
#line 957 "/src/zeek/src/strings.bif"
1450
0
{
1451
0
#line 957 "/src/zeek/src/strings.bif"
1452
1453
0
  unsigned j = 0;
1454
0
  const u_char* src = source->Bytes();
1455
0
  unsigned n = source->Len();
1456
0
  byte_vec dst = new u_char[n * 2 + 1 + 2];
1457
0
  dst[j++] = '"';
1458
1459
0
  for ( unsigned i = 0; i < n; ++i )
1460
0
    {
1461
0
    switch ( src[i] ) {
1462
0
    case '`': case '"': case '\\': case '$':
1463
0
      dst[j++] = '\\';
1464
0
      break;
1465
0
    default:
1466
0
      break;
1467
0
    }
1468
1469
0
    dst[j++] = src[i];
1470
0
    }
1471
1472
0
  dst[j++] = '"';
1473
0
  dst[j] = '\0';
1474
0
  return zeek::make_intrusive<zeek::StringVal>(new zeek::String(true, dst, j));
1475
0
  } // end of BifFunc::safe_shell_quote
1476
1477
#line 980 "/src/zeek/src/strings.bif"
1478
1479
#line 956 "/src/zeek/src/strings.bif"
1480
zeek::ValPtr zeek::BifFunc::safe_shell_quote_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1481
#line 956 "/src/zeek/src/strings.bif"
1482
0
  {
1483
0
#line 956 "/src/zeek/src/strings.bif"
1484
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1485
0
#line 956 "/src/zeek/src/strings.bif"
1486
0
    {
1487
0
#line 956 "/src/zeek/src/strings.bif"
1488
0
    zeek::emit_builtin_error(zeek::util::fmt("safe_shell_quote() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1489
0
#line 956 "/src/zeek/src/strings.bif"
1490
0
    return nullptr;
1491
0
#line 956 "/src/zeek/src/strings.bif"
1492
0
    }
1493
0
#line 956 "/src/zeek/src/strings.bif"
1494
0
  zeek::StringVal* source = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1495
0
#line 980 "/src/zeek/src/strings.bif"
1496
0
  return zeek::BifFunc::safe_shell_quote_native(source);
1497
0
#line 980 "/src/zeek/src/strings.bif"
1498
0
  } // end of shim for BifFunc::safe_shell_quote
1499
1500
#line 982 "/src/zeek/src/strings.bif"
1501
1502
static bool exceeds_max_string_length(int str_len, int max_size, zeek::detail::Frame* frame)
1503
537k
  {
1504
537k
  bool using_constant = false;
1505
537k
  if ( max_size < 0 )
1506
537k
    {
1507
537k
    static auto max_find_all_string_length = zeek::id::find_val<zeek::IntVal>("max_find_all_string_length");
1508
537k
    max_size = max_find_all_string_length->Get();
1509
537k
    using_constant = true;
1510
537k
    }
1511
1512
537k
  if ( max_size > 0 && str_len > max_size )
1513
182
    {
1514
182
    zeek::ODesc desc;
1515
182
    frame->GetCallLocation()->Describe(&desc);
1516
182
    std::string addl = zeek::util::fmt("%s: length %d exceeded %d", desc.Description(), str_len, max_size);
1517
182
    if ( using_constant )
1518
182
      addl.append("(from constant max_find_all_string_length");
1519
1520
182
    zeek::reporter->Weird("max_find_all_string_length_exceeded", addl.c_str());
1521
182
    return true;
1522
182
    }
1523
1524
537k
  return false;
1525
537k
  }
1526
zeek::ValPtr zeek::BifFunc::find_all_native(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1527
86.8k
  {
1528
86.8k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1529
86.8k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1530
86.8k
  zeek_int_t max_str_size = (zeek_int_t) ((*BiF_ARGS)[2].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1531
1532
86.8k
#line 1023 "/src/zeek/src/strings.bif"
1533
1534
  
1535
86.8k
#line 1024 "/src/zeek/src/strings.bif"
1536
1537
86.8k
#line 1024 "/src/zeek/src/strings.bif"
1538
1539
86.8k
  auto a = zeek::make_intrusive<zeek::TableVal>(zeek::id::string_set);
1540
1541
86.8k
  if ( exceeds_max_string_length(str->Len(), max_str_size, frame) )
1542
50
    return a;
1543
1544
86.8k
  const u_char* s = str->Bytes();
1545
86.8k
  const u_char* e = s + str->Len();
1546
1547
692k
  for ( const u_char* t = s; t < e; ++t )
1548
605k
    {
1549
605k
    int n = re->MatchPrefix(t, e - t);
1550
605k
    if ( n >= 0 )
1551
11.7k
      {
1552
11.7k
      auto idx = zeek::make_intrusive<zeek::StringVal>(n, reinterpret_cast<const char*>(t));
1553
11.7k
      a->Assign(std::move(idx), nullptr);
1554
11.7k
      t += n - 1;
1555
11.7k
      }
1556
605k
    }
1557
1558
86.8k
  return a;
1559
86.8k
  } // end of BifFunc::find_all
1560
1561
#line 1045 "/src/zeek/src/strings.bif"
1562
1563
#line 1023 "/src/zeek/src/strings.bif"
1564
zeek::ValPtr zeek::BifFunc::find_all_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1565
#line 1023 "/src/zeek/src/strings.bif"
1566
86.8k
  {
1567
86.8k
#line 1023 "/src/zeek/src/strings.bif"
1568
86.8k
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
1569
0
#line 1023 "/src/zeek/src/strings.bif"
1570
0
    {
1571
0
#line 1023 "/src/zeek/src/strings.bif"
1572
0
    zeek::emit_builtin_error(zeek::util::fmt("find_all() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
1573
0
#line 1023 "/src/zeek/src/strings.bif"
1574
0
    return nullptr;
1575
0
#line 1023 "/src/zeek/src/strings.bif"
1576
0
    }
1577
86.8k
#line 1023 "/src/zeek/src/strings.bif"
1578
86.8k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1579
86.8k
#line 1023 "/src/zeek/src/strings.bif"
1580
86.8k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1581
86.8k
#line 1023 "/src/zeek/src/strings.bif"
1582
86.8k
  zeek_int_t max_str_size = (zeek_int_t) ((*BiF_ARGS)[2].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1583
86.8k
#line 1045 "/src/zeek/src/strings.bif"
1584
86.8k
  return zeek::BifFunc::find_all_native(frame, BiF_ARGS);
1585
86.8k
#line 1045 "/src/zeek/src/strings.bif"
1586
86.8k
  } // end of shim for BifFunc::find_all
1587
zeek::ValPtr zeek::BifFunc::find_all_ordered_native(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1588
450k
  {
1589
450k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1590
450k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1591
450k
  zeek_int_t max_str_size = (zeek_int_t) ((*BiF_ARGS)[2].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1592
1593
450k
#line 1063 "/src/zeek/src/strings.bif"
1594
1595
  
1596
450k
#line 1064 "/src/zeek/src/strings.bif"
1597
1598
450k
#line 1064 "/src/zeek/src/strings.bif"
1599
1600
450k
  auto a = zeek::make_intrusive<zeek::VectorVal>(zeek::id::string_vec);
1601
1602
450k
  if ( exceeds_max_string_length(str->Len(), max_str_size, frame) )
1603
132
    return a;
1604
1605
450k
  const u_char* s = str->Bytes();
1606
450k
  const u_char* e = s + str->Len();
1607
1608
3.22M
  for ( const u_char* t = s; t < e; ++t )
1609
2.77M
    {
1610
2.77M
    int n = re->MatchPrefix(t, e - t);
1611
2.77M
    if ( n >= 0 )
1612
839
      {
1613
839
      auto idx = zeek::make_intrusive<zeek::StringVal>(n, reinterpret_cast<const char*>(t));
1614
839
      a->Assign(a->Size(), std::move(idx));
1615
839
      t += n - 1;
1616
839
      }
1617
2.77M
    }
1618
1619
450k
  return a;
1620
450k
  } // end of BifFunc::find_all_ordered
1621
1622
#line 1085 "/src/zeek/src/strings.bif"
1623
1624
#line 1063 "/src/zeek/src/strings.bif"
1625
zeek::ValPtr zeek::BifFunc::find_all_ordered_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1626
#line 1063 "/src/zeek/src/strings.bif"
1627
450k
  {
1628
450k
#line 1063 "/src/zeek/src/strings.bif"
1629
450k
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
1630
0
#line 1063 "/src/zeek/src/strings.bif"
1631
0
    {
1632
0
#line 1063 "/src/zeek/src/strings.bif"
1633
0
    zeek::emit_builtin_error(zeek::util::fmt("find_all_ordered() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
1634
0
#line 1063 "/src/zeek/src/strings.bif"
1635
0
    return nullptr;
1636
0
#line 1063 "/src/zeek/src/strings.bif"
1637
0
    }
1638
450k
#line 1063 "/src/zeek/src/strings.bif"
1639
450k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1640
450k
#line 1063 "/src/zeek/src/strings.bif"
1641
450k
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1642
450k
#line 1063 "/src/zeek/src/strings.bif"
1643
450k
  zeek_int_t max_str_size = (zeek_int_t) ((*BiF_ARGS)[2].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1644
450k
#line 1085 "/src/zeek/src/strings.bif"
1645
450k
  return zeek::BifFunc::find_all_ordered_native(frame, BiF_ARGS);
1646
450k
#line 1085 "/src/zeek/src/strings.bif"
1647
450k
  } // end of shim for BifFunc::find_all_ordered
1648
zeek::ValPtr zeek::BifFunc::find_last_native(zeek::StringVal* str, RE_Matcher* re, zeek_uint_t offset)
1649
#line 1101 "/src/zeek/src/strings.bif"
1650
1651
  
1652
#line 1102 "/src/zeek/src/strings.bif"
1653
66
{
1654
66
#line 1102 "/src/zeek/src/strings.bif"
1655
1656
66
  const u_char* s = str->Bytes();
1657
66
  const u_char* e = s + str->Len();
1658
1659
66
  if ( offset >= static_cast<zeek_uint_t>(str->Len()) )
1660
0
    return zeek::val_mgr->EmptyString();
1661
      
1662
  // Scans backwards from end of string, but stops at the offset.
1663
330
  for ( const u_char* t = e - 1; t >=  s + offset; --t )
1664
264
    {
1665
264
    int n = re->MatchPrefix(t, e - t);
1666
264
    if ( n >= 0 )
1667
0
      return zeek::make_intrusive<zeek::StringVal>(n, reinterpret_cast<const char*>(t));
1668
264
    }
1669
1670
66
  return zeek::val_mgr->EmptyString();
1671
66
  } // end of BifFunc::find_last
1672
1673
#line 1118 "/src/zeek/src/strings.bif"
1674
1675
#line 1101 "/src/zeek/src/strings.bif"
1676
zeek::ValPtr zeek::BifFunc::find_last_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1677
#line 1101 "/src/zeek/src/strings.bif"
1678
66
  {
1679
66
#line 1101 "/src/zeek/src/strings.bif"
1680
66
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
1681
0
#line 1101 "/src/zeek/src/strings.bif"
1682
0
    {
1683
0
#line 1101 "/src/zeek/src/strings.bif"
1684
0
    zeek::emit_builtin_error(zeek::util::fmt("find_last() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
1685
0
#line 1101 "/src/zeek/src/strings.bif"
1686
0
    return nullptr;
1687
0
#line 1101 "/src/zeek/src/strings.bif"
1688
0
    }
1689
66
#line 1101 "/src/zeek/src/strings.bif"
1690
66
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1691
66
#line 1101 "/src/zeek/src/strings.bif"
1692
66
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1693
66
#line 1101 "/src/zeek/src/strings.bif"
1694
66
  zeek_uint_t offset = (zeek_uint_t) ((*BiF_ARGS)[2].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1695
66
#line 1118 "/src/zeek/src/strings.bif"
1696
66
  return zeek::BifFunc::find_last_native(str, re, offset);
1697
66
#line 1118 "/src/zeek/src/strings.bif"
1698
66
  } // end of shim for BifFunc::find_last
1699
zeek::ValPtr zeek::BifFunc::find_first_native(zeek::StringVal* str, RE_Matcher* re, zeek_uint_t offset)
1700
#line 1131 "/src/zeek/src/strings.bif"
1701
1702
  
1703
#line 1132 "/src/zeek/src/strings.bif"
1704
0
{
1705
0
#line 1132 "/src/zeek/src/strings.bif"
1706
1707
0
  const u_char* s = str->Bytes();
1708
0
  const u_char* e = s + str->Len();
1709
1710
0
  if ( offset >= static_cast<zeek_uint_t>(str->Len()) )
1711
0
    return zeek::val_mgr->EmptyString();
1712
1713
  // Starts scanning from the offset
1714
0
  for ( const u_char* t = s + offset; t < e; ++t )
1715
0
    {
1716
0
    int n = re->MatchPrefix(t, e - t);
1717
0
    if ( n >= 0 )
1718
0
      return zeek::make_intrusive<zeek::StringVal>(n, reinterpret_cast<const char*>(t));
1719
0
    }
1720
1721
0
  return zeek::val_mgr->EmptyString();
1722
0
  } // end of BifFunc::find_first
1723
1724
#line 1148 "/src/zeek/src/strings.bif"
1725
1726
#line 1131 "/src/zeek/src/strings.bif"
1727
zeek::ValPtr zeek::BifFunc::find_first_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1728
#line 1131 "/src/zeek/src/strings.bif"
1729
0
  {
1730
0
#line 1131 "/src/zeek/src/strings.bif"
1731
0
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
1732
0
#line 1131 "/src/zeek/src/strings.bif"
1733
0
    {
1734
0
#line 1131 "/src/zeek/src/strings.bif"
1735
0
    zeek::emit_builtin_error(zeek::util::fmt("find_first() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
1736
0
#line 1131 "/src/zeek/src/strings.bif"
1737
0
    return nullptr;
1738
0
#line 1131 "/src/zeek/src/strings.bif"
1739
0
    }
1740
0
#line 1131 "/src/zeek/src/strings.bif"
1741
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1742
0
#line 1131 "/src/zeek/src/strings.bif"
1743
0
  RE_Matcher* re = (RE_Matcher*) ((*BiF_ARGS)[1].get()->AsPattern()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1744
0
#line 1131 "/src/zeek/src/strings.bif"
1745
0
  zeek_uint_t offset = (zeek_uint_t) ((*BiF_ARGS)[2].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1746
0
#line 1148 "/src/zeek/src/strings.bif"
1747
0
  return zeek::BifFunc::find_first_native(str, re, offset);
1748
0
#line 1148 "/src/zeek/src/strings.bif"
1749
0
  } // end of shim for BifFunc::find_first
1750
zeek::ValPtr zeek::BifFunc::hexdump_native(zeek::StringVal* data_str)
1751
#line 1162 "/src/zeek/src/strings.bif"
1752
1753
  
1754
#line 1163 "/src/zeek/src/strings.bif"
1755
0
{
1756
0
#line 1163 "/src/zeek/src/strings.bif"
1757
1758
1759
  // The width of a line of text in the hex-mode view, consisting
1760
  // of offset, hex view and ASCII view:
1761
  //
1762
  // 32 +     16 characters per 8 bytes, twice
1763
  // (2*7) +  Single space between bytes, twice
1764
  // 4 +      Two spaces between 8-byte sets and ASCII
1765
  // 1 +      For newline
1766
  // 17 +     For ASCII display, with spacer column
1767
  // 6        For 5-digit offset counter, including spacer
1768
  //
1769
0
  constexpr int HEX_LINE_WIDTH = 74;
1770
0
  constexpr char NULL_CHAR = '.';
1771
0
  constexpr char NONPRINT_CHAR = '.';
1772
1773
0
  const u_char* data = data_str->Bytes();
1774
0
  unsigned data_size = data_str->Len();
1775
1776
0
  if ( ! data )
1777
0
    return zeek::val_mgr->EmptyString();
1778
1779
0
  int num_lines = (data_size / 16) + 1;
1780
0
  int len = num_lines * HEX_LINE_WIDTH;
1781
0
  u_char* hex_data = new u_char[len + 1];
1782
0
  if ( ! hex_data )
1783
0
    return zeek::val_mgr->EmptyString();
1784
1785
0
  memset(hex_data, ' ', len);
1786
1787
0
  u_char* hex_data_ptr = hex_data;
1788
0
  u_char* ascii_ptr = hex_data_ptr + 50;
1789
0
  int x = 0;
1790
0
  int y = 0;
1791
1792
0
  for ( const u_char* data_ptr = data; data_ptr < data + data_size;
1793
0
        ++data_ptr )
1794
0
    {
1795
0
    if ( x == 0 )
1796
0
      {
1797
0
      char offset[5];
1798
0
      snprintf(offset, sizeof(offset),
1799
0
          "%.4tx", data_ptr - data);
1800
0
      memcpy(hex_data_ptr, offset, 4);
1801
0
      hex_data_ptr += 6;
1802
0
      ascii_ptr = hex_data_ptr + 50;
1803
0
      }
1804
1805
0
    char hex_byte[3];
1806
0
    snprintf(hex_byte, sizeof(hex_byte), "%.2x", *data_ptr);
1807
1808
0
    int val = *data_ptr;
1809
1810
0
    u_char ascii_byte = val;
1811
1812
    // If unprintable, use special characters:
1813
0
    if ( val < 0x20 || val >= 0x7f )
1814
0
      {
1815
0
      if ( val == 0x00 )
1816
0
        ascii_byte = NULL_CHAR;
1817
0
      else
1818
0
        ascii_byte = NONPRINT_CHAR;
1819
0
      }
1820
1821
0
    *hex_data_ptr++ = hex_byte[0];
1822
0
    *hex_data_ptr++ = hex_byte[1];
1823
0
    *hex_data_ptr++ = ' ';
1824
0
    *ascii_ptr++ = ascii_byte;
1825
1826
0
    if ( x == 7 )
1827
0
      {
1828
0
      *hex_data_ptr++ = ' ';
1829
0
      *ascii_ptr++ = ' ';
1830
0
      }
1831
1832
0
    ++x;
1833
1834
0
    if ( x == 16 )
1835
0
      {
1836
0
      x = 0;
1837
0
      *ascii_ptr++ = '\n';
1838
0
      hex_data_ptr = ascii_ptr;
1839
0
      }
1840
0
    }
1841
1842
  // Terminate the string, but ensure it ends with a newline.
1843
0
  if ( ascii_ptr[-1] != '\n' )
1844
0
    *ascii_ptr++ = '\n';
1845
0
  *ascii_ptr = 0;
1846
1847
0
  auto result = zeek::make_intrusive<zeek::StringVal>(reinterpret_cast<const char*>(hex_data));
1848
0
  delete [] hex_data;
1849
1850
0
  return result;
1851
0
  } // end of BifFunc::hexdump
1852
1853
#line 1257 "/src/zeek/src/strings.bif"
1854
1855
#line 1162 "/src/zeek/src/strings.bif"
1856
zeek::ValPtr zeek::BifFunc::hexdump_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1857
#line 1162 "/src/zeek/src/strings.bif"
1858
0
  {
1859
0
#line 1162 "/src/zeek/src/strings.bif"
1860
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1861
0
#line 1162 "/src/zeek/src/strings.bif"
1862
0
    {
1863
0
#line 1162 "/src/zeek/src/strings.bif"
1864
0
    zeek::emit_builtin_error(zeek::util::fmt("hexdump() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1865
0
#line 1162 "/src/zeek/src/strings.bif"
1866
0
    return nullptr;
1867
0
#line 1162 "/src/zeek/src/strings.bif"
1868
0
    }
1869
0
#line 1162 "/src/zeek/src/strings.bif"
1870
0
  zeek::StringVal* data_str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1871
0
#line 1257 "/src/zeek/src/strings.bif"
1872
0
  return zeek::BifFunc::hexdump_native(data_str);
1873
0
#line 1257 "/src/zeek/src/strings.bif"
1874
0
  } // end of shim for BifFunc::hexdump
1875
zeek::ValPtr zeek::BifFunc::reverse_native(zeek::StringVal* str)
1876
#line 1265 "/src/zeek/src/strings.bif"
1877
1878
  
1879
#line 1266 "/src/zeek/src/strings.bif"
1880
0
{
1881
0
#line 1266 "/src/zeek/src/strings.bif"
1882
1883
0
  string s = str->ToStdString();
1884
0
  std::ranges::reverse(s);
1885
0
  return zeek::make_intrusive<zeek::StringVal>(s.length(), s.c_str());
1886
0
  } // end of BifFunc::reverse
1887
1888
#line 1270 "/src/zeek/src/strings.bif"
1889
1890
#line 1265 "/src/zeek/src/strings.bif"
1891
zeek::ValPtr zeek::BifFunc::reverse_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1892
#line 1265 "/src/zeek/src/strings.bif"
1893
0
  {
1894
0
#line 1265 "/src/zeek/src/strings.bif"
1895
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
1896
0
#line 1265 "/src/zeek/src/strings.bif"
1897
0
    {
1898
0
#line 1265 "/src/zeek/src/strings.bif"
1899
0
    zeek::emit_builtin_error(zeek::util::fmt("reverse() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
1900
0
#line 1265 "/src/zeek/src/strings.bif"
1901
0
    return nullptr;
1902
0
#line 1265 "/src/zeek/src/strings.bif"
1903
0
    }
1904
0
#line 1265 "/src/zeek/src/strings.bif"
1905
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1906
0
#line 1270 "/src/zeek/src/strings.bif"
1907
0
  return zeek::BifFunc::reverse_native(str);
1908
0
#line 1270 "/src/zeek/src/strings.bif"
1909
0
  } // end of shim for BifFunc::reverse
1910
zeek_uint_t zeek::BifFunc::count_substr_native(zeek::StringVal* str, zeek::StringVal* sub)
1911
#line 1279 "/src/zeek/src/strings.bif"
1912
1913
  
1914
#line 1280 "/src/zeek/src/strings.bif"
1915
0
{
1916
0
#line 1280 "/src/zeek/src/strings.bif"
1917
1918
0
  auto s = str->ToStdStringView();
1919
0
  auto sub_s = sub->ToStdStringView();
1920
1921
0
  size_t count = 0;
1922
0
  size_t pos = s.find(sub_s);
1923
0
  while ( pos != string::npos )
1924
0
    {
1925
0
    ++count;
1926
0
    pos = s.find(sub_s, pos + sub_s.size());
1927
0
    }
1928
1929
0
  return count;
1930
0
  } // end of BifFunc::count_substr
1931
1932
#line 1293 "/src/zeek/src/strings.bif"
1933
1934
#line 1279 "/src/zeek/src/strings.bif"
1935
zeek::ValPtr zeek::BifFunc::count_substr_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
1936
#line 1279 "/src/zeek/src/strings.bif"
1937
0
  {
1938
0
#line 1279 "/src/zeek/src/strings.bif"
1939
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
1940
0
#line 1279 "/src/zeek/src/strings.bif"
1941
0
    {
1942
0
#line 1279 "/src/zeek/src/strings.bif"
1943
0
    zeek::emit_builtin_error(zeek::util::fmt("count_substr() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
1944
0
#line 1279 "/src/zeek/src/strings.bif"
1945
0
    return nullptr;
1946
0
#line 1279 "/src/zeek/src/strings.bif"
1947
0
    }
1948
0
#line 1279 "/src/zeek/src/strings.bif"
1949
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1950
0
#line 1279 "/src/zeek/src/strings.bif"
1951
0
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
1952
0
#line 1293 "/src/zeek/src/strings.bif"
1953
0
  return zeek::val_mgr->Count(zeek::BifFunc::count_substr_native(str, sub));
1954
0
#line 1293 "/src/zeek/src/strings.bif"
1955
0
  } // end of shim for BifFunc::count_substr
1956
1957
#line 1295 "/src/zeek/src/strings.bif"
1958
1959
1960
static int64_t do_find_str(zeek::StringVal* str, zeek::StringVal* sub, int64_t start, int64_t end, bool rfind, bool case_sensitive)
1961
0
  {
1962
  // Don't bother if the start is after the end of the string.
1963
0
  if ( start > str->Len() )
1964
0
    return -1;
1965
1966
  // Also don't bother (and return an error) if the end is before the start.
1967
0
  if ( (end != -1 ) && end < start )
1968
0
    {
1969
0
    zeek::reporter->Error("find_str: end position must be greater than start position");
1970
0
    return -1;
1971
0
    }
1972
1973
0
  int64_t end_pos = str->Len();
1974
0
  if ( end >= 0 && end < str->Len() )
1975
0
    end_pos = end;
1976
1977
  // One last sanity check, don't bother doing string operations at all if the range is shorter than
1978
  // the length of the search string.
1979
0
  if ( (end_pos - start + 1) < sub->Len() )
1980
0
    return -1;
1981
1982
0
  string s = str->ToStdString().substr(start, end_pos);
1983
0
  string sb = sub->ToStdString();
1984
0
  size_t pos = string::npos;
1985
1986
0
  if ( ! case_sensitive )
1987
0
    {
1988
0
    std::ranges::transform(s, s.begin(), ::tolower);
1989
0
    std::ranges::transform(sb, sb.begin(), ::tolower);
1990
0
    }
1991
1992
0
  if ( rfind )
1993
0
    pos = s.rfind(sb);
1994
0
  else
1995
0
    pos = s.find(sb);
1996
1997
0
  if ( pos == string::npos )
1998
0
    return -1;
1999
2000
0
  return pos + start;
2001
0
  }
2002
2003
zeek_int_t zeek::BifFunc::find_str_native(zeek::StringVal* str, zeek::StringVal* sub, zeek_uint_t start, zeek_int_t end, int case_sensitive)
2004
#line 1360 "/src/zeek/src/strings.bif"
2005
2006
  
2007
#line 1361 "/src/zeek/src/strings.bif"
2008
0
{
2009
0
#line 1361 "/src/zeek/src/strings.bif"
2010
2011
0
  return do_find_str(str, sub, start, end, false, case_sensitive);
2012
0
  } // end of BifFunc::find_str
2013
2014
#line 1363 "/src/zeek/src/strings.bif"
2015
2016
#line 1360 "/src/zeek/src/strings.bif"
2017
zeek::ValPtr zeek::BifFunc::find_str_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2018
#line 1360 "/src/zeek/src/strings.bif"
2019
0
  {
2020
0
#line 1360 "/src/zeek/src/strings.bif"
2021
0
  if ( BiF_ARGS->size() != 5 ) // NOLINT(readability-container-size-empty)
2022
0
#line 1360 "/src/zeek/src/strings.bif"
2023
0
    {
2024
0
#line 1360 "/src/zeek/src/strings.bif"
2025
0
    zeek::emit_builtin_error(zeek::util::fmt("find_str() takes exactly 5 argument(s), got %lu", BiF_ARGS->size()));
2026
0
#line 1360 "/src/zeek/src/strings.bif"
2027
0
    return nullptr;
2028
0
#line 1360 "/src/zeek/src/strings.bif"
2029
0
    }
2030
0
#line 1360 "/src/zeek/src/strings.bif"
2031
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2032
0
#line 1360 "/src/zeek/src/strings.bif"
2033
0
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2034
0
#line 1360 "/src/zeek/src/strings.bif"
2035
0
  zeek_uint_t start = (zeek_uint_t) ((*BiF_ARGS)[2].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2036
0
#line 1360 "/src/zeek/src/strings.bif"
2037
0
  zeek_int_t end = (zeek_int_t) ((*BiF_ARGS)[3].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2038
0
#line 1360 "/src/zeek/src/strings.bif"
2039
0
  int case_sensitive = (int) ((*BiF_ARGS)[4].get()->AsBool()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2040
0
#line 1363 "/src/zeek/src/strings.bif"
2041
0
  return zeek::val_mgr->Int(zeek::BifFunc::find_str_native(str, sub, start, end, case_sensitive));
2042
0
#line 1363 "/src/zeek/src/strings.bif"
2043
0
  } // end of shim for BifFunc::find_str
2044
zeek_int_t zeek::BifFunc::rfind_str_native(zeek::StringVal* str, zeek::StringVal* sub, zeek_uint_t start, zeek_int_t end, int case_sensitive)
2045
#line 1380 "/src/zeek/src/strings.bif"
2046
2047
  
2048
#line 1381 "/src/zeek/src/strings.bif"
2049
0
{
2050
0
#line 1381 "/src/zeek/src/strings.bif"
2051
2052
0
  return do_find_str(str, sub, start, end, true, case_sensitive);
2053
0
  } // end of BifFunc::rfind_str
2054
2055
#line 1383 "/src/zeek/src/strings.bif"
2056
2057
#line 1380 "/src/zeek/src/strings.bif"
2058
zeek::ValPtr zeek::BifFunc::rfind_str_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2059
#line 1380 "/src/zeek/src/strings.bif"
2060
0
  {
2061
0
#line 1380 "/src/zeek/src/strings.bif"
2062
0
  if ( BiF_ARGS->size() != 5 ) // NOLINT(readability-container-size-empty)
2063
0
#line 1380 "/src/zeek/src/strings.bif"
2064
0
    {
2065
0
#line 1380 "/src/zeek/src/strings.bif"
2066
0
    zeek::emit_builtin_error(zeek::util::fmt("rfind_str() takes exactly 5 argument(s), got %lu", BiF_ARGS->size()));
2067
0
#line 1380 "/src/zeek/src/strings.bif"
2068
0
    return nullptr;
2069
0
#line 1380 "/src/zeek/src/strings.bif"
2070
0
    }
2071
0
#line 1380 "/src/zeek/src/strings.bif"
2072
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2073
0
#line 1380 "/src/zeek/src/strings.bif"
2074
0
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2075
0
#line 1380 "/src/zeek/src/strings.bif"
2076
0
  zeek_uint_t start = (zeek_uint_t) ((*BiF_ARGS)[2].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2077
0
#line 1380 "/src/zeek/src/strings.bif"
2078
0
  zeek_int_t end = (zeek_int_t) ((*BiF_ARGS)[3].get()->AsInt()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2079
0
#line 1380 "/src/zeek/src/strings.bif"
2080
0
  int case_sensitive = (int) ((*BiF_ARGS)[4].get()->AsBool()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2081
0
#line 1383 "/src/zeek/src/strings.bif"
2082
0
  return zeek::val_mgr->Int(zeek::BifFunc::rfind_str_native(str, sub, start, end, case_sensitive));
2083
0
#line 1383 "/src/zeek/src/strings.bif"
2084
0
  } // end of shim for BifFunc::rfind_str
2085
bool zeek::BifFunc::starts_with_native(zeek::StringVal* str, zeek::StringVal* sub)
2086
#line 1387 "/src/zeek/src/strings.bif"
2087
2088
  
2089
#line 1388 "/src/zeek/src/strings.bif"
2090
782k
{
2091
782k
#line 1388 "/src/zeek/src/strings.bif"
2092
2093
782k
  if ( sub->Len() > str->Len() )
2094
725k
    return false;
2095
2096
57.0k
  auto sub_s = sub->ToStdStringView();
2097
57.0k
  auto s = str->ToStdStringView();
2098
57.0k
  return s.starts_with(sub_s);
2099
782k
  } // end of BifFunc::starts_with
2100
2101
#line 1395 "/src/zeek/src/strings.bif"
2102
2103
#line 1387 "/src/zeek/src/strings.bif"
2104
zeek::ValPtr zeek::BifFunc::starts_with_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2105
#line 1387 "/src/zeek/src/strings.bif"
2106
782k
  {
2107
782k
#line 1387 "/src/zeek/src/strings.bif"
2108
782k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
2109
0
#line 1387 "/src/zeek/src/strings.bif"
2110
0
    {
2111
0
#line 1387 "/src/zeek/src/strings.bif"
2112
0
    zeek::emit_builtin_error(zeek::util::fmt("starts_with() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
2113
0
#line 1387 "/src/zeek/src/strings.bif"
2114
0
    return nullptr;
2115
0
#line 1387 "/src/zeek/src/strings.bif"
2116
0
    }
2117
782k
#line 1387 "/src/zeek/src/strings.bif"
2118
782k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2119
782k
#line 1387 "/src/zeek/src/strings.bif"
2120
782k
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2121
782k
#line 1395 "/src/zeek/src/strings.bif"
2122
782k
  return zeek::val_mgr->Bool(zeek::BifFunc::starts_with_native(str, sub));
2123
782k
#line 1395 "/src/zeek/src/strings.bif"
2124
782k
  } // end of shim for BifFunc::starts_with
2125
bool zeek::BifFunc::ends_with_native(zeek::StringVal* str, zeek::StringVal* sub)
2126
#line 1399 "/src/zeek/src/strings.bif"
2127
2128
  
2129
#line 1400 "/src/zeek/src/strings.bif"
2130
599k
{
2131
599k
#line 1400 "/src/zeek/src/strings.bif"
2132
2133
599k
  if ( sub->Len() > str->Len() )
2134
528k
    return false;
2135
2136
70.5k
  auto sub_s = sub->ToStdStringView();
2137
70.5k
  auto s = str->ToStdStringView();
2138
2139
  // Create a string_view that only looks at the end of the string being searched
2140
  // with the same number of characters as the search string. This avoids possible
2141
  // pathological searches of big strings if the search string doesn't exist.
2142
70.5k
  auto end_s = std::string_view{s.data() + s.size() - sub_s.size(), sub_s.size()};
2143
2144
70.5k
  return end_s == sub_s;
2145
599k
  } // end of BifFunc::ends_with
2146
2147
#line 1413 "/src/zeek/src/strings.bif"
2148
2149
#line 1399 "/src/zeek/src/strings.bif"
2150
zeek::ValPtr zeek::BifFunc::ends_with_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2151
#line 1399 "/src/zeek/src/strings.bif"
2152
599k
  {
2153
599k
#line 1399 "/src/zeek/src/strings.bif"
2154
599k
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
2155
0
#line 1399 "/src/zeek/src/strings.bif"
2156
0
    {
2157
0
#line 1399 "/src/zeek/src/strings.bif"
2158
0
    zeek::emit_builtin_error(zeek::util::fmt("ends_with() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
2159
0
#line 1399 "/src/zeek/src/strings.bif"
2160
0
    return nullptr;
2161
0
#line 1399 "/src/zeek/src/strings.bif"
2162
0
    }
2163
599k
#line 1399 "/src/zeek/src/strings.bif"
2164
599k
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2165
599k
#line 1399 "/src/zeek/src/strings.bif"
2166
599k
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2167
599k
#line 1413 "/src/zeek/src/strings.bif"
2168
599k
  return zeek::val_mgr->Bool(zeek::BifFunc::ends_with_native(str, sub));
2169
599k
#line 1413 "/src/zeek/src/strings.bif"
2170
599k
  } // end of shim for BifFunc::ends_with
2171
bool zeek::BifFunc::is_num_native(zeek::StringVal* str)
2172
#line 1418 "/src/zeek/src/strings.bif"
2173
2174
  
2175
#line 1419 "/src/zeek/src/strings.bif"
2176
0
{
2177
0
#line 1419 "/src/zeek/src/strings.bif"
2178
2179
  // Python's version of this method (which this is based on) just checks to see if every
2180
  // character in the string is a numeric value. If something more than this is desired, we
2181
  // could use something like std::from_chars or std::strto{ul,f} to check it.
2182
0
  if ( str->Len() == 0 )
2183
0
    return false;
2184
2185
0
  const char* s = str->CheckString();
2186
0
  for ( int i = 0; i < str->Len(); i++ )
2187
0
    if ( ! std::isdigit(s[i]) )
2188
0
      return false;
2189
2190
0
  return true;
2191
0
  } // end of BifFunc::is_num
2192
2193
#line 1432 "/src/zeek/src/strings.bif"
2194
2195
#line 1418 "/src/zeek/src/strings.bif"
2196
zeek::ValPtr zeek::BifFunc::is_num_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2197
#line 1418 "/src/zeek/src/strings.bif"
2198
0
  {
2199
0
#line 1418 "/src/zeek/src/strings.bif"
2200
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
2201
0
#line 1418 "/src/zeek/src/strings.bif"
2202
0
    {
2203
0
#line 1418 "/src/zeek/src/strings.bif"
2204
0
    zeek::emit_builtin_error(zeek::util::fmt("is_num() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
2205
0
#line 1418 "/src/zeek/src/strings.bif"
2206
0
    return nullptr;
2207
0
#line 1418 "/src/zeek/src/strings.bif"
2208
0
    }
2209
0
#line 1418 "/src/zeek/src/strings.bif"
2210
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2211
0
#line 1432 "/src/zeek/src/strings.bif"
2212
0
  return zeek::val_mgr->Bool(zeek::BifFunc::is_num_native(str));
2213
0
#line 1432 "/src/zeek/src/strings.bif"
2214
0
  } // end of shim for BifFunc::is_num
2215
bool zeek::BifFunc::is_alpha_native(zeek::StringVal* str)
2216
#line 1437 "/src/zeek/src/strings.bif"
2217
2218
  
2219
#line 1438 "/src/zeek/src/strings.bif"
2220
0
{
2221
0
#line 1438 "/src/zeek/src/strings.bif"
2222
2223
0
  if ( str->Len() == 0 )
2224
0
    return false;
2225
2226
0
  const char* s = str->CheckString();
2227
0
  for ( int i = 0; i < str->Len(); i++ )
2228
0
    if ( ! std::isalpha(s[i]) )
2229
0
      return false;
2230
2231
0
  return true;
2232
0
  } // end of BifFunc::is_alpha
2233
2234
#line 1448 "/src/zeek/src/strings.bif"
2235
2236
#line 1437 "/src/zeek/src/strings.bif"
2237
zeek::ValPtr zeek::BifFunc::is_alpha_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2238
#line 1437 "/src/zeek/src/strings.bif"
2239
0
  {
2240
0
#line 1437 "/src/zeek/src/strings.bif"
2241
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
2242
0
#line 1437 "/src/zeek/src/strings.bif"
2243
0
    {
2244
0
#line 1437 "/src/zeek/src/strings.bif"
2245
0
    zeek::emit_builtin_error(zeek::util::fmt("is_alpha() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
2246
0
#line 1437 "/src/zeek/src/strings.bif"
2247
0
    return nullptr;
2248
0
#line 1437 "/src/zeek/src/strings.bif"
2249
0
    }
2250
0
#line 1437 "/src/zeek/src/strings.bif"
2251
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2252
0
#line 1448 "/src/zeek/src/strings.bif"
2253
0
  return zeek::val_mgr->Bool(zeek::BifFunc::is_alpha_native(str));
2254
0
#line 1448 "/src/zeek/src/strings.bif"
2255
0
  } // end of shim for BifFunc::is_alpha
2256
bool zeek::BifFunc::is_alnum_native(zeek::StringVal* str)
2257
#line 1453 "/src/zeek/src/strings.bif"
2258
2259
  
2260
#line 1454 "/src/zeek/src/strings.bif"
2261
0
{
2262
0
#line 1454 "/src/zeek/src/strings.bif"
2263
2264
0
  if ( str->Len() == 0 )
2265
0
    return false;
2266
2267
0
  const char* s = str->CheckString();
2268
0
  for ( int i = 0; i < str->Len(); i++ )
2269
0
    if ( ! std::isalnum(s[i]) )
2270
0
      return false;
2271
2272
0
  return true;
2273
0
  } // end of BifFunc::is_alnum
2274
2275
#line 1464 "/src/zeek/src/strings.bif"
2276
2277
#line 1453 "/src/zeek/src/strings.bif"
2278
zeek::ValPtr zeek::BifFunc::is_alnum_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2279
#line 1453 "/src/zeek/src/strings.bif"
2280
0
  {
2281
0
#line 1453 "/src/zeek/src/strings.bif"
2282
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
2283
0
#line 1453 "/src/zeek/src/strings.bif"
2284
0
    {
2285
0
#line 1453 "/src/zeek/src/strings.bif"
2286
0
    zeek::emit_builtin_error(zeek::util::fmt("is_alnum() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
2287
0
#line 1453 "/src/zeek/src/strings.bif"
2288
0
    return nullptr;
2289
0
#line 1453 "/src/zeek/src/strings.bif"
2290
0
    }
2291
0
#line 1453 "/src/zeek/src/strings.bif"
2292
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2293
0
#line 1464 "/src/zeek/src/strings.bif"
2294
0
  return zeek::val_mgr->Bool(zeek::BifFunc::is_alnum_native(str));
2295
0
#line 1464 "/src/zeek/src/strings.bif"
2296
0
  } // end of shim for BifFunc::is_alnum
2297
zeek::ValPtr zeek::BifFunc::ljust_native(zeek::StringVal* str, zeek_uint_t width, zeek::StringVal* fill)
2298
#line 1479 "/src/zeek/src/strings.bif"
2299
2300
  
2301
#line 1480 "/src/zeek/src/strings.bif"
2302
0
{
2303
0
#line 1480 "/src/zeek/src/strings.bif"
2304
2305
0
  if ( fill->Len() != 1 )
2306
0
    {
2307
0
    reporter->Error("Fill string passed to ljust() must be a single character");
2308
0
    return nullptr;
2309
0
    }
2310
2311
0
  string new_s = str->ToStdString();
2312
2313
0
  if ( width <= new_s.size() )
2314
0
    return zeek::StringValPtr(zeek::NewRef{}, str);
2315
2316
0
  new_s.insert(new_s.size(), width - new_s.size(), fill->CheckString()[0]);
2317
0
  return zeek::make_intrusive<zeek::StringVal>(new_s);
2318
0
  } // end of BifFunc::ljust
2319
2320
#line 1494 "/src/zeek/src/strings.bif"
2321
2322
#line 1479 "/src/zeek/src/strings.bif"
2323
zeek::ValPtr zeek::BifFunc::ljust_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2324
#line 1479 "/src/zeek/src/strings.bif"
2325
0
  {
2326
0
#line 1479 "/src/zeek/src/strings.bif"
2327
0
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
2328
0
#line 1479 "/src/zeek/src/strings.bif"
2329
0
    {
2330
0
#line 1479 "/src/zeek/src/strings.bif"
2331
0
    zeek::emit_builtin_error(zeek::util::fmt("ljust() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
2332
0
#line 1479 "/src/zeek/src/strings.bif"
2333
0
    return nullptr;
2334
0
#line 1479 "/src/zeek/src/strings.bif"
2335
0
    }
2336
0
#line 1479 "/src/zeek/src/strings.bif"
2337
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2338
0
#line 1479 "/src/zeek/src/strings.bif"
2339
0
  zeek_uint_t width = (zeek_uint_t) ((*BiF_ARGS)[1].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2340
0
#line 1479 "/src/zeek/src/strings.bif"
2341
0
  zeek::StringVal* fill = (zeek::StringVal*) ((*BiF_ARGS)[2].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2342
0
#line 1494 "/src/zeek/src/strings.bif"
2343
0
  return zeek::BifFunc::ljust_native(str, width, fill);
2344
0
#line 1494 "/src/zeek/src/strings.bif"
2345
0
  } // end of shim for BifFunc::ljust
2346
2347
#line 1496 "/src/zeek/src/strings.bif"
2348
2349
2350
static zeek::StringValPtr do_rjust(zeek::StringVal* str, uint64_t width, char fill)
2351
0
  {
2352
0
  string new_s = str->ToStdString();
2353
2354
0
  if ( width <= new_s.size() )
2355
0
    return { zeek::NewRef{}, str };
2356
2357
0
  new_s.insert(0, width - new_s.size(), fill);
2358
0
  return zeek::make_intrusive<zeek::StringVal>(new_s);
2359
0
  }
2360
2361
zeek::ValPtr zeek::BifFunc::rjust_native(zeek::StringVal* str, zeek_uint_t width, zeek::StringVal* fill)
2362
#line 1524 "/src/zeek/src/strings.bif"
2363
2364
  
2365
#line 1525 "/src/zeek/src/strings.bif"
2366
0
{
2367
0
#line 1525 "/src/zeek/src/strings.bif"
2368
2369
0
  if ( fill->Len() != 1 )
2370
0
    {
2371
0
    reporter->Error("Fill string passed to rjust() must be a single character");
2372
0
    return nullptr;
2373
0
    }
2374
2375
0
  return do_rjust(str, width, fill->CheckString()[0]);
2376
0
  } // end of BifFunc::rjust
2377
2378
#line 1533 "/src/zeek/src/strings.bif"
2379
2380
#line 1524 "/src/zeek/src/strings.bif"
2381
zeek::ValPtr zeek::BifFunc::rjust_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2382
#line 1524 "/src/zeek/src/strings.bif"
2383
0
  {
2384
0
#line 1524 "/src/zeek/src/strings.bif"
2385
0
  if ( BiF_ARGS->size() != 3 ) // NOLINT(readability-container-size-empty)
2386
0
#line 1524 "/src/zeek/src/strings.bif"
2387
0
    {
2388
0
#line 1524 "/src/zeek/src/strings.bif"
2389
0
    zeek::emit_builtin_error(zeek::util::fmt("rjust() takes exactly 3 argument(s), got %lu", BiF_ARGS->size()));
2390
0
#line 1524 "/src/zeek/src/strings.bif"
2391
0
    return nullptr;
2392
0
#line 1524 "/src/zeek/src/strings.bif"
2393
0
    }
2394
0
#line 1524 "/src/zeek/src/strings.bif"
2395
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2396
0
#line 1524 "/src/zeek/src/strings.bif"
2397
0
  zeek_uint_t width = (zeek_uint_t) ((*BiF_ARGS)[1].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2398
0
#line 1524 "/src/zeek/src/strings.bif"
2399
0
  zeek::StringVal* fill = (zeek::StringVal*) ((*BiF_ARGS)[2].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2400
0
#line 1533 "/src/zeek/src/strings.bif"
2401
0
  return zeek::BifFunc::rjust_native(str, width, fill);
2402
0
#line 1533 "/src/zeek/src/strings.bif"
2403
0
  } // end of shim for BifFunc::rjust
2404
zeek::ValPtr zeek::BifFunc::swap_case_native(zeek::StringVal* str)
2405
#line 1541 "/src/zeek/src/strings.bif"
2406
2407
  
2408
#line 1542 "/src/zeek/src/strings.bif"
2409
0
{
2410
0
#line 1542 "/src/zeek/src/strings.bif"
2411
2412
0
  string s = str->ToStdString();
2413
0
  for ( auto& c : s )
2414
0
    {
2415
0
    if ( std::islower(c) )
2416
0
      c = toupper(c);
2417
0
    else if ( std::isupper(c) )
2418
0
      c = tolower(c);
2419
0
    }
2420
2421
0
  return zeek::make_intrusive<zeek::StringVal>(s);
2422
0
  } // end of BifFunc::swap_case
2423
2424
#line 1553 "/src/zeek/src/strings.bif"
2425
2426
#line 1541 "/src/zeek/src/strings.bif"
2427
zeek::ValPtr zeek::BifFunc::swap_case_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2428
#line 1541 "/src/zeek/src/strings.bif"
2429
0
  {
2430
0
#line 1541 "/src/zeek/src/strings.bif"
2431
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
2432
0
#line 1541 "/src/zeek/src/strings.bif"
2433
0
    {
2434
0
#line 1541 "/src/zeek/src/strings.bif"
2435
0
    zeek::emit_builtin_error(zeek::util::fmt("swap_case() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
2436
0
#line 1541 "/src/zeek/src/strings.bif"
2437
0
    return nullptr;
2438
0
#line 1541 "/src/zeek/src/strings.bif"
2439
0
    }
2440
0
#line 1541 "/src/zeek/src/strings.bif"
2441
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2442
0
#line 1553 "/src/zeek/src/strings.bif"
2443
0
  return zeek::BifFunc::swap_case_native(str);
2444
0
#line 1553 "/src/zeek/src/strings.bif"
2445
0
  } // end of shim for BifFunc::swap_case
2446
zeek::ValPtr zeek::BifFunc::to_title_native(zeek::StringVal* str)
2447
#line 1562 "/src/zeek/src/strings.bif"
2448
2449
  
2450
#line 1563 "/src/zeek/src/strings.bif"
2451
0
{
2452
0
#line 1563 "/src/zeek/src/strings.bif"
2453
2454
0
  string s = str->ToStdString();
2455
0
  size_t pos = s.find_first_not_of(' ');
2456
0
  if ( pos == string::npos )
2457
0
    return zeek::IntrusivePtr<zeek::StringVal>(NewRef{}, str);
2458
2459
0
  while ( pos != string::npos )
2460
0
    {
2461
0
    s[pos] = std::toupper(s[pos]);
2462
0
    pos = s.find(' ', pos+1);
2463
0
    if ( pos == string::npos )
2464
0
      break;
2465
2466
0
    pos = s.find_first_not_of(' ', pos+1);
2467
0
    }
2468
2469
0
  return zeek::make_intrusive<zeek::StringVal>(s);
2470
0
  } // end of BifFunc::to_title
2471
2472
#line 1580 "/src/zeek/src/strings.bif"
2473
2474
#line 1562 "/src/zeek/src/strings.bif"
2475
zeek::ValPtr zeek::BifFunc::to_title_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2476
#line 1562 "/src/zeek/src/strings.bif"
2477
0
  {
2478
0
#line 1562 "/src/zeek/src/strings.bif"
2479
0
  if ( BiF_ARGS->size() != 1 ) // NOLINT(readability-container-size-empty)
2480
0
#line 1562 "/src/zeek/src/strings.bif"
2481
0
    {
2482
0
#line 1562 "/src/zeek/src/strings.bif"
2483
0
    zeek::emit_builtin_error(zeek::util::fmt("to_title() takes exactly 1 argument(s), got %lu", BiF_ARGS->size()));
2484
0
#line 1562 "/src/zeek/src/strings.bif"
2485
0
    return nullptr;
2486
0
#line 1562 "/src/zeek/src/strings.bif"
2487
0
    }
2488
0
#line 1562 "/src/zeek/src/strings.bif"
2489
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2490
0
#line 1580 "/src/zeek/src/strings.bif"
2491
0
  return zeek::BifFunc::to_title_native(str);
2492
0
#line 1580 "/src/zeek/src/strings.bif"
2493
0
  } // end of shim for BifFunc::to_title
2494
zeek::ValPtr zeek::BifFunc::zfill_native(zeek::StringVal* str, zeek_uint_t width)
2495
#line 1583 "/src/zeek/src/strings.bif"
2496
2497
  
2498
#line 1584 "/src/zeek/src/strings.bif"
2499
0
{
2500
0
#line 1584 "/src/zeek/src/strings.bif"
2501
2502
0
  return do_rjust(str, width, '0');
2503
0
  } // end of BifFunc::zfill
2504
2505
#line 1586 "/src/zeek/src/strings.bif"
2506
2507
#line 1583 "/src/zeek/src/strings.bif"
2508
zeek::ValPtr zeek::BifFunc::zfill_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2509
#line 1583 "/src/zeek/src/strings.bif"
2510
0
  {
2511
0
#line 1583 "/src/zeek/src/strings.bif"
2512
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
2513
0
#line 1583 "/src/zeek/src/strings.bif"
2514
0
    {
2515
0
#line 1583 "/src/zeek/src/strings.bif"
2516
0
    zeek::emit_builtin_error(zeek::util::fmt("zfill() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
2517
0
#line 1583 "/src/zeek/src/strings.bif"
2518
0
    return nullptr;
2519
0
#line 1583 "/src/zeek/src/strings.bif"
2520
0
    }
2521
0
#line 1583 "/src/zeek/src/strings.bif"
2522
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2523
0
#line 1583 "/src/zeek/src/strings.bif"
2524
0
  zeek_uint_t width = (zeek_uint_t) ((*BiF_ARGS)[1].get()->AsCount()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2525
0
#line 1586 "/src/zeek/src/strings.bif"
2526
0
  return zeek::BifFunc::zfill_native(str, width);
2527
0
#line 1586 "/src/zeek/src/strings.bif"
2528
0
  } // end of shim for BifFunc::zfill
2529
zeek::ValPtr zeek::BifFunc::remove_prefix_native(zeek::StringVal* str, zeek::StringVal* sub)
2530
#line 1589 "/src/zeek/src/strings.bif"
2531
2532
  
2533
#line 1590 "/src/zeek/src/strings.bif"
2534
0
{
2535
0
#line 1590 "/src/zeek/src/strings.bif"
2536
2537
  // This could just use repeated calls to lstrip(), except for a couple of reasons:
2538
  // 1) lstrip() creates a StringVal at the end, and that would mean repeated recreation of objects
2539
  // 2) lstrip() searches for any character in the string, not the string as a whole.
2540
0
  auto s = str->ToStdStringView();
2541
0
  auto sub_s = sub->ToStdStringView();
2542
2543
0
  size_t pos = s.find(sub_s);
2544
0
  if ( pos != 0 )
2545
0
    return zeek::IntrusivePtr<zeek::StringVal>(NewRef{}, str);
2546
2547
0
  pos = s.find(sub_s, pos+1);
2548
0
  size_t next_pos = sub_s.size();
2549
0
  while ( pos == next_pos && next_pos < s.size() )
2550
0
    {
2551
0
    next_pos += sub_s.size();
2552
0
    pos = s.find(sub_s, pos+1);
2553
0
    }
2554
2555
0
  return zeek::make_intrusive<zeek::StringVal>(s.substr(next_pos));
2556
0
  } // end of BifFunc::remove_prefix
2557
2558
#line 1610 "/src/zeek/src/strings.bif"
2559
2560
#line 1589 "/src/zeek/src/strings.bif"
2561
zeek::ValPtr zeek::BifFunc::remove_prefix_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2562
#line 1589 "/src/zeek/src/strings.bif"
2563
0
  {
2564
0
#line 1589 "/src/zeek/src/strings.bif"
2565
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
2566
0
#line 1589 "/src/zeek/src/strings.bif"
2567
0
    {
2568
0
#line 1589 "/src/zeek/src/strings.bif"
2569
0
    zeek::emit_builtin_error(zeek::util::fmt("remove_prefix() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
2570
0
#line 1589 "/src/zeek/src/strings.bif"
2571
0
    return nullptr;
2572
0
#line 1589 "/src/zeek/src/strings.bif"
2573
0
    }
2574
0
#line 1589 "/src/zeek/src/strings.bif"
2575
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2576
0
#line 1589 "/src/zeek/src/strings.bif"
2577
0
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2578
0
#line 1610 "/src/zeek/src/strings.bif"
2579
0
  return zeek::BifFunc::remove_prefix_native(str, sub);
2580
0
#line 1610 "/src/zeek/src/strings.bif"
2581
0
  } // end of shim for BifFunc::remove_prefix
2582
zeek::ValPtr zeek::BifFunc::remove_suffix_native(zeek::StringVal* str, zeek::StringVal* sub)
2583
#line 1613 "/src/zeek/src/strings.bif"
2584
2585
  
2586
#line 1614 "/src/zeek/src/strings.bif"
2587
0
{
2588
0
#line 1614 "/src/zeek/src/strings.bif"
2589
2590
  // See the note in removeprefix for why this doesn't just call rstrip.
2591
0
  auto s = str->ToStdStringView();
2592
0
  auto sub_s = sub->ToStdStringView();
2593
2594
0
  size_t pos = s.rfind(sub_s);
2595
0
  size_t next_pos = s.size() - sub_s.size();
2596
2597
0
  if ( pos != next_pos )
2598
0
    return zeek::IntrusivePtr<zeek::StringVal>(NewRef{}, str);
2599
2600
0
  while ( pos == next_pos )
2601
0
    {
2602
0
    next_pos -= sub_s.size();
2603
0
    pos = s.rfind(sub_s, pos-1);
2604
0
    }
2605
2606
0
  return zeek::make_intrusive<zeek::StringVal>(s.substr(0, next_pos + sub_s.size()));
2607
0
  } // end of BifFunc::remove_suffix
2608
2609
#line 1632 "/src/zeek/src/strings.bif"
2610
2611
#line 1613 "/src/zeek/src/strings.bif"
2612
zeek::ValPtr zeek::BifFunc::remove_suffix_bif(zeek::detail::Frame* frame, const zeek::Args* BiF_ARGS)
2613
#line 1613 "/src/zeek/src/strings.bif"
2614
0
  {
2615
0
#line 1613 "/src/zeek/src/strings.bif"
2616
0
  if ( BiF_ARGS->size() != 2 ) // NOLINT(readability-container-size-empty)
2617
0
#line 1613 "/src/zeek/src/strings.bif"
2618
0
    {
2619
0
#line 1613 "/src/zeek/src/strings.bif"
2620
0
    zeek::emit_builtin_error(zeek::util::fmt("remove_suffix() takes exactly 2 argument(s), got %lu", BiF_ARGS->size()));
2621
0
#line 1613 "/src/zeek/src/strings.bif"
2622
0
    return nullptr;
2623
0
#line 1613 "/src/zeek/src/strings.bif"
2624
0
    }
2625
0
#line 1613 "/src/zeek/src/strings.bif"
2626
0
  zeek::StringVal* str = (zeek::StringVal*) ((*BiF_ARGS)[0].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2627
0
#line 1613 "/src/zeek/src/strings.bif"
2628
0
  zeek::StringVal* sub = (zeek::StringVal*) ((*BiF_ARGS)[1].get()->AsStringVal()); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,modernize-avoid-c-style-cast)
2629
0
#line 1632 "/src/zeek/src/strings.bif"
2630
0
  return zeek::BifFunc::remove_suffix_native(str, sub);
2631
0
#line 1632 "/src/zeek/src/strings.bif"
2632
0
  } // end of shim for BifFunc::remove_suffix