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