forked from Yellow-Camper/libevhtp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numtoa.c
77 lines (61 loc) · 1.52 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
72
73
74
75
76
77
/*
* 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.
*
*/
#ifdef HAVE_CONFIG_H
#include "libevhtp-config.h" /* generated by config & autoconf */
#endif
#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 TARGET_CPU == x86_64
return evhtp_modp_u64toa(value, str);
#elif TARGET_CPU == i686
return evhtp_modp_u32toa(value, str);
#elif TARGET_CPU == armv71
return evhtp_modp_u32toa(value, str);
#else
#warning "UNKNOWN ARCH"
#endif
}