Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strtok

5 views
Skip to first unread message

Franz Bachler

unread,
Jan 11, 2012, 10:25:22 AM1/11/12
to
Hello,

here I've found an example for a strtok implementation:

http://www.koders.com/c/fid4BE42498019181532F7F7A05702D7120CDE48B38.aspx

Is it possible to encode it without using of "goto"?

cont:
c = *s++;
for (spanp = delim; (sc = *spanp++) != 0;) {
if (c == sc)
goto cont;
}

Greetings,
Franz

--
Franz Bachler, A-3250 Wieselburg
E-Mail: fraba (at) gmx.at
Homepage: http://members.aon.at/fraba
oder http://home.pages.at/fraba


Ulrich Eckhardt

unread,
Jan 12, 2012, 2:57:16 AM1/12/12
to
Am 11.01.2012 16:25, schrieb Franz Bachler:
> here I've found an example for a strtok implementation:
>
> http://www.koders.com/c/fid4BE42498019181532F7F7A05702D7120CDE48B38.aspx
>
> Is it possible to encode it without using of "goto"?
>
> cont:
> c = *s++;
> for (spanp = delim; (sc = *spanp++) != 0;) {
> if (c == sc)
> goto cont;
> }

Yes. There is a bunch of ways, using recursion or a separate flag to
jump between the inner and outer loop come to mind.

Uli


R.Wieser

unread,
Jan 12, 2012, 10:52:09 AM1/12/12
to
Hello Franz,

I'm not sure what language that is in, but would replacing that "goto cont"
with re-initializing "c" and "spanp" not do the trick ?

> c = *s++;
> for (spanp = delim; (sc = *spanp++) != 0;) {
> if (c == sc) {
c = *s++;
spanp = delim
}
> }


By the way: That "for" is just a "while" in disguise.

Regards,
Rudy Wieser


-- Origional message:
Franz Bachler <fraba....@gmx.at> schreef in berichtnieuws
4f0da9f8$0$1573$91ce...@newsreader04.highway.telekom.at...

Jasen Betts

unread,
Jan 14, 2012, 11:44:06 PM1/14/12
to
On 2012-01-11, Franz Bachler <fraba....@gmx.at> wrote:
> Hello,
>
> here I've found an example for a strtok implementation:
>
> http://www.koders.com/c/fid4BE42498019181532F7F7A05702D7120CDE48B38.aspx

> Is it possible to encode it without using of "goto"?

why?

> cont:
> c = *s++;
> for (spanp = delim; (sc = *spanp++) != 0;) {
> if (c == sc)
> goto cont;
> }

s+=strspn(s,spanp);

--
⚂⚃ 100% natural

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---

Franz Bachler

unread,
Jan 18, 2012, 11:53:55 AM1/18/12
to

"Jasen Betts" <ja...@xnet.co.nz> schrieb im Newsbeitrag
news:jetlim$usm$1...@reversiblemaps.ath.cx...
> On 2012-01-11, Franz Bachler <fraba....@gmx.at> wrote:
>> Hello,
>>
>> here I've found an example for a strtok implementation:
>>
>> http://www.koders.com/c/fid4BE42498019181532F7F7A05702D7120CDE48B38.aspx
>
>> Is it possible to encode it without using of "goto"?
>
> why?

goto ist bad *g*

really: curiosity



Jasen Betts

unread,
Jan 20, 2012, 7:47:49 AM1/20/12
to
On 2012-01-18, Franz Bachler <fraba....@gmx.at> wrote:
>
> "Jasen Betts" <ja...@xnet.co.nz> schrieb im Newsbeitrag
> news:jetlim$usm$1...@reversiblemaps.ath.cx...

>>> Is it possible to encode it without using of "goto"?
>>
>> why?
>
> goto ist bad *g*
>
> really: curiosity
>

here's another way

c=*s++;
spanp=delim ;
while( (sc=*spanp++) )
if (c == sc)
{
c=*s++;
spanp=delim;

Gernot Frisch

unread,
Jan 24, 2012, 9:50:30 AM1/24/12
to

beware of strtok. It's not thread safe (will store a pointer internally)
and will modify the buffer, so you mostly will need a copy of it.
Write your own splitter that returns a std::vector<std::string> instead:

void str_split(const std::string& in, std::vector<std::string>& out, const
std::string& delim=" \t\r\n")
{
std::string::size_type firstPos = in.find_first_not_of(delim);
std::string::size_type secondPos = in.find_first_of(delim, firstPos);
out.clear();
if(firstPos != std::string::npos)
out.push_back( in.substr( firstPos, secondPos - firstPos ) );
while( secondPos != std::string::npos )
{
firstPos = in.find_first_not_of(delim, secondPos);
if(firstPos == std::string::npos)
break;
secondPos = in.find_first_of( delim, firstPos );
out.push_back( in.substr( firstPos, secondPos - firstPos ) );
}
}


Stefan Kuhr

unread,
Jan 24, 2012, 10:03:55 AM1/24/12
to
Hello Gernot,

On 1/24/2012 3:50 PM, Gernot Frisch wrote:
>
> beware of strtok. It's not thread safe (will store a pointer internally)
> and will modify the buffer, so you mostly will need a copy of it.
> Write your own splitter that returns a std::vector<std::string> instead:
>

That's actually incorrect, at least if we talk about MSVC runtimes. All
multithreaded runtimes of VC that I can remember store their internal
pointer in a TLS slot and recent versions of MSVC don't even offer a
single-threaded runtime anymore.



--
S

Gernot Frisch

unread,
Jan 24, 2012, 12:12:34 PM1/24/12
to
> That's actually incorrect, at least if we talk about MSVC runtimes. All
> multithreaded runtimes of VC that I can remember store their internal
> pointer in a TLS slot and recent versions of MSVC don't even offer a
> single-threaded runtime anymore.

You're right for MSVC. This is w.prog.win32 - I forgot.

But beware that you don't nest the calls:
http://msdn.microsoft.com/en-us/library/2c8d19sb.aspx
"However, within a single thread, interleaving calls to one of these
functions is highly likely to produce data corruption and inaccurate
results."

0 new messages