| 1 | /* Copyright (c) 2000 TXT DataKonsult Ab & Monty Program Ab |
| 2 | Copyright (c) 2009-2011, Monty Program Ab |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are |
| 6 | met: |
| 7 | |
| 8 | 1. Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | 2. Redistributions in binary form must the following disclaimer in |
| 12 | the documentation and/or other materials provided with the |
| 13 | distribution. |
| 14 | |
| 15 | THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY |
| 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR |
| 19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 21 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 22 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | Defines: longlong2str(); |
| 31 | |
| 32 | longlong2str(dst, radix, val) |
| 33 | converts the (longlong) integer "val" to character form and moves it to |
| 34 | the destination string "dst" followed by a terminating NUL. The |
| 35 | result is normally a pointer to this NUL character, but if the radix |
| 36 | is dud the result will be NullS and nothing will be changed. |
| 37 | |
| 38 | If radix is -2..-36, val is taken to be SIGNED. |
| 39 | If radix is 2.. 36, val is taken to be UNSIGNED. |
| 40 | That is, val is signed if and only if radix is. You will normally |
| 41 | use radix -10 only through itoa and ltoa, for radix 2, 8, or 16 |
| 42 | unsigned is what you generally want. |
| 43 | |
| 44 | _dig_vec is public just in case someone has a use for it. |
| 45 | The definitions of itoa and ltoa are actually macros in m_string.h, |
| 46 | but this is where the code is. |
| 47 | |
| 48 | Note: The standard itoa() returns a pointer to the argument, when int2str |
| 49 | returns the pointer to the end-null. |
| 50 | itoa assumes that 10 -base numbers are always signed and other aren't. |
| 51 | */ |
| 52 | |
| 53 | #include "strings_def.h" |
| 54 | |
| 55 | #ifndef ll2str |
| 56 | |
| 57 | /* |
| 58 | This assumes that longlong multiplication is faster than longlong division. |
| 59 | */ |
| 60 | |
| 61 | char *ll2str(longlong val,char *dst,int radix, int upcase) |
| 62 | { |
| 63 | char buffer[65]; |
| 64 | register char *p; |
| 65 | long long_val; |
| 66 | const char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower; |
| 67 | ulonglong uval= (ulonglong) val; |
| 68 | |
| 69 | if (radix < 0) |
| 70 | { |
| 71 | if (radix < -36 || radix > -2) return (char*) 0; |
| 72 | if (val < 0) { |
| 73 | *dst++ = '-'; |
| 74 | /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */ |
| 75 | uval = (ulonglong)0 - uval; |
| 76 | } |
| 77 | radix = -radix; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | if (radix > 36 || radix < 2) return (char*) 0; |
| 82 | } |
| 83 | if (uval == 0) |
| 84 | { |
| 85 | *dst++='0'; |
| 86 | *dst='\0'; |
| 87 | return dst; |
| 88 | } |
| 89 | p = &buffer[sizeof(buffer)-1]; |
| 90 | *p = '\0'; |
| 91 | |
| 92 | while (uval > (ulonglong) LONG_MAX) |
| 93 | { |
| 94 | ulonglong quo= uval/(uint) radix; |
| 95 | uint rem= (uint) (uval- quo* (uint) radix); |
| 96 | *--p= dig_vec[rem]; |
| 97 | uval= quo; |
| 98 | } |
| 99 | long_val= (long) uval; |
| 100 | while (long_val != 0) |
| 101 | { |
| 102 | long quo= long_val/radix; |
| 103 | *--p= dig_vec[(uchar) (long_val - quo*radix)]; |
| 104 | long_val= quo; |
| 105 | } |
| 106 | while ((*dst++ = *p++) != 0) ; |
| 107 | return dst-1; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #ifndef longlong10_to_str |
| 112 | char *longlong10_to_str(longlong val,char *dst,int radix) |
| 113 | { |
| 114 | char buffer[65]; |
| 115 | register char *p; |
| 116 | long long_val; |
| 117 | ulonglong uval= (ulonglong) val; |
| 118 | |
| 119 | if (radix < 0) |
| 120 | { |
| 121 | if (val < 0) |
| 122 | { |
| 123 | *dst++ = '-'; |
| 124 | /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */ |
| 125 | uval = (ulonglong)0 - uval; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (uval == 0) |
| 130 | { |
| 131 | *dst++='0'; |
| 132 | *dst='\0'; |
| 133 | return dst; |
| 134 | } |
| 135 | p = &buffer[sizeof(buffer)-1]; |
| 136 | *p = '\0'; |
| 137 | |
| 138 | while (uval > (ulonglong) LONG_MAX) |
| 139 | { |
| 140 | ulonglong quo= uval/(uint) 10; |
| 141 | uint rem= (uint) (uval- quo* (uint) 10); |
| 142 | *--p = _dig_vec_upper[rem]; |
| 143 | uval= quo; |
| 144 | } |
| 145 | long_val= (long) uval; |
| 146 | while (long_val != 0) |
| 147 | { |
| 148 | long quo= long_val/10; |
| 149 | *--p = _dig_vec_upper[(uchar) (long_val - quo*10)]; |
| 150 | long_val= quo; |
| 151 | } |
| 152 | while ((*dst++ = *p++) != 0) ; |
| 153 | return dst-1; |
| 154 | } |
| 155 | #endif |
| 156 | |