PSA: Minor printing related improvements

Hi all,

I want to summarize a few minor printing related improvements that I have committed over the past few weeks that folks might find useful:

  1. formatv() validation: formatv() calls now validate that the replacement field indices start with 0 and are contiguous (no gaps) and that the number of supplied arguments to formatv() match the number of replacement field indices. This ensures that formatv() arguments are neither under not over specified. For cases where this validation is not desired, use the formatv(false,...) overload.

  2. formatv() automatic index assignment: formatv() now support not specifying the replacement field indices, in which case they will be auto assigned incrementally. So something like β€œ{0} = {1}.{2}” can become β€œ{}={}.{}”. When using automatic assignment, all fields need to use that, else validation will fail. This should make modifying code that uses formatv() easier as fields are added or removed from the format string.

  3. indent helper for indentation: For code that generates text with indentation, you can now use the indent type for better ergonomics. It can help code such as

    OS.indent(4) << "Hello";
    

    look a little more streamlined (and clang-formats to better use the space) as such:

    OS << indent(4) << "Hello";
    

    The indent struct supports +/-/++/-- operators, as well as scaling when all your indentations are a multiple of a fixed size. It’s most useful when you can have the indent object created upront and reuse it multiple times.

Thanks,
Rahul

2 Likes