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