itoa.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Tencent is pleased to support the open source community by making RapidJSON
  2. // available.
  3. //
  4. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
  5. // rights reserved.
  6. //
  7. // Licensed under the MIT License (the "License"); you may not use this file
  8. // except in compliance with the License. You may obtain a copy of the License
  9. // at
  10. //
  11. // http://opensource.org/licenses/MIT
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. #ifndef RAPIDJSON_ITOA_
  19. #define RAPIDJSON_ITOA_
  20. #include "../rapidjson.h"
  21. RAPIDJSON_NAMESPACE_BEGIN
  22. namespace internal {
  23. inline const char *GetDigitsLut() {
  24. static const char cDigitsLut[200] = {
  25. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0',
  26. '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
  27. '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', '2', '1', '2',
  28. '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
  29. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3',
  30. '7', '3', '8', '3', '9', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4',
  31. '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '5', '0', '5', '1', '5',
  32. '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  33. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6',
  34. '7', '6', '8', '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4',
  35. '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8',
  36. '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
  37. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9',
  38. '7', '9', '8', '9', '9'};
  39. return cDigitsLut;
  40. }
  41. inline char *u32toa(uint32_t value, char *buffer) {
  42. RAPIDJSON_ASSERT(buffer != 0);
  43. const char *cDigitsLut = GetDigitsLut();
  44. if (value < 10000) {
  45. const uint32_t d1 = (value / 100) << 1;
  46. const uint32_t d2 = (value % 100) << 1;
  47. if (value >= 1000) *buffer++ = cDigitsLut[d1];
  48. if (value >= 100) *buffer++ = cDigitsLut[d1 + 1];
  49. if (value >= 10) *buffer++ = cDigitsLut[d2];
  50. *buffer++ = cDigitsLut[d2 + 1];
  51. } else if (value < 100000000) {
  52. // value = bbbbcccc
  53. const uint32_t b = value / 10000;
  54. const uint32_t c = value % 10000;
  55. const uint32_t d1 = (b / 100) << 1;
  56. const uint32_t d2 = (b % 100) << 1;
  57. const uint32_t d3 = (c / 100) << 1;
  58. const uint32_t d4 = (c % 100) << 1;
  59. if (value >= 10000000) *buffer++ = cDigitsLut[d1];
  60. if (value >= 1000000) *buffer++ = cDigitsLut[d1 + 1];
  61. if (value >= 100000) *buffer++ = cDigitsLut[d2];
  62. *buffer++ = cDigitsLut[d2 + 1];
  63. *buffer++ = cDigitsLut[d3];
  64. *buffer++ = cDigitsLut[d3 + 1];
  65. *buffer++ = cDigitsLut[d4];
  66. *buffer++ = cDigitsLut[d4 + 1];
  67. } else {
  68. // value = aabbbbcccc in decimal
  69. const uint32_t a = value / 100000000; // 1 to 42
  70. value %= 100000000;
  71. if (a >= 10) {
  72. const unsigned i = a << 1;
  73. *buffer++ = cDigitsLut[i];
  74. *buffer++ = cDigitsLut[i + 1];
  75. } else
  76. *buffer++ = static_cast<char>('0' + static_cast<char>(a));
  77. const uint32_t b = value / 10000; // 0 to 9999
  78. const uint32_t c = value % 10000; // 0 to 9999
  79. const uint32_t d1 = (b / 100) << 1;
  80. const uint32_t d2 = (b % 100) << 1;
  81. const uint32_t d3 = (c / 100) << 1;
  82. const uint32_t d4 = (c % 100) << 1;
  83. *buffer++ = cDigitsLut[d1];
  84. *buffer++ = cDigitsLut[d1 + 1];
  85. *buffer++ = cDigitsLut[d2];
  86. *buffer++ = cDigitsLut[d2 + 1];
  87. *buffer++ = cDigitsLut[d3];
  88. *buffer++ = cDigitsLut[d3 + 1];
  89. *buffer++ = cDigitsLut[d4];
  90. *buffer++ = cDigitsLut[d4 + 1];
  91. }
  92. return buffer;
  93. }
  94. inline char *i32toa(int32_t value, char *buffer) {
  95. RAPIDJSON_ASSERT(buffer != 0);
  96. uint32_t u = static_cast<uint32_t>(value);
  97. if (value < 0) {
  98. *buffer++ = '-';
  99. u = ~u + 1;
  100. }
  101. return u32toa(u, buffer);
  102. }
  103. inline char *u64toa(uint64_t value, char *buffer) {
  104. RAPIDJSON_ASSERT(buffer != 0);
  105. const char *cDigitsLut = GetDigitsLut();
  106. const uint64_t kTen8 = 100000000;
  107. const uint64_t kTen9 = kTen8 * 10;
  108. const uint64_t kTen10 = kTen8 * 100;
  109. const uint64_t kTen11 = kTen8 * 1000;
  110. const uint64_t kTen12 = kTen8 * 10000;
  111. const uint64_t kTen13 = kTen8 * 100000;
  112. const uint64_t kTen14 = kTen8 * 1000000;
  113. const uint64_t kTen15 = kTen8 * 10000000;
  114. const uint64_t kTen16 = kTen8 * kTen8;
  115. if (value < kTen8) {
  116. uint32_t v = static_cast<uint32_t>(value);
  117. if (v < 10000) {
  118. const uint32_t d1 = (v / 100) << 1;
  119. const uint32_t d2 = (v % 100) << 1;
  120. if (v >= 1000) *buffer++ = cDigitsLut[d1];
  121. if (v >= 100) *buffer++ = cDigitsLut[d1 + 1];
  122. if (v >= 10) *buffer++ = cDigitsLut[d2];
  123. *buffer++ = cDigitsLut[d2 + 1];
  124. } else {
  125. // value = bbbbcccc
  126. const uint32_t b = v / 10000;
  127. const uint32_t c = v % 10000;
  128. const uint32_t d1 = (b / 100) << 1;
  129. const uint32_t d2 = (b % 100) << 1;
  130. const uint32_t d3 = (c / 100) << 1;
  131. const uint32_t d4 = (c % 100) << 1;
  132. if (value >= 10000000) *buffer++ = cDigitsLut[d1];
  133. if (value >= 1000000) *buffer++ = cDigitsLut[d1 + 1];
  134. if (value >= 100000) *buffer++ = cDigitsLut[d2];
  135. *buffer++ = cDigitsLut[d2 + 1];
  136. *buffer++ = cDigitsLut[d3];
  137. *buffer++ = cDigitsLut[d3 + 1];
  138. *buffer++ = cDigitsLut[d4];
  139. *buffer++ = cDigitsLut[d4 + 1];
  140. }
  141. } else if (value < kTen16) {
  142. const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
  143. const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
  144. const uint32_t b0 = v0 / 10000;
  145. const uint32_t c0 = v0 % 10000;
  146. const uint32_t d1 = (b0 / 100) << 1;
  147. const uint32_t d2 = (b0 % 100) << 1;
  148. const uint32_t d3 = (c0 / 100) << 1;
  149. const uint32_t d4 = (c0 % 100) << 1;
  150. const uint32_t b1 = v1 / 10000;
  151. const uint32_t c1 = v1 % 10000;
  152. const uint32_t d5 = (b1 / 100) << 1;
  153. const uint32_t d6 = (b1 % 100) << 1;
  154. const uint32_t d7 = (c1 / 100) << 1;
  155. const uint32_t d8 = (c1 % 100) << 1;
  156. if (value >= kTen15) *buffer++ = cDigitsLut[d1];
  157. if (value >= kTen14) *buffer++ = cDigitsLut[d1 + 1];
  158. if (value >= kTen13) *buffer++ = cDigitsLut[d2];
  159. if (value >= kTen12) *buffer++ = cDigitsLut[d2 + 1];
  160. if (value >= kTen11) *buffer++ = cDigitsLut[d3];
  161. if (value >= kTen10) *buffer++ = cDigitsLut[d3 + 1];
  162. if (value >= kTen9) *buffer++ = cDigitsLut[d4];
  163. *buffer++ = cDigitsLut[d4 + 1];
  164. *buffer++ = cDigitsLut[d5];
  165. *buffer++ = cDigitsLut[d5 + 1];
  166. *buffer++ = cDigitsLut[d6];
  167. *buffer++ = cDigitsLut[d6 + 1];
  168. *buffer++ = cDigitsLut[d7];
  169. *buffer++ = cDigitsLut[d7 + 1];
  170. *buffer++ = cDigitsLut[d8];
  171. *buffer++ = cDigitsLut[d8 + 1];
  172. } else {
  173. const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
  174. value %= kTen16;
  175. if (a < 10)
  176. *buffer++ = static_cast<char>('0' + static_cast<char>(a));
  177. else if (a < 100) {
  178. const uint32_t i = a << 1;
  179. *buffer++ = cDigitsLut[i];
  180. *buffer++ = cDigitsLut[i + 1];
  181. } else if (a < 1000) {
  182. *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
  183. const uint32_t i = (a % 100) << 1;
  184. *buffer++ = cDigitsLut[i];
  185. *buffer++ = cDigitsLut[i + 1];
  186. } else {
  187. const uint32_t i = (a / 100) << 1;
  188. const uint32_t j = (a % 100) << 1;
  189. *buffer++ = cDigitsLut[i];
  190. *buffer++ = cDigitsLut[i + 1];
  191. *buffer++ = cDigitsLut[j];
  192. *buffer++ = cDigitsLut[j + 1];
  193. }
  194. const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
  195. const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
  196. const uint32_t b0 = v0 / 10000;
  197. const uint32_t c0 = v0 % 10000;
  198. const uint32_t d1 = (b0 / 100) << 1;
  199. const uint32_t d2 = (b0 % 100) << 1;
  200. const uint32_t d3 = (c0 / 100) << 1;
  201. const uint32_t d4 = (c0 % 100) << 1;
  202. const uint32_t b1 = v1 / 10000;
  203. const uint32_t c1 = v1 % 10000;
  204. const uint32_t d5 = (b1 / 100) << 1;
  205. const uint32_t d6 = (b1 % 100) << 1;
  206. const uint32_t d7 = (c1 / 100) << 1;
  207. const uint32_t d8 = (c1 % 100) << 1;
  208. *buffer++ = cDigitsLut[d1];
  209. *buffer++ = cDigitsLut[d1 + 1];
  210. *buffer++ = cDigitsLut[d2];
  211. *buffer++ = cDigitsLut[d2 + 1];
  212. *buffer++ = cDigitsLut[d3];
  213. *buffer++ = cDigitsLut[d3 + 1];
  214. *buffer++ = cDigitsLut[d4];
  215. *buffer++ = cDigitsLut[d4 + 1];
  216. *buffer++ = cDigitsLut[d5];
  217. *buffer++ = cDigitsLut[d5 + 1];
  218. *buffer++ = cDigitsLut[d6];
  219. *buffer++ = cDigitsLut[d6 + 1];
  220. *buffer++ = cDigitsLut[d7];
  221. *buffer++ = cDigitsLut[d7 + 1];
  222. *buffer++ = cDigitsLut[d8];
  223. *buffer++ = cDigitsLut[d8 + 1];
  224. }
  225. return buffer;
  226. }
  227. inline char *i64toa(int64_t value, char *buffer) {
  228. RAPIDJSON_ASSERT(buffer != 0);
  229. uint64_t u = static_cast<uint64_t>(value);
  230. if (value < 0) {
  231. *buffer++ = '-';
  232. u = ~u + 1;
  233. }
  234. return u64toa(u, buffer);
  235. }
  236. } // namespace internal
  237. RAPIDJSON_NAMESPACE_END
  238. #endif // RAPIDJSON_ITOA_