On Tue, 2020-08-25, Siri Cruise wrote:
> In article
> <
MPG.39ae05b3e...@news.eternal-september.org>,
> Mike Copeland <
mrc...@cox.net> wrote:
>
>> > On 24/08/2020 00:05, Mike Copeland wrote:
>> > > My intent is to strip the leading whitespace from each text line.
>> > > The code below doesn't work. Please advise. TIA
>> >
>> > If all you want to do is slip the leading whitespace it would probably
>> > be more efficient to scan for the first non-space character in your read
>> > line, then output the rest of the line. Less in-memory copying.
>> >
>> That's my intent...but how do I do this? TIA
>
> C++ is so much better than C.
>
> resetbuffer();
> int c = 0; while (c=fgetc(fn), c!=EOF && isspace(c)) ;
> while (c!='\n' && c!=EOF) {addbuffer(c); c = fgetc(fn);}
> if (emptybuffer() && c==EOF) eofbuffer();
>
> How crude and inefficient.
I'd do it like this:
std::string s;
while(std::getline(is, s)) {
lstrip(s);
do_stuff_with(s);
}
lstrip(std::string&) is trivial to write.
Or if I really didn't want the copying, I'd use std::string_view, or
iterators and implement:
It lstrip(It a, It b);
I don't think I've ever done this though. Normally, once you've
stripped leading whitespace you still have plenty of parsing to do,
and you haven't won a lot by doing one tiny part of it.
It's e.g. more commonly useful to have a split() function which splits
a string on whitespace, while removing said whitespace.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/
snipabacken.se> O o .