NumberFormatter.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. //
  2. // NumberFormatter.h
  3. //
  4. // Library: Foundation
  5. // Package: Core
  6. // Module: NumberFormatter
  7. //
  8. // Definition of the NumberFormatter class.
  9. //
  10. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_NumberFormatter_INCLUDED
  16. #define Foundation_NumberFormatter_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/NumericString.h"
  19. namespace Poco {
  20. class Foundation_API NumberFormatter
  21. /// The NumberFormatter class provides static methods
  22. /// for formatting numeric values into strings.
  23. ///
  24. /// There are two kind of static member functions:
  25. /// * format* functions return a std::string containing
  26. /// the formatted value.
  27. /// * append* functions append the formatted value to
  28. /// an existing string.
  29. {
  30. public:
  31. enum BoolFormat
  32. {
  33. FMT_TRUE_FALSE,
  34. FMT_YES_NO,
  35. FMT_ON_OFF
  36. };
  37. static const unsigned NF_MAX_INT_STRING_LEN = 32; // increase for 64-bit binary formatting support
  38. static const unsigned NF_MAX_FLT_STRING_LEN = POCO_MAX_FLT_STRING_LEN;
  39. static std::string format(int value);
  40. /// Formats an integer value in decimal notation.
  41. static std::string format(int value, int width);
  42. /// Formats an integer value in decimal notation,
  43. /// right justified in a field having at least
  44. /// the specified width.
  45. static std::string format0(int value, int width);
  46. /// Formats an integer value in decimal notation,
  47. /// right justified and zero-padded in a field
  48. /// having at least the specified width.
  49. static std::string formatHex(int value, bool prefix = false);
  50. /// Formats an int value in hexadecimal notation.
  51. /// If prefix is true, "0x" prefix is prepended to the
  52. /// resulting string.
  53. /// The value is treated as unsigned.
  54. static std::string formatHex(int value, int width, bool prefix = false);
  55. /// Formats a int value in hexadecimal notation,
  56. /// right justified and zero-padded in
  57. /// a field having at least the specified width.
  58. /// If prefix is true, "0x" prefix is prepended to the
  59. /// resulting string.
  60. /// The value is treated as unsigned.
  61. static std::string format(unsigned value);
  62. /// Formats an unsigned int value in decimal notation.
  63. static std::string format(unsigned value, int width);
  64. /// Formats an unsigned long int in decimal notation,
  65. /// right justified in a field having at least the
  66. /// specified width.
  67. static std::string format0(unsigned int value, int width);
  68. /// Formats an unsigned int value in decimal notation,
  69. /// right justified and zero-padded in a field having at
  70. /// least the specified width.
  71. static std::string formatHex(unsigned value, bool prefix = false);
  72. /// Formats an unsigned int value in hexadecimal notation.
  73. /// If prefix is true, "0x" prefix is prepended to the
  74. /// resulting string.
  75. static std::string formatHex(unsigned value, int width, bool prefix = false);
  76. /// Formats a int value in hexadecimal notation,
  77. /// right justified and zero-padded in
  78. /// a field having at least the specified width.
  79. /// If prefix is true, "0x" prefix is prepended to the
  80. /// resulting string.
  81. static std::string format(long value);
  82. /// Formats a long value in decimal notation.
  83. static std::string format(long value, int width);
  84. /// Formats a long value in decimal notation,
  85. /// right justified in a field having at least the
  86. /// specified width.
  87. static std::string format0(long value, int width);
  88. /// Formats a long value in decimal notation,
  89. /// right justified and zero-padded in a field
  90. /// having at least the specified width.
  91. static std::string formatHex(long value, bool prefix = false);
  92. /// Formats an unsigned long value in hexadecimal notation.
  93. /// If prefix is true, "0x" prefix is prepended to the
  94. /// resulting string.
  95. /// The value is treated as unsigned.
  96. static std::string formatHex(long value, int width, bool prefix = false);
  97. /// Formats an unsigned long value in hexadecimal notation,
  98. /// right justified and zero-padded in a field having at least the
  99. /// specified width.
  100. /// If prefix is true, "0x" prefix is prepended to the
  101. /// resulting string.
  102. /// The value is treated as unsigned.
  103. static std::string format(unsigned long value);
  104. /// Formats an unsigned long value in decimal notation.
  105. static std::string format(unsigned long value, int width);
  106. /// Formats an unsigned long value in decimal notation,
  107. /// right justified in a field having at least the specified
  108. /// width.
  109. static std::string format0(unsigned long value, int width);
  110. /// Formats an unsigned long value in decimal notation,
  111. /// right justified and zero-padded
  112. /// in a field having at least the specified width.
  113. static std::string formatHex(unsigned long value, bool prefix = false);
  114. /// Formats an unsigned long value in hexadecimal notation.
  115. /// If prefix is true, "0x" prefix is prepended to the
  116. /// resulting string.
  117. static std::string formatHex(unsigned long value, int width, bool prefix = false);
  118. /// Formats an unsigned long value in hexadecimal notation,
  119. /// right justified and zero-padded in a field having at least the
  120. /// specified width.
  121. /// If prefix is true, "0x" prefix is prepended to the
  122. /// resulting string.
  123. #ifdef POCO_HAVE_INT64
  124. #ifdef POCO_LONG_IS_64_BIT
  125. static std::string format(long long value);
  126. /// Formats a 64-bit integer value in decimal notation.
  127. static std::string format(long long value, int width);
  128. /// Formats a 64-bit integer value in decimal notation,
  129. /// right justified in a field having at least the specified width.
  130. static std::string format0(long long value, int width);
  131. /// Formats a 64-bit integer value in decimal notation,
  132. /// right justified and zero-padded in a field having at least
  133. /// the specified width.
  134. static std::string formatHex(long long value, bool prefix = false);
  135. /// Formats a 64-bit integer value in hexadecimal notation.
  136. /// If prefix is true, "0x" prefix is prepended to the
  137. /// resulting string.
  138. /// The value is treated as unsigned.
  139. static std::string formatHex(long long value, int width, bool prefix = false);
  140. /// Formats a 64-bit integer value in hexadecimal notation,
  141. /// right justified and zero-padded in a field having at least
  142. /// the specified width.
  143. /// The value is treated as unsigned.
  144. /// If prefix is true, "0x" prefix is prepended to the resulting string.
  145. static std::string format(unsigned long long value);
  146. /// Formats an unsigned 64-bit integer value in decimal notation.
  147. static std::string format(unsigned long long value, int width);
  148. /// Formats an unsigned 64-bit integer value in decimal notation,
  149. /// right justified in a field having at least the specified width.
  150. static std::string format0(unsigned long long value, int width);
  151. /// Formats an unsigned 64-bit integer value in decimal notation,
  152. /// right justified and zero-padded in a field having at least the
  153. /// specified width.
  154. static std::string formatHex(unsigned long long value, bool prefix = false);
  155. /// Formats a 64-bit integer value in hexadecimal notation.
  156. /// If prefix is true, "0x" prefix is prepended to the
  157. /// resulting string.
  158. static std::string formatHex(unsigned long long value, int width, bool prefix = false);
  159. /// Formats a 64-bit integer value in hexadecimal notation,
  160. /// right justified and zero-padded in a field having at least
  161. /// the specified width. If prefix is true, "0x" prefix is
  162. /// prepended to the resulting string.
  163. #else // ifndef POCO_LONG_IS_64_BIT
  164. static std::string format(Int64 value);
  165. /// Formats a 64-bit integer value in decimal notation.
  166. static std::string format(Int64 value, int width);
  167. /// Formats a 64-bit integer value in decimal notation,
  168. /// right justified in a field having at least the specified width.
  169. static std::string format0(Int64 value, int width);
  170. /// Formats a 64-bit integer value in decimal notation,
  171. /// right justified and zero-padded in a field having at least
  172. /// the specified width.
  173. static std::string formatHex(Int64 value, bool prefix = false);
  174. /// Formats a 64-bit integer value in hexadecimal notation.
  175. /// If prefix is true, "0x" prefix is prepended to the
  176. /// resulting string.
  177. /// The value is treated as unsigned.
  178. static std::string formatHex(Int64 value, int width, bool prefix = false);
  179. /// Formats a 64-bit integer value in hexadecimal notation,
  180. /// right justified and zero-padded in a field having at least
  181. /// the specified width.
  182. /// The value is treated as unsigned.
  183. /// If prefix is true, "0x" prefix is prepended to the resulting string.
  184. static std::string format(UInt64 value);
  185. /// Formats an unsigned 64-bit integer value in decimal notation.
  186. static std::string format(UInt64 value, int width);
  187. /// Formats an unsigned 64-bit integer value in decimal notation,
  188. /// right justified in a field having at least the specified width.
  189. static std::string format0(UInt64 value, int width);
  190. /// Formats an unsigned 64-bit integer value in decimal notation,
  191. /// right justified and zero-padded in a field having at least the
  192. /// specified width.
  193. static std::string formatHex(UInt64 value, bool prefix = false);
  194. /// Formats a 64-bit integer value in hexadecimal notation.
  195. /// If prefix is true, "0x" prefix is prepended to the
  196. /// resulting string.
  197. static std::string formatHex(UInt64 value, int width, bool prefix = false);
  198. /// Formats a 64-bit integer value in hexadecimal notation,
  199. /// right justified and zero-padded in a field having at least
  200. /// the specified width. If prefix is true, "0x" prefix is
  201. /// prepended to the resulting string.
  202. #endif // ifdef POCO_LONG_IS_64_BIT
  203. #endif // ifdef POCO_HAVE_INT64
  204. static std::string format(float value);
  205. /// Formats a float value in decimal floating-point notation,
  206. /// according to std::printf's %g format with a precision of 8 fractional digits.
  207. static std::string format(float value, int precision);
  208. /// Formats a double value in decimal floating-point notation,
  209. /// according to std::printf's %f format with the given precision.
  210. static std::string format(float value, int width, int precision);
  211. /// Formats a double value in decimal floating-point notation,
  212. /// right justified in a field of the specified width,
  213. /// with the number of fractional digits given in precision.
  214. static std::string format(double value);
  215. /// Formats a double value in decimal floating-point notation,
  216. /// according to std::printf's %g format with a precision of 16 fractional digits.
  217. static std::string format(double value, int precision);
  218. /// Formats a double value in decimal floating-point notation,
  219. /// according to std::printf's %f format with the given precision.
  220. static std::string format(double value, int width, int precision);
  221. /// Formats a double value in decimal floating-point notation,
  222. /// right justified in a field of the specified width,
  223. /// with the number of fractional digits given in precision.
  224. static std::string format(const void* ptr);
  225. /// Formats a pointer in an eight (32-bit architectures) or
  226. /// sixteen (64-bit architectures) characters wide
  227. /// field in hexadecimal notation.
  228. static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE);
  229. /// Formats a bool value in decimal/text notation,
  230. /// according to format parameter.
  231. static void append(std::string& str, int value);
  232. /// Formats an integer value in decimal notation.
  233. static void append(std::string& str, int value, int width);
  234. /// Formats an integer value in decimal notation,
  235. /// right justified in a field having at least
  236. /// the specified width.
  237. static void append0(std::string& str, int value, int width);
  238. /// Formats an integer value in decimal notation,
  239. /// right justified and zero-padded in a field
  240. /// having at least the specified width.
  241. static void appendHex(std::string& str, int value);
  242. /// Formats an int value in hexadecimal notation.
  243. /// The value is treated as unsigned.
  244. static void appendHex(std::string& str, int value, int width);
  245. /// Formats a int value in hexadecimal notation,
  246. /// right justified and zero-padded in
  247. /// a field having at least the specified width.
  248. /// The value is treated as unsigned.
  249. static void append(std::string& str, unsigned value);
  250. /// Formats an unsigned int value in decimal notation.
  251. static void append(std::string& str, unsigned value, int width);
  252. /// Formats an unsigned long int in decimal notation,
  253. /// right justified in a field having at least the
  254. /// specified width.
  255. static void append0(std::string& str, unsigned int value, int width);
  256. /// Formats an unsigned int value in decimal notation,
  257. /// right justified and zero-padded in a field having at
  258. /// least the specified width.
  259. static void appendHex(std::string& str, unsigned value);
  260. /// Formats an unsigned int value in hexadecimal notation.
  261. static void appendHex(std::string& str, unsigned value, int width);
  262. /// Formats a int value in hexadecimal notation,
  263. /// right justified and zero-padded in
  264. /// a field having at least the specified width.
  265. static void append(std::string& str, long value);
  266. /// Formats a long value in decimal notation.
  267. static void append(std::string& str, long value, int width);
  268. /// Formats a long value in decimal notation,
  269. /// right justified in a field having at least the
  270. /// specified width.
  271. static void append0(std::string& str, long value, int width);
  272. /// Formats a long value in decimal notation,
  273. /// right justified and zero-padded in a field
  274. /// having at least the specified width.
  275. static void appendHex(std::string& str, long value);
  276. /// Formats an unsigned long value in hexadecimal notation.
  277. /// The value is treated as unsigned.
  278. static void appendHex(std::string& str, long value, int width);
  279. /// Formats an unsigned long value in hexadecimal notation,
  280. /// right justified and zero-padded in a field having at least the
  281. /// specified width.
  282. /// The value is treated as unsigned.
  283. static void append(std::string& str, unsigned long value);
  284. /// Formats an unsigned long value in decimal notation.
  285. static void append(std::string& str, unsigned long value, int width);
  286. /// Formats an unsigned long value in decimal notation,
  287. /// right justified in a field having at least the specified
  288. /// width.
  289. static void append0(std::string& str, unsigned long value, int width);
  290. /// Formats an unsigned long value in decimal notation,
  291. /// right justified and zero-padded
  292. /// in a field having at least the specified width.
  293. static void appendHex(std::string& str, unsigned long value);
  294. /// Formats an unsigned long value in hexadecimal notation.
  295. static void appendHex(std::string& str, unsigned long value, int width);
  296. /// Formats an unsigned long value in hexadecimal notation,
  297. /// right justified and zero-padded in a field having at least the
  298. /// specified width.
  299. #ifdef POCO_HAVE_INT64
  300. #ifdef POCO_LONG_IS_64_BIT
  301. static void append(std::string& str, long long value);
  302. /// Formats a 64-bit integer value in decimal notation.
  303. static void append(std::string& str, long long value, int width);
  304. /// Formats a 64-bit integer value in decimal notation,
  305. /// right justified in a field having at least the specified width.
  306. static void append0(std::string& str, long long value, int width);
  307. /// Formats a 64-bit integer value in decimal notation,
  308. /// right justified and zero-padded in a field having at least
  309. /// the specified width.
  310. static void appendHex(std::string& str, long long value);
  311. /// Formats a 64-bit integer value in hexadecimal notation.
  312. /// The value is treated as unsigned.
  313. static void appendHex(std::string& str, long long value, int width);
  314. /// Formats a 64-bit integer value in hexadecimal notation,
  315. /// right justified and zero-padded in a field having at least
  316. /// the specified width.
  317. /// The value is treated as unsigned.
  318. static void append(std::string& str, unsigned long long value);
  319. /// Formats an unsigned 64-bit integer value in decimal notation.
  320. static void append(std::string& str, unsigned long long value, int width);
  321. /// Formats an unsigned 64-bit integer value in decimal notation,
  322. /// right justified in a field having at least the specified width.
  323. static void append0(std::string& str, unsigned long long value, int width);
  324. /// Formats an unsigned 64-bit integer value in decimal notation,
  325. /// right justified and zero-padded in a field having at least the
  326. /// specified width.
  327. static void appendHex(std::string& str, unsigned long long value);
  328. /// Formats a 64-bit integer value in hexadecimal notation.
  329. static void appendHex(std::string& str, unsigned long long value, int width);
  330. /// Formats a 64-bit integer value in hexadecimal notation,
  331. /// right justified and zero-padded in a field having at least
  332. /// the specified width.
  333. #else // ifndef POCO_LONG_IS_64_BIT
  334. static void append(std::string& str, Int64 value);
  335. /// Formats a 64-bit integer value in decimal notation.
  336. static void append(std::string& str, Int64 value, int width);
  337. /// Formats a 64-bit integer value in decimal notation,
  338. /// right justified in a field having at least the specified width.
  339. static void append0(std::string& str, Int64 value, int width);
  340. /// Formats a 64-bit integer value in decimal notation,
  341. /// right justified and zero-padded in a field having at least
  342. /// the specified width.
  343. static void appendHex(std::string& str, Int64 value);
  344. /// Formats a 64-bit integer value in hexadecimal notation.
  345. /// The value is treated as unsigned.
  346. static void appendHex(std::string& str, Int64 value, int width);
  347. /// Formats a 64-bit integer value in hexadecimal notation,
  348. /// right justified and zero-padded in a field having at least
  349. /// the specified width.
  350. /// The value is treated as unsigned.
  351. static void append(std::string& str, UInt64 value);
  352. /// Formats an unsigned 64-bit integer value in decimal notation.
  353. static void append(std::string& str, UInt64 value, int width);
  354. /// Formats an unsigned 64-bit integer value in decimal notation,
  355. /// right justified in a field having at least the specified width.
  356. static void append0(std::string& str, UInt64 value, int width);
  357. /// Formats an unsigned 64-bit integer value in decimal notation,
  358. /// right justified and zero-padded in a field having at least the
  359. /// specified width.
  360. static void appendHex(std::string& str, UInt64 value);
  361. /// Formats a 64-bit integer value in hexadecimal notation.
  362. static void appendHex(std::string& str, UInt64 value, int width);
  363. /// Formats a 64-bit integer value in hexadecimal notation,
  364. /// right justified and zero-padded in a field having at least
  365. /// the specified width.
  366. #endif // ifdef POCO_LONG_IS_64_BIT
  367. #endif // ifdef POCO_HAVE_INT64
  368. static void append(std::string& str, float value);
  369. /// Formats a float value in decimal floating-point notation,
  370. /// according to std::printf's %g format with a precision of 8 fractional digits.
  371. static void append(std::string& str, float value, int precision);
  372. /// Formats a double value in decimal floating-point notation,
  373. /// according to std::printf's %f format with the given precision.
  374. static void append(std::string& str, float value, int width, int precision);
  375. /// Formats a double value in decimal floating-point notation,
  376. /// right justified in a field of the specified width,
  377. /// with the number of fractional digits given in precision.
  378. static void append(std::string& str, double value);
  379. /// Formats a double value in decimal floating-point notation,
  380. /// according to std::printf's %g format with a precision of 16 fractional digits.
  381. static void append(std::string& str, double value, int precision);
  382. /// Formats a double value in decimal floating-point notation,
  383. /// according to std::printf's %f format with the given precision.
  384. static void append(std::string& str, double value, int width, int precision);
  385. /// Formats a double value in decimal floating-point notation,
  386. /// right justified in a field of the specified width,
  387. /// with the number of fractional digits given in precision.
  388. static void append(std::string& str, const void* ptr);
  389. /// Formats a pointer in an eight (32-bit architectures) or
  390. /// sixteen (64-bit architectures) characters wide
  391. /// field in hexadecimal notation.
  392. private:
  393. };
  394. //
  395. // inlines
  396. //
  397. inline std::string NumberFormatter::format(int value)
  398. {
  399. std::string result;
  400. intToStr(value, 10, result);
  401. return result;
  402. }
  403. inline std::string NumberFormatter::format(int value, int width)
  404. {
  405. std::string result;
  406. intToStr(value, 10, result, false, width, ' ');
  407. return result;
  408. }
  409. inline std::string NumberFormatter::format0(int value, int width)
  410. {
  411. std::string result;
  412. intToStr(value, 10, result, false, width, '0');
  413. return result;
  414. }
  415. inline std::string NumberFormatter::formatHex(int value, bool prefix)
  416. {
  417. std::string result;
  418. uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix);
  419. return result;
  420. }
  421. inline std::string NumberFormatter::formatHex(int value, int width, bool prefix)
  422. {
  423. std::string result;
  424. uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0');
  425. return result;
  426. }
  427. inline std::string NumberFormatter::format(unsigned value)
  428. {
  429. std::string result;
  430. uIntToStr(value, 10, result);
  431. return result;
  432. }
  433. inline std::string NumberFormatter::format(unsigned value, int width)
  434. {
  435. std::string result;
  436. uIntToStr(value, 10, result, false, width, ' ');
  437. return result;
  438. }
  439. inline std::string NumberFormatter::format0(unsigned int value, int width)
  440. {
  441. std::string result;
  442. uIntToStr(value, 10, result, false, width, '0');
  443. return result;
  444. }
  445. inline std::string NumberFormatter::formatHex(unsigned value, bool prefix)
  446. {
  447. std::string result;
  448. uIntToStr(value, 0x10, result, prefix);
  449. return result;
  450. }
  451. inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix)
  452. {
  453. std::string result;
  454. uIntToStr(value, 0x10, result, prefix, width, '0');
  455. return result;
  456. }
  457. inline std::string NumberFormatter::format(long value)
  458. {
  459. std::string result;
  460. intToStr(value, 10, result);
  461. return result;
  462. }
  463. inline std::string NumberFormatter::format(long value, int width)
  464. {
  465. std::string result;
  466. intToStr(value, 10, result, false, width, ' ');
  467. return result;
  468. }
  469. inline std::string NumberFormatter::format0(long value, int width)
  470. {
  471. std::string result;
  472. intToStr(value, 10, result, false, width, '0');
  473. return result;
  474. }
  475. inline std::string NumberFormatter::formatHex(long value, bool prefix)
  476. {
  477. std::string result;
  478. uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix);
  479. return result;
  480. }
  481. inline std::string NumberFormatter::formatHex(long value, int width, bool prefix)
  482. {
  483. std::string result;
  484. uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0');
  485. return result;
  486. }
  487. inline std::string NumberFormatter::format(unsigned long value)
  488. {
  489. std::string result;
  490. uIntToStr(value, 10, result);
  491. return result;
  492. }
  493. inline std::string NumberFormatter::format(unsigned long value, int width)
  494. {
  495. std::string result;
  496. uIntToStr(value, 10, result, false, width, ' ');
  497. return result;
  498. }
  499. inline std::string NumberFormatter::format0(unsigned long value, int width)
  500. {
  501. std::string result;
  502. uIntToStr(value, 10, result, false, width, '0');
  503. return result;
  504. }
  505. inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
  506. {
  507. std::string result;
  508. uIntToStr(value, 0x10, result, prefix);
  509. return result;
  510. }
  511. inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix)
  512. {
  513. std::string result;
  514. uIntToStr(value, 0x10, result, prefix, width, '0');
  515. return result;
  516. }
  517. #ifdef POCO_HAVE_INT64
  518. #ifdef POCO_LONG_IS_64_BIT
  519. inline std::string NumberFormatter::format(long long value)
  520. {
  521. std::string result;
  522. intToStr(value, 10, result);
  523. return result;
  524. }
  525. inline std::string NumberFormatter::format(long long value, int width)
  526. {
  527. std::string result;
  528. intToStr(value, 10, result, false, width, ' ');
  529. return result;
  530. }
  531. inline std::string NumberFormatter::format0(long long value, int width)
  532. {
  533. std::string result;
  534. intToStr(value, 10, result, false, width, '0');
  535. return result;
  536. }
  537. inline std::string NumberFormatter::formatHex(long long value, bool prefix)
  538. {
  539. std::string result;
  540. uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix);
  541. return result;
  542. }
  543. inline std::string NumberFormatter::formatHex(long long value, int width, bool prefix)
  544. {
  545. std::string result;
  546. uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix, width, '0');
  547. return result;
  548. }
  549. inline std::string NumberFormatter::format(unsigned long long value)
  550. {
  551. std::string result;
  552. uIntToStr(value, 10, result);
  553. return result;
  554. }
  555. inline std::string NumberFormatter::format(unsigned long long value, int width)
  556. {
  557. std::string result;
  558. uIntToStr(value, 10, result, false, width, ' ');
  559. return result;
  560. }
  561. inline std::string NumberFormatter::format0(unsigned long long value, int width)
  562. {
  563. std::string result;
  564. uIntToStr(value, 10, result, false, width, '0');
  565. return result;
  566. }
  567. inline std::string NumberFormatter::formatHex(unsigned long long value, bool prefix)
  568. {
  569. std::string result;
  570. uIntToStr(value, 0x10, result, prefix);
  571. return result;
  572. }
  573. inline std::string NumberFormatter::formatHex(unsigned long long value, int width, bool prefix)
  574. {
  575. std::string result;
  576. uIntToStr(value, 0x10, result, prefix, width, '0');
  577. return result;
  578. }
  579. #else // ifndef POCO_LONG_IS_64_BIT
  580. inline std::string NumberFormatter::format(Int64 value)
  581. {
  582. std::string result;
  583. intToStr(value, 10, result);
  584. return result;
  585. }
  586. inline std::string NumberFormatter::format(Int64 value, int width)
  587. {
  588. std::string result;
  589. intToStr(value, 10, result, false, width, ' ');
  590. return result;
  591. }
  592. inline std::string NumberFormatter::format0(Int64 value, int width)
  593. {
  594. std::string result;
  595. intToStr(value, 10, result, false, width, '0');
  596. return result;
  597. }
  598. inline std::string NumberFormatter::formatHex(Int64 value, bool prefix)
  599. {
  600. std::string result;
  601. uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix);
  602. return result;
  603. }
  604. inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix)
  605. {
  606. std::string result;
  607. uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0');
  608. return result;
  609. }
  610. inline std::string NumberFormatter::format(UInt64 value)
  611. {
  612. std::string result;
  613. uIntToStr(value, 10, result);
  614. return result;
  615. }
  616. inline std::string NumberFormatter::format(UInt64 value, int width)
  617. {
  618. std::string result;
  619. uIntToStr(value, 10, result, false, width, ' ');
  620. return result;
  621. }
  622. inline std::string NumberFormatter::format0(UInt64 value, int width)
  623. {
  624. std::string result;
  625. uIntToStr(value, 10, result, false, width, '0');
  626. return result;
  627. }
  628. inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
  629. {
  630. std::string result;
  631. uIntToStr(value, 0x10, result, prefix);
  632. return result;
  633. }
  634. inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
  635. {
  636. std::string result;
  637. uIntToStr(value, 0x10, result, prefix, width, '0');
  638. return result;
  639. }
  640. #endif // ifdef POCO_LONG_IS_64_BIT
  641. #endif // ifdef POCO_HAVE_INT64
  642. inline std::string NumberFormatter::format(float value)
  643. {
  644. char buffer[POCO_MAX_FLT_STRING_LEN];
  645. floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
  646. return std::string(buffer);
  647. }
  648. inline std::string NumberFormatter::format(float value, int precision)
  649. {
  650. char buffer[POCO_MAX_FLT_STRING_LEN];
  651. floatToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision);
  652. return std::string(buffer);
  653. }
  654. inline std::string NumberFormatter::format(float value, int width, int precision)
  655. {
  656. std::string result;
  657. floatToFixedStr(result, value, precision, width);
  658. return result;
  659. }
  660. inline std::string NumberFormatter::format(double value)
  661. {
  662. char buffer[POCO_MAX_FLT_STRING_LEN];
  663. doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
  664. return std::string(buffer);
  665. }
  666. inline std::string NumberFormatter::format(double value, int precision)
  667. {
  668. char buffer[POCO_MAX_FLT_STRING_LEN];
  669. doubleToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision);
  670. return std::string(buffer);
  671. }
  672. inline std::string NumberFormatter::format(double value, int width, int precision)
  673. {
  674. std::string result;
  675. doubleToFixedStr(result, value, precision, width);
  676. return result;
  677. }
  678. inline std::string NumberFormatter::format(const void* ptr)
  679. {
  680. std::string result;
  681. append(result, ptr);
  682. return result;
  683. }
  684. } // namespace Poco
  685. #endif // Foundation_NumberFormatter_INCLUDED