forked from Yellow-Camper/libevhtp
-
Notifications
You must be signed in to change notification settings - Fork 17
/
numtoa.c
71 lines (56 loc) · 1.36 KB
/
numtoa.c
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
/*
* DERIVED FROM the stringencoders library's modp_numtoa
*
* Copyright ; 2007, Nick Galbreath -- nickg [at] client9 [dot] com
* All rights reserved.
* http://code.google.com/p/stringencoders/
* Released under the MIT license.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "internal.h"
#include "numtoa.h"
static inline void
strreverse(char * begin, char * end) {
char aux;
while (end > begin) {
aux = *end, *end-- = *begin, *begin++ = aux;
}
}
size_t
evhtp_modp_u64toa(uint64_t value, char * str) {
char * wstr = str;
/* Conversion. Number is reversed. */
do {
*wstr++ = (char)(48 + (value % 10));
} while (value /= 10);
*wstr = '\0';
/* Reverse string */
strreverse(str, wstr - 1);
return (size_t)(wstr - str);
}
size_t
evhtp_modp_u32toa(uint32_t value, char * str) {
char * wstr = str;
/* Conversion. Number is reversed. */
do {
*wstr++ = (char)(48 + (value % 10));
} while (value /= 10);
*wstr = '\0';
/* Reverse string */
strreverse(str, wstr - 1);
return (size_t)(wstr - str);
}
inline size_t
evhtp_modp_sizetoa(size_t value, char * str) {
#if EVHTP_SYS_ARCH == 64
return evhtp_modp_u64toa(value, str);
#elif EVHTP_SYS_ARCH == 32
return evhtp_modp_u32toa(value, str);
#else
#warning "UNKNOWN ARCH"
#endif
}