mycpp

Coverage Report

Created: 2024-07-09 17:10

/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
3
BigStr* ToStr(BigInt b) {
25
3
  char buf[kInt64BufSize];
26
3
  int len = snprintf(buf, kInt64BufSize, "%" PRId64, b);
27
3
  return ::StrFromC(buf, len);
28
3
}
29
30
3
BigStr* ToOctal(BigInt b) {
31
3
  char buf[kInt64BufSize];
32
3
  int len = snprintf(buf, kInt64BufSize, "%" PRIo64, b);
33
3
  return ::StrFromC(buf, len);
34
3
}
35
36
3
BigStr* ToHexUpper(BigInt b) {
37
3
  char buf[kInt64BufSize];
38
3
  int len = snprintf(buf, kInt64BufSize, "%" PRIX64, b);
39
3
  return ::StrFromC(buf, len);
40
3
}
41
42
3
BigStr* ToHexLower(BigInt b) {
43
3
  char buf[kInt64BufSize];
44
3
  int len = snprintf(buf, kInt64BufSize, "%" PRIx64, b);
45
3
  return ::StrFromC(buf, len);
46
3
}
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