cpp

Coverage Report

Created: 2025-06-15 14:02

/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 <math.h>      // isnan(), isinf()
6
#include <stdio.h>
7
8
#include "mycpp/gc_alloc.h"
9
#include "mycpp/gc_builtins.h"  // StringToInt64
10
#include "mycpp/gc_str.h"
11
12
namespace mops {
13
14
const BigInt ZERO = BigInt{0};
15
const BigInt ONE = BigInt{1};
16
const BigInt MINUS_ONE = BigInt{-1};
17
const BigInt MINUS_TWO = BigInt{-2};  // for printf
18
19
static const int kInt64BufSize = 32;  // more than twice as big as kIntBufSize
20
21
// Note: Could also use OverAllocatedStr, but most strings are small?
22
23
// Similar to str(int i) in gc_builtins.cc
24
25
0
BigStr* ToStr(BigInt b) {
26
0
  char buf[kInt64BufSize];
27
0
  int len = snprintf(buf, kInt64BufSize, "%" PRId64, b);
28
0
  return ::StrFromC(buf, len);
29
0
}
30
31
0
BigStr* ToOctal(BigInt b) {
32
0
  char buf[kInt64BufSize];
33
0
  int len = snprintf(buf, kInt64BufSize, "%" PRIo64, b);
34
0
  return ::StrFromC(buf, len);
35
0
}
36
37
0
BigStr* ToHexUpper(BigInt b) {
38
0
  char buf[kInt64BufSize];
39
0
  int len = snprintf(buf, kInt64BufSize, "%" PRIX64, b);
40
0
  return ::StrFromC(buf, len);
41
0
}
42
43
0
BigStr* ToHexLower(BigInt b) {
44
0
  char buf[kInt64BufSize];
45
0
  int len = snprintf(buf, kInt64BufSize, "%" PRIx64, b);
46
0
  return ::StrFromC(buf, len);
47
0
}
48
49
// Copied from gc_builtins - to_int()
50
0
BigInt FromStr(BigStr* s, int base) {
51
0
  int64_t i;
52
0
  if (StringToInt64(s->data_, len(s), base, &i)) {
53
0
    return i;
54
0
  } else {
55
0
    throw Alloc<ValueError>();
56
0
  }
57
0
}
58
59
0
Tuple2<bool, BigInt> FromStr2(BigStr* s, int base) {
60
0
  int64_t i;
61
0
  if (StringToInt64(s->data_, len(s), base, &i)) {
62
0
    return Tuple2<bool, BigInt>(true, i);
63
0
  } else {
64
0
    return Tuple2<bool, BigInt>(false, MINUS_ONE);
65
0
  }
66
0
}
67
68
0
Tuple2<bool, BigInt> FromFloat(double f) {
69
0
  if (isnan(f) || isinf(f)) {
70
0
    return Tuple2<bool, BigInt>(false, MINUS_ONE);
71
0
  }
72
#ifdef BIGINT
73
  // Testing that _bin/cxx-opt+bigint/ysh is actually different!
74
  log("*** BIGINT active ***");
75
#endif
76
0
  return Tuple2<bool, BigInt>(true, static_cast<BigInt>(f));
77
0
}
78
79
}  // namespace mops