Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quotes for string support. #766

Merged
merged 2 commits into from
Jun 8, 2018
Merged

quotes for string support. #766

merged 2 commits into from
Jun 8, 2018

Conversation

Remotion
Copy link
Contributor

@Remotion Remotion commented Jun 7, 2018

std::string and types like it are now printed in double quotes "str".
chars are now printed in single quotes 'A'.

#include <tuple>
#include <vector>
#include <string_view>

#define FMT_HEADER_ONLY
#include <fmt/ranges.h>

int main() {
    std::tuple tup = {1, '1', "10", std::string("20"), std::string_view{"30"}};
    fmt::print("{}\n", tup); // (1, '1', "10", "20", "30")

    std::vector vec = {'1', '2'};
    fmt::print("{}\n", vec); // {'1', '2'}

    std::vector vec2 = {"2", "3"};
    fmt::print("{}\n", vec2); // {"2", "3"}
}


template<typename Arg>
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
typename std::enable_if<!is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you wrap long (>80 char length) lines per the style guide (https://google.github.io/styleguide/cppguide.html#Line_Length) here and below?

@Sarcasm
Copy link

Sarcasm commented Jun 8, 2018

It would be nice if the quoting worked the same as std::quoted.
That is, embedded quotes are escaped too.

Also, I'm would really love it if there was something similar to python format's value conversion flag:

Notably, an equivalent to the !r flag.

That is:

std::string s = "abc \"def\" hij";
fmt::print("s is: {!r}\n", s);

Would output:

s is: "abc \"def\" hij"

The reason I'm asking for this, is when formatting error messages.
If I get an invalid string value, which might be a path, an executable name, or anything else.
I'd like to make sure my error messages fits on one line and is somewhat unambiguous:

No quote at all:

error: Value abc def is not valid.
error: Value abc "def is not valid.
error: Value abc
def is not valid.
error: Invalid value: abc def

Vs "dumb quotes" (fmt::format(""{}"", value)):

error: Value "abc def" is not valid.
error: Value "abc "def" is not valid.
error: Value "abc
def" is not valid.
error: Invalid value: "abc def  "

Vs string representation:

error: Value "abc def" is not valid.
error: Value "abc \"def" is not valid.
error: Value "abc\ndef" is not valid.
error: Invalid value: "abc def  "

Note: std::quoted does not handle newline specifically.

Sorry if this comment is out of scope for this PR.

@vitaut
Copy link
Contributor

vitaut commented Jun 8, 2018

It would be nice if the quoting worked the same as std::quoted.

That makes sense, but I suggest doing it in a separate PR.

@SuperWig
Copy link
Contributor

SuperWig commented Jun 8, 2018

Would it be a good idea to have an is_char type trait so people have the option to add support for their own chars?

@Remotion
Copy link
Contributor Author

Remotion commented Jun 8, 2018

Adding fmt::is_char and use it instate of function specialization for char ?

How about char, wchar_t, char16_t, char32_t ?

@SuperWig
Copy link
Contributor

SuperWig commented Jun 8, 2018

Yeah was thinking something like

template <typename T>
struct is_char : std::integral_constant<bool,
	std::is_same<char, T>::value ||
	std::is_same<char16_t, T>::value ||
	std::is_same<char32_t, T>::value> {};
---------------
template<typename Arg>
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, 
    typename std::enable_if<!is_like_std_string<typename  std::decay<Arg>::type>::value &&
	!is_char<typename std::decay<Arg>::type>::value>::type* = nullptr) {
  return add_space ? " {}" : "{}";
}
------------------------
template <typename Arg>
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
	typename std::enable_if<is_char<typename std::decay<Arg>::type>::value>::type* = nullptr) {
    return add_space ? " '{}'" : "'{}'";
}

Though wouldn't you need to separate wchar_t since it uses a different format? And apparently char16_t and char32_t aren't supported by fmt. I get this error

fmt\core.h(541) error C2079: 'f' uses undefined struct 'fmt::v5::formatter<T,char,void>'

@vitaut vitaut merged commit 5b5886a into fmtlib:master Jun 8, 2018
@vitaut
Copy link
Contributor

vitaut commented Jun 8, 2018

@Remotion, merged, thanks! Could you by any chance submit a section about the new ranges functionality including quotation to the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants