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

how to parse string #108

Closed
mirekh68 opened this issue Apr 12, 2024 · 1 comment
Closed

how to parse string #108

mirekh68 opened this issue Apr 12, 2024 · 1 comment

Comments

@mirekh68
Copy link

I have problem with parsing example like this:

auto result1 = scn::scan<std::string, double>(":x22x , 3.14", ":x{}x , {}");

Either I dont understand something or documentation is missing important case ?
the other case like this:

auto result1 = scn::scan<std::string, double>(":x22x , 3.14", ":x{}x , {}");
works as expected. How to tackle the string case ?

@eliaskosunen
Copy link
Owner

eliaskosunen commented May 19, 2024

The default behavior for reading a string is to scan until a whitespace character: https://www.scnlib.dev/latest/group__format-string.html#type-string, which is the same behavior as with scanf and %s. Therefore, the following is what happens:

auto result = scn::scan<std::string>("x22x", "x{}");
// result->value() == "22x"

The format string following the {} is not inspected to determine, what characters to accept when reading a string, and when to stop reading. To determine what characters to accept, the character set syntax can be used:

// [^x]: accept everything but 'x'
auto result = scn::scan<std::string, double>(":x22x , 3.14", ":x{:[^x]}x , {}");
// std::get<0>(result->values()) == "22"
// std::get<1>(result->values()) == 3.14

Your second case is identical to the first, so I can't help with that.

See also: #94 (comment)

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

No branches or pull requests

2 participants