mycpp

Coverage Report

Created: 2025-05-01 05:48

/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
3
BigStr* ToStr(BigInt b) {
26
3
  char buf[kInt64BufSize];
27
3
  int len = snprintf(buf, kInt64BufSize, "%" PRId64, b);
28
3
  return ::StrFromC(buf, len);
29
3
}
30
31
3
BigStr* ToOctal(BigInt b) {
32
3
  char buf[kInt64BufSize];
33
3
  int len = snprintf(buf, kInt64BufSize, "%" PRIo64, b);
34
3
  return ::StrFromC(buf, len);
35
3
}
36
37
3
BigStr* ToHexUpper(BigInt b) {
38
3
  char buf[kInt64BufSize];
39
3
  int len = snprintf(buf, kInt64BufSize, "%" PRIX64, b);
40
3
  return ::StrFromC(buf, len);
41
3
}
42
43
3
BigStr* ToHexLower(BigInt b) {
44
3
  char buf[kInt64BufSize];
45
3
  int len = snprintf(buf, kInt64BufSize, "%" PRIx64, b);
46
3
  return ::StrFromC(buf, len);
47
3
}
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
2
Tuple2<bool, BigInt> FromFloat(double f) {
69
2
  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
2
  return Tuple2<bool, BigInt>(true, static_cast<BigInt>(f));
77
2
}
78
79
}  // namespace mops