/home/uke/oil/mycpp/gc_mops.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "mycpp/gc_mops.h" |
2 | | |
3 | | #include <errno.h> |
4 | | #include <inttypes.h> // PRIo64, PRIx64 |
5 | | #include <stdio.h> |
6 | | |
7 | | #include "mycpp/gc_alloc.h" |
8 | | #include "mycpp/gc_builtins.h" // StringToInt64 |
9 | | #include "mycpp/gc_str.h" |
10 | | |
11 | | namespace mops { |
12 | | |
13 | | const BigInt ZERO = BigInt{0}; |
14 | | const BigInt ONE = BigInt{1}; |
15 | | const BigInt MINUS_ONE = BigInt{-1}; |
16 | | const BigInt MINUS_TWO = BigInt{-2}; // for printf |
17 | | |
18 | | static const int kInt64BufSize = 32; // more than twice as big as kIntBufSize |
19 | | |
20 | | // Note: Could also use OverAllocatedStr, but most strings are small? |
21 | | |
22 | | // Similar to str(int i) in gc_builtins.cc |
23 | | |
24 | 0 | BigStr* ToStr(BigInt b) { |
25 | 0 | char buf[kInt64BufSize]; |
26 | 0 | int len = snprintf(buf, kInt64BufSize, "%" PRId64, b); |
27 | 0 | return ::StrFromC(buf, len); |
28 | 0 | } |
29 | | |
30 | 0 | BigStr* ToOctal(BigInt b) { |
31 | 0 | char buf[kInt64BufSize]; |
32 | 0 | int len = snprintf(buf, kInt64BufSize, "%" PRIo64, b); |
33 | 0 | return ::StrFromC(buf, len); |
34 | 0 | } |
35 | | |
36 | 0 | BigStr* ToHexUpper(BigInt b) { |
37 | 0 | char buf[kInt64BufSize]; |
38 | 0 | int len = snprintf(buf, kInt64BufSize, "%" PRIX64, b); |
39 | 0 | return ::StrFromC(buf, len); |
40 | 0 | } |
41 | | |
42 | 0 | BigStr* ToHexLower(BigInt b) { |
43 | 0 | char buf[kInt64BufSize]; |
44 | 0 | int len = snprintf(buf, kInt64BufSize, "%" PRIx64, b); |
45 | 0 | return ::StrFromC(buf, len); |
46 | 0 | } |
47 | | |
48 | | // Copied from gc_builtins - to_int() |
49 | 0 | BigInt FromStr(BigStr* s, int base) { |
50 | 0 | int64_t i; |
51 | 0 | if (StringToInt64(s->data_, len(s), base, &i)) { |
52 | 0 | return i; |
53 | 0 | } else { |
54 | 0 | throw Alloc<ValueError>(); |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | | } // namespace mops |