OILS / mycpp / gc_mops.cc View on Github | oilshell.org

36 lines, 22 significant
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
10namespace mops {
11
12const BigInt ZERO = BigInt{0};
13const BigInt ONE = BigInt{1};
14const BigInt MINUS_ONE = BigInt{-1};
15
16static const int kInt64BufSize = 32; // more than twice as big as kIntBufSize
17
18// Copied from gc_builtins - str(int i)
19BigStr* 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()
27BigInt 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