Comments (8)
There's better string parsing support in the roadmap. With the present facilities, a fixed number of chars can be scanned with using a span
:
char buf1[20], buf2[20];
auto span1 = scn::make_span(buf1), span2 = make_span(buf2);
auto ret = scn::scan("42,43", "{},{}", span1, span2);
// buf1 == {'4','2'}
// buf2 == {'4','3'}
Although, that's probably not really necessary: something like std::copy_n
could be used, since the input given to scan
is just a range:
char buf1[20], buf2[20];
auto it = std::copy_n("42,43", 2, buf1);
// buf1 == {'4',2'}
++it; // skip ','
it = std::copy_n(it, 2, buf2);
// it points at the null terminator of the string literal
// buf2 == {'4','3'}
As an aside, the format string parsing for scn::scanf
is really lacking. It's possible I'll be taking it out in the next release, as I don't really want to be maintaining two distinct format string syntaxes.
from scnlib.
In fact, I don't care about scanf. If there comes
But I am failed to compile this example:
char buf1[20], buf2[20];
auto span1 = scn::make_span(buf1), span2 = make_span(buf2);
auto ret = scn::scan("42,43", "{},{}", span1, span2);
version: 3601563 MSVC
I also tried to modify the code, it compiles, but failed to scan:
char buf1[20], buf2[20];
auto span1 = scn::make_span(buf1, buf1+20), span2 = scn::make_span(buf2, buf2+20);
auto ret = scn::scan("42,43", "{},{}", span1, span2);
//error: eof.
from scnlib.
In this issue, I want to say that if there is a string as output, all continous inputs will be scaned into that string.
In this example, "42,43" will be scaned into first string, left nothing to the second string.
So this is why I want a width specifier.
Maybe there are some other solutions, but the solution should USE ONLY scnlib, but not some tricks.
from scnlib.
Even tough std::copy_n can to what I want, but why a scanning library does'nt support that?
For example, you can parse "123456" by just write a function parse_int and invoke. But since we have scnlib, we of cause will want to make the whole thing easier.
Since sscanf can parse an input like "42,43" into two string, why scnlib couldn't?
from scnlib.
My bad, I forgot to include the size of the span in my example. Sorry about that.
The size of the span determines the amount of characters (bytes) to be read from the input range. So, to read two chars, the size of the span needs to be 2.
char buf[1024] = {0};
auto s = make_span(buf, 3);
scan("abcdef", "{}", s);
// s == {'a', 'b', 'c'}
Your attempt failed, because scn::scan
tried to read 20 characters (the size of the span was 20) from a five-character string ("42,43"
).
from scnlib.
So, the correct code is:
char buf1[20], buf2[20];
auto span1 = scn::make_span(buf1, 2), span2 = make_span(buf2, 2);
auto ret = scn::scan("42,43", "{},{}", span1, span2);
// buf1 == {'4','2'}
// buf2 == {'4','3'}
from scnlib.
Good, it works.
from scnlib.
Anyway, I think haveing a length specifer(or a customize separator) will be better .
I'm working on a project, which has others developers that not that familier with C++. They use some language such python which are pretty easy to use. But for now, for some reason, they have to use C++ to write some code.
the code:
std::string_view s1, s2;
scn::scan(input/*"42,43"*/, "{:2},{:2}", s1, s2);
or
std::string_view s1, s2;
scn::scan(input/*"42,43"*/, "{},{}", s1, s2); // ',' as separator.
is simple and clear.
but in this code:
char buf1[20], buf2[20];
auto span1 = scn::make_span(buf1, 2), span2 = scn::make_span(buf2, 2);
scn::scan(input/*"42,43"*/, "{:2},{:2}", s1, s2);
Developer must pre-allocate memory(declare buffers), and specifc the start and end position of buffers. which is too complex for newbees. Also, if the parsing syntax gets a little more complex, the code gets much more ugly, making it hard for people to review.
If changing the formating syntax is not a good idea, we can make some change outside the format parser, such as :
std::string_view s1, s2;
scn::scan(input/*"42,43"*/, "{},{}", lengh_limited(s1, 2), length_limited(s2, 2));
Supporting this, your scnlib might be more easy for people to accept. What's you opinon about this?
from scnlib.
Related Issues (20)
- Library not working in Godbolt Compiler explorer HOT 11
- Improvements for embedded targets HOT 1
- Minor issues in documentation HOT 1
- Running fuzzers in CI HOT 2
- Error parsing '+12345' as a float HOT 3
- Format specifier to skip a field? HOT 3
- Add COMPONENT to install targets HOT 1
- Header only build is too complex HOT 1
- gcc-10 won't compile with rvalue string_view as range HOT 1
- scan_list_ex: unable to use separator when scanning list of strings HOT 1
- Capture matches in tuple HOT 1
- `scn::basic_string_view::substr` does not compile. HOT 1
- scn::scan_tuple compilation error with custom type HOT 1
- Cmake <3.5 deprecation warning HOT 1
- Quadratic Complexity Issue with scnlib's scan Function for Long Texts HOT 5
- Wich packages does this come with ? HOT 1
- Dead documentation link HOT 1
- Scan format "{} = ({}, {})" with std::string not properly parsed HOT 2
- issue with scn::getline on file input HOT 2
- Can't compile scnlib/2.0.0 on MSVC HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scnlib.