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