GithubHelp home page GithubHelp logo

Comments (8)

eliaskosunen avatar eliaskosunen commented on May 31, 2024

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.

nanoric avatar nanoric commented on May 31, 2024

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.

nanoric avatar nanoric commented on May 31, 2024

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.

nanoric avatar nanoric commented on May 31, 2024

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.

eliaskosunen avatar eliaskosunen commented on May 31, 2024

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.

eliaskosunen avatar eliaskosunen commented on May 31, 2024

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.

nanoric avatar nanoric commented on May 31, 2024

Good, it works.

from scnlib.

nanoric avatar nanoric commented on May 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.