cpp

Coverage Report

Created: 2025-06-16 00:44

/home/uke/oil/mycpp/gc_builtins.cc
Line
Count
Source (jump to first uncovered line)
1
#include <errno.h>  // errno
2
#include <float.h>  // DBL_MIN, DBL_MAX
3
#include <math.h>   // INFINITY
4
#include <stdio.h>  // required for readline/readline.h (man readline)
5
6
#include "_build/detected-cpp-config.h"
7
#include "mycpp/gc_list.h"
8
#include "mycpp/gc_str.h"
9
#ifdef HAVE_READLINE
10
  #include "cpp/frontend_pyreadline.h"
11
#endif
12
13
// Translation of Python's print().
14
5
void print(BigStr* s) {
15
5
  fputs(s->data_, stdout);  // print until first NUL
16
5
  fputc('\n', stdout);
17
5
}
18
19
0
BigStr* str(int i) {
20
0
  BigStr* s = OverAllocatedStr(kIntBufSize);
21
0
  int length = snprintf(s->data(), kIntBufSize, "%d", i);
22
0
  s->MaybeShrink(length);
23
0
  return s;
24
0
}
25
26
0
BigStr* str(double d) {
27
0
  char buf[64];  // overestimate, but we use snprintf() to be safe
28
29
0
  int n = sizeof(buf) - 2;  // in case we add '.0'
30
31
  // The round tripping test in mycpp/float_test.cc tells us:
32
  // %.9g - FLOAT round trip
33
  // %.17g - DOUBLE round trip
34
  // But this causes problems in practice, e.g. for 3.14, or 1/3
35
  // int length = snprintf(buf, n, "%.17g", d);
36
37
  // So use 1 less digit, which happens to match Python 3 and node.js (but not
38
  // Python 2)
39
0
  int length = snprintf(buf, n, "%.16g", d);
40
41
  // TODO: This may depend on LC_NUMERIC locale!
42
43
  // We may return the strings:
44
  //    inf  -inf   nan
45
  // But this shouldn't come up much, because Python code changes it to:
46
  //    INFINITY   -INFINITY   NAN
47
0
  if (strchr(buf, 'i') || strchr(buf, 'n')) {
48
0
    return StrFromC(buf);  // don't add .0
49
0
  }
50
51
  // Problem:
52
  // %f prints 3.0000000 and 3.500000
53
  // %g prints 3 and 3.5
54
  //
55
  // We want 3.0 and 3.5, so add '.0' in some cases
56
0
  if (!strchr(buf, '.')) {  // 12345 -> 12345.0
57
0
    buf[length] = '.';
58
0
    buf[length + 1] = '0';
59
0
    buf[length + 2] = '\0';
60
0
  }
61
62
0
  return StrFromC(buf);
63
0
}
64
// %a is a hexfloat form, probably don't need that
65
// int length = snprintf(buf, n, "%a", d);
66
67
// Do we need this API?  Or is mylib.InternedStr(BigStr* s, int start, int end)
68
// better for getting values out of Token.line without allocating?
69
//
70
// e.g. mylib.InternedStr(tok.line, tok.start, tok.start+1)
71
//
72
// Also for SmallStr, we don't care about interning.  Only for HeapStr.
73
74
0
BigStr* intern(BigStr* s) {
75
  // TODO: put in table gHeap.interned_
76
0
  return s;
77
0
}
78
79
// Print quoted string.  Called by StrFormat('%r').
80
// TODO: consider using J8 notation instead, since error messages show that
81
// string.
82
1
BigStr* repr(BigStr* s) {
83
  // Worst case: \0 becomes 4 bytes as '\\x00', and then two quote bytes.
84
1
  int n = len(s);
85
1
  int upper_bound = n * 4 + 2;
86
87
1
  BigStr* result = OverAllocatedStr(upper_bound);
88
89
  // Single quote by default.
90
1
  char quote = '\'';
91
1
  if (memchr(s->data_, '\'', n) && !memchr(s->data_, '"', n)) {
92
0
    quote = '"';
93
0
  }
94
1
  char* p = result->data_;
95
96
  // From PyString_Repr()
97
1
  *p++ = quote;
98
8
  for (int i = 0; i < n; ++i) {
99
7
    unsigned char c = static_cast<unsigned char>(s->data_[i]);
100
7
    if (c == quote || c == '\\') {
101
0
      *p++ = '\\';
102
0
      *p++ = c;
103
7
    } else if (c == '\t') {
104
0
      *p++ = '\\';
105
0
      *p++ = 't';
106
7
    } else if (c == '\n') {
107
0
      *p++ = '\\';
108
0
      *p++ = 'n';
109
7
    } else if (c == '\r') {
110
0
      *p++ = '\\';
111
0
      *p++ = 'r';
112
7
    } else if (0x20 <= c && c < 0x80) {
113
7
      *p++ = c;
114
7
    } else {
115
      // Unprintable becomes \xff.
116
      // TODO: Consider \yff.  This is similar to J8 strings, but we don't
117
      // decode UTF-8.
118
0
      sprintf(p, "\\x%02x", c & 0xff);
119
0
      p += 4;
120
0
    }
121
7
  }
122
1
  *p++ = quote;
123
1
  *p = '\0';
124
125
1
  int length = p - result->data_;
126
1
  result->MaybeShrink(length);
127
1
  return result;
128
1
}
129
130
// Helper functions that don't use exceptions.
131
132
2
bool StringToInt(const char* s, int length, int base, int* result) {
133
2
  if (length == 0) {
134
0
    return false;  // empty string isn't a valid integer
135
0
  }
136
137
  // Note: sizeof(int) is often 4 bytes on both 32-bit and 64-bit
138
  //       sizeof(long) is often 4 bytes on both 32-bit but 8 bytes on 64-bit
139
  // static_assert(sizeof(long) == 8);
140
141
2
  char* pos;  // mutated by strtol
142
143
2
  errno = 0;
144
2
  long v = strtol(s, &pos, base);
145
146
2
  if (errno == ERANGE) {
147
0
    switch (v) {
148
0
    case LONG_MIN:
149
0
      return false;  // underflow of long, which may be 64 bits
150
0
    case LONG_MAX:
151
0
      return false;  // overflow of long
152
0
    }
153
0
  }
154
155
  // It should ALSO fit in an int, not just a long
156
2
  if (v > INT_MAX) {
157
0
    return false;
158
0
  }
159
2
  if (v < INT_MIN) {
160
0
    return false;
161
0
  }
162
163
2
  const char* end = s + length;
164
2
  if (pos == end) {
165
1
    *result = v;
166
1
    return true;  // strtol() consumed ALL characters.
167
1
  }
168
169
1
  while (pos < end) {
170
1
    if (!IsAsciiWhitespace(*pos)) {
171
1
      return false;  // Trailing non-space
172
1
    }
173
0
    pos++;
174
0
  }
175
176
0
  *result = v;
177
0
  return true;  // Trailing space is OK
178
1
}
179
180
0
bool StringToInt64(const char* s, int length, int base, int64_t* result) {
181
0
  if (length == 0) {
182
0
    return false;  // empty string isn't a valid integer
183
0
  }
184
185
  // These should be the same type
186
0
  static_assert(sizeof(long long) == sizeof(int64_t), "");
187
188
0
  char* pos;  // mutated by strtol
189
190
0
  errno = 0;
191
0
  long long v = strtoll(s, &pos, base);
192
193
0
  if (errno == ERANGE) {
194
0
    switch (v) {
195
0
    case LLONG_MIN:
196
0
      return false;  // underflow
197
0
    case LLONG_MAX:
198
0
      return false;  // overflow
199
0
    }
200
0
  }
201
202
0
  const char* end = s + length;
203
0
  if (pos == end) {
204
0
    *result = v;
205
0
    return true;  // strtol() consumed ALL characters.
206
0
  }
207
208
0
  while (pos < end) {
209
0
    if (!IsAsciiWhitespace(*pos)) {
210
0
      return false;  // Trailing non-space
211
0
    }
212
0
    pos++;
213
0
  }
214
215
0
  *result = v;
216
0
  return true;  // Trailing space is OK
217
0
}
218
219
2
int to_int(BigStr* s, int base) {
220
2
  int i;
221
2
  if (StringToInt(s->data_, len(s), base, &i)) {
222
1
    return i;  // truncated to int
223
1
  } else {
224
1
    throw Alloc<ValueError>();
225
1
  }
226
2
}
227
228
0
BigStr* chr(int i) {
229
  // NOTE: i should be less than 256, in which we could return an object from
230
  // GLOBAL_STR() pool, like StrIter
231
0
  auto result = NewStr(1);
232
0
  result->data_[0] = i;
233
0
  return result;
234
0
}
235
236
0
int ord(BigStr* s) {
237
0
  assert(len(s) == 1);
238
  // signed to unsigned conversion, so we don't get values like -127
239
0
  uint8_t c = static_cast<uint8_t>(s->data_[0]);
240
0
  return c;
241
0
}
242
243
0
bool to_bool(BigStr* s) {
244
0
  return len(s) != 0;
245
0
}
246
247
0
double to_float(int i) {
248
0
  return static_cast<double>(i);
249
0
}
250
251
0
double to_float(BigStr* s) {
252
0
  char* begin = s->data_;
253
0
  char* end_pos = begin + len(s);
254
0
  char* orig_end = end_pos;
255
256
0
  errno = 0;
257
0
  double result = strtod(begin, &end_pos);
258
259
0
  if (errno == ERANGE) {  // error: overflow or underflow
260
0
    if (result >= HUGE_VAL) {
261
0
      return INFINITY;
262
0
    } else if (result <= -HUGE_VAL) {
263
0
      return -INFINITY;
264
0
    } else if (-DBL_MIN <= result && result <= DBL_MIN) {
265
0
      return 0.0;
266
0
    } else {
267
0
      FAIL("Invalid value after ERANGE");
268
0
    }
269
0
  }
270
0
  if (end_pos == begin) {  // error: not a floating point number
271
0
    throw Alloc<ValueError>();
272
0
  }
273
0
  if (end_pos != orig_end) {  // trailing data like '5s' not alowed
274
0
    while (end_pos < orig_end) {
275
0
      if (!IsAsciiWhitespace(*end_pos)) {
276
0
        throw Alloc<ValueError>();  // Trailing non-space
277
0
      }
278
0
      end_pos++;
279
0
    }
280
0
  }
281
282
0
  return result;
283
0
}
284
285
// e.g. ('a' in 'abc')
286
0
bool str_contains(BigStr* haystack, BigStr* needle) {
287
  // Common case
288
0
  if (len(needle) == 1) {
289
0
    return memchr(haystack->data_, needle->data_[0], len(haystack));
290
0
  }
291
292
0
  if (len(needle) > len(haystack)) {
293
0
    return false;
294
0
  }
295
296
  // General case. TODO: We could use a smarter substring algorithm.
297
298
0
  const char* end = haystack->data_ + len(haystack);
299
0
  const char* last_possible = end - len(needle);
300
0
  const char* p = haystack->data_;
301
302
0
  while (p <= last_possible) {
303
0
    if (memcmp(p, needle->data_, len(needle)) == 0) {
304
0
      return true;
305
0
    }
306
0
    p++;
307
0
  }
308
0
  return false;
309
0
}
310
311
0
BigStr* str_repeat(BigStr* s, int times) {
312
  // Python allows -1 too, and Oil used that
313
0
  if (times <= 0) {
314
0
    return kEmptyString;
315
0
  }
316
0
  int len_ = len(s);
317
0
  int new_len = len_ * times;
318
0
  BigStr* result = NewStr(new_len);
319
320
0
  char* dest = result->data_;
321
0
  for (int i = 0; i < times; i++) {
322
0
    memcpy(dest, s->data_, len_);
323
0
    dest += len_;
324
0
  }
325
0
  return result;
326
0
}
327
328
// for os_path.join()
329
// NOTE(Jesse): Perfect candidate for BoundedBuffer
330
0
BigStr* str_concat3(BigStr* a, BigStr* b, BigStr* c) {
331
0
  int a_len = len(a);
332
0
  int b_len = len(b);
333
0
  int c_len = len(c);
334
335
0
  int new_len = a_len + b_len + c_len;
336
0
  BigStr* result = NewStr(new_len);
337
0
  char* pos = result->data_;
338
339
0
  memcpy(pos, a->data_, a_len);
340
0
  pos += a_len;
341
342
0
  memcpy(pos, b->data_, b_len);
343
0
  pos += b_len;
344
345
0
  memcpy(pos, c->data_, c_len);
346
347
0
  assert(pos + c_len == result->data_ + new_len);
348
349
0
  return result;
350
0
}
351
352
0
BigStr* str_concat(BigStr* a, BigStr* b) {
353
0
  int a_len = len(a);
354
0
  int b_len = len(b);
355
0
  int new_len = a_len + b_len;
356
0
  BigStr* result = NewStr(new_len);
357
0
  char* buf = result->data_;
358
359
0
  memcpy(buf, a->data_, a_len);
360
0
  memcpy(buf + a_len, b->data_, b_len);
361
362
0
  return result;
363
0
}
364
365
//
366
// Comparators
367
//
368
369
32
bool str_equals(BigStr* left, BigStr* right) {
370
  // Fast path for identical strings.  String deduplication during GC could
371
  // make this more likely.  String interning could guarantee it, allowing us
372
  // to remove memcmp().
373
32
  if (left == right) {
374
1
    return true;
375
1
  }
376
377
  // TODO: It would be nice to remove this condition, but I think we need MyPy
378
  // strict None checking for it
379
31
  if (left == nullptr || right == nullptr) {
380
0
    return false;
381
0
  }
382
383
31
  if (left->len_ != right->len_) {
384
1
    return false;
385
1
  }
386
387
30
  return memcmp(left->data_, right->data_, left->len_) == 0;
388
31
}
389
390
0
bool maybe_str_equals(BigStr* left, BigStr* right) {
391
0
  if (left && right) {
392
0
    return str_equals(left, right);
393
0
  }
394
395
0
  if (!left && !right) {
396
0
    return true;  // None == None
397
0
  }
398
399
0
  return false;  // one is None and one is a BigStr*
400
0
}
401
402
7
bool items_equal(BigStr* left, BigStr* right) {
403
7
  return str_equals(left, right);
404
7
}
405
406
7
bool keys_equal(BigStr* left, BigStr* right) {
407
7
  return items_equal(left, right);
408
7
}
409
410
0
bool items_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2) {
411
0
  return (t1->at0() == t2->at0()) && (t1->at1() == t2->at1());
412
0
}
413
414
0
bool keys_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2) {
415
0
  return items_equal(t1, t2);
416
0
}
417
418
0
bool items_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2) {
419
0
  return items_equal(t1->at0(), t2->at0()) && (t1->at1() == t2->at1());
420
0
}
421
422
0
bool keys_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2) {
423
0
  return items_equal(t1, t2);
424
0
}
425
426
0
bool str_equals_c(BigStr* s, const char* c_string, int c_len) {
427
  // Needs SmallStr change
428
0
  if (len(s) == c_len) {
429
0
    return memcmp(s->data_, c_string, c_len) == 0;
430
0
  } else {
431
0
    return false;
432
0
  }
433
0
}
434
435
129
bool str_equals0(const char* c_string, BigStr* s) {
436
129
  int n = strlen(c_string);
437
129
  if (len(s) == n) {
438
13
    return memcmp(s->data_, c_string, n) == 0;
439
116
  } else {
440
116
    return false;
441
116
  }
442
129
}
443
444
0
int hash(BigStr* s) {
445
0
  return s->hash(fnv1);
446
0
}
447
448
0
int max(int a, int b) {
449
0
  return std::max(a, b);
450
0
}
451
452
0
int min(int a, int b) {
453
0
  return std::min(a, b);
454
0
}
455
456
0
int max(List<int>* elems) {
457
0
  int n = len(elems);
458
0
  if (n < 1) {
459
0
    throw Alloc<ValueError>();
460
0
  }
461
462
0
  int ret = elems->at(0);
463
0
  for (int i = 0; i < n; ++i) {
464
0
    int cand = elems->at(i);
465
0
    if (cand > ret) {
466
0
      ret = cand;
467
0
    }
468
0
  }
469
470
0
  return ret;
471
0
}