My post was an attempt at humour that seems to have failed. Sorry about
that.
I suspected, but wasn't 100% sure. Now that it's certain, I'll say that
the post was pretty funny, even if thoroughly punishing.
Because style discussions are generally unproductive (people who
use non-standard styles always know that the reasons they do it are
worth it) I wanted to say "Personally, I don't like your choices and BTW
you forgot to include ctype.h" in a more interesting way than that.
I'm sorry if it was not funny.
It was. It's still too bad that it doesn't appeal to everyone, though,
and is worth consideration in a group project.
Yes, stylistically. I find his highly non-standard layout very hard to
read.
')*p ' threw me off for a few seconds.
No, as you later posted it was the continue at the bottom of every loop.
++oops; --oops;
If so,
it's a storage-class specifier. While it suggests that access to
so-specified objects "be as fast as possible," it also ensures that
the address of a so-specified object cannot be taken. In this
fashion, it is used in my code to prevent accidentally taking the
address of the corresponding objects.
You see? I was sure you'd have a reason for that, Just chuckle at my
Luddite failure to perceive the value of it.
*Chuckle*
Why is it one greater than it needs to be? If 'c' is not the null
terminator, then one past the end of the string is at least two
characters away. That distance seems useful for the 'malloc'.
It's swings and roundabouts. You need to --diff to set the null and do
the memcpy and if you keep the pointer "nearer" you need to +1 when you
malloc. The latter is, at least, a common idiom. I.e.:
char c;
const char * cur_pos = string;
const char * one_past_end = string;
ptrdiff_t diff;
char * copy;
if (!chars) {
while ((c = *cur_pos++))
if (!isspace(c))
one_past_end = cur_pos;
}
else {
while ((c = *cur_pos++))
if (!in_set(c, chars))
one_past_end = cur_pos;
}
diff = one_past_end - string;
copy = malloc(diff + 1);
if (!copy)
return NULL;
copy[diff] = '\0';
return memcpy(copy, string, diff);
Yes, that does seem to be a more common approach that I've seen. I
guess that I figured this 'diff + 1' idiom above would be implemented in
one of two ways:
increment the register for 'diff'
use that with 'malloc'
decrement it back to what it was
use that with the last two lines
or:
copy 'diff' into another register B
increment that other register B
use that B with 'malloc'
register B is free for repurposing
Neither of which I particularly care for, versus:
use 'diff' with 'malloc'
decrement 'diff'
use that with the last two lines
Since both flavours use 'one_past_end' and 'cur_pos', I figured the
earlier 'one_past_end = cur_pos + 2' would probably go:
copy 'cur_pos' into 'one_past_end'
increment 'one_past_end'
increment 'one_past_end'
But of course, these are totally wild assumptions.
Personally, I'd then change "diff" to "length". I've also moved the ++
of cur_pos to the loop, but if you don't like that you can go back to
while ((c = *cur_pos)) {
if (!isspace(c))
one_past_end = cur_pos + 1;
++cur_pos;
}
but it's then clear that the ++cur_pos could go above the "if" and thus
into the loop condition.
Actually, I prefer '*cur_pos++' (and had that originally), but thought
it might be too wild for the OP, for whatever reason. However, since
there aren't any comments to follow along with, it's weak for any
instructional consideration, anyway.
Pleasant discussion, though!