itoa.h 10 KB

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