cpp

Coverage Report

Created: 2024-08-25 11:48

/home/andy/git/oilshell/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
14
BigStr* ToStr(BigInt b) {
26
14
  char buf[kInt64BufSize];
27
14
  int len = snprintf(buf, kInt64BufSize, "%" PRId64, b);
28
14
  return ::StrFromC(buf, len);
29
14
}
30
31
6
BigStr* ToOctal(BigInt b) {
32
6
  char buf[kInt64BufSize];
33
6
  int len = snprintf(buf, kInt64BufSize, "%" PRIo64, b);
34
6
  return ::StrFromC(buf, len);
35
6
}
36
37
6
BigStr* ToHexUpper(BigInt b) {
38
6
  char buf[kInt64BufSize];
39
6
  int len = snprintf(buf, kInt64BufSize, "%" PRIX64, b);
40
6
  return ::StrFromC(buf, len);
41
6
}
42
43
6
BigStr* ToHexLower(BigInt b) {
44
6
  char buf[kInt64BufSize];
45
6
  int len = snprintf(buf, kInt64BufSize, "%" PRIx64, b);
46
6
  return ::StrFromC(buf, len);
47
6
}
48
49
// Copied from gc_builtins - to_int()
50
1
BigInt FromStr(BigStr* s, int base) {
51
1
  int64_t i;
52
1
  if (StringToInt64(s->data_, len(s), base, &i)) {
53
1
    return i;
54
1
  } else {
55
0
    throw Alloc<ValueError>();
56
0
  }
57
1
}
58
59
4
Tuple2<bool, BigInt> FromFloat(double f) {
60
4
  if (isnan(f) || isinf(f)) {
61
0
    return Tuple2<bool, BigInt>(false, MINUS_ONE);
62
0
  }
63
#ifdef BIGINT
64
  // Testing that _bin/cxx-opt+bigint/ysh is actually different!
65
  log("*** BIGINT active ***");
66
#endif
67
4
  return Tuple2<bool, BigInt>(true, static_cast<BigInt>(f));
68
4
}
69
70
}  // namespace mops