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