Deutsch   English   Français   Italiano  
<vhqhii$d5e$1@reader2.panix.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.unix.shell,comp.unix.programmer,comp.lang.misc
Subject: Re: Command Languages Versus Programming Languages
Date: Fri, 22 Nov 2024 18:12:34 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <vhqhii$d5e$1@reader2.panix.com>
References: <uu54la$3su5b$6@dont-email.me> <877c8vtgx6.fsf@doppelsaurus.mobileactivedefense.com> <vhqebq$c71$1@reader2.panix.com> <87o727rwga.fsf@doppelsaurus.mobileactivedefense.com>
Injection-Date: Fri, 22 Nov 2024 18:12:34 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="13486"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)
Bytes: 3873
Lines: 89

In article <87o727rwga.fsf@doppelsaurus.mobileactivedefense.com>,
Rainer Weikusat  <rweikusat@talktalk.net> wrote:
>>>Something which would match [0-9]+ in its first argument (if any) would
>>>be:
>>>
>>>#include "string.h"
>>>#include "stdlib.h"
>>>
>>>int main(int argc, char **argv)
>>>{
>>>    char *p;
>>>    unsigned c;
>>>
>>>    p = argv[1];
>>>    if (!p) exit(1);
>>>    while (c = *p, c && c - '0' > 10) ++p;
>>>    if (!c) exit(1);
>>>    return 0;
>>>}
>>>
>>>but that's 14 lines of text, 13 of which have absolutely no relation to
>>>the problem of recognizing a digit.
>>
>> This is wrong in many ways.  Did you actually test that program?
>>
>> First of all, why `"string.h"` and not `<string.h>`?  Ok, that's
>> not technically an error, but it's certainly unconventional, and
>> raises questions that are ultimately a distraction.
>
>Such as your paragraph above. 
>
>> Second, suppose that `argc==0` (yes, this can happen under
>> POSIX).
>
>It can happen in case of some piece of functionally hostile software
>intentionally creating such a situation. Tangential, irrelevant
>point. If you break it, you get to keep the parts.
>
>> Third, the loop: why `> 10`? Don't you mean `< 10`?  You are
>> trying to match digits, not non-digits.
>
>Mistake I made. The opposite of < 10 is > 9.

I see.  So you want to skip non-digits and exit the first time
you see a digit.  Ok, fair enough, though that program has
already been written, and is called `grep`.

>> Fourth, you exit with failure (`exit(1)`) if `!p` *and* if `!c`
>> at the end, but `!c` there means you've reached the end of the
>> string; which should be success.
>
>Mistake you made: [0-9]+ matches if there's at least one digit in the
>string. That's why the loop terminates once one was found. In this case,
>c cannot be 0.

Ah, you are trying to match `[0-9]` (though you're calling it
`[0-9]+`).  Yeah, your program was not at all equivalent to one
I wrote, though this is what you posted in response to mine, so
I assumed you were trying to emulate that behavior (matching
`^[0-9]+$`).

But I see above that you mentioned `[0-9]+`.  But as I mentioned
above, really you're just matching any digit, so you may as well
be matching `[0-9]`; again, this not the same as the actual
regexp, because you are ignoring the semantics of what regular
expressions actually describe.

In any event, this seems simpler than what you posted:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
        if (argc != 2) {
                fprintf(stderr, "Usage: matchd <str>\n");
                return EXIT_FAILURE;
        }

        for (const char *p = argv[1]; *p != '\0'; p++)
                if ('0' <= *p && *p <= '9')
                        return EXIT_SUCCESS;

        return EXIT_FAILURE;
}

	- Dan C.