cpp

Coverage Report

Created: 2024-07-25 00:04

/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
14
BigStr* ToStr(BigInt b) {
25
14
  char buf[kInt64BufSize];
26
14
  int len = snprintf(buf, kInt64BufSize, "%" PRId64, b);
27
14
  return ::StrFromC(buf, len);
28
14
}
29
30
6
BigStr* ToOctal(BigInt b) {
31
6
  char buf[kInt64BufSize];
32
6
  int len = snprintf(buf, kInt64BufSize, "%" PRIo64, b);
33
6
  return ::StrFromC(buf, len);
34
6
}
35
36
6
BigStr* ToHexUpper(BigInt b) {
37
6
  char buf[kInt64BufSize];
38
6
  int len = snprintf(buf, kInt64BufSize, "%" PRIX64, b);
39
6
  return ::StrFromC(buf, len);
40
6
}
41
42
6
BigStr* ToHexLower(BigInt b) {
43
6
  char buf[kInt64BufSize];
44
6
  int len = snprintf(buf, kInt64BufSize, "%" PRIx64, b);
45
6
  return ::StrFromC(buf, len);
46
6
}
47
48
// Copied from gc_builtins - to_int()
49
1
BigInt FromStr(BigStr* s, int base) {
50
1
  int64_t i;
51
1
  if (StringToInt64(s->data_, len(s), base, &i)) {
52
1
    return i;
53
1
  } else {
54
0
    throw Alloc<ValueError>();
55
0
  }
56
1
}
57
58
}  // namespace mops