itoa.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. RAPIDJSON_ASSERT(buffer != 0);
  36. const char* cDigitsLut = GetDigitsLut();
  37. if (value < 10000) {
  38. const uint32_t d1 = (value / 100) << 1;
  39. const uint32_t d2 = (value % 100) << 1;
  40. if (value >= 1000)
  41. *buffer++ = cDigitsLut[d1];
  42. if (value >= 100)
  43. *buffer++ = cDigitsLut[d1 + 1];
  44. if (value >= 10)
  45. *buffer++ = cDigitsLut[d2];
  46. *buffer++ = cDigitsLut[d2 + 1];
  47. }
  48. else if (value < 100000000) {
  49. // value = bbbbcccc
  50. const uint32_t b = value / 10000;
  51. const uint32_t c = value % 10000;
  52. const uint32_t d1 = (b / 100) << 1;
  53. const uint32_t d2 = (b % 100) << 1;
  54. const uint32_t d3 = (c / 100) << 1;
  55. const uint32_t d4 = (c % 100) << 1;
  56. if (value >= 10000000)
  57. *buffer++ = cDigitsLut[d1];
  58. if (value >= 1000000)
  59. *buffer++ = cDigitsLut[d1 + 1];
  60. if (value >= 100000)
  61. *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. }
  68. else {
  69. // value = aabbbbcccc in decimal
  70. const uint32_t a = value / 100000000; // 1 to 42
  71. value %= 100000000;
  72. if (a >= 10) {
  73. const unsigned i = a << 1;
  74. *buffer++ = cDigitsLut[i];
  75. *buffer++ = cDigitsLut[i + 1];
  76. }
  77. else
  78. *buffer++ = static_cast<char>('0' + static_cast<char>(a));
  79. const uint32_t b = value / 10000; // 0 to 9999
  80. const uint32_t c = value % 10000; // 0 to 9999
  81. const uint32_t d1 = (b / 100) << 1;
  82. const uint32_t d2 = (b % 100) << 1;
  83. const uint32_t d3 = (c / 100) << 1;
  84. const uint32_t d4 = (c % 100) << 1;
  85. *buffer++ = cDigitsLut[d1];
  86. *buffer++ = cDigitsLut[d1 + 1];
  87. *buffer++ = cDigitsLut[d2];
  88. *buffer++ = cDigitsLut[d2 + 1];
  89. *buffer++ = cDigitsLut[d3];
  90. *buffer++ = cDigitsLut[d3 + 1];
  91. *buffer++ = cDigitsLut[d4];
  92. *buffer++ = cDigitsLut[d4 + 1];
  93. }
  94. return buffer;
  95. }
  96. inline char* i32toa(int32_t value, char* buffer) {
  97. RAPIDJSON_ASSERT(buffer != 0);
  98. uint32_t u = static_cast<uint32_t>(value);
  99. if (value < 0) {
  100. *buffer++ = '-';
  101. u = ~u + 1;
  102. }
  103. return u32toa(u, buffer);
  104. }
  105. inline char* u64toa(uint64_t value, char* buffer) {
  106. RAPIDJSON_ASSERT(buffer != 0);
  107. const char* cDigitsLut = GetDigitsLut();
  108. const uint64_t kTen8 = 100000000;
  109. const uint64_t kTen9 = kTen8 * 10;
  110. const uint64_t kTen10 = kTen8 * 100;
  111. const uint64_t kTen11 = kTen8 * 1000;
  112. const uint64_t kTen12 = kTen8 * 10000;
  113. const uint64_t kTen13 = kTen8 * 100000;
  114. const uint64_t kTen14 = kTen8 * 1000000;
  115. const uint64_t kTen15 = kTen8 * 10000000;
  116. const uint64_t kTen16 = kTen8 * kTen8;
  117. if (value < kTen8) {
  118. uint32_t v = static_cast<uint32_t>(value);
  119. if (v < 10000) {
  120. const uint32_t d1 = (v / 100) << 1;
  121. const uint32_t d2 = (v % 100) << 1;
  122. if (v >= 1000)
  123. *buffer++ = cDigitsLut[d1];
  124. if (v >= 100)
  125. *buffer++ = cDigitsLut[d1 + 1];
  126. if (v >= 10)
  127. *buffer++ = cDigitsLut[d2];
  128. *buffer++ = cDigitsLut[d2 + 1];
  129. }
  130. else {
  131. // value = bbbbcccc
  132. const uint32_t b = v / 10000;
  133. const uint32_t c = v % 10000;
  134. const uint32_t d1 = (b / 100) << 1;
  135. const uint32_t d2 = (b % 100) << 1;
  136. const uint32_t d3 = (c / 100) << 1;
  137. const uint32_t d4 = (c % 100) << 1;
  138. if (value >= 10000000)
  139. *buffer++ = cDigitsLut[d1];
  140. if (value >= 1000000)
  141. *buffer++ = cDigitsLut[d1 + 1];
  142. if (value >= 100000)
  143. *buffer++ = cDigitsLut[d2];
  144. *buffer++ = cDigitsLut[d2 + 1];
  145. *buffer++ = cDigitsLut[d3];
  146. *buffer++ = cDigitsLut[d3 + 1];
  147. *buffer++ = cDigitsLut[d4];
  148. *buffer++ = cDigitsLut[d4 + 1];
  149. }
  150. }
  151. else if (value < kTen16) {
  152. const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
  153. const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
  154. const uint32_t b0 = v0 / 10000;
  155. const uint32_t c0 = v0 % 10000;
  156. const uint32_t d1 = (b0 / 100) << 1;
  157. const uint32_t d2 = (b0 % 100) << 1;
  158. const uint32_t d3 = (c0 / 100) << 1;
  159. const uint32_t d4 = (c0 % 100) << 1;
  160. const uint32_t b1 = v1 / 10000;
  161. const uint32_t c1 = v1 % 10000;
  162. const uint32_t d5 = (b1 / 100) << 1;
  163. const uint32_t d6 = (b1 % 100) << 1;
  164. const uint32_t d7 = (c1 / 100) << 1;
  165. const uint32_t d8 = (c1 % 100) << 1;
  166. if (value >= kTen15)
  167. *buffer++ = cDigitsLut[d1];
  168. if (value >= kTen14)
  169. *buffer++ = cDigitsLut[d1 + 1];
  170. if (value >= kTen13)
  171. *buffer++ = cDigitsLut[d2];
  172. if (value >= kTen12)
  173. *buffer++ = cDigitsLut[d2 + 1];
  174. if (value >= kTen11)
  175. *buffer++ = cDigitsLut[d3];
  176. if (value >= kTen10)
  177. *buffer++ = cDigitsLut[d3 + 1];
  178. if (value >= kTen9)
  179. *buffer++ = cDigitsLut[d4];
  180. *buffer++ = cDigitsLut[d4 + 1];
  181. *buffer++ = cDigitsLut[d5];
  182. *buffer++ = cDigitsLut[d5 + 1];
  183. *buffer++ = cDigitsLut[d6];
  184. *buffer++ = cDigitsLut[d6 + 1];
  185. *buffer++ = cDigitsLut[d7];
  186. *buffer++ = cDigitsLut[d7 + 1];
  187. *buffer++ = cDigitsLut[d8];
  188. *buffer++ = cDigitsLut[d8 + 1];
  189. }
  190. else {
  191. const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
  192. value %= kTen16;
  193. if (a < 10)
  194. *buffer++ = static_cast<char>('0' + static_cast<char>(a));
  195. else if (a < 100) {
  196. const uint32_t i = a << 1;
  197. *buffer++ = cDigitsLut[i];
  198. *buffer++ = cDigitsLut[i + 1];
  199. }
  200. else if (a < 1000) {
  201. *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
  202. const uint32_t i = (a % 100) << 1;
  203. *buffer++ = cDigitsLut[i];
  204. *buffer++ = cDigitsLut[i + 1];
  205. }
  206. else {
  207. const uint32_t i = (a / 100) << 1;
  208. const uint32_t j = (a % 100) << 1;
  209. *buffer++ = cDigitsLut[i];
  210. *buffer++ = cDigitsLut[i + 1];
  211. *buffer++ = cDigitsLut[j];
  212. *buffer++ = cDigitsLut[j + 1];
  213. }
  214. const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
  215. const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
  216. const uint32_t b0 = v0 / 10000;
  217. const uint32_t c0 = v0 % 10000;
  218. const uint32_t d1 = (b0 / 100) << 1;
  219. const uint32_t d2 = (b0 % 100) << 1;
  220. const uint32_t d3 = (c0 / 100) << 1;
  221. const uint32_t d4 = (c0 % 100) << 1;
  222. const uint32_t b1 = v1 / 10000;
  223. const uint32_t c1 = v1 % 10000;
  224. const uint32_t d5 = (b1 / 100) << 1;
  225. const uint32_t d6 = (b1 % 100) << 1;
  226. const uint32_t d7 = (c1 / 100) << 1;
  227. const uint32_t d8 = (c1 % 100) << 1;
  228. *buffer++ = cDigitsLut[d1];
  229. *buffer++ = cDigitsLut[d1 + 1];
  230. *buffer++ = cDigitsLut[d2];
  231. *buffer++ = cDigitsLut[d2 + 1];
  232. *buffer++ = cDigitsLut[d3];
  233. *buffer++ = cDigitsLut[d3 + 1];
  234. *buffer++ = cDigitsLut[d4];
  235. *buffer++ = cDigitsLut[d4 + 1];
  236. *buffer++ = cDigitsLut[d5];
  237. *buffer++ = cDigitsLut[d5 + 1];
  238. *buffer++ = cDigitsLut[d6];
  239. *buffer++ = cDigitsLut[d6 + 1];
  240. *buffer++ = cDigitsLut[d7];
  241. *buffer++ = cDigitsLut[d7 + 1];
  242. *buffer++ = cDigitsLut[d8];
  243. *buffer++ = cDigitsLut[d8 + 1];
  244. }
  245. return buffer;
  246. }
  247. inline char* i64toa(int64_t value, char* buffer) {
  248. RAPIDJSON_ASSERT(buffer != 0);
  249. uint64_t u = static_cast<uint64_t>(value);
  250. if (value < 0) {
  251. *buffer++ = '-';
  252. u = ~u + 1;
  253. }
  254. return u64toa(u, buffer);
  255. }
  256. } // namespace internal
  257. RAPIDJSON_NAMESPACE_END
  258. #endif // RAPIDJSON_ITOA_