Liste des Groupes | Revenir à cu programmer |
cross@spitfire.i.gajendra.net (Dan Cross) writes:In article <87o728qzbc.fsf@doppelsaurus.mobileactivedefense.com>,>
Rainer Weikusat <rweikusat@talktalk.net> wrote:mas@a4.home writes:>On 2024-11-20, Rainer Weikusat <rweikusat@talktalk.net> wrote:>Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:>
>
Assuming that p is a pointer to the current position in a string, e is a
pointer to the end of it (ie, point just past the last byte) and -
that's important - both are pointers to unsigned quantities, the 'bulky'
C equivalent of [0-9]+ is
>
while (p < e && *p - '0' < 10) ++p;
>
That's not too bad. And it's really a hell lot faster than a
general-purpose automaton programmed to recognize the same pattern
(which might not matter most of the time, but sometimes, it does).
int
main(int argc, char **argv) {
unsigned char *p, *e;
unsigned char mystr[] = "12#45XY ";
>
p = mystr;
e = mystr + sizeof(mystr);
while (p < e && *p - '0' < 10) ++p;
The code I'm actually using is
>
while (p < e && (unsigned)*p - '0' < 10)
++p;
>
I just omitted that when posting this beause I mistakenly assumed that
it probably wasn't needed, ;-). You could have pointed this out instead
of trying to dress it up as some sort of mystery problem¹. Especially as
I did mention that using unsigned arithmetic was necessary (should
really be self-evident).
Well, no, not exactly. You said that it was important that the
pointers point to unsigned quantities, but that wasn't the
issue.
The issue here is that I mistakenly assumed the (unsigned) in the code
was a left-over from before the time when I had changed to pointers to
unsigned char to fix a different issue.
As C tries really hard to force
signed arithmetic onto people despite this basically never makes any
sense,
the type of '0' is int *p gets promoted to int and hence, the
result of the subtraction will also be an int and the '< 10' condition
will be true for every codepoint numerically less than '9' which
obviously won't work as intended.
That's a mistake I made which would have warranted being pointed out and
possibly explained instead of posting some broken code together with
some output the broken code certainly never generated due it being
broken.
Les messages affichés proviennent d'usenet.