NumberFormatter.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. //
  2. // NumberFormatter.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/NumberFormatter.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: NumberFormatter
  9. //
  10. // Definition of the NumberFormatter class.
  11. //
  12. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_NumberFormatter_INCLUDED
  18. #define Foundation_NumberFormatter_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/NumericString.h"
  21. namespace Poco {
  22. class Foundation_API NumberFormatter
  23. /// The NumberFormatter class provides static methods
  24. /// for formatting numeric values into strings.
  25. ///
  26. /// There are two kind of static member functions:
  27. /// * format* functions return a std::string containing
  28. /// the formatted value.
  29. /// * append* functions append the formatted value to
  30. /// an existing string.
  31. {
  32. public:
  33. enum BoolFormat
  34. {
  35. FMT_TRUE_FALSE,
  36. FMT_YES_NO,
  37. FMT_ON_OFF
  38. };
  39. static const unsigned NF_MAX_INT_STRING_LEN = 32; // increase for 64-bit binary formatting support
  40. static const unsigned NF_MAX_FLT_STRING_LEN = POCO_MAX_FLT_STRING_LEN;
  41. static std::string format(int value);
  42. /// Formats an integer value in decimal notation.
  43. static std::string format(int value, int width);
  44. /// Formats an integer value in decimal notation,
  45. /// right justified in a field having at least
  46. /// the specified width.
  47. static std::string format0(int value, int width);
  48. /// Formats an integer value in decimal notation,
  49. /// right justified and zero-padded in a field
  50. /// having at least the specified width.
  51. static std::string formatHex(int value, bool prefix = false);
  52. /// Formats an int value in hexadecimal notation.
  53. /// If prefix is true, "0x" prefix is prepended to the
  54. /// resulting string.
  55. /// The value is treated as unsigned.
  56. static std::string formatHex(int value, int width, bool prefix = false);
  57. /// Formats a int value in hexadecimal notation,
  58. /// right justified and zero-padded in
  59. /// a field having at least the specified width.
  60. /// If prefix is true, "0x" prefix is prepended to the
  61. /// resulting string.
  62. /// The value is treated as unsigned.
  63. static std::string format(unsigned value);
  64. /// Formats an unsigned int value in decimal notation.
  65. static std::string format(unsigned value, int width);
  66. /// Formats an unsigned long int in decimal notation,
  67. /// right justified in a field having at least the
  68. /// specified width.
  69. static std::string format0(unsigned int value, int width);
  70. /// Formats an unsigned int value in decimal notation,
  71. /// right justified and zero-padded in a field having at
  72. /// least the specified width.
  73. static std::string formatHex(unsigned value, bool prefix = false);
  74. /// Formats an unsigned int value in hexadecimal notation.
  75. /// If prefix is true, "0x" prefix is prepended to the
  76. /// resulting string.
  77. static std::string formatHex(unsigned value, int width, bool prefix = false);
  78. /// Formats a int value in hexadecimal notation,
  79. /// right justified and zero-padded in
  80. /// a field having at least the specified width.
  81. /// If prefix is true, "0x" prefix is prepended to the
  82. /// resulting string.
  83. static std::string format(long value);
  84. /// Formats a long value in decimal notation.
  85. static std::string format(long value, int width);
  86. /// Formats a long value in decimal notation,
  87. /// right justified in a field having at least the
  88. /// specified width.
  89. static std::string format0(long value, int width);
  90. /// Formats a long value in decimal notation,
  91. /// right justified and zero-padded in a field
  92. /// having at least the specified width.
  93. static std::string formatHex(long value, bool prefix = false);
  94. /// Formats an unsigned long value in hexadecimal notation.
  95. /// If prefix is true, "0x" prefix is prepended to the
  96. /// resulting string.
  97. /// The value is treated as unsigned.
  98. static std::string formatHex(long value, int width, bool prefix = false);
  99. /// Formats an unsigned long value in hexadecimal notation,
  100. /// right justified and zero-padded in a field having at least the
  101. /// specified width.
  102. /// If prefix is true, "0x" prefix is prepended to the
  103. /// resulting string.
  104. /// The value is treated as unsigned.
  105. static std::string format(unsigned long value);
  106. /// Formats an unsigned long value in decimal notation.
  107. static std::string format(unsigned long value, int width);
  108. /// Formats an unsigned long value in decimal notation,
  109. /// right justified in a field having at least the specified
  110. /// width.
  111. static std::string format0(unsigned long value, int width);
  112. /// Formats an unsigned long value in decimal notation,
  113. /// right justified and zero-padded
  114. /// in a field having at least the specified width.
  115. static std::string formatHex(unsigned long value, bool prefix = false);
  116. /// Formats an unsigned long value in hexadecimal notation.
  117. /// If prefix is true, "0x" prefix is prepended to the
  118. /// resulting string.
  119. static std::string formatHex(unsigned long value, int width, bool prefix = false);
  120. /// Formats an unsigned long value in hexadecimal notation,
  121. /// right justified and zero-padded in a field having at least the
  122. /// specified width.
  123. /// If prefix is true, "0x" prefix is prepended to the
  124. /// resulting string.
  125. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  126. static std::string format(Int64 value);
  127. /// Formats a 64-bit integer value in decimal notation.
  128. static std::string format(Int64 value, int width);
  129. /// Formats a 64-bit integer value in decimal notation,
  130. /// right justified in a field having at least the specified width.
  131. static std::string format0(Int64 value, int width);
  132. /// Formats a 64-bit integer value in decimal notation,
  133. /// right justified and zero-padded in a field having at least
  134. /// the specified width.
  135. static std::string formatHex(Int64 value, bool prefix = false);
  136. /// Formats a 64-bit integer value in hexadecimal notation.
  137. /// If prefix is true, "0x" prefix is prepended to the
  138. /// resulting string.
  139. /// The value is treated as unsigned.
  140. static std::string formatHex(Int64 value, int width, bool prefix = false);
  141. /// Formats a 64-bit integer value in hexadecimal notation,
  142. /// right justified and zero-padded in a field having at least
  143. /// the specified width.
  144. /// The value is treated as unsigned.
  145. /// If prefix is true, "0x" prefix is prepended to the resulting string.
  146. static std::string format(UInt64 value);
  147. /// Formats an unsigned 64-bit integer value in decimal notation.
  148. static std::string format(UInt64 value, int width);
  149. /// Formats an unsigned 64-bit integer value in decimal notation,
  150. /// right justified in a field having at least the specified width.
  151. static std::string format0(UInt64 value, int width);
  152. /// Formats an unsigned 64-bit integer value in decimal notation,
  153. /// right justified and zero-padded in a field having at least the
  154. /// specified width.
  155. static std::string formatHex(UInt64 value, bool prefix = false);
  156. /// Formats a 64-bit integer value in hexadecimal notation.
  157. /// If prefix is true, "0x" prefix is prepended to the
  158. /// resulting string.
  159. static std::string formatHex(UInt64 value, int width, bool prefix = false);
  160. /// Formats a 64-bit integer value in hexadecimal notation,
  161. /// right justified and zero-padded in a field having at least
  162. /// the specified width. If prefix is true, "0x" prefix is
  163. /// prepended to the resulting string.
  164. #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  165. static std::string format(float value);
  166. /// Formats a float value in decimal floating-point notation,
  167. /// according to std::printf's %g format with a precision of 8 fractional digits.
  168. static std::string format(double value);
  169. /// Formats a double value in decimal floating-point notation,
  170. /// according to std::printf's %g format with a precision of 16 fractional digits.
  171. static std::string format(double value, int precision);
  172. /// Formats a double value in decimal floating-point notation,
  173. /// according to std::printf's %f format with the given precision.
  174. static std::string format(double value, int width, int precision);
  175. /// Formats a double value in decimal floating-point notation,
  176. /// right justified in a field of the specified width,
  177. /// with the number of fractional digits given in precision.
  178. static std::string format(const void* ptr);
  179. /// Formats a pointer in an eight (32-bit architectures) or
  180. /// sixteen (64-bit architectures) characters wide
  181. /// field in hexadecimal notation.
  182. static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE);
  183. /// Formats a bool value in decimal/text notation,
  184. /// according to format parameter.
  185. static void append(std::string& str, int value);
  186. /// Formats an integer value in decimal notation.
  187. static void append(std::string& str, int value, int width);
  188. /// Formats an integer value in decimal notation,
  189. /// right justified in a field having at least
  190. /// the specified width.
  191. static void append0(std::string& str, int value, int width);
  192. /// Formats an integer value in decimal notation,
  193. /// right justified and zero-padded in a field
  194. /// having at least the specified width.
  195. static void appendHex(std::string& str, int value);
  196. /// Formats an int value in hexadecimal notation.
  197. /// The value is treated as unsigned.
  198. static void appendHex(std::string& str, int value, int width);
  199. /// Formats a int value in hexadecimal notation,
  200. /// right justified and zero-padded in
  201. /// a field having at least the specified width.
  202. /// The value is treated as unsigned.
  203. static void append(std::string& str, unsigned value);
  204. /// Formats an unsigned int value in decimal notation.
  205. static void append(std::string& str, unsigned value, int width);
  206. /// Formats an unsigned long int in decimal notation,
  207. /// right justified in a field having at least the
  208. /// specified width.
  209. static void append0(std::string& str, unsigned int value, int width);
  210. /// Formats an unsigned int value in decimal notation,
  211. /// right justified and zero-padded in a field having at
  212. /// least the specified width.
  213. static void appendHex(std::string& str, unsigned value);
  214. /// Formats an unsigned int value in hexadecimal notation.
  215. static void appendHex(std::string& str, unsigned value, int width);
  216. /// Formats a int value in hexadecimal notation,
  217. /// right justified and zero-padded in
  218. /// a field having at least the specified width.
  219. static void append(std::string& str, long value);
  220. /// Formats a long value in decimal notation.
  221. static void append(std::string& str, long value, int width);
  222. /// Formats a long value in decimal notation,
  223. /// right justified in a field having at least the
  224. /// specified width.
  225. static void append0(std::string& str, long value, int width);
  226. /// Formats a long value in decimal notation,
  227. /// right justified and zero-padded in a field
  228. /// having at least the specified width.
  229. static void appendHex(std::string& str, long value);
  230. /// Formats an unsigned long value in hexadecimal notation.
  231. /// The value is treated as unsigned.
  232. static void appendHex(std::string& str, long value, int width);
  233. /// Formats an unsigned long value in hexadecimal notation,
  234. /// right justified and zero-padded in a field having at least the
  235. /// specified width.
  236. /// The value is treated as unsigned.
  237. static void append(std::string& str, unsigned long value);
  238. /// Formats an unsigned long value in decimal notation.
  239. static void append(std::string& str, unsigned long value, int width);
  240. /// Formats an unsigned long value in decimal notation,
  241. /// right justified in a field having at least the specified
  242. /// width.
  243. static void append0(std::string& str, unsigned long value, int width);
  244. /// Formats an unsigned long value in decimal notation,
  245. /// right justified and zero-padded
  246. /// in a field having at least the specified width.
  247. static void appendHex(std::string& str, unsigned long value);
  248. /// Formats an unsigned long value in hexadecimal notation.
  249. static void appendHex(std::string& str, unsigned long value, int width);
  250. /// Formats an unsigned long value in hexadecimal notation,
  251. /// right justified and zero-padded in a field having at least the
  252. /// specified width.
  253. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  254. static void append(std::string& str, Int64 value);
  255. /// Formats a 64-bit integer value in decimal notation.
  256. static void append(std::string& str, Int64 value, int width);
  257. /// Formats a 64-bit integer value in decimal notation,
  258. /// right justified in a field having at least the specified width.
  259. static void append0(std::string& str, Int64 value, int width);
  260. /// Formats a 64-bit integer value in decimal notation,
  261. /// right justified and zero-padded in a field having at least
  262. /// the specified width.
  263. static void appendHex(std::string& str, Int64 value);
  264. /// Formats a 64-bit integer value in hexadecimal notation.
  265. /// The value is treated as unsigned.
  266. static void appendHex(std::string& str, Int64 value, int width);
  267. /// Formats a 64-bit integer value in hexadecimal notation,
  268. /// right justified and zero-padded in a field having at least
  269. /// the specified width.
  270. /// The value is treated as unsigned.
  271. static void append(std::string& str, UInt64 value);
  272. /// Formats an unsigned 64-bit integer value in decimal notation.
  273. static void append(std::string& str, UInt64 value, int width);
  274. /// Formats an unsigned 64-bit integer value in decimal notation,
  275. /// right justified in a field having at least the specified width.
  276. static void append0(std::string& str, UInt64 value, int width);
  277. /// Formats an unsigned 64-bit integer value in decimal notation,
  278. /// right justified and zero-padded in a field having at least the
  279. /// specified width.
  280. static void appendHex(std::string& str, UInt64 value);
  281. /// Formats a 64-bit integer value in hexadecimal notation.
  282. static void appendHex(std::string& str, UInt64 value, int width);
  283. /// Formats a 64-bit integer value in hexadecimal notation,
  284. /// right justified and zero-padded in a field having at least
  285. /// the specified width.
  286. #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  287. static void append(std::string& str, float value);
  288. /// Formats a float value in decimal floating-point notation,
  289. /// according to std::printf's %g format with a precision of 8 fractional digits.
  290. static void append(std::string& str, double value);
  291. /// Formats a double value in decimal floating-point notation,
  292. /// according to std::printf's %g format with a precision of 16 fractional digits.
  293. static void append(std::string& str, double value, int precision);
  294. /// Formats a double value in decimal floating-point notation,
  295. /// according to std::printf's %f format with the given precision.
  296. static void append(std::string& str, double value, int width, int precision);
  297. /// Formats a double value in decimal floating-point notation,
  298. /// right justified in a field of the specified width,
  299. /// with the number of fractional digits given in precision.
  300. static void append(std::string& str, const void* ptr);
  301. /// Formats a pointer in an eight (32-bit architectures) or
  302. /// sixteen (64-bit architectures) characters wide
  303. /// field in hexadecimal notation.
  304. private:
  305. };
  306. //
  307. // inlines
  308. //
  309. inline std::string NumberFormatter::format(int value)
  310. {
  311. std::string result;
  312. intToStr(value, 10, result);
  313. return result;
  314. }
  315. inline std::string NumberFormatter::format(int value, int width)
  316. {
  317. std::string result;
  318. intToStr(value, 10, result, false, width, ' ');
  319. return result;
  320. }
  321. inline std::string NumberFormatter::format0(int value, int width)
  322. {
  323. std::string result;
  324. intToStr(value, 10, result, false, width, '0');
  325. return result;
  326. }
  327. inline std::string NumberFormatter::formatHex(int value, bool prefix)
  328. {
  329. std::string result;
  330. uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix);
  331. return result;
  332. }
  333. inline std::string NumberFormatter::formatHex(int value, int width, bool prefix)
  334. {
  335. std::string result;
  336. uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0');
  337. return result;
  338. }
  339. inline std::string NumberFormatter::format(unsigned value)
  340. {
  341. std::string result;
  342. uIntToStr(value, 10, result);
  343. return result;
  344. }
  345. inline std::string NumberFormatter::format(unsigned value, int width)
  346. {
  347. std::string result;
  348. uIntToStr(value, 10, result, false, width, ' ');
  349. return result;
  350. }
  351. inline std::string NumberFormatter::format0(unsigned int value, int width)
  352. {
  353. std::string result;
  354. uIntToStr(value, 10, result, false, width, '0');
  355. return result;
  356. }
  357. inline std::string NumberFormatter::formatHex(unsigned value, bool prefix)
  358. {
  359. std::string result;
  360. uIntToStr(value, 0x10, result, prefix);
  361. return result;
  362. }
  363. inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix)
  364. {
  365. std::string result;
  366. uIntToStr(value, 0x10, result, prefix, width, '0');
  367. return result;
  368. }
  369. inline std::string NumberFormatter::format(long value)
  370. {
  371. std::string result;
  372. intToStr(value, 10, result);
  373. return result;
  374. }
  375. inline std::string NumberFormatter::format(long value, int width)
  376. {
  377. std::string result;
  378. intToStr(value, 10, result, false, width, ' ');
  379. return result;
  380. }
  381. inline std::string NumberFormatter::format0(long value, int width)
  382. {
  383. std::string result;
  384. intToStr(value, 10, result, false, width, '0');
  385. return result;
  386. }
  387. inline std::string NumberFormatter::formatHex(long value, bool prefix)
  388. {
  389. std::string result;
  390. uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix);
  391. return result;
  392. }
  393. inline std::string NumberFormatter::formatHex(long value, int width, bool prefix)
  394. {
  395. std::string result;
  396. uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0');
  397. return result;
  398. }
  399. inline std::string NumberFormatter::format(unsigned long value)
  400. {
  401. std::string result;
  402. uIntToStr(value, 10, result);
  403. return result;
  404. }
  405. inline std::string NumberFormatter::format(unsigned long value, int width)
  406. {
  407. std::string result;
  408. uIntToStr(value, 10, result, false, width, ' ');
  409. return result;
  410. }
  411. inline std::string NumberFormatter::format0(unsigned long value, int width)
  412. {
  413. std::string result;
  414. uIntToStr(value, 10, result, false, width, '0');
  415. return result;
  416. }
  417. inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
  418. {
  419. std::string result;
  420. uIntToStr(value, 0x10, result, prefix);
  421. return result;
  422. }
  423. inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix)
  424. {
  425. std::string result;
  426. uIntToStr(value, 0x10, result, prefix, width, '0');
  427. return result;
  428. }
  429. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  430. inline std::string NumberFormatter::format(Int64 value)
  431. {
  432. std::string result;
  433. intToStr(value, 10, result);
  434. return result;
  435. }
  436. inline std::string NumberFormatter::format(Int64 value, int width)
  437. {
  438. std::string result;
  439. intToStr(value, 10, result, false, width, ' ');
  440. return result;
  441. }
  442. inline std::string NumberFormatter::format0(Int64 value, int width)
  443. {
  444. std::string result;
  445. intToStr(value, 10, result, false, width, '0');
  446. return result;
  447. }
  448. inline std::string NumberFormatter::formatHex(Int64 value, bool prefix)
  449. {
  450. std::string result;
  451. uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix);
  452. return result;
  453. }
  454. inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix)
  455. {
  456. std::string result;
  457. uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0');
  458. return result;
  459. }
  460. inline std::string NumberFormatter::format(UInt64 value)
  461. {
  462. std::string result;
  463. uIntToStr(value, 10, result);
  464. return result;
  465. }
  466. inline std::string NumberFormatter::format(UInt64 value, int width)
  467. {
  468. std::string result;
  469. uIntToStr(value, 10, result, false, width, ' ');
  470. return result;
  471. }
  472. inline std::string NumberFormatter::format0(UInt64 value, int width)
  473. {
  474. std::string result;
  475. uIntToStr(value, 10, result, false, width, '0');
  476. return result;
  477. }
  478. inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
  479. {
  480. std::string result;
  481. uIntToStr(value, 0x10, result, prefix);
  482. return result;
  483. }
  484. inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
  485. {
  486. std::string result;
  487. uIntToStr(value, 0x10, result, prefix, width, '0');
  488. return result;
  489. }
  490. #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  491. inline std::string NumberFormatter::format(float value)
  492. {
  493. std::string result;
  494. floatToStr(result, value);
  495. return result;
  496. }
  497. inline std::string NumberFormatter::format(double value)
  498. {
  499. std::string result;
  500. doubleToStr(result, value);
  501. return result;
  502. }
  503. inline std::string NumberFormatter::format(double value, int precision)
  504. {
  505. std::string result;
  506. doubleToStr(result, value, precision);
  507. return result;
  508. }
  509. inline std::string NumberFormatter::format(double value, int width, int precision)
  510. {
  511. std::string result;
  512. doubleToStr(result, value, precision, width);
  513. return result;
  514. }
  515. inline std::string NumberFormatter::format(const void* ptr)
  516. {
  517. std::string result;
  518. append(result, ptr);
  519. return result;
  520. }
  521. } // namespace Poco
  522. #endif // Foundation_NumberFormatter_INCLUDED