[llvm-dev] RFC: General purpose type-safe formatting library
Zachary Turner via llvm-dev
llvm-dev at lists.llvm.org
Wed Oct 12 06:37:45 PDT 2016
Actually I should elaborate because this is a tad misleading.
If the *syntax* is illegal it will ignore. Like if you write {0;} or {0{.
In this case the entire thing is pasted into the output and no replacement
happens.
If it can successfully parse into X, Y, and Z where X is the index, Y is
the alignment, and Z is the option string, then what happens depends on
which of X, Y, and Z are illegal.
If X is empty the sequence is replaced with an empty string. Otherwise, If
X is not a positive integer the sequence is pasted into the output and
everything else ignored.
If Y is illegal, Y is ignored as if it wasn't specified.
Whether Z is illegal is up to the format provider, so each one decides how
to react to an invalid string
Note that any point here we can assert. This would allow us to catch these
in debug builds while silently doing the best we can in non debug builds
On Wed, Oct 12, 2016 at 6:11 AM Zachary Turner <zturner at google.com> wrote:
> In my current implementation, it's up to the format provider. If you have
> an illegal format spec (eg {0;0}) it ignores it and prints the format spec
> as a literal. We could also add an assert here in theory.
>
> If/when we move to c++14, a constexpr StringRef implementation would allow
> us to parse and validate the entire format string at compile time.
>
> Since conciseness is one of the main goals of a library such as this, I
> would hate to actively hamper this for more compile time checking. If you
> write an invalid format spec presumably your test will fail since it will
> ignore it
>
> On Wed, Oct 12, 2016 at 2:34 AM Pavel Labath <labath at google.com> wrote:
>
> On 12 October 2016 at 05:26, Zachary Turner via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
> > os << format_string("Start: {0}, End: {1}, Elapsed: {2:ms}", start, end,
> > start-end);
>
> What would happen if I accidentally type "ps" instead of "ms" (I am
> assuming we will not support picoseconds here)?
>
> Will this abort at runtime?
>
> I would prefer if *all* arguments to the format were checkable at compile
> time:
> I.e. something like:
> os << "blah blah" << format<std::milli>(end-start) << "blah blah";
>
> I understand this may clash a bit with the desire for a compact
> representation, but maybe with some clever design we could achieve
> both?
>
> pl
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161012/4042ad46/attachment-0001.html>
More information about the llvm-dev
mailing list