summaryrefslogtreecommitdiff
path: root/utils/utoa.h
blob: 8d304f97e860e85c9af8a1cd97c61a1122b20729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef UTOA_H
#define UTOA_H

#include <stdint.h>
#include <string>
#include <cstring>

#ifndef DIGIT_LOOKUP_TABLE
# define DIGIT_LOOKUP_TABLE 0
#endif

// The largest 32-bit integer is 4294967295, that is 10 chars
// 1 more for sign, and 1 for 0-termination of string
// generally: 2 + std::numeric_limits<T>::is_signed + std::numeric_limits<T>::digits10
const unsigned utoa_bufsize=12;
const unsigned utoa_bufsizem1=utoa_bufsize-1;

#ifdef DIGIT_LOOKUP_TABLE
namespace {
char digits[] = "0123456789";
}
#endif

inline char digit_to_char(int d) {
  return
#ifdef DIGIT_LOOKUP_TABLE
    digits[d];
#else
    '0'+d;
#endif
}

// returns n in string [return,num); *num=0 yourself before calling if you want a c_str
inline char *utoa(char *num,uint32_t n) {
  if ( !n ) {
    *--num='0';
  } else {
    uint32_t rem;
    // 3digit lookup table, divide by 1000 faster?
    while ( n ) {
#if 1
      rem = n;
      n /= 10;
      rem -= 10*n;		// maybe this is faster than mod because we are already dividing
#else
      rem = n%10; // would optimizer combine these together?
      n   = n/10;
#endif
      *--num = digit_to_char(rem);
    }
  }
  return num;
}

inline char *itoa(char *p,int32_t n) {
  if (n<0) {
    p=utoa(p,-n); // (unsigned)(-INT_MIN) == 0x1000000 in 2s complement and not == 0.
    *--p='-';
    return p;
  } else
    return utoa(p,n);
}

inline std::string utos(uint32_t n) {
  char buf[utoa_bufsize];
  char *end=buf+utoa_bufsize;
  char *p=utoa(end,n);
  return std::string(p,end);
}

inline std::string itos(int32_t n) {
  char buf[utoa_bufsize];
  char *end=buf+utoa_bufsize;
  char *p=itoa(end,n);
  return std::string(p,end);
}

//returns position of '\0' terminating number written starting at to
inline char* append_utoa(char *to,uint32_t n) {
  char buf[utoa_bufsize];
  char *end=buf+utoa_bufsize;
  char *s=utoa(end,n);
  int ns=end-s;
  std::memcpy(to,s,ns);
  to+=ns;
  *to++=0;
  return to;
}

//returns position of '\0' terminating number written starting at to
inline char* append_itoa(char *to,int32_t n) {
  char buf[utoa_bufsize];
  char *end=buf+utoa_bufsize;
  char *s=itoa(end,n);
  int ns=end-s;
  std::memcpy(to,s,ns);
  to+=ns;
  *to++=0;
  return to;
}

#endif