NumberFormatter.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // NumberFormatter.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/NumberFormatter.cpp#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: NumberFormatter
  9. //
  10. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/NumberFormatter.h"
  16. #include "Poco/MemoryStream.h"
  17. #include <iomanip>
  18. #if !defined(POCO_NO_LOCALE)
  19. #include <locale>
  20. #endif
  21. #include <cstdio>
  22. #if defined(_MSC_VER) || defined(__MINGW32__)
  23. #define I64_FMT "I64"
  24. #elif defined(__APPLE__)
  25. #define I64_FMT "q"
  26. #else
  27. #define I64_FMT "ll"
  28. #endif
  29. namespace Poco {
  30. std::string NumberFormatter::format(bool value, BoolFormat format)
  31. {
  32. switch(format)
  33. {
  34. default:
  35. case FMT_TRUE_FALSE:
  36. if (value == true)
  37. return "true";
  38. return "false";
  39. case FMT_YES_NO:
  40. if (value == true)
  41. return "yes";
  42. return "no";
  43. case FMT_ON_OFF:
  44. if (value == true)
  45. return "on";
  46. return "off";
  47. }
  48. }
  49. void NumberFormatter::append(std::string& str, int value)
  50. {
  51. char result[NF_MAX_INT_STRING_LEN];
  52. std::size_t sz = NF_MAX_INT_STRING_LEN;
  53. intToStr(value, 10, result, sz);
  54. str.append(result, sz);
  55. }
  56. void NumberFormatter::append(std::string& str, int value, int width)
  57. {
  58. char result[NF_MAX_INT_STRING_LEN];
  59. std::size_t sz = NF_MAX_INT_STRING_LEN;
  60. intToStr(value, 10, result, sz, false, width);
  61. str.append(result, sz);
  62. }
  63. void NumberFormatter::append0(std::string& str, int value, int width)
  64. {
  65. char result[NF_MAX_INT_STRING_LEN];
  66. std::size_t sz = NF_MAX_INT_STRING_LEN;
  67. intToStr(value, 10, result, sz, false, width, '0');
  68. str.append(result, sz);
  69. }
  70. void NumberFormatter::appendHex(std::string& str, int value)
  71. {
  72. char result[NF_MAX_INT_STRING_LEN];
  73. std::size_t sz = NF_MAX_INT_STRING_LEN;
  74. uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz);
  75. str.append(result, sz);
  76. }
  77. void NumberFormatter::appendHex(std::string& str, int value, int width)
  78. {
  79. char result[NF_MAX_INT_STRING_LEN];
  80. std::size_t sz = NF_MAX_INT_STRING_LEN;
  81. uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz, false, width, '0');
  82. str.append(result, sz);
  83. }
  84. void NumberFormatter::append(std::string& str, unsigned value)
  85. {
  86. char result[NF_MAX_INT_STRING_LEN];
  87. std::size_t sz = NF_MAX_INT_STRING_LEN;
  88. uIntToStr(value, 10, result, sz);
  89. str.append(result, sz);
  90. }
  91. void NumberFormatter::append(std::string& str, unsigned value, int width)
  92. {
  93. char result[NF_MAX_INT_STRING_LEN];
  94. std::size_t sz = NF_MAX_INT_STRING_LEN;
  95. uIntToStr(value, 10, result, sz, false, width);
  96. str.append(result, sz);
  97. }
  98. void NumberFormatter::append0(std::string& str, unsigned int value, int width)
  99. {
  100. char result[NF_MAX_INT_STRING_LEN];
  101. std::size_t sz = NF_MAX_INT_STRING_LEN;
  102. uIntToStr(value, 10, result, sz, false, width, '0');
  103. str.append(result, sz);
  104. }
  105. void NumberFormatter::appendHex(std::string& str, unsigned value)
  106. {
  107. char result[NF_MAX_INT_STRING_LEN];
  108. std::size_t sz = NF_MAX_INT_STRING_LEN;
  109. uIntToStr(value, 0x10, result, sz);
  110. str.append(result, sz);
  111. }
  112. void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
  113. {
  114. char result[NF_MAX_INT_STRING_LEN];
  115. std::size_t sz = NF_MAX_INT_STRING_LEN;
  116. uIntToStr(value, 0x10, result, sz, false, width, '0');
  117. str.append(result, sz);
  118. }
  119. void NumberFormatter::append(std::string& str, long value)
  120. {
  121. char result[NF_MAX_INT_STRING_LEN];
  122. std::size_t sz = NF_MAX_INT_STRING_LEN;
  123. intToStr(value, 10, result, sz);
  124. str.append(result, sz);
  125. }
  126. void NumberFormatter::append(std::string& str, long value, int width)
  127. {
  128. char result[NF_MAX_INT_STRING_LEN];
  129. std::size_t sz = NF_MAX_INT_STRING_LEN;
  130. intToStr(value, 10, result, sz, false, width);
  131. str.append(result, sz);
  132. }
  133. void NumberFormatter::append0(std::string& str, long value, int width)
  134. {
  135. char result[NF_MAX_INT_STRING_LEN];
  136. std::size_t sz = NF_MAX_INT_STRING_LEN;
  137. intToStr(value, 10, result, sz, false, width, '0');
  138. str.append(result, sz);
  139. }
  140. void NumberFormatter::appendHex(std::string& str, long value)
  141. {
  142. char result[NF_MAX_INT_STRING_LEN];
  143. std::size_t sz = NF_MAX_INT_STRING_LEN;
  144. uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz);
  145. str.append(result, sz);
  146. }
  147. void NumberFormatter::appendHex(std::string& str, long value, int width)
  148. {
  149. char result[NF_MAX_INT_STRING_LEN];
  150. std::size_t sz = NF_MAX_INT_STRING_LEN;
  151. uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz, false, width, '0');
  152. str.append(result, sz);
  153. }
  154. void NumberFormatter::append(std::string& str, unsigned long value)
  155. {
  156. char result[NF_MAX_INT_STRING_LEN];
  157. std::size_t sz = NF_MAX_INT_STRING_LEN;
  158. uIntToStr(value, 10, result, sz);
  159. str.append(result, sz);
  160. }
  161. void NumberFormatter::append(std::string& str, unsigned long value, int width)
  162. {
  163. char result[NF_MAX_INT_STRING_LEN];
  164. std::size_t sz = NF_MAX_INT_STRING_LEN;
  165. uIntToStr(value, 10, result, sz, false, width, '0');
  166. str.append(result, sz);
  167. }
  168. void NumberFormatter::append0(std::string& str, unsigned long value, int width)
  169. {
  170. char result[NF_MAX_INT_STRING_LEN];
  171. std::size_t sz = NF_MAX_INT_STRING_LEN;
  172. uIntToStr(value, 10, result, sz, false, width, '0');
  173. str.append(result, sz);
  174. }
  175. void NumberFormatter::appendHex(std::string& str, unsigned long value)
  176. {
  177. char result[NF_MAX_INT_STRING_LEN];
  178. std::size_t sz = NF_MAX_INT_STRING_LEN;
  179. uIntToStr(value, 0x10, result, sz);
  180. str.append(result, sz);
  181. }
  182. void NumberFormatter::appendHex(std::string& str, unsigned long value, int width)
  183. {
  184. char result[NF_MAX_INT_STRING_LEN];
  185. std::size_t sz = NF_MAX_INT_STRING_LEN;
  186. uIntToStr(value, 0x10, result, sz, false, width, '0');
  187. str.append(result, sz);
  188. }
  189. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  190. void NumberFormatter::append(std::string& str, Int64 value)
  191. {
  192. char result[NF_MAX_INT_STRING_LEN];
  193. std::size_t sz = NF_MAX_INT_STRING_LEN;
  194. intToStr(value, 10, result, sz);
  195. str.append(result, sz);
  196. }
  197. void NumberFormatter::append(std::string& str, Int64 value, int width)
  198. {
  199. char result[NF_MAX_INT_STRING_LEN];
  200. std::size_t sz = NF_MAX_INT_STRING_LEN;
  201. intToStr(value, 10, result, sz, false, width, '0');
  202. str.append(result, sz);
  203. }
  204. void NumberFormatter::append0(std::string& str, Int64 value, int width)
  205. {
  206. char result[NF_MAX_INT_STRING_LEN];
  207. std::size_t sz = NF_MAX_INT_STRING_LEN;
  208. intToStr(value, 10, result, sz, false, width, '0');
  209. str.append(result, sz);
  210. }
  211. void NumberFormatter::appendHex(std::string& str, Int64 value)
  212. {
  213. char result[NF_MAX_INT_STRING_LEN];
  214. std::size_t sz = NF_MAX_INT_STRING_LEN;
  215. uIntToStr(static_cast<UInt64>(value), 0x10, result, sz);
  216. str.append(result, sz);
  217. }
  218. void NumberFormatter::appendHex(std::string& str, Int64 value, int width)
  219. {
  220. char result[NF_MAX_INT_STRING_LEN];
  221. std::size_t sz = NF_MAX_INT_STRING_LEN;
  222. uIntToStr(static_cast<UInt64>(value), 0x10, result, sz, false, width, '0');
  223. str.append(result, sz);
  224. }
  225. void NumberFormatter::append(std::string& str, UInt64 value)
  226. {
  227. char result[NF_MAX_INT_STRING_LEN];
  228. std::size_t sz = NF_MAX_INT_STRING_LEN;
  229. uIntToStr(value, 10, result, sz);
  230. str.append(result, sz);
  231. }
  232. void NumberFormatter::append(std::string& str, UInt64 value, int width)
  233. {
  234. char result[NF_MAX_INT_STRING_LEN];
  235. std::size_t sz = NF_MAX_INT_STRING_LEN;
  236. uIntToStr(value, 10, result, sz, false, width, '0');
  237. str.append(result, sz);
  238. }
  239. void NumberFormatter::append0(std::string& str, UInt64 value, int width)
  240. {
  241. char result[NF_MAX_INT_STRING_LEN];
  242. std::size_t sz = NF_MAX_INT_STRING_LEN;
  243. uIntToStr(value, 10, result, sz, false, width, '0');
  244. str.append(result, sz);
  245. }
  246. void NumberFormatter::appendHex(std::string& str, UInt64 value)
  247. {
  248. char result[NF_MAX_INT_STRING_LEN];
  249. std::size_t sz = NF_MAX_INT_STRING_LEN;
  250. uIntToStr(value, 0x10, result, sz);
  251. str.append(result, sz);
  252. }
  253. void NumberFormatter::appendHex(std::string& str, UInt64 value, int width)
  254. {
  255. char result[NF_MAX_INT_STRING_LEN];
  256. std::size_t sz = NF_MAX_INT_STRING_LEN;
  257. uIntToStr(value, 0x10, result, sz, false, width, '0');
  258. str.append(result, sz);
  259. }
  260. #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  261. void NumberFormatter::append(std::string& str, float value)
  262. {
  263. char buffer[NF_MAX_FLT_STRING_LEN];
  264. floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
  265. str.append(buffer);
  266. }
  267. void NumberFormatter::append(std::string& str, double value)
  268. {
  269. char buffer[NF_MAX_FLT_STRING_LEN];
  270. doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
  271. str.append(buffer);
  272. }
  273. void NumberFormatter::append(std::string& str, double value, int precision)
  274. {
  275. std::string result;
  276. str.append(doubleToStr(result, value, precision));
  277. }
  278. void NumberFormatter::append(std::string& str, double value, int width, int precision)
  279. {
  280. std::string result;
  281. str.append(doubleToStr(result, value, precision, width));
  282. }
  283. void NumberFormatter::append(std::string& str, const void* ptr)
  284. {
  285. char buffer[24];
  286. #if defined(POCO_PTR_IS_64_BIT)
  287. #if defined(POCO_LONG_IS_64_BIT)
  288. std::sprintf(buffer, "%016lX", (UIntPtr) ptr);
  289. #else
  290. std::sprintf(buffer, "%016" I64_FMT "X", (UIntPtr) ptr);
  291. #endif
  292. #else
  293. std::sprintf(buffer, "%08lX", (UIntPtr) ptr);
  294. #endif
  295. str.append(buffer);
  296. }
  297. } // namespace Poco