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