copy number to numberf so people can bugfix it/rewrite it
authorMagnus Olsen <magnus@greatlord.com>
Sun, 28 May 2006 05:59:33 +0000 (05:59 +0000)
committerMagnus Olsen <magnus@greatlord.com>
Sun, 28 May 2006 05:59:33 +0000 (05:59 +0000)
svn path=/trunk/; revision=22085

reactos/lib/rtl/sprintf.c
reactos/lib/rtl/swprintf.c

index 4e33439..dd497db 100644 (file)
@@ -180,6 +180,109 @@ number(char * buf, char * end, long long num, int base, int size, int precision,
        return buf;\r
 }\r
 \r
+static char *\r
+numberf(char * buf, char * end, double num, int base, int size, int precision, int type)\r
+{\r
+       char c,sign,tmp[66];\r
+       const char *digits;\r
+       const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";\r
+       const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";\r
+       int i;\r
+       long long x;\r
+\r
+    /* FIXME \r
+       the float version of number is direcly copy of number \r
+    */\r
+    \r
+       digits = (type & LARGE) ? large_digits : small_digits;\r
+       if (type & LEFT)\r
+               type &= ~ZEROPAD;\r
+       if (base < 2 || base > 36)\r
+               return 0;\r
+       c = (type & ZEROPAD) ? '0' : ' ';\r
+       sign = 0;\r
+       if (type & SIGN) {\r
+               if (num < 0) {\r
+                       sign = '-';\r
+                       num = -num;\r
+                       size--;\r
+               } else if (type & PLUS) {\r
+                       sign = '+';\r
+                       size--;\r
+               } else if (type & SPACE) {\r
+                       sign = ' ';\r
+                       size--;\r
+               }\r
+       }\r
+       if (type & SPECIAL) {\r
+               if (base == 16)\r
+                       size -= 2;\r
+               else if (base == 8)\r
+                       size--;\r
+       }\r
+       i = 0;\r
+       if (num == 0)\r
+               tmp[i++] = '0';\r
+       else while (num != 0)\r
+    {\r
+        x = num;\r
+               tmp[i++] = digits[do_div(&x,base)];\r
+               num=x;\r
+    }\r
+       if (i > precision)\r
+               precision = i;\r
+       size -= precision;\r
+       if (!(type&(ZEROPAD+LEFT))) {\r
+               while(size-->0) {\r
+                       if (buf <= end)\r
+                               *buf = ' ';\r
+                       ++buf;\r
+               }\r
+       }\r
+       if (sign) {\r
+               if (buf <= end)\r
+                       *buf = sign;\r
+               ++buf;\r
+       }\r
+       if (type & SPECIAL) {\r
+               if (base==8) {\r
+                       if (buf <= end)\r
+                               *buf = '0';\r
+                       ++buf;\r
+               } else if (base==16) {\r
+                       if (buf <= end)\r
+                               *buf = '0';\r
+                       ++buf;\r
+                       if (buf <= end)\r
+                               *buf = digits[33];\r
+                       ++buf;\r
+               }\r
+       }\r
+       if (!(type & LEFT)) {\r
+               while (size-- > 0) {\r
+                       if (buf <= end)\r
+                               *buf = c;\r
+                       ++buf;\r
+               }\r
+       }\r
+       while (i < precision--) {\r
+               if (buf <= end)\r
+                       *buf = '0';\r
+               ++buf;\r
+       }\r
+       while (i-- > 0) {\r
+               if (buf <= end)\r
+                       *buf = tmp[i];\r
+               ++buf;\r
+       }\r
+       while (size-- > 0) {\r
+               if (buf <= end)\r
+                       *buf = ' ';\r
+               ++buf;\r
+       }\r
+       return buf;\r
+}\r
+\r
 static char*\r
 string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)\r
 {\r
@@ -516,9 +619,8 @@ int _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
             }\r
          } else {\r
             if ( precision == -1 )\r
-               precision = 6;\r
-                /* FIXME the float version of number */\r
-                       str = number(str, end, (int)_double, base, field_width, precision, flags);           \r
+               precision = 6;                \r
+                       str = numberf(str, end, (int)_double, base, field_width, precision, flags);           \r
          }\r
             \r
           continue;\r
index 04fddc8..8ff1b4d 100644 (file)
@@ -180,6 +180,110 @@ number(wchar_t * buf, wchar_t * end, long long num, int base, int size, int prec
        return buf;\r
 }\r
 \r
+static wchar_t *\r
+numberf(wchar_t * buf, wchar_t * end, double num, int base, int size, int precision, int type)\r
+{\r
+       wchar_t c, sign, tmp[66];\r
+       const wchar_t *digits;\r
+       const wchar_t *small_digits = L"0123456789abcdefghijklmnopqrstuvwxyz";\r
+       const wchar_t *large_digits = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";\r
+       int i;\r
+       long long x;\r
+\r
+    /* FIXME \r
+       the float version of number is direcly copy of number \r
+    */\r
+    \r
+    \r
+       digits = (type & LARGE) ? large_digits : small_digits;\r
+       if (type & LEFT)\r
+               type &= ~ZEROPAD;\r
+       if (base < 2 || base > 36)\r
+               return 0;\r
+       c = (type & ZEROPAD) ? L'0' : L' ';\r
+       sign = 0;\r
+       if (type & SIGN) {\r
+               if (num < 0) {\r
+                       sign = L'-';\r
+                       num = -num;\r
+                       size--;\r
+               } else if (type & PLUS) {\r
+                       sign = L'+';\r
+                       size--;\r
+               } else if (type & SPACE) {\r
+                       sign = ' ';\r
+                       size--;\r
+               }\r
+       }\r
+       if (type & SPECIAL) {\r
+               if (base == 16)\r
+                       size -= 2;\r
+               else if (base == 8)\r
+                       size--;\r
+       }\r
+       i = 0;\r
+       if (num == 0)\r
+               tmp[i++] = L'0';\r
+       else while (num != 0)\r
+       {\r
+        x = num;\r
+               tmp[i++] = digits[do_div(&x,base)];\r
+               num = x;\r
+    }\r
+       if (i > precision)\r
+               precision = i;\r
+       size -= precision;\r
+       if (!(type&(ZEROPAD+LEFT))) {\r
+               while(size-->0) {\r
+                       if (buf <= end)\r
+                               *buf = L' ';\r
+                       ++buf;\r
+               }\r
+       }\r
+       if (sign) {\r
+               if (buf <= end)\r
+                       *buf = sign;\r
+               ++buf;\r
+       }\r
+       if (type & SPECIAL) {\r
+               if (base==8) {\r
+                       if (buf <= end)\r
+                               *buf = L'0';\r
+                       ++buf;\r
+               } else if (base==16) {\r
+                       if (buf <= end)\r
+                               *buf = L'0';\r
+                       ++buf;\r
+                       if (buf <= end)\r
+                               *buf = digits[33];\r
+                       ++buf;\r
+               }\r
+       }\r
+       if (!(type & LEFT)) {\r
+               while (size-- > 0) {\r
+                       if (buf <= end)\r
+                               *buf = c;\r
+                       ++buf;\r
+               }\r
+       }\r
+       while (i < precision--) {\r
+               if (buf <= end)\r
+                       *buf = L'0';\r
+               ++buf;\r
+       }\r
+       while (i-- > 0) {\r
+               if (buf <= end)\r
+                       *buf = tmp[i];\r
+               ++buf;\r
+       }\r
+       while (size-- > 0) {\r
+               if (buf <= end)\r
+                       *buf = L' ';\r
+               ++buf;\r
+       }\r
+       return buf;\r
+}\r
+\r
 static wchar_t*\r
 string(wchar_t* buf, wchar_t* end, const char* s, int len, int field_width, int precision, int flags)\r
 {\r
@@ -514,9 +618,8 @@ int _vsnwprintf(wchar_t *buf, size_t cnt, const wchar_t *fmt, va_list args)
             }\r
          } else {\r
             if ( precision == -1 )\r
-               precision = 6;\r
-               /* FIXME the float version of number */\r
-                       str = number(str, end, (int)_double, base, field_width, precision, flags);           \r
+               precision = 6;               \r
+                       str = numberf(str, end, _double, base, field_width, precision, flags);           \r
          }\r
             \r
           continue;\r