Format.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Format.h
  3. //
  4. // Library: Foundation
  5. // Package: Core
  6. // Module: Format
  7. //
  8. // Definition of the format freestanding function.
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Format_INCLUDED
  16. #define Foundation_Format_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Any.h"
  19. #include <vector>
  20. namespace Poco {
  21. std::string Foundation_API format(const std::string& fmt, const Any& value);
  22. /// This function implements sprintf-style formatting in a typesafe way.
  23. /// Various variants of the function are available, supporting a
  24. /// different number of arguments (up to six).
  25. ///
  26. /// The formatting is controlled by the format string in fmt.
  27. /// Format strings are quite similar to those of the std::printf() function, but
  28. /// there are some minor differences.
  29. ///
  30. /// The format string can consist of any sequence of characters; certain
  31. /// characters have a special meaning. Characters without a special meaning
  32. /// are copied verbatim to the result. A percent sign (%) marks the beginning
  33. /// of a format specification. Format specifications have the following syntax:
  34. ///
  35. /// %[<index>][<flags>][<width>][.<precision>][<modifier>]<type>
  36. ///
  37. /// Index, flags, width, precision and prefix are optional. The only required part of
  38. /// the format specification, apart from the percent sign, is the type.
  39. ///
  40. /// The optional index argument has the format "[<n>]" and allows to
  41. /// address an argument by its zero-based position (see the example below).
  42. ///
  43. /// Following are valid type specifications and their meaning:
  44. ///
  45. /// * b boolean (true = 1, false = 0)
  46. /// * c character
  47. /// * d signed decimal integer
  48. /// * i signed decimal integer
  49. /// * o unsigned octal integer
  50. /// * u unsigned decimal integer
  51. /// * x unsigned hexadecimal integer (lower case)
  52. /// * X unsigned hexadecimal integer (upper case)
  53. /// * e signed floating-point value in the form [-]d.dddde[<sign>]dd[d]
  54. /// * E signed floating-point value in the form [-]d.ddddE[<sign>]dd[d]
  55. /// * f signed floating-point value in the form [-]dddd.dddd
  56. /// * s std::string
  57. /// * z std::size_t
  58. ///
  59. /// The following flags are supported:
  60. ///
  61. /// * - left align the result within the given field width
  62. /// * + prefix the output value with a sign (+ or -) if the output value is of a signed type
  63. /// * 0 if width is prefixed with 0, zeros are added until the minimum width is reached
  64. /// * # For o, x, X, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively;
  65. /// for e, E, f, the # flag forces the output value to contain a decimal point in all cases.
  66. ///
  67. /// The following modifiers are supported:
  68. ///
  69. /// * (none) argument is char (c), int (d, i), unsigned (o, u, x, X) double (e, E, f, g, G) or string (s)
  70. /// * l argument is long (d, i), unsigned long (o, u, x, X) or long double (e, E, f, g, G)
  71. /// * L argument is long long (d, i), unsigned long long (o, u, x, X)
  72. /// * h argument is short (d, i), unsigned short (o, u, x, X) or float (e, E, f, g, G)
  73. /// * ? argument is any signed or unsigned int, short, long, or 64-bit integer (d, i, o, x, X)
  74. ///
  75. /// The width argument is a nonnegative decimal integer controlling the minimum number of characters printed.
  76. /// If the number of characters in the output value is less than the specified width, blanks or
  77. /// leading zeros are added, according to the specified flags (-, +, 0).
  78. ///
  79. /// Precision is a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters
  80. /// to be printed, the number of decimal places, or the number of significant digits.
  81. ///
  82. /// Throws an InvalidArgumentException if an argument index is out of range.
  83. ///
  84. /// Starting with release 1.4.3, an argument that does not match the format
  85. /// specifier no longer results in a BadCastException. The string [ERRFMT] is
  86. /// written to the result string instead.
  87. ///
  88. /// If there are more format specifiers than values, the format specifiers without a corresponding value
  89. /// are copied verbatim to output.
  90. ///
  91. /// If there are more values than format specifiers, the superfluous values are ignored.
  92. ///
  93. /// Usage Examples:
  94. /// std::string s1 = format("The answer to life, the universe, and everything is %d", 42);
  95. /// std::string s2 = format("second: %[1]d, first: %[0]d", 1, 2);
  96. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2);
  97. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3);
  98. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4);
  99. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5);
  100. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6);
  101. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7);
  102. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7, const Any& value8);
  103. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7, const Any& value8, const Any& value9);
  104. std::string Foundation_API format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7, const Any& value8, const Any& value9, const Any& value10);
  105. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value);
  106. /// Appends the formatted string to result.
  107. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2);
  108. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3);
  109. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4);
  110. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5);
  111. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6);
  112. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7);
  113. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7, const Any& value8);
  114. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7, const Any& value8, const Any& value9);
  115. void Foundation_API format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6, const Any& value7, const Any& value8, const Any& value9, const Any& value10);
  116. void Foundation_API format(std::string& result, const std::string& fmt, const std::vector<Any>& values);
  117. /// Supports a variable number of arguments and is used by
  118. /// all other variants of format().
  119. } // namespace Poco
  120. #endif // Foundation_Format_INCLUDED