-
Notifications
You must be signed in to change notification settings - Fork 0
/
tFormat.cc
51 lines (43 loc) · 1.18 KB
/
tFormat.cc
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
#include "tFormat.hh"
namespace bvc {
namespace base {
uint32_t tFormat::format_chars(const char * const first, const char * const last)
{
if (first >= last) {
return 0;
}
register char * next = first;
register uint32_t value = 0;
do {
value = static_cast<uint32_t>((value << 3) + (value << 1) + _Digit_from_char(*next));
++next;
} while(next != last);
return value;
}
char* tFormat::format_uint(register uint32_t value)
{
char _buffer[BUFFER_SIZE];
bzero(_buffer, BUFFER_SIZE);
register char* ptr = _buffer + (BUFFER_SIZE - 1);
register unsigned index;
while (value >= 100) {
index = static_cast<unsigned>((value % 100) << 1);
value /= 100;
*--ptr = DIGITS[index + 1]; // "--ptr" this promise the last char is '\0'
*--ptr = DIGITS[index];
}
if (value < 10) {
*--ptr = static_cast<char>('0' + value);
return ptr;
}
index = static_cast<unsigned>(value << 1);
*--ptr = DIGITS[index + 1];
*--ptr = DIGITS[index];
return ptr;
}
std::string tFormat::format_uint_to_string(register uint32_t value)
{
return std::move(std::string(format_uint(value)));
}
}// base
}// bvc