| 1 | #include "mycpp/gc_mops.h"
|
| 2 |
|
| 3 | #include <errno.h>
|
| 4 | #include <stdio.h>
|
| 5 |
|
| 6 | #include "mycpp/gc_alloc.h"
|
| 7 | #include "mycpp/gc_builtins.h" // StringToInt64
|
| 8 | #include "mycpp/gc_str.h"
|
| 9 |
|
| 10 | namespace mops {
|
| 11 |
|
| 12 | const BigInt ZERO = BigInt{0};
|
| 13 | const BigInt ONE = BigInt{1};
|
| 14 | const BigInt MINUS_ONE = BigInt{-1};
|
| 15 |
|
| 16 | static const int kInt64BufSize = 32; // more than twice as big as kIntBufSize
|
| 17 |
|
| 18 | // Copied from gc_builtins - str(int i)
|
| 19 | BigStr* ToStr(BigInt b) {
|
| 20 | BigStr* s = OverAllocatedStr(kInt64BufSize);
|
| 21 | int length = snprintf(s->data(), kInt64BufSize, "%ld", b);
|
| 22 | s->MaybeShrink(length);
|
| 23 | return s;
|
| 24 | }
|
| 25 |
|
| 26 | // Copied from gc_builtins - to_int()
|
| 27 | BigInt FromStr(BigStr* s, int base) {
|
| 28 | int64_t i;
|
| 29 | if (StringToInt64(s->data_, len(s), base, &i)) {
|
| 30 | return i;
|
| 31 | } else {
|
| 32 | throw Alloc<ValueError>();
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | } // namespace mops
|