@@ -50,96 +50,101 @@ void reverse(char* begin, char* end) {
50
50
}
51
51
}
52
52
53
- char * itoa (int value, char * result, int base) {
54
- if (base < 2 || base > 16 ) {
55
- *result = 0 ;
56
- return result;
57
- }
58
-
59
- char * out = result;
60
- int quotient = abs (value);
53
+ char * itoa ( int val, char *string, int radix )
54
+ {
55
+ return ltoa ( val, string, radix ) ;
56
+ }
61
57
62
- do {
63
- const int tmp = quotient / base;
64
- *out = " 0123456789abcdef" [quotient - (tmp * base)];
65
- ++out;
66
- quotient = tmp;
67
- } while (quotient);
58
+ char * ltoa ( long val, char *string, int radix )
59
+ {
60
+ char tmp[33 ];
61
+ char *tp = tmp;
62
+ long i;
63
+ unsigned long v;
64
+ int sign;
65
+ char *sp;
68
66
69
- // Apply negative sign
70
- if (value < 0 )
71
- *out++ = ' -' ;
67
+ if ( string == NULL )
68
+ {
69
+ return 0 ;
70
+ }
72
71
73
- reverse (result, out);
74
- *out = 0 ;
75
- return result ;
76
- }
72
+ if (radix > 36 || radix <= 1 )
73
+ {
74
+ return 0 ;
75
+ }
77
76
78
- char * ltoa (long value, char * result, int base) {
79
- if (base < 2 || base > 16 ) {
80
- *result = 0 ;
81
- return result;
77
+ sign = (radix == 10 && val < 0 );
78
+ if (sign)
79
+ {
80
+ v = -val;
81
+ }
82
+ else
83
+ {
84
+ v = (unsigned long )val;
82
85
}
83
86
84
- char * out = result;
85
- long quotient = abs (value);
87
+ while (v || tp == tmp)
88
+ {
89
+ i = v % radix;
90
+ v = v / radix;
91
+ if (i < 10 )
92
+ *tp++ = i+' 0' ;
93
+ else
94
+ *tp++ = i + ' a' - 10 ;
95
+ }
86
96
87
- do {
88
- const long tmp = quotient / base;
89
- *out = " 0123456789abcdef" [quotient - (tmp * base)];
90
- ++out;
91
- quotient = tmp;
92
- } while (quotient);
97
+ sp = string;
93
98
94
- // Apply negative sign
95
- if (value < 0 )
96
- *out++ = ' -' ;
99
+ if (sign)
100
+ *sp++ = ' -' ;
101
+ while (tp > tmp)
102
+ *sp++ = *--tp;
103
+ *sp = 0 ;
97
104
98
- reverse (result, out);
99
- *out = 0 ;
100
- return result;
105
+ return string;
101
106
}
102
107
103
- char * utoa (unsigned value, char * result, int base) {
104
- if (base < 2 || base > 16 ) {
105
- *result = 0 ;
106
- return result;
107
- }
108
+ char * utoa ( unsigned int val, char *string, int radix )
109
+ {
110
+ return ultoa ( val, string, radix ) ;
111
+ }
108
112
109
- char * out = result;
110
- unsigned quotient = value;
113
+ char * ultoa ( unsigned long val, char *string, int radix )
114
+ {
115
+ char tmp[33 ];
116
+ char *tp = tmp;
117
+ long i;
118
+ unsigned long v = val;
119
+ char *sp;
111
120
112
- do {
113
- const unsigned tmp = quotient / base;
114
- *out = " 0123456789abcdef" [quotient - (tmp * base)];
115
- ++out;
116
- quotient = tmp;
117
- } while (quotient);
121
+ if ( string == NULL )
122
+ {
123
+ return 0 ;
124
+ }
118
125
119
- reverse (result, out);
120
- *out = 0 ;
121
- return result ;
122
- }
126
+ if (radix > 36 || radix <= 1 )
127
+ {
128
+ return 0 ;
129
+ }
123
130
124
- char * ultoa (unsigned long value, char * result, int base) {
125
- if (base < 2 || base > 16 ) {
126
- *result = 0 ;
127
- return result;
131
+ while (v || tp == tmp)
132
+ {
133
+ i = v % radix;
134
+ v = v / radix;
135
+ if (i < 10 )
136
+ *tp++ = i+' 0' ;
137
+ else
138
+ *tp++ = i + ' a' - 10 ;
128
139
}
129
140
130
- char * out = result;
131
- unsigned long quotient = value;
141
+ sp = string;
132
142
133
- do {
134
- const unsigned long tmp = quotient / base;
135
- *out = " 0123456789abcdef" [quotient - (tmp * base)];
136
- ++out;
137
- quotient = tmp;
138
- } while (quotient);
143
+ while (tp > tmp)
144
+ *sp++ = *--tp;
145
+ *sp = 0 ;
139
146
140
- reverse (result, out);
141
- *out = 0 ;
142
- return result;
147
+ return string;
143
148
}
144
149
145
150
char * dtostrf (double number, signed char width, unsigned char prec, char *s) {
@@ -190,7 +195,6 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
190
195
*out = ' .' ;
191
196
++out;
192
197
193
-
194
198
for (unsigned char decShift = prec; decShift > 0 ; decShift--) {
195
199
remainder *= 10.0 ;
196
200
}
0 commit comments