Browse code

core: ut.h: added sint2strbuf()

- added sint2strbuf() that works like sint2str(), but isntead of
using a static buffer, requires a buffer to be provided by the
called (hence being much safer).
- increased INST2STR_MAX_LEN to accommodate for sign.

Andrei Pelinescu-Onciul authored on 16/03/2010 18:28:36
Showing 1 changed files
... ...
@@ -42,6 +42,11 @@
42 42
  * 2007-05-14 added get_sys_ver() (andrei)
43 43
  * 2007-06-05 added MAX_UVAR_VALUE(), MAX_int(a,b) MIN_int(a,b) (andrei)
44 44
  * 2008-05-21 added ushort2sbuf(), ushort2str() (andrei)
45
+ * 2009-03-16 added sint2strbuf() and incremented INST2STR_MAX_LEN to account
46
+ *             for sign (andrei)
47
+ */
48
+/** various helper functions.
49
+ * @file
45 50
  */
46 51
 
47 52
 
... ...
@@ -245,7 +250,8 @@ static inline int btostr( char *p,  unsigned char val)
245 250
 }
246 251
 
247 252
 
248
-#define INT2STR_MAX_LEN  (19+1+1) /* 2^64~= 16*10^18 => 19+1 digits + \0 */
253
+#define INT2STR_MAX_LEN  (19+1+1+1) /* 2^64~= 16*10^18 =>
254
+									   19+1 digits + sign + \0 */
249 255
 
250 256
 /* 
251 257
  * returns a pointer to a static buffer containing l in asciiz (with base "base") & sets len 
... ...
@@ -292,9 +298,19 @@ static inline char* int2str_base(unsigned int l, int* len, int base)
292 298
 
293 299
 
294 300
 
295
-/* print int to asciiz in a string buffer
296
- * - be sure result buffer is at least INT2STR_MAX_LEN in size */
297
-static inline char* int2strbuf(unsigned int l, char *r, int r_size, int* len)
301
+/** unsigned long to str conversion using a provided buffer.
302
+ * Converts/prints an unsigned long to a string. The result buffer must be
303
+ * provided  and its length must be at least INT2STR_MAX_LEN.
304
+ * @param l - unsigned long to be converted
305
+ * @param r - pointer to result buffer
306
+ * @param r_size - result buffer size, must be at least INT2STR_MAX_LEN.
307
+ * @param *len - length of the written string, _without_ the terminating 0.
308
+ * @return  pointer _inside_ r, to the converted string (note: the string
309
+ *  is written from the end of the buffer and not from the start and hence
310
+ *  the returned pointer will most likely not be equal to r). In case of error
311
+ *  it returns 0 (the only error being insufficient provided buffer size).
312
+ */
313
+static inline char* int2strbuf(unsigned long l, char *r, int r_size, int* len)
298 314
 {
299 315
 	int i;
300 316
 
... ...
@@ -318,33 +334,70 @@ static inline char* int2strbuf(unsigned int l, char *r, int r_size, int* len)
318 334
 }
319 335
 
320 336
 extern char ut_buf_int2str[INT2STR_MAX_LEN];
321
-/* returns a pointer to a static buffer containing l in asciiz & sets len */
337
+/** interger(long) to string conversion.
338
+ * This version uses a static buffer (shared with sint2str()).
339
+ * WARNING: other function calls might overwrite the static buffer, so
340
+ * either always save the result immediately or use int2strbuf(...).
341
+ * @param l - unsigned long to be converted/printed.
342
+ * @param *len - will be filled with the final length (without the terminating
343
+ *   0).
344
+ * @return a pointer to a static buffer containing l in asciiz & sets len.
345
+ */
322 346
 static inline char* int2str(unsigned long l, int* len)
323 347
 {
324 348
 	return int2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
325 349
 }
326 350
 
327
-/* Signed INTeger-TO-STRing: convers a long to a string
328
- * returns a pointer to a static buffer containing l in asciiz & sets len */
329
-static inline char* sint2str(long l, int* len)
351
+
352
+
353
+/** signed long to str conversion using a provided buffer.
354
+ * Converts a long to a signed string. The result buffer must be provided
355
+ * and its length must be at least INT2STR_MAX_LEN.
356
+ * @param l - long to be converted
357
+ * @param r - pointer to result buffer
358
+ * @param r_size - result buffer size, must be at least INT2STR_MAX_LEN.
359
+ * @param *len - length of the written string, _without_ the terminating 0.
360
+ * @return  pointer _inside_ r, to the converted string (note: the string
361
+ *  is written from the end of the buffer and not from the start and hence
362
+ *  the returned pointer will most likely not be equal to r). In case of error
363
+ *  it returns 0 (the only error being insufficient provided buffer size).
364
+ */
365
+static inline char* sint2strbuf(long l, char* r, int r_size, int* len)
330 366
 {
331 367
 	int sign;
332 368
 	char *p;
369
+	int p_len;
333 370
 
334 371
 	sign = 0;
335 372
 	if(l<0) {
336 373
 		sign = 1;
337 374
 		l = -l;
338 375
 	}
339
-	p = int2str((unsigned long)l, len);
340
-	if(sign && *len<(INT2STR_MAX_LEN-1)) {
376
+	p = int2strbuf((unsigned long)l, r, r_size, &p_len);
377
+	if(sign && *len<(r_size-1)) {
341 378
 		*(--p) = '-';
342
-		if (len) (*len)++;
379
+		p_len++;;
343 380
 	}
381
+	if (likely(len))
382
+		*len = p_len;
344 383
 	return p;
345 384
 }
346 385
 
347 386
 
387
+/** Signed INTeger-TO-STRing: converts a long to a string.
388
+ * This version uses a static buffer, shared with int2str().
389
+ * WARNING: other function calls might overwrite the static buffer, so
390
+ * either always save the result immediately or use sint2strbuf(...).
391
+ * @param l - long to be converted/printed.
392
+ * @param *len - will be filled with the final length (without the terminating
393
+ *   0).
394
+ * @return a pointer to a static buffer containing l in asciiz & sets len.
395
+ */
396
+static inline char* sint2str(long l, int* len)
397
+{
398
+	return sint2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
399
+}
400
+
348 401
 
349 402
 
350 403
 #define USHORT2SBUF_MAX_LEN  5 /* 65535*/