ufmt_cmn.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1998-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File ufmt_cmn.c
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 12/02/98 stephen Creation.
  17. * 03/12/99 stephen Modified for new C API.
  18. * 03/15/99 stephen Added defaultCPToUnicode, unicodeToDefaultCP
  19. * 07/19/99 stephen Fixed bug in defaultCPToUnicode
  20. ******************************************************************************
  21. */
  22. #include "cstring.h"
  23. #include "cmemory.h"
  24. #include "ufmt_cmn.h"
  25. #include "unicode/uchar.h"
  26. #include "unicode/ucnv.h"
  27. #include "ustr_cnv.h"
  28. #if !UCONFIG_NO_CONVERSION
  29. #define DIGIT_0 0x0030
  30. #define DIGIT_9 0x0039
  31. #define LOWERCASE_A 0x0061
  32. #define UPPERCASE_A 0x0041
  33. #define LOWERCASE_Z 0x007A
  34. #define UPPERCASE_Z 0x005A
  35. int
  36. ufmt_digitvalue(char16_t c)
  37. {
  38. if( ((c>=DIGIT_0)&&(c<=DIGIT_9)) ||
  39. ((c>=LOWERCASE_A)&&(c<=LOWERCASE_Z)) ||
  40. ((c>=UPPERCASE_A)&&(c<=UPPERCASE_Z)) )
  41. {
  42. return c - DIGIT_0 - (c >= 0x0041 ? (c >= 0x0061 ? 39 : 7) : 0);
  43. }
  44. else
  45. {
  46. return -1;
  47. }
  48. }
  49. UBool
  50. ufmt_isdigit(char16_t c,
  51. int32_t radix)
  52. {
  53. int digitVal = ufmt_digitvalue(c);
  54. return digitVal < radix && digitVal >= 0;
  55. }
  56. #define TO_UC_DIGIT(a) a <= 9 ? (DIGIT_0 + a) : (0x0037 + a)
  57. #define TO_LC_DIGIT(a) a <= 9 ? (DIGIT_0 + a) : (0x0057 + a)
  58. void
  59. ufmt_64tou(char16_t *buffer,
  60. int32_t *len,
  61. uint64_t value,
  62. uint8_t radix,
  63. UBool uselower,
  64. int32_t minDigits)
  65. {
  66. int32_t length = 0;
  67. uint32_t digit;
  68. char16_t *left, *right, temp;
  69. do {
  70. digit = static_cast<uint32_t>(value % radix);
  71. value = value / radix;
  72. buffer[length++] = static_cast<char16_t>(uselower ? TO_LC_DIGIT(digit)
  73. : TO_UC_DIGIT(digit));
  74. } while(value);
  75. /* pad with zeroes to make it minDigits long */
  76. if(minDigits != -1 && length < minDigits) {
  77. while(length < minDigits && length < *len)
  78. buffer[length++] = DIGIT_0; /*zero padding */
  79. }
  80. /* reverse the buffer */
  81. left = buffer;
  82. right = buffer + length;
  83. while(left < --right) {
  84. temp = *left;
  85. *left++ = *right;
  86. *right = temp;
  87. }
  88. *len = length;
  89. }
  90. void
  91. ufmt_ptou(char16_t *buffer,
  92. int32_t *len,
  93. void *value,
  94. UBool uselower)
  95. {
  96. int32_t i;
  97. int32_t length = 0;
  98. uint8_t* ptrIdx = reinterpret_cast<uint8_t*>(&value);
  99. #if U_IS_BIG_ENDIAN
  100. for (i = 0; i < (int32_t)sizeof(void *); i++)
  101. #else
  102. for (i = static_cast<int32_t>(sizeof(void*)) - 1; i >= 0; i--)
  103. #endif
  104. {
  105. uint8_t byteVal = ptrIdx[i];
  106. uint16_t firstNibble = static_cast<uint16_t>(byteVal >> 4);
  107. uint16_t secondNibble = static_cast<uint16_t>(byteVal & 0xF);
  108. if (uselower) {
  109. buffer[length++]=TO_LC_DIGIT(firstNibble);
  110. buffer[length++]=TO_LC_DIGIT(secondNibble);
  111. }
  112. else {
  113. buffer[length++]=TO_UC_DIGIT(firstNibble);
  114. buffer[length++]=TO_UC_DIGIT(secondNibble);
  115. }
  116. }
  117. *len = length;
  118. }
  119. int64_t
  120. ufmt_uto64(const char16_t *buffer,
  121. int32_t *len,
  122. int8_t radix)
  123. {
  124. const char16_t *limit;
  125. int32_t count;
  126. uint64_t result;
  127. /* initialize parameters */
  128. limit = buffer + *len;
  129. count = 0;
  130. result = 0;
  131. /* iterate through buffer */
  132. while(ufmt_isdigit(*buffer, radix) && buffer < limit) {
  133. /* read the next digit */
  134. result *= radix;
  135. result += ufmt_digitvalue(*buffer++);
  136. /* increment our count */
  137. ++count;
  138. }
  139. *len = count;
  140. return static_cast<int64_t>(result);
  141. }
  142. #define NIBBLE_PER_BYTE 2
  143. void *
  144. ufmt_utop(const char16_t *buffer,
  145. int32_t *len)
  146. {
  147. int32_t count, resultIdx, incVal, offset;
  148. /* This union allows the pointer to be written as an array. */
  149. union {
  150. void *ptr;
  151. uint8_t bytes[sizeof(void*)];
  152. } result;
  153. /* initialize variables */
  154. count = 0;
  155. offset = 0;
  156. result.ptr = nullptr;
  157. /* Skip the leading zeros */
  158. while(buffer[count] == DIGIT_0 || u_isspace(buffer[count])) {
  159. count++;
  160. offset++;
  161. }
  162. /* iterate through buffer, stop when you hit the end */
  163. while(count < *len && ufmt_isdigit(buffer[count], 16)) {
  164. /* increment the count consumed */
  165. ++count;
  166. }
  167. /* detect overflow */
  168. if (count - offset > static_cast<int32_t>(sizeof(void*) * NIBBLE_PER_BYTE)) {
  169. offset = count - static_cast<int32_t>(sizeof(void*) * NIBBLE_PER_BYTE);
  170. }
  171. /* Initialize the direction of the input */
  172. #if U_IS_BIG_ENDIAN
  173. incVal = -1;
  174. resultIdx = (int32_t)(sizeof(void*) - 1);
  175. #else
  176. incVal = 1;
  177. resultIdx = 0;
  178. #endif
  179. /* Write how much was consumed. */
  180. *len = count;
  181. while(--count >= offset) {
  182. /* Get the first nibble of the byte */
  183. uint8_t byte = static_cast<uint8_t>(ufmt_digitvalue(buffer[count]));
  184. if (count > offset) {
  185. /* Get the second nibble of the byte when available */
  186. byte = static_cast<uint8_t>(byte + (ufmt_digitvalue(buffer[--count]) << 4));
  187. }
  188. /* Write the byte into the array */
  189. result.bytes[resultIdx] = byte;
  190. resultIdx += incVal;
  191. }
  192. return result.ptr;
  193. }
  194. char16_t*
  195. ufmt_defaultCPToUnicode(const char *s, int32_t sSize,
  196. char16_t *target, int32_t tSize)
  197. {
  198. char16_t *alias;
  199. UErrorCode status = U_ZERO_ERROR;
  200. UConverter *defConverter = u_getDefaultConverter(&status);
  201. if (U_FAILURE(status) || defConverter == nullptr)
  202. return nullptr;
  203. if(sSize <= 0) {
  204. sSize = static_cast<int32_t>(uprv_strlen(s)) + 1;
  205. }
  206. /* perform the conversion in one swoop */
  207. if (target != nullptr) {
  208. alias = target;
  209. ucnv_toUnicode(defConverter, &alias, alias + tSize, &s, s + sSize - 1,
  210. nullptr, true, &status);
  211. /* add the null terminator */
  212. *alias = 0x0000;
  213. }
  214. u_releaseDefaultConverter(defConverter);
  215. return target;
  216. }
  217. #endif