Browse code

- added ushort2sbuf() and ushort2str(): they convert an unsigned short into its ascii representation (base 10). ushort2str() returns a pointer to a null terminated static string, while ushor2sbuf() fills the supplied buffer.

Andrei Pelinescu-Onciul authored on 21/05/2008 21:31:18
Showing 1 changed files
... ...
@@ -41,6 +41,7 @@
41 41
  * 2005-12-09 added msgid_var (andrei)
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
+ * 2008-05-21 added ushort2sbuf(), ushort2str() (andrei)
44 45
  */
45 46
 
46 47
 
... ...
@@ -56,6 +57,7 @@
56 56
 #include <unistd.h>
57 57
 #include <ctype.h>
58 58
 
59
+#include "compiler_opt.h"
59 60
 #include "config.h"
60 61
 #include "dprint.h"
61 62
 #include "str.h"
... ...
@@ -232,36 +234,37 @@ static inline int btostr( char *p,  unsigned char val)
232 232
  * returns a pointer to a static buffer containing l in asciiz (with base "base") & sets len 
233 233
  * left padded with 0 to "size"
234 234
  */
235
-static inline char* int2str_base_0pad(unsigned int l, int* len, int base, int size)
235
+static inline char* int2str_base_0pad(unsigned int l, int* len, int base, 
236
+											int size)
236 237
 {
237
-        static char r[INT2STR_MAX_LEN];
238
-        int i, j;
238
+	static char r[INT2STR_MAX_LEN];
239
+	int i, j;
239 240
 
240
-        if (base < 2) {
241
-                BUG("base underflow\n");
241
+	if (base < 2) {
242
+		BUG("base underflow\n");
242 243
 		return NULL;
243
-        }
244
-        if (base > 36) {
245
-                BUG("base overflow\n");
244
+	}
245
+	if (base > 36) {
246
+		BUG("base overflow\n");
246 247
 		return NULL;
247
-        }
248
-        i=INT2STR_MAX_LEN-2;
249
-        j=i-size;
250
-        r[INT2STR_MAX_LEN-1]=0; /* null terminate */
251
-        do{
252
-                r[i]=l%base;
253
-                if (r[i]<10)
254
-                        r[i]+='0';
255
-                else
256
-                        r[i]+='a'-10;
257
-                i--;
258
-                l/=base;
259
-        }while((l || i>j) && (i>=0));
260
-        if (l && (i<0)){
261
-                BUG("result buffer overflow\n");
262
-        }
263
-        if (len) *len=(INT2STR_MAX_LEN-2)-i;
264
-        return &r[i+1];
248
+	}
249
+	i=INT2STR_MAX_LEN-2;
250
+	j=i-size;
251
+	r[INT2STR_MAX_LEN-1]=0; /* null terminate */
252
+	do{
253
+		r[i]=l%base;
254
+		if (r[i]<10)
255
+		        r[i]+='0';
256
+		else
257
+		        r[i]+='a'-10;
258
+		i--;
259
+		l/=base;
260
+	}while((l || i>j) && (i>=0));
261
+	if (l && (i<0)){
262
+		BUG("result buffer overflow\n");
263
+	}
264
+	if (len) *len=(INT2STR_MAX_LEN-2)-i;
265
+	return &r[i+1];
265 266
 }
266 267
 
267 268
 /* returns a pointer to a static buffer containing l in asciiz (with base "base") & sets len */
... ...
@@ -294,6 +297,49 @@ static inline char* int2str(unsigned int l, int* len)
294 294
 
295 295
 
296 296
 
297
+#define USHORT2SBUF_MAX_LEN  5 /* 65535*/
298
+/* converts an unsigned short (16 bits) to asciiz
299
+ * returns bytes written or 0 on error
300
+ * the passed len must be at least USHORT2SBUF_MAX chars or error
301
+ * would be returned.
302
+ * (optimized for port conversion (4 or 5 digits most of the time)*/
303
+static inline int ushort2sbuf(unsigned short u, char* buf, int len)
304
+{
305
+	int offs;
306
+	unsigned char a, b, c, d;
307
+	
308
+	if (unlikely(len<USHORT2SBUF_MAX_LEN))
309
+		return 0;
310
+	offs=0;
311
+	a=u/10000; u%=10000;
312
+	buf[offs]=a+'0'; offs+=(a!=0);
313
+	b=u/1000;  u%=1000;
314
+	buf[offs]=b+'0'; offs+=((offs|b)!=0);
315
+	c=u/100;   u%=100;
316
+	buf[offs]=c+'0'; offs+=((offs|c)!=0);
317
+	d=u/10;    u%=10;
318
+	buf[offs]=d+'0'; offs+=((offs|d)!=0);
319
+	buf[offs]=(unsigned char)u+'0';
320
+	return offs+1;
321
+}
322
+
323
+
324
+
325
+#define USHORT2STR_MAX_LEN  (USHORT2SBUF_MAX_LEN+1) /* 65535\0*/
326
+/* converts an unsigned short (16 bits) to asciiz
327
+ * (optimized for port conversiob (4 or 5 digits most of the time)*/
328
+static inline char* ushort2str(unsigned short u)
329
+{
330
+	static char buf[USHORT2STR_MAX_LEN];
331
+	int len;
332
+
333
+	len=ushort2sbuf(u, buf, sizeof(buf)-1);
334
+	buf[len]=0;
335
+	return buf;
336
+}
337
+
338
+
339
+
297 340
 /* faster memchr version */
298 341
 static inline char* q_memchr(char* p, int c, unsigned int size)
299 342
 {