Format.h 8.0 KB

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