Deutsch English Français Italiano |
<vhq3j8$2jo$1@reader2.panix.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail From: cross@spitfire.i.gajendra.net (Dan Cross) Newsgroups: comp.unix.programmer Subject: Re: Command Languages Versus Programming Languages Date: Fri, 22 Nov 2024 14:14:00 -0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: <vhq3j8$2jo$1@reader2.panix.com> References: <uu54la$3su5b$6@dont-email.me> <875xohbxre.fsf@doppelsaurus.mobileactivedefense.com> <slrnvjulf2.rfrc.mas@a4.home> <87o728qzbc.fsf@doppelsaurus.mobileactivedefense.com> Injection-Date: Fri, 22 Nov 2024 14:14:00 -0000 (UTC) Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80"; logging-data="2680"; mail-complaints-to="abuse@panix.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: cross@spitfire.i.gajendra.net (Dan Cross) 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 is that both operands of the `-` are promoted to _signed_ int before the subtraction (sec 6.3.1.8 of n220), and so the comparison is done against signed quantities. Your cast fixes this by forcing promotion of '0' and 10 to unsigned int before either operation. I do agree that the presentation was overly terse and came off poorly. - Dan C.