Liste des Groupes | Revenir à cu programmer |
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.
Les messages affichés proviennent d'usenet.