You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.7 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (c) 2000, 2001, 2004, 2006, 2007 MySQL AB
  2. Use is subject to license terms.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. #include <my_global.h>
  15. #include "m_string.h"
  16. /*
  17. _dig_vec arrays are public because they are used in several outer places.
  18. */
  19. char NEAR _dig_vec_upper[] =
  20. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  21. char NEAR _dig_vec_lower[] =
  22. "0123456789abcdefghijklmnopqrstuvwxyz";
  23. /*
  24. Convert integer to its string representation in given scale of notation.
  25. SYNOPSIS
  26. int2str()
  27. val - value to convert
  28. dst - points to buffer where string representation should be stored
  29. radix - radix of scale of notation
  30. upcase - set to 1 if we should use upper-case digits
  31. DESCRIPTION
  32. Converts the (long) integer value to its character form and moves it to
  33. the destination buffer followed by a terminating NUL.
  34. If radix is -2..-36, val is taken to be SIGNED, if radix is 2..36, val is
  35. taken to be UNSIGNED. That is, val is signed if and only if radix is.
  36. All other radixes treated as bad and nothing will be changed in this case.
  37. For conversion to decimal representation (radix is -10 or 10) one can use
  38. optimized int10_to_str() function.
  39. RETURN VALUE
  40. Pointer to ending NUL character or NullS if radix is bad.
  41. */
  42. char *
  43. int2str(register long int val, register char *dst, register int radix,
  44. int upcase)
  45. {
  46. char buffer[65];
  47. register char *p;
  48. long int new_val;
  49. char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
  50. ulong uval= (ulong) val;
  51. if (radix < 0)
  52. {
  53. if (radix < -36 || radix > -2)
  54. return NullS;
  55. if (val < 0)
  56. {
  57. *dst++ = '-';
  58. /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
  59. uval = (ulong)0 - uval;
  60. }
  61. radix = -radix;
  62. }
  63. else if (radix > 36 || radix < 2)
  64. return NullS;
  65. /*
  66. The slightly contorted code which follows is due to the fact that
  67. few machines directly support unsigned long / and %. Certainly
  68. the VAX C compiler generates a subroutine call. In the interests
  69. of efficiency (hollow laugh) I let this happen for the first digit
  70. only; after that "val" will be in range so that signed integer
  71. division will do. Sorry 'bout that. CHECK THE CODE PRODUCED BY
  72. YOUR C COMPILER. The first % and / should be unsigned, the second
  73. % and / signed, but C compilers tend to be extraordinarily
  74. sensitive to minor details of style. This works on a VAX, that's
  75. all I claim for it.
  76. */
  77. p = &buffer[sizeof(buffer)-1];
  78. *p = '\0';
  79. new_val= uval / (ulong) radix;
  80. *--p = dig_vec[(uchar) (uval- (ulong) new_val*(ulong) radix)];
  81. val = new_val;
  82. #ifdef HAVE_LDIV
  83. while (val != 0)
  84. {
  85. ldiv_t res;
  86. res=ldiv(val,radix);
  87. *--p = dig_vec[res.rem];
  88. val= res.quot;
  89. }
  90. #else
  91. while (val != 0)
  92. {
  93. new_val=val/radix;
  94. *--p = dig_vec[(uchar) (val-new_val*radix)];
  95. val= new_val;
  96. }
  97. #endif
  98. while ((*dst++ = *p++) != 0) ;
  99. return dst-1;
  100. }
  101. /*
  102. Converts integer to its string representation in decimal notation.
  103. SYNOPSIS
  104. int10_to_str()
  105. val - value to convert
  106. dst - points to buffer where string representation should be stored
  107. radix - flag that shows whenever val should be taken as signed or not
  108. DESCRIPTION
  109. This is version of int2str() function which is optimized for normal case
  110. of radix 10/-10. It takes only sign of radix parameter into account and
  111. not its absolute value.
  112. RETURN VALUE
  113. Pointer to ending NUL character.
  114. */
  115. char *int10_to_str(long int val,char *dst,int radix)
  116. {
  117. char buffer[65];
  118. register char *p;
  119. long int new_val;
  120. unsigned long int uval = (unsigned long int) val;
  121. if (radix < 0) /* -10 */
  122. {
  123. if (val < 0)
  124. {
  125. *dst++ = '-';
  126. /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
  127. uval = (unsigned long int)0 - uval;
  128. }
  129. }
  130. p = &buffer[sizeof(buffer)-1];
  131. *p = '\0';
  132. new_val= (long) (uval / 10);
  133. *--p = '0'+ (char) (uval - (unsigned long) new_val * 10);
  134. val = new_val;
  135. while (val != 0)
  136. {
  137. new_val=val/10;
  138. *--p = '0' + (char) (val-new_val*10);
  139. val= new_val;
  140. }
  141. while ((*dst++ = *p++) != 0) ;
  142. return dst-1;
  143. }