HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Format.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Format.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_Format__
12 #define __UT_Format__
13 
14 #include "UT_API.h"
15 #include "UT_Assert.h"
16 #include "UT_NonCopyable.h"
17 #include <SYS/SYS_Types.h>
18 
19 #include <initializer_list>
20 #include <iosfwd>
21 #include <limits>
22 #include <string>
23 #include <type_traits>
24 
25 template <typename T> class UT_Array;
26 class UT_String;
27 class UT_StringHolder;
28 class UT_StringLit;
29 class UT_StringRef;
30 class UT_StringView;
31 
32 /// @file UT_Format.h
33 /// @brief Type-safe formatting, modeled on the Python @c str.format function.
34 ///
35 /// This formatter supports a sub-set of the Python format syntax. Notably,
36 /// only indexed arguments are supported for now, not named ones.
37 ///
38 /// Like with the @c printf() formatting, the format string contains
39 /// replacement fields that will be substituted by the textual representation
40 /// of the arguments to the various format functions. The replacement fields
41 /// for this formatter are surrounded by curly braces `{}`, rather than
42 /// prefixed with a `%`.
43 ///
44 /// Note the string `{{` or `}}` will result in a single `{` or `}` being
45 /// output.
46 ///
47 /// @section format_grammar Format Grammar
48 /// The grammar for the replacement field is as follows:
49 ///
50 /// {[<index>][:<spec>]}
51 ///
52 /// The `<index>` is an optional integer value, which refers to the position of
53 /// the argument after the format string. If the `<index>` is omitted for all
54 /// replacement fields, then they are automatically substituted with
55 /// `0, 1, 2, ...`
56 /// It is not permitted to mix indexed and non-indexed.
57 ///
58 /// If a colon separator is found, the string after that is interpreted as a
59 /// formatting specification. The grammar for the `<spec>` field is as follows:
60 ///
61 /// [[<fill>]<align>][<sign>][#][,][0][<width>][.<precision>][<type>]
62 ///
63 /// Where:
64 /// - `<fill>` is the fill character to use when the width of the field is
65 /// greater than the length of the argument value being printed.
66 /// - `<align>` is a single character code which sets the alignment of the
67 /// value within the field:
68 /// - `<`: The field is left-aligned. This is the default for non-numeric
69 /// values.
70 /// - `>`: The field is right-aligned. This is the default for numeric values.
71 /// - `^`: The field is center-aligned.
72 /// - `=`: The field is number aligned. This is the same as right-aligned,
73 /// except the fill value is inserted between the sign indicator and
74 /// the numeric value. This alignment is only available for numeric
75 /// values.
76 /// - `<sign>` is a single character that specifies how the sign is printed for
77 /// numeric values:
78 /// - `+`: Always print a sign, whether positive or negative.
79 /// - `-`: Only print a sign when the number is negative.
80 /// - `<space>`: Use a leading space on positive numbers, minus sign for
81 /// negative.
82 /// - The '#' option is only available for binary, octal or hexadecimal output.
83 /// It indicates that they should be prefixed with '0b', '0' and '0x',
84 /// respectively.
85 /// - The ',' option will add digit grouping to numerical outputs. For float
86 //// numbers, this only applies to the integer portion of the float.
87 /// - `<width>` specifies the minimum width of the field. Arguments wider than
88 /// this value will still be printed out in full. Preceding the width field
89 /// with `0` will automatically set the alignment to number alignment, and
90 /// the fill value to `0`.
91 /// - `<precision>` sets the precision for floating point numbers. For other
92 /// types it specifies the maximum width of the field.
93 /// - `<type>` is a single character code that specifies how the type should be
94 /// interpreted. If an argument type is not valid for the formatting type,
95 /// the formatting type is ignored. The supported types are:
96 /// - `b`: Output integers as binary numbers.
97 /// - `B`: Output integers as binary numbers with an upper-case prefix (if
98 /// using the `#` option.
99 /// - `c`: Output integers interpreted as a Unicode code point.
100 /// - `d`: Output integers as decimal numbers. This is the default for
101 /// integers.
102 /// - `e`: Use the exponent notation for floating point numbers.
103 /// - `E`: Same as `e`, except the exponent separator is upper-cased.
104 /// - `f`: Use the fixed point notation for floating point numbers.
105 /// - `F`: Same as `f`.
106 /// - `g`: Format a floating point number as either fixed point or scientific,
107 /// depending on its magnitude, which mostly results in the shortest
108 /// form being used. This is the default for floats.
109 /// - `G`: Same as `g`, except the exponent separator is upper-cased.
110 /// - `o`: Output integers as octals.
111 /// - `x`: Output integers and floats as hexadecimal. For floats this uses
112 /// the special hexadecimal float output.
113 /// - `X`: The same as `x`, except all alphabetic digits and prefix are
114 /// output in upper-case.
115 /// - `%`: Takes a floating point number, multiplies it by 100 and outputs it
116 /// with a percentage sign appended, using fixed point notation. The
117 /// default precision in this mode is 2.
118 ///
119 /// Example:
120 /// @code{.cpp}
121 /// UTformat("Number: {}\n", 1);
122 /// UTformat("String: {}\n", "foobar");
123 /// UTformat("Field : {:=^20}\n", 1);
124 /// UTformat("Index : {3} {0} {2} {1}\n", 'A', 'B', 'C', 'D');
125 /// UTformat("Perc : {:.1%}\n", 0.1234);
126 /// @endcode
127 /// Result:
128 /// @code{.txt}
129 /// Number: 1
130 /// String: foobar
131 /// Field : =========1==========
132 /// Index : D A C B
133 /// Perc : 12.3%
134 /// @endcode
135 /// @section printf_grammar Printf Grammar
136 /// The UTprintf and UT_Format::printf functions use formatting code very
137 /// similar to the POSIX specification for printf.
138 /// The code begins with a single @c % character, followed by zero or more
139 /// modifiers, ending with the type specifier. Where this implementation of the
140 /// @c printf formatting code differs, is that there is no need to add length
141 /// modifiers. Any length modifers will simply be ignored.
142 
143 /// @section custom_format Custom Formatting
144 /// Custom types are supported through custom formatting functions. To define
145 /// a custom formatting function for a specific type, the following function
146 /// specialization needs to be implemented:
147 /// @code{.cpp}
148 /// size_t format(char *buffer, size_t buffer_size, const CustomType &val);
149 /// @endcode
150 /// Where:
151 /// - @c buffer is a byte buffer to write the result into. If @c buffer is a
152 /// @c nullptr, then the function should return the number of bytes required
153 /// to store the final result.
154 /// - @c buffer_size is the size of the byte buffer passed in via @c buffer.
155 /// This should be ignored if @c buffer is @c nullptr.
156 /// - @val is value to custom format.
157 /// The function should return the number of bytes written out, or, if @c buffer
158 /// is @c nullptr, the amount of bytes needed to write out the entire custom
159 /// type representation.
160 
161 namespace UT { namespace Format {
162 
163 /// @name Writer functors for formatted strings
164 /// @{
165 
166 /// Base class. This is the only api that Formatter uses. This serves as a
167 /// template when writing a new @ref Formatter writer for cases that the
168 /// @ref FileWriter, @ref StreamWriter, and @ref Writer don't cover.
170 {
171  /// Write the range of bytes from @c begin to @c end, non-inclusive (i.e.
172  /// don't write out the byte pointed to by @c end). Return the number of
173  /// bytes written.
174  virtual size_t operator()(const char *begin, const char *end) = 0;
175 
176  /// pointer+length api
177  size_t operator()(const char *begin, size_t length) { return (*this)(begin, begin+length); }
178 
179  CustomWriter() = default;
180  virtual ~CustomWriter() = default;
181 
183 };
184 
185 
186 /// A writer class that writes to a raw character buffer, truncating if it fills the
187 /// buffer. Returns the number of bytes that it *tried* to write, to get actual
188 /// number of bytes written min this with the buffer_size.
189 struct UT_API Writer : public CustomWriter
190 {
191  Writer(char *buffer, size_t buffer_size) :
192  myBuffer(buffer),
193  myBufferSize(buffer ? buffer_size : 0) { }
194 
195  void setBuffer(char *buffer, size_t buffer_size)
196  {
197  myBuffer = buffer;
198  myBufferSize = buffer_size;
199  }
200 
201  size_t operator()(const char *begin, const char *end) override;
202 
203  size_t operator()(const char *begin, size_t length) { return (*this)(begin, begin+length); }
204 
205 protected:
206  char *myBuffer;
207  size_t myBufferSize;
208 };
209 
210 
211 /// @}
212 
213 // A template to check whether a variation of format(...) exists for the
214 // given type.
215 namespace Impl
216 {
217  template<typename> struct TrueType : std::true_type {};
218  // SFINAE will pick one of the two HasFormat() variants below, depending on
219  // if it can resolve the declaration type of the format() declaration given
220  // below. We wrap the result through the TrueType class above (which
221  // accepts any type) in order to get a true value for it in the actual
222  // HasFormat implementation below.
223  template<typename T>
224  static auto TestHasFormat(int) ->
226  size_t(), std::declval<T>()))>;
227  template<typename T>
228  static auto TestHasFormat(long) -> std::false_type;
229 }
230 
231 template<typename T>
232 struct HasFormat
233 {
234  static constexpr bool value = decltype(Impl::TestHasFormat<T>(0))::value;
235 };
236 
237 /// A container class that holds the individual argument values to be formatted
238 /// with the format string.
240 {
241 
242 public:
243  enum class Type
244  {
245  None,
246  Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64,
247  Float16, Float32, Float64,
248  ZeroString, SizedString, CharString,
249  Pointer, Custom
250  };
251 
252  ArgValue(char v) : myType(Type::CharString), myChar(v) {}
253  ArgValue(int8 v) : myType(Type::Int8), myI8(v) {}
254  ArgValue(uint8 v) : myType(Type::UInt8), myU8(v) {}
255  ArgValue(int16 v) : myType(Type::Int16), myI16(v) {}
256  ArgValue(uint16 v) : myType(Type::UInt16), myU16(v) {}
257  ArgValue(int32 v) : myType(Type::Int32), myI32(v) {}
258  ArgValue(uint32 v) : myType(Type::UInt32), myU32(v) {}
259  ArgValue(int64 v) : myType(Type::Int64), myI64(v) {}
260  ArgValue(uint64 v) : myType(Type::UInt64), myU64(v) {}
261  ArgValue(fpreal16 v) : myType(Type::Float16), myF16(v) {}
262  ArgValue(fpreal32 v) : myType(Type::Float32), myF32(v) {}
263  ArgValue(fpreal64 v) : myType(Type::Float64), myF64(v) {}
264 
265 #if (defined(LINUX) && defined(AMD64)) || (defined(LINUX) && defined(ARM64))
266  // Special overload to deal with long longs, which both gcc and clang
267  // treat as a distinct type from long, on 64-bit Linux, even if they're the
268  // same size.
269  static_assert(sizeof(long long) == 8, "long long should be 8 bytes.");
270  ArgValue(long long v) : myType(Type::Int64), myI64(int64(v)) {}
271  ArgValue(unsigned long long v) : myType(Type::UInt64), myU64(uint64(v)) {}
272 #endif
273 #if defined(MBSD) && defined(__clang__)
274  // Special overload for size_t with clang on OSX.
275  ArgValue(size_t myValuePtr) : myType(Type::UInt64), myU64(uint64(myValuePtr)) {}
276 #endif
277 #if defined(MBSD)
278  // Special overload for long with OSX.
279  ArgValue(long v) : myType(Type::Int64), myI64(int64(v)) {}
280  // Note: unsigned long covered by size_t case above
281 #elif defined(_WIN32)
282  // Special overload for long with Windows.
283  ArgValue(long v) : myType(Type::Int32), myI32(int32(v)) {}
284  ArgValue(unsigned long v) : myType(Type::UInt32), myU32(uint32(v)) {}
285 #endif
286  ArgValue(bool v) : myType(Type::SizedString)
287  {
288  if (v)
289  {
290  mySizedStr.myStr = "true";
291  mySizedStr.mySize = 4;
292  }
293  else
294  {
295  mySizedStr.myStr = "false";
296  mySizedStr.mySize = 5;
297  }
298  }
299 
300  ArgValue(char *v) : myType(Type::ZeroString), myStr(v) {}
301  ArgValue(const char *v) : myType(Type::ZeroString), myStr(v) {}
302  ArgValue(const UT_String &v);
303  ArgValue(const UT_StringLit &v);
304  ArgValue(const UT_StringRef &v);
305  ArgValue(const UT_StringHolder &v);
306  ArgValue(const UT_StringView &v);
308  myType(Type::SizedString), mySizedStr(v.c_str(), v.length()) {}
309 
310  // void pointer overrides because std::is_pointer<void*>::value is false!
311  ArgValue(void *v) :
312  myType(Type::Pointer), myPtr(v)
313  {
314  }
315  ArgValue(const void *v) :
316  myType(Type::Pointer), myPtr(v)
317  {
318  }
319 
320 #define UT_ENABLE_IF(T) typename std::enable_if<T>::type * = nullptr
321 #define UT_HAS_FMT(T) HasFormat<T>::value
322 #define UT_REMOVE_PTR(T) typename std::remove_pointer<T>::type
323 #define UT_IS_PTR(T) std::is_pointer<T>::value
324 #define UT_IS_ENUM(T) std::is_enum<T>::value
325 
326  // Accept any pointer type that doesn't have a custom formatter.
327  // We use std::enable_if magic to allow SFINAE to elide any constructor
328  // that doesn't properly evaluate (a template argument value of false to
329  // std::enable_if results in std::enable_if::type not being defined).
330  template<typename T>
333  myType(Type::Pointer), myPtr(v)
334  {
335  }
336 
337  // For pointer types that have custom formatters. In this case we print
338  // out the pointer value and the de-referenced formatted value pointed to,
339  // or (null), if the value is \c nullptr.
340  template<typename T>
341  ArgValue(T v,
343  myType(Type::Custom)
344  {
345  myCustom.myFormatFunc = customPtrWrapper<UT_REMOVE_PTR(T)>;
346  myCustom.myValuePtr = static_cast<const void *>(v);
347  }
348 
349  // For fixed length arrays
350  template<typename T, unsigned N>
351  ArgValue(T (&v)[N]) :
352  myType(Type::Custom)
353  {
354  myCustom.myFormatFunc = customArrayWrapper<UT_REMOVE_PTR(T),N>;
355  myCustom.myValuePtr = static_cast<const void *>(&v[0]);
356  }
357 
358  template <typename T>
359  ArgValue(T &v,
360  UT_ENABLE_IF( UT_IS_ENUM(T) && !UT_HAS_FMT(T) ))
361  {
362  *this = ArgValue(static_cast<typename std::underlying_type<T>::type>(v));
363  }
364 
365  template <typename T>
366  ArgValue(T &v,
367  UT_ENABLE_IF( UT_IS_ENUM(T) && UT_HAS_FMT(T) )) :
368  myType(Type::Custom)
369  {
370  myCustom.myFormatFunc = customRefWrapper<T>;
371  myCustom.myValuePtr = &v;
372  }
373 
374  // For reference types that have custom formatters.
375  template<typename T>
377  myType(Type::Custom)
378  {
379  static_assert(UT_HAS_FMT(T), "No formatting function for custom type.");
380  myCustom.myFormatFunc = customRefWrapper<T>;
381  myCustom.myValuePtr = &t;
382  }
383 
384  size_t formatCustom(char *buffer, size_t buffer_size) const
385  {
386  UT_ASSERT(isCustom());
387  if (!isCustom())
388  return 0;
389 
390  return myCustom.myFormatFunc(buffer, buffer_size, myCustom.myValuePtr);
391  }
392 
393 #undef UT_ENABLE_IF
394 #undef UT_HAS_FMT
395 #undef UT_REMOVE_PTR
396 #undef UT_IS_PTR
397 
398  // In the presence of a union with non-POD types, we have to provide our
399  // own destructor, even if there are no destructors to call.
400  ~ArgValue() = default;
401  ArgValue(const ArgValue &) = default;
402  ArgValue &operator=(const ArgValue &) = default;
403 
404  Type type() const { return myType; }
405 
406  bool isEmpty() const { return myType == Type::None; }
407 
408  bool isInteger() const
409  {
410  return myType == Type::Int8 || myType == Type::UInt8 ||
411  myType == Type::Int16 || myType == Type::UInt16 ||
412  myType == Type::Int32 || myType == Type::UInt32 ||
413  myType == Type::Int64 || myType == Type::UInt64;
414  }
415 
416  bool isFloat() const
417  {
418  return myType == Type::Float16 ||
419  myType == Type::Float32 ||
420  myType == Type::Float64;
421  }
422 
423  bool isSigned() const
424  {
425  return myType == Type::Int8 ||
426  myType == Type::Int16 ||
427  myType == Type::Int32 ||
428  myType == Type::Int64 ||
429  isFloat();
430  }
431 
432  bool isNumeric() const
433  {
434  return isInteger() || isFloat();
435  }
436 
437  bool isString() const
438  {
439  return myType == Type::CharString ||
440  myType == Type::ZeroString ||
441  myType == Type::SizedString;
442  }
443 
444  bool isPointer() const
445  {
446  return myType == Type::Pointer;
447  }
448 
449  bool isCustom() const
450  {
451  return myType == Type::Custom;
452  }
453 
454 protected:
455  friend class Formatter;
456 
457  ArgValue() : myType(Type::None), myU64(0) {}
458  ArgValue(Type type) : myType(type), myI64(0) {}
459 
460  struct SizedString
461  {
462  SizedString(const char *str, size_t size) : myStr(str), mySize(size) {}
463  const char *myStr;
464  size_t mySize;
465  };
466 
467  using CustomFormat = size_t(*)(char *buffer, size_t buffer_size, const void *value);
468 
469  struct Custom
470  {
471  const void *myValuePtr;
473  };
474 
475  union
476  {
488  char myChar;
489  const char *myStr;
491  const void *myPtr;
493  };
494 
495 private:
496  Type myType;
497 
498  // Wraper function to invoke the writer for the custom type.
499  template <typename T>
500  static inline size_t
501  customRefWrapper(char *buffer, size_t buffer_size, const void *value)
502  {
503  return format(buffer, buffer_size, *static_cast<const T*>(value));
504  }
505 
506  template <typename T>
507  static inline size_t
508  customPtrWrapper(char *buffer, size_t buffer_size, const void *value);
509 
510  template <typename T, unsigned N>
511  static inline size_t
512  customArrayWrapper(char *buffer, size_t buffer_size, const void *value);
513 };
514 
515 /// The main formatting class. This accepts a set of arguments and can
516 /// subsequently format them in various ways.
518 {
519 public:
520  using W = CustomWriter;
521 
522  /// Constructs a new @ref Formatter object.
524  ~Formatter() = default;
525 
527 
528  /// Sets whether to report format string errors or not.
529  void setReportErrors(bool flag) { myReportErrors = flag; }
530 
531  /// Sets whether to force numbers to print with digit grouping. Otherwise
532  /// only the ',' (format), or '\'' (printf) flags dictate whether digit
533  /// grouping is done.
534  void setForceDigitGroupings(bool flag) { myDigitGroupings = flag; }
535 
536  /// Format the arguments into the writer, using the format string given.
537  /// Returns the number of bytes written.
538  /// @param writer A functor that does the writing of the formatted output.
539  /// See @ref CustomWriter for an example of how to do a custom writer.
540  /// @param format The formatting codes, as defined in @ref format_grammar.
541  /// If nullptr; will be treated as space separated {} for each argument.
542  /// @param args An initializer list that forms the list of arguments to
543  /// bake into the formatted result using the format string.
544  /// @return The number of bytes written out to the @p writer functor.
545  size_t format(W &writer, const char *format,
546  std::initializer_list<ArgValue> args);
547 
548  /// Same as regular format() except with array instead of initializer list
549  size_t format(W &writer, const char *format,
550  const UT_Array<ArgValue> &args);
551 
552  /// Format the arguments into the writer, using the format string given.
553  /// Returns the number of bytes written.
554  /// @param writer A functor that does the writing of the formatted output.
555  /// See @ref CustomWriter for an example of how to do a custom writer.
556  /// @param format The formatting codes, as defined by
557  /// [std::printf](http://www.cplusplus.com/reference/cstdio/printf/)
558  /// If nullptr; will be treated as space separated {} for each argument.
559  /// @param args An initializer list that forms the list of arguments to
560  /// bake into the formatted result using the format string.
561  /// @return The number of bytes written out to the @p writer functor.
562  size_t printf(W &writer, const char * format,
563  std::initializer_list<ArgValue> args);
564 
565 private:
566  struct FormatSpec
567  {
568  FormatSpec() {}
569  size_t field_width = 0;
570  size_t str_width = std::numeric_limits<size_t>::max();
571  int64 precision = -1;
572  enum { Left, Center, Right, Number } align = Left;
573  enum { Minus, Plus, Space } sign = Minus;
574  bool alt_form = false;
575  bool digit_grouping = false;
576  char fill = ' ';
577  char type = 0;
578  };
579 
580  bool parseFormatSpec(const char *&ptr, const ArgValue &arg,
581  const ArgValue args[], size_t nb_args,
582  FormatSpec &spec);
583  bool parsePrintfSpec(const char *&ptr, const ArgValue &arg,
584  const ArgValue args[], size_t nb_args,
585  FormatSpec &spec);
586 
587  template <typename ListT>
588  size_t formatImpl(W &writer, const char *format, ListT args);
589 
590  size_t formatArg(W &writer, const FormatSpec &spec, const ArgValue &arg);
591  size_t formatInteger(W &writer, const FormatSpec &spec, const ArgValue &arg);
592  size_t formatFloat(W &writer, const FormatSpec &spec, const ArgValue &arg);
593  size_t formatString(W &writer, const FormatSpec &spec, const ArgValue &arg);
594  size_t formatPointer(W &writer, const FormatSpec &spec, const ArgValue &arg);
595  size_t formatCustom(W &writer, const FormatSpec &spec, const ArgValue &arg);
596  size_t formatCodePoint(W &writer, const FormatSpec &spec, const ArgValue &arg);
597  size_t formatPercentage(W &writer, const FormatSpec &spec, const ArgValue &arg);
598 
599  size_t formatAdjustAndFill(W &writer, const char *str, size_t str_size,
600  const FormatSpec &spec);
601 
602  // Get the argument pointed at by the current location of the format string.
603  // If the first character is a digit, assume the argument is being referred
604  // to by index, otherwise assume we're using the next argument.
605  const ArgValue &getFormatArg(const char *&ptr, const ArgValue args[],
606  size_t nb_args);
607  const ArgValue &getPrintfArg(const char *&ptr, const ArgValue args[],
608  size_t nb_args);
609  static int64 getIntValueFromArg(const ArgValue &arg);
610  static fpreal64 getFloatValueFromArg(const ArgValue &arg);
611 
612  int64 myNextArgIndex = 0;
613  bool myReportErrors = true;
614  bool myDigitGroupings = false;
615  char myLast = '\0';
616 };
617 
618 template <typename T, unsigned N>
619 inline size_t
620 ArgValue::customArrayWrapper(
621  char *buffer, size_t buffer_size, const void *value)
622 {
623  Writer writer(buffer, buffer_size);
624  Formatter f;
625  const T* ptr = static_cast<const T*>(value);
626  size_t nb_needed = writer("[", 1);
627  for (unsigned i = 0; i < N; ++i)
628  nb_needed += f.format(writer, i ? ", {}" : "{}", {ptr[i]});
629  nb_needed += writer("]", 1);
630  return nb_needed;
631 }
632 
633 UT_API size_t format(FILE *file, const char *format, std::initializer_list<ArgValue> args);
634 UT_API size_t format(std::ostream&, const char *format, std::initializer_list<ArgValue> args);
635 UT_API size_t format(char*, size_t, const char *format, std::initializer_list<ArgValue> args);
636 UT_API size_t printf(FILE *file, const char *format, std::initializer_list<ArgValue> args);
637 UT_API size_t printf(std::ostream&, const char *format, std::initializer_list<ArgValue> args);
638 UT_API size_t printf(char*, size_t, const char *format, std::initializer_list<ArgValue> args);
639 
640 template <typename T>
641 inline size_t
642 ArgValue::customPtrWrapper(char *buffer, size_t buffer_size, const void *value)
643 {
644  if (value)
645  return format(buffer, buffer_size, "<{} {}>",
646  {value, *static_cast<const T*>(value)});
647  else
648  return format(buffer, buffer_size, "<{}>", {value});
649 }
650 
651 } // namespace Format
652 } // namespace UT
653 
654 /// @name Standalone formatting functions
655 /// @{
656 
657 /// Takes a formatting string and a set of arguments and writes out the
658 /// formatted string to the FILE pointer.
659 /// @param file The @c FILE pointer to write to. The FILE must be opened in
660 /// write or append mode.
661 /// @param format The formatting codes, as defined in @ref format_grammar.
662 /// @param args The arguments to the formatting.
663 /// @return The number of bytes successfully written out.
664 template<typename... Args>
665 size_t UTformat(FILE *file, const char *format, const Args& ...args)
666 {
667  return UT::Format::format(file, format, {args...});
668 }
669 
670 /// Takes a formatting string and a set of arguments and writes out the
671 /// formatted string to @c stdout.
672 /// @param format The formatting codes, as defined in @ref format_grammar.
673 /// @param args The arguments to the formatting.
674 /// @return The number of bytes successfully written out.
675 template<typename... Args>
676 size_t UTformat(const char *format, const Args& ...args)
677 {
678  return UTformat(stdout, format, args...);
679 }
680 
681 /// Takes a formatting string and a set of arguments and writes out the
682 /// formatted string to the output stream object.
683 /// @param os The output stream to write to.
684 /// @param format The formatting codes, as defined in @ref format_grammar.
685 /// @param args The arguments to the formatting.
686 /// @return The number of bytes successfully written out.
687 template<typename... Args>
688 size_t UTformat(std::ostream &os, const char *format, const Args& ...args)
689 {
690  return UT::Format::format(os, format, {args...});
691 }
692 
693 /// Takes a formatting string and a set of arguments and writes out the
694 /// formatted string into the buffer provided. If @c buffer is @c nullptr,
695 /// then nothing is written out, but the number of bytes needed to store
696 /// the entire formatted string is returned. A termination byte is not written
697 /// out to the buffer. If termination is required, the caller must take care
698 /// of it.
699 /// @param buffer The character buffer to write the formatted string to.
700 /// @param buffer_size The size of the character buffer.
701 /// @param format The formatting codes, as defined in @ref format_grammar.
702 /// @param args The arguments to the formatting.
703 /// @return The number of bytes successfully written out.
704 template<typename... Args>
705 size_t UTformat(char *buffer, size_t buffer_size, const char *format,
706  const Args& ...args)
707 {
708  return UT::Format::format(buffer, buffer_size, format, {args...});
709 }
710 
711 /// @}
712 
713 /// @name Standalone printf-like functions
714 /// @{
715 
716 /// Takes a formatting string and a set of arguments and writes out the
717 /// formatted string to the FILE pointer.
718 /// @param file The @c FILE pointer to write to. The FILE must be opened in
719 /// write or append mode.
720 /// @param format The formatting codes, as defined in @ref printf_grammar.
721 /// @param args The arguments to the formatting.
722 /// @return The number of bytes successfully written out.
723 template<typename... Args>
724 size_t UTprintf(FILE *file, const char *format, const Args& ...args)
725 {
726  return UT::Format::printf(file, format, {args...});
727 }
728 
729 /// Takes a formatting string and a set of arguments and writes out the
730 /// formatted string to @c stdout.
731 /// @param format The formatting codes, as defined in @ref printf_grammar.
732 /// @param args The arguments to the formatting.
733 /// @return The number of bytes successfully written out.
734 template<typename... Args>
735 size_t UTprintf(const char *format, const Args& ...args)
736 {
737  return UTprintf(stdout, format, args...);
738 }
739 
740 /// Takes a formatting string and a set of arguments and writes out the
741 /// formatted string to the output stream object.
742 /// @param os The output stream to write to.
743 /// @param format The formatting codes, as defined in @ref printf_grammar.
744 /// @param args The arguments to the formatting.
745 /// @return The number of bytes successfully written out.
746 template<typename... Args>
747 size_t UTprintf(std::ostream &os, const char *format, const Args& ...args)
748 {
749  return UT::Format::printf(os, format, {args...});
750 }
751 
752 /// Takes a formatting string and a set of arguments and writes out the
753 /// formatted string into the buffer provided. If @c buffer is @c nullptr,
754 /// then nothing is written out, but the number of bytes needed to store
755 /// the entire formatted string is returned. A termination byte is not written
756 /// out to the buffer. If termination is required, the caller must take care
757 /// of it.
758 /// @param buffer The character buffer to write the formatted string to.
759 /// @param buffer_size The size of the character buffer.
760 /// @param format The formatting codes, as defined in @ref printf_grammar.
761 /// @param args The arguments to the formatting.
762 /// @return The number of bytes successfully written out.
763 template<typename... Args>
764 size_t UTprintf(char *buffer, size_t buffer_size, const char *format,
765  const Args& ...args)
766 {
767  return UT::Format::printf(buffer, buffer_size, format, {args...});
768 }
769 
770 /// @}
771 
772 #endif // __UT_Format__
ArgValue(T &v, UT_ENABLE_IF(UT_IS_ENUM(T)&&!UT_HAS_FMT(T)))
Definition: UT_Format.h:359
#define UT_IS_PTR(T)
Definition: UT_Format.h:323
unsigned short uint16
Definition: SYS_Types.h:38
int int32
Definition: SYS_Types.h:39
size_t formatCustom(char *buffer, size_t buffer_size) const
Definition: UT_Format.h:384
SizedString(const char *str, size_t size)
Definition: UT_Format.h:462
#define UT_ENABLE_IF(T)
Definition: UT_Format.h:320
void setBuffer(char *buffer, size_t buffer_size)
Definition: UT_Format.h:195
void
Definition: png.h:1083
UT_API size_t format(char *, size_t, const char *format, std::initializer_list< ArgValue > args)
const GLdouble * v
Definition: glcorearb.h:837
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
ArgValue(fpreal64 v)
Definition: UT_Format.h:263
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
size_t operator()(const char *begin, size_t length)
pointer+length api
Definition: UT_Format.h:177
ArgValue(const std::string &v)
Definition: UT_Format.h:307
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
#define UT_API
Definition: UT_API.h:14
ArgValue(uint32 v)
Definition: UT_Format.h:258
#define UT_HAS_FMT(T)
Definition: UT_Format.h:321
unsigned long long uint64
Definition: SYS_Types.h:117
size_t(*)(char *buffer, size_t buffer_size, const void *value) CustomFormat
Definition: UT_Format.h:467
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
float fpreal32
Definition: SYS_Types.h:200
GLuint buffer
Definition: glcorearb.h:660
FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n, const fill_t< Char > &fill) -> OutputIt
Definition: format.h:1262
Formatter()
Constructs a new Formatter object.
Definition: UT_Format.h:523
ArgValue(const char *v)
Definition: UT_Format.h:301
ArgValue(uint16 v)
Definition: UT_Format.h:256
bool isFloat() const
Definition: UT_Format.h:416
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:39
Format
Definition: oidn.hpp:118
GLfloat f
Definition: glcorearb.h:1926
const TypeValidator< double > Number("NUMBER")
Check for a number.
#define UT_IS_ENUM(T)
Definition: UT_Format.h:324
Definition: core.h:760
virtual size_t operator()(const char *begin, const char *end)=0
ArgValue(T v, UT_ENABLE_IF(!UT_IS_ENUM(T)&&UT_IS_PTR(T)&&!UT_HAS_FMT(UT_REMOVE_PTR(T))))
Definition: UT_Format.h:331
bool isString() const
Definition: UT_Format.h:437
bool isNumeric() const
Definition: UT_Format.h:432
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER typedef long long unsigned int Int64
Int64 - unsigned 64-bit integer.
Definition: ImathInt64.h:37
#define UT_REMOVE_PTR(T)
Definition: UT_Format.h:322
GLuint GLuint end
Definition: glcorearb.h:475
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
ArgValue(T &t, UT_ENABLE_IF(!UT_IS_PTR(T)&&!UT_IS_ENUM(T)))
Definition: UT_Format.h:376
bool isPointer() const
Definition: UT_Format.h:444
long long int64
Definition: SYS_Types.h:116
size_t operator()(const char *begin, size_t length)
Definition: UT_Format.h:203
ArgValue(fpreal32 v)
Definition: UT_Format.h:262
IMATH_HOSTDEVICE constexpr int sign(T a) IMATH_NOEXCEPT
Definition: ImathFun.h:33
signed char int8
Definition: SYS_Types.h:35
ArgValue(T v, UT_ENABLE_IF(!UT_IS_ENUM(T)&&UT_IS_PTR(T)&&UT_HAS_FMT(UT_REMOVE_PTR(T))))
Definition: UT_Format.h:341
const void * myPtr
Definition: UT_Format.h:491
GLdouble t
Definition: glad.h:2397
Type type() const
Definition: UT_Format.h:404
size_t UTprintf(FILE *file, const char *format, const Args &...args)
Definition: UT_Format.h:724
GLenum GLint GLint * precision
Definition: glcorearb.h:1925
bool isEmpty() const
Definition: UT_Format.h:406
GLsizeiptr size
Definition: glcorearb.h:664
Writer(char *buffer, size_t buffer_size)
Definition: UT_Format.h:191
UT_API size_t format(FILE *file, const char *format, std::initializer_list< ArgValue > args)
SizedString mySizedStr
Definition: UT_Format.h:490
__hostdev__ bool isInteger(GridType gridType)
Return true if the GridType maps to a POD integer type.
Definition: NanoVDB.h:820
short int16
Definition: SYS_Types.h:37
bool isSigned() const
Definition: UT_Format.h:423
LeafData & operator=(const LeafData &)=delete
friend class Formatter
Definition: UT_Format.h:455
UT_API size_t printf(FILE *file, const char *format, std::initializer_list< ArgValue > args)
auto ptr(T p) -> const void *
Definition: format.h:2448
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
ArgValue(T(&v)[N])
Definition: UT_Format.h:351
void setForceDigitGroupings(bool flag)
Definition: UT_Format.h:534
GA_API const UT_StringHolder N
ArgValue(T &v, UT_ENABLE_IF(UT_IS_ENUM(T)&&UT_HAS_FMT(T)))
Definition: UT_Format.h:366
size_t UTformat(FILE *file, const char *format, const Args &...args)
Definition: UT_Format.h:665
**If you just want to fire and args
Definition: thread.h:609
unsigned int uint32
Definition: SYS_Types.h:40
ArgValue(Type type)
Definition: UT_Format.h:458
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: core.h:1131
virtual ~CustomWriter()=default
ArgValue(fpreal16 v)
Definition: UT_Format.h:261
ArgValue(const void *v)
Definition: UT_Format.h:315
type
Definition: core.h:1059
const char * myStr
Definition: UT_Format.h:489
bool isInteger() const
Definition: UT_Format.h:408
bool isCustom() const
Definition: UT_Format.h:449
ArgValue(uint64 v)
Definition: UT_Format.h:260
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558