@@ -78,131 +78,74 @@ String::String(char c)
78
78
*this = buf;
79
79
}
80
80
81
- static char * c_spec_signed[] = {
82
- (char *) " %d" ,
83
- (char *) " %ld" ,
84
- (char *) " %o" ,
85
- (char *) " %x" ,
86
- (char *) " unsupported base" ,
87
- };
88
-
89
- static char * c_spec_unsigned[] = {
90
- (char *) " %u" ,
91
- (char *) " %lu" ,
92
- (char *) " %o" ,
93
- (char *) " %x" ,
94
- (char *) " unsupported base" ,
95
- };
96
-
97
-
98
- char * String::getCSpec (int base, bool issigned, bool islong){
99
- int int_idx = 0 ;
100
-
101
- if (islong == true )
102
- int_idx = 1 ;
103
-
104
- switch (base){
105
- case 8 :
106
- if (issigned == true )
107
- return c_spec_signed[2 ];
108
- else
109
- return c_spec_unsigned[2 ];
110
- case 10 :
111
- if (issigned == true )
112
- return c_spec_signed[int_idx];
113
- else
114
- return c_spec_unsigned[int_idx];
115
- case 16 :
116
- if (issigned == true )
117
- return c_spec_signed[3 ];
118
- else
119
- return c_spec_unsigned[3 ];
120
- default :
121
- return c_spec_unsigned[4 ];
122
- }
123
- };
124
-
125
81
String::String (unsigned char value, unsigned char base)
126
82
{
127
83
init ();
128
84
char buf[1 + 8 * sizeof (unsigned char )];
129
- // utoa(value, buf, base);
130
- snprintf (buf, sizeof (buf), getCSpec (base, false , false ), value);
85
+ utoa (value, buf, base);
131
86
*this = buf;
132
87
}
133
88
134
89
String::String (int value, unsigned char base)
135
90
{
136
91
init ();
137
92
char buf[2 + 8 * sizeof (int )];
138
- // itoa(value, buf, base);
139
- snprintf (buf, sizeof (buf), getCSpec (base, true , false ), value);
93
+ itoa (value, buf, base);
140
94
*this = buf;
141
95
}
142
96
143
97
String::String (unsigned int value, unsigned char base)
144
98
{
145
99
init ();
146
100
char buf[1 + 8 * sizeof (unsigned int )];
147
- // utoa(value, buf, base);
148
- snprintf (buf, sizeof (buf), getCSpec (base, false , false ), value);
101
+ utoa (value, buf, base);
149
102
*this = buf;
150
103
}
151
104
152
105
String::String (long value, unsigned char base)
153
106
{
154
107
init ();
155
108
char buf[2 + 8 * sizeof (long )];
156
- // ltoa(value, buf, base);
157
- snprintf (buf, sizeof (buf), getCSpec (base, true , true ), value);
109
+ ltoa (value, buf, base);
158
110
*this = buf;
159
111
}
160
112
161
113
String::String (unsigned long value, unsigned char base)
162
114
{
163
115
init ();
164
116
char buf[1 + 8 * sizeof (unsigned long )];
165
- // ultoa(value, buf, base);
166
- snprintf (buf, sizeof (buf), getCSpec (base, false , true ), value);
117
+ ultoa (value, buf, base);
167
118
*this = buf;
168
119
}
169
120
170
121
String::String (long long value, unsigned char base)
171
122
{
172
123
init ();
173
124
char buf[2 + 8 * sizeof (long long )];
174
- // ltoa(value, buf, base);
175
- snprintf (buf, sizeof (buf), getCSpec (base, true , true ), value);
125
+ ltoa (value, buf, base);
176
126
*this = buf;
177
127
}
178
128
179
129
String::String (unsigned long long value, unsigned char base)
180
130
{
181
131
init ();
182
132
char buf[1 + 8 * sizeof (unsigned long long )];
183
- // ultoa(value, buf, base);
184
- snprintf (buf, sizeof (buf), getCSpec (base, false , true ), value);
133
+ ultoa (value, buf, base);
185
134
*this = buf;
186
135
}
187
136
188
137
String::String (float value, unsigned char decimalPlaces)
189
138
{
190
139
init ();
191
140
char buf[33 ];
192
- String dec (decimalPlaces);
193
- String tmp = " %." + dec + " f" ;
194
- snprintf (buf, sizeof (buf), tmp.c_str (), value);
195
- *this = buf;
141
+ *this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
196
142
}
197
143
198
144
String::String (double value, unsigned char decimalPlaces)
199
145
{
200
- init ();
201
- char buf[33 ];
202
- String dec (decimalPlaces);
203
- String tmp = " %." + dec + " f" ;
204
- snprintf (buf, sizeof (buf), tmp.c_str (), value);
205
- *this = buf;
146
+ init ();
147
+ char buf[33 ];
148
+ *this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
206
149
}
207
150
208
151
String::~String ()
@@ -328,11 +271,11 @@ unsigned char String::concat(const String &s)
328
271
return concat (s.buffer , s.len );
329
272
}
330
273
331
- unsigned char String::concat (const char *cstr, unsigned int _length )
274
+ unsigned char String::concat (const char *cstr, unsigned int length )
332
275
{
333
- unsigned int newlen = len + _length ;
276
+ unsigned int newlen = len + length ;
334
277
if (!cstr) return 0 ;
335
- if (_length == 0 ) return 1 ;
278
+ if (length == 0 ) return 1 ;
336
279
if (!reserve (newlen)) return 0 ;
337
280
strcpy (buffer + len, cstr);
338
281
len = newlen;
@@ -356,71 +299,64 @@ unsigned char String::concat(char c)
356
299
unsigned char String::concat (unsigned char num)
357
300
{
358
301
char buf[1 + 3 * sizeof (unsigned char )];
359
- // itoa(num, buf, 10);
360
- snprintf (buf, sizeof (buf), getCSpec (10 , true , false ), num);
302
+ itoa (num, buf, 10 );
361
303
return concat (buf, strlen (buf));
362
304
}
363
305
364
306
unsigned char String::concat (int num)
365
307
{
366
308
char buf[2 + 3 * sizeof (int )];
367
- // itoa(num, buf, 10);
368
- snprintf (buf, sizeof (buf), getCSpec (10 , true , false ), num);
309
+ itoa (num, buf, 10 );
369
310
return concat (buf, strlen (buf));
370
311
}
371
312
372
313
unsigned char String::concat (unsigned int num)
373
314
{
374
315
char buf[1 + 3 * sizeof (unsigned int )];
375
- // utoa(num, buf, 10);
376
- snprintf (buf, sizeof (buf), getCSpec (10 , false , false ), num);
316
+ utoa (num, buf, 10 );
377
317
return concat (buf, strlen (buf));
378
318
}
379
319
380
320
unsigned char String::concat (long num)
381
321
{
382
322
char buf[2 + 3 * sizeof (long )];
383
- // ltoa(num, buf, 10);
384
- snprintf (buf, sizeof (buf), getCSpec (10 , true , true ), num);
323
+ ltoa (num, buf, 10 );
385
324
return concat (buf, strlen (buf));
386
325
}
387
326
388
327
unsigned char String::concat (unsigned long num)
389
328
{
390
329
char buf[1 + 3 * sizeof (unsigned long )];
391
- // ultoa(num, buf, 10);
392
- snprintf (buf, sizeof (buf), getCSpec (10 , false , true ), num);
330
+ ultoa (num, buf, 10 );
393
331
return concat (buf, strlen (buf));
394
332
}
395
333
396
334
unsigned char String::concat (long long num)
397
335
{
398
- char buf[12 ];
399
- // ltoa(num, buf, 10);
400
- snprintf (buf, sizeof (buf), getCSpec (10 , true , true ), num);
336
+ char buf[2 + 3 * sizeof (long long )];
337
+ ltoa (num, buf, 10 );
401
338
return concat (buf, strlen (buf));
402
339
}
403
340
404
341
unsigned char String::concat (unsigned long long num)
405
342
{
406
- char buf[11 ];
407
- // ultoa(num, buf, 10);
408
- snprintf (buf, sizeof (buf), getCSpec (10 , false , true ), num);
343
+ char buf[1 + 3 * sizeof (unsigned long long )];
344
+ ultoa (num, buf, 10 );
409
345
return concat (buf, strlen (buf));
410
346
}
411
347
412
348
unsigned char String::concat (float num)
413
349
{
414
350
char buf[20 ];
415
- snprintf (buf, sizeof (buf), " %f " , num );
416
- return concat (buf , strlen (buf ));
351
+ char * string = dtostrf (num, 4 , 2 , buf );
352
+ return concat (string , strlen (string ));
417
353
}
418
354
419
355
unsigned char String::concat (double num)
420
356
{
421
- char buf[20 ];
422
- snprintf (buf, sizeof (buf), " %f " , num );
423
- return concat (buf , strlen (buf ));
357
+ char buf[20 ];
358
+ char * string = dtostrf (num, 4 , 2 , buf );
359
+ return concat (string , strlen (string ));
424
360
}
425
361
426
362
/* ********************************************/
0 commit comments