@@ -50,96 +50,101 @@ void reverse(char* begin, char* end) {
5050 }
5151}
5252
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+ }
6157
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;
6866
69- // Apply negative sign
70- if (value < 0 )
71- *out++ = ' -' ;
67+ if ( string == NULL )
68+ {
69+ return 0 ;
70+ }
7271
73- reverse (result, out);
74- *out = 0 ;
75- return result ;
76- }
72+ if (radix > 36 || radix <= 1 )
73+ {
74+ return 0 ;
75+ }
7776
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;
8285 }
8386
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+ }
8696
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;
9398
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 ;
97104
98- reverse (result, out);
99- *out = 0 ;
100- return result;
105+ return string;
101106}
102107
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+ }
108112
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;
111120
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+ }
118125
119- reverse (result, out);
120- *out = 0 ;
121- return result ;
122- }
126+ if (radix > 36 || radix <= 1 )
127+ {
128+ return 0 ;
129+ }
123130
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 ;
128139 }
129140
130- char * out = result;
131- unsigned long quotient = value;
141+ sp = string;
132142
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 ;
139146
140- reverse (result, out);
141- *out = 0 ;
142- return result;
147+ return string;
143148}
144149
145150char * 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) {
190195 *out = ' .' ;
191196 ++out;
192197
193-
194198 for (unsigned char decShift = prec; decShift > 0 ; decShift--) {
195199 remainder *= 10.0 ;
196200 }
0 commit comments