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 245
 }
246 246
 
247 247
 
248
-#define INT2STR_MAX_LEN  (19+1+1) /* 2^64~= 16*10^18 => 19+1 digits + \0 */
248
+#define INT2STR_MAX_LEN  (19+1+1+1) /* 2^64~= 16*10^18 =>
249
+									   19+1 digits + sign + \0 */
249 250
 
250 251
 /* 
251 252
  * 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 292
 
293 293
 
294 294
 
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)
295
+/** unsigned long to str conversion using a provided buffer.
296
+ * Converts/prints an unsigned long to a string. The result buffer must be
297
+ * provided  and its length must be at least INT2STR_MAX_LEN.
298
+ * @param l - unsigned long to be converted
299
+ * @param r - pointer to result buffer
300
+ * @param r_size - result buffer size, must be at least INT2STR_MAX_LEN.
301
+ * @param *len - length of the written string, _without_ the terminating 0.
302
+ * @return  pointer _inside_ r, to the converted string (note: the string
303
+ *  is written from the end of the buffer and not from the start and hence
304
+ *  the returned pointer will most likely not be equal to r). In case of error
305
+ *  it returns 0 (the only error being insufficient provided buffer size).
306
+ */
307
+static inline char* int2strbuf(unsigned long l, char *r, int r_size, int* len)
298 308
 {
299 309
 	int i;
300 310
 
... ...
@@ -318,33 +334,70 @@ static inline char* int2strbuf(unsigned int l, char *r, int r_size, int* len)
318 318
 }
319 319
 
320 320
 extern char ut_buf_int2str[INT2STR_MAX_LEN];
321
-/* returns a pointer to a static buffer containing l in asciiz & sets len */
321
+/** interger(long) to string conversion.
322
+ * This version uses a static buffer (shared with sint2str()).
323
+ * WARNING: other function calls might overwrite the static buffer, so
324
+ * either always save the result immediately or use int2strbuf(...).
325
+ * @param l - unsigned long to be converted/printed.
326
+ * @param *len - will be filled with the final length (without the terminating
327
+ *   0).
328
+ * @return a pointer to a static buffer containing l in asciiz & sets len.
329
+ */
322 330
 static inline char* int2str(unsigned long l, int* len)
323 331
 {
324 332
 	return int2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
325 333
 }
326 334
 
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)
335
+
336
+
337
+/** signed long to str conversion using a provided buffer.
338
+ * Converts a long to a signed string. The result buffer must be provided
339
+ * and its length must be at least INT2STR_MAX_LEN.
340
+ * @param l - long to be converted
341
+ * @param r - pointer to result buffer
342
+ * @param r_size - result buffer size, must be at least INT2STR_MAX_LEN.
343
+ * @param *len - length of the written string, _without_ the terminating 0.
344
+ * @return  pointer _inside_ r, to the converted string (note: the string
345
+ *  is written from the end of the buffer and not from the start and hence
346
+ *  the returned pointer will most likely not be equal to r). In case of error
347
+ *  it returns 0 (the only error being insufficient provided buffer size).
348
+ */
349
+static inline char* sint2strbuf(long l, char* r, int r_size, int* len)
330 350
 {
331 351
 	int sign;
332 352
 	char *p;
353
+	int p_len;
333 354
 
334 355
 	sign = 0;
335 356
 	if(l<0) {
336 357
 		sign = 1;
337 358
 		l = -l;
338 359
 	}
339
-	p = int2str((unsigned long)l, len);
340
-	if(sign && *len<(INT2STR_MAX_LEN-1)) {
360
+	p = int2strbuf((unsigned long)l, r, r_size, &p_len);
361
+	if(sign && *len<(r_size-1)) {
341 362
 		*(--p) = '-';
342
-		if (len) (*len)++;
363
+		p_len++;;
343 364
 	}
365
+	if (likely(len))
366
+		*len = p_len;
344 367
 	return p;
345 368
 }
346 369
 
347 370
 
371
+/** Signed INTeger-TO-STRing: converts a long to a string.
372
+ * This version uses a static buffer, shared with int2str().
373
+ * WARNING: other function calls might overwrite the static buffer, so
374
+ * either always save the result immediately or use sint2strbuf(...).
375
+ * @param l - long to be converted/printed.
376
+ * @param *len - will be filled with the final length (without the terminating
377
+ *   0).
378
+ * @return a pointer to a static buffer containing l in asciiz & sets len.
379
+ */
380
+static inline char* sint2str(long l, int* len)
381
+{
382
+	return sint2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
383
+}
384
+
348 385
 
349 386
 
350 387
 #define USHORT2SBUF_MAX_LEN  5 /* 65535*/