Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #103621 > unrolled thread
| Started by | fir <profesor.fir@gmail.com> |
|---|---|
| First post | 2017-02-10 12:24 -0800 |
| Last post | 2017-02-19 11:10 -0500 |
| Articles | 20 on this page of 59 — 11 participants |
Back to article view | Back to comp.lang.c
better atoi fir <profesor.fir@gmail.com> - 2017-02-10 12:24 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-10 12:35 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 14:48 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-10 14:12 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:36 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-11 01:07 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 10:10 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-11 09:26 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:36 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 00:18 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 00:31 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 03:47 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 04:22 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 06:08 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 06:13 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 07:31 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:02 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:08 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:17 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:23 -0800
Re: better atoi Robert Latest <boblatest@yahoo.com> - 2017-02-19 11:11 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 17:33 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:35 -0700
Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-11 17:52 -0500
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 18:53 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-10 22:50 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 21:59 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 12:08 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 09:39 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 17:26 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:30 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 22:58 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 18:48 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-12 02:16 +0000
Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-12 09:57 -0500
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 07:27 -0800
Re: better atoi jameskuyper@verizon.net - 2017-02-12 08:44 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 09:15 -0800
Re: better atoi Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-12 09:44 -0800
Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 18:32 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-12 18:56 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 19:18 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 00:16 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 01:56 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 10:44 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 11:38 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 14:37 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 15:00 +0000
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-12 11:15 -0800
Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 19:25 +0000
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-12 11:54 -0800
Re: better atoi supercat@casperkitty.com - 2017-02-12 13:13 -0800
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-10 15:16 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:01 -0700
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-11 01:30 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:26 -0700
Re: better atoi Nick Bowler <nbowler@draconx.ca> - 2017-02-11 00:26 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:32 -0700
Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-19 11:10 -0500
Page 1 of 3 [1] 2 3 Next page →
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-10 12:24 -0800 |
| Subject | better atoi |
| Message-ID | <853138f9-54d1-435f-90d5-ef75a38e65bf@googlegroups.com> |
atoi is weird as it do not allows to detect if an input is wrong or overflows it is sad that if one need to check it can use strtol but this strtol also seems weird, on this page http://www.cplusplus.com/reference/cstdlib/strtol/ it is said that strtol sets errno to ERANGE but is not mentioned with wrong value, so im not sure if it can be uniformly checked (probably yest though but only not noted (i not checked it though)) even if this strtol works ok there is also a doubt how it shoud treat 'non pure' numbers (like " 0034aaa" or " 035 aaa") should it rise an error or just return 34 - not fully clear maybe (maybe clear maybe not it needs a bit of insight)
[toc] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-10 12:35 -0800 |
| Message-ID | <3907cdcb-ad0b-4d78-a07a-6006ce0b3481@googlegroups.com> |
| In reply to | #103621 |
W dniu piątek, 10 lutego 2017 21:24:51 UTC+1 użytkownik fir napisał: > atoi is weird as it do not allows to detect if an > input is wrong or overflows > > it is sad that if one need to check it can use > strtol but this strtol also seems weird, > on this page > > http://www.cplusplus.com/reference/cstdlib/strtol/ > > it is said that strtol sets errno to ERANGE but is > not mentioned with wrong value, so im not sure if > it can be uniformly checked (probably yest though > but only not noted (i not checked it though)) > > even if this strtol works ok there is also a doubt > how it shoud treat 'non pure' numbers (like " 0034aaa" > or " 035 aaa") > should it rise an error or just return 34 - not fully > clear maybe (maybe clear maybe not it needs a bit of > insight) other way it would be use atof and check if the value taken is in the rangoe of INT_MAX, that could be in some way simpler, im not sure though if it would not be slower (yet beside range one would have to check if output is integer value - but the wrapper build around atof seem simplest) question if i would build wraper about atoi atof strtol or whatewer working this way that it takes integer value but on "out of range", "no value" or even "dirty value" (by dirty i mean possibly anything except the pure number "-3223" (well say spaces like " -33 " i could call "slightly dirty" and other chars like "aa33dhh" like "dirty) - so i would have 4 kind of errors) i would just show message box with error - what you suggest i should use to write such wrapper?
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2017-02-10 14:48 -0700 |
| Message-ID | <1b1sv5k98w.fsf@pfeifferfamily.net> |
| In reply to | #103621 |
fir <profesor.fir@gmail.com> writes:
>
> http://www.cplusplus.com/reference/cstdlib/strtol/
>
> it is said that strtol sets errno to ERANGE but is
> not mentioned with wrong value, so im not sure if
> it can be uniformly checked (probably yest though
> but only not noted (i not checked it though))
It says it returns LONG_MAX or LONG_MIN in this case (so it appears it
is possible that if the string does in fact represent one of those
values, you can't reliably detect the conversion was successful -- as
we've seen in other places, we can't rely on ERRNO being 0 on success.
We'd sort of hope for it in this function...).
> even if this strtol works ok there is also a doubt
> how it shoud treat 'non pure' numbers (like " 0034aaa"
> or " 035 aaa")
> should it rise an error or just return 34 - not fully
> clear maybe (maybe clear maybe not it needs a bit of
> insight)
No, this is clearly stated in the page you cited:
The function first discards as many whitespace characters as
necessary until the first non-whitespace character is found. Then,
starting from this character, takes as many characters as possible
that are valid following a syntax that depends on the base
parameter, and interprets them as a numerical value. Finally, a
pointer to the first character following the integer representation
in str is stored in the object pointed by endptr.
(and it goes on to define the syntax)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-10 14:12 -0800 |
| Message-ID | <c74e3f79-52b6-4c5a-b000-167ea48b26f0@googlegroups.com> |
| In reply to | #103631 |
W dniu piątek, 10 lutego 2017 22:48:38 UTC+1 użytkownik Joe Pfeiffer napisał: > fir <profesor.fir@gmail.com> writes: > > > > http://www.cplusplus.com/reference/cstdlib/strtol/ > > > > it is said that strtol sets errno to ERANGE but is > > not mentioned with wrong value, so im not sure if > > it can be uniformly checked (probably yest though > > but only not noted (i not checked it though)) > > It says it returns LONG_MAX or LONG_MIN in this case (so it appears it > is possible that if the string does in fact represent one of those > values, you can't reliably detect the conversion was successful -- as > we've seen in other places, we can't rely on ERRNO being 0 on success. > We'd sort of hope for it in this function...). > > > even if this strtol works ok there is also a doubt > > how it shoud treat 'non pure' numbers (like " 0034aaa" > > or " 035 aaa") > > should it rise an error or just return 34 - not fully > > clear maybe (maybe clear maybe not it needs a bit of > > insight) > > No, this is clearly stated in the page you cited: > > The function first discards as many whitespace characters as > necessary until the first non-whitespace character is found. Then, > starting from this character, takes as many characters as possible > that are valid following a syntax that depends on the base > parameter, and interprets them as a numerical value. Finally, a > pointer to the first character following the integer representation > in str is stored in the object pointed by endptr. > > (and it goes on to define the syntax) i was reading that but still it is not clear to me from this if it should take " 0034aaa" as 34 or this is a error input, same with " 035 aaa" problem is a bit more complex, as i mentioned i would like to get a function that would take string and returned integer yet with a couple of information of errors (if no number, or out of range) and also on succes (if it accepts dirty numbers as input) this errno could be used as this information though i never used it im not sure if it can be context dependant value or only some globally predefined errors im not sure if in fact this present implementation is ok in those mentioned regard or it is really flawed for some
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2017-02-10 22:36 -0700 |
| Message-ID | <1bfujli90k.fsf@pfeifferfamily.net> |
| In reply to | #103633 |
fir <profesor.fir@gmail.com> writes: > W dniu piątek, 10 lutego 2017 22:48:38 UTC+1 użytkownik Joe Pfeiffer napisał: >> fir <profesor.fir@gmail.com> writes: >> > >> > http://www.cplusplus.com/reference/cstdlib/strtol/ >> > >> > it is said that strtol sets errno to ERANGE but is >> > not mentioned with wrong value, so im not sure if >> > it can be uniformly checked (probably yest though >> > but only not noted (i not checked it though)) >> >> It says it returns LONG_MAX or LONG_MIN in this case (so it appears it >> is possible that if the string does in fact represent one of those >> values, you can't reliably detect the conversion was successful -- as >> we've seen in other places, we can't rely on ERRNO being 0 on success. >> We'd sort of hope for it in this function...). >> >> > even if this strtol works ok there is also a doubt >> > how it shoud treat 'non pure' numbers (like " 0034aaa" >> > or " 035 aaa") >> > should it rise an error or just return 34 - not fully >> > clear maybe (maybe clear maybe not it needs a bit of >> > insight) >> >> No, this is clearly stated in the page you cited: >> >> The function first discards as many whitespace characters as >> necessary until the first non-whitespace character is found. Then, >> starting from this character, takes as many characters as possible >> that are valid following a syntax that depends on the base >> parameter, and interprets them as a numerical value. Finally, a >> pointer to the first character following the integer representation >> in str is stored in the object pointed by endptr. >> >> (and it goes on to define the syntax) > > i was reading that but still it is not clear to me from this if it > should take " 0034aaa" as 34 or this is a error input, same with " 035 > aaa" If the radix is less that 11, 'a' is not a numeric character. Therefore the result with " 0034aaa" is the conversion of "0034" (and what that result is depends on the radix) for radixes up to 10, and the conversion of "0034aaa" for radixes from 11 to 36. ' ' is not a numeric character in any radix, so the result is the conversion of "035" (again, the result depends on the radix). > problem is a bit more complex, as i mentioned i would like to get a > function that would take string and returned > integer yet with a couple of information of errors (if no number, or > out of range) and also on succes (if it accepts > dirty numbers as input) Near I can tell, strtol() does exactly what you're looking for. > this errno could be used as this information though i never used it im > not sure if it can be context dependant value or only some globally > predefined errors > > im not sure if in fact this present implementation is > ok in those mentioned regard or it is really flawed > for some It appears to do what you want.
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-11 01:07 -0800 |
| Message-ID | <a8da9423-4e95-4ad5-be29-9499d341f331@googlegroups.com> |
| In reply to | #103661 |
W dniu sobota, 11 lutego 2017 06:36:33 UTC+1 użytkownik Joe Pfeiffer napisał: > fir <profesor.fir@gmail.com> writes: > > > W dniu piątek, 10 lutego 2017 22:48:38 UTC+1 użytkownik Joe Pfeiffer napisał: > >> fir <profesor.fir@gmail.com> writes: > >> > > >> > http://www.cplusplus.com/reference/cstdlib/strtol/ > >> > > >> > it is said that strtol sets errno to ERANGE but is > >> > not mentioned with wrong value, so im not sure if > >> > it can be uniformly checked (probably yest though > >> > but only not noted (i not checked it though)) > >> > >> It says it returns LONG_MAX or LONG_MIN in this case (so it appears it > >> is possible that if the string does in fact represent one of those > >> values, you can't reliably detect the conversion was successful -- as > >> we've seen in other places, we can't rely on ERRNO being 0 on success. > >> We'd sort of hope for it in this function...). > >> > >> > even if this strtol works ok there is also a doubt > >> > how it shoud treat 'non pure' numbers (like " 0034aaa" > >> > or " 035 aaa") > >> > should it rise an error or just return 34 - not fully > >> > clear maybe (maybe clear maybe not it needs a bit of > >> > insight) > >> > >> No, this is clearly stated in the page you cited: > >> > >> The function first discards as many whitespace characters as > >> necessary until the first non-whitespace character is found. Then, > >> starting from this character, takes as many characters as possible > >> that are valid following a syntax that depends on the base > >> parameter, and interprets them as a numerical value. Finally, a > >> pointer to the first character following the integer representation > >> in str is stored in the object pointed by endptr. > >> > >> (and it goes on to define the syntax) > > > > i was reading that but still it is not clear to me from this if it > > should take " 0034aaa" as 34 or this is a error input, same with " 035 > > aaa" > > If the radix is less that 11, 'a' is not a numeric character. Therefore > the result with " 0034aaa" is the conversion of "0034" (and what that > result is depends on the radix) for radixes up to 10, and the conversion > of "0034aaa" for radixes from 11 to 36. ' ' is not a numeric character > in any radix, so the result is the conversion of "035" (again, the > result depends on the radix). > > > problem is a bit more complex, as i mentioned i would like to get a > > function that would take string and returned > > integer yet with a couple of information of errors (if no number, or > > out of range) and also on succes (if it accepts > > dirty numbers as input) > > Near I can tell, strtol() does exactly what you're looking for. > > > this errno could be used as this information though i never used it im > > not sure if it can be context dependant value or only some globally > > predefined errors > > > > im not sure if in fact this present implementation is > > ok in those mentioned regard or it is really flawed > > for some > > It appears to do what you want. well im not sure, could you tell maybe how i can check if there was an out of range, or non numer , and number but with 'dirty' input provided?
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2017-02-11 10:10 -0700 |
| Message-ID | <1b60kgirfs.fsf@pfeifferfamily.net> |
| In reply to | #103663 |
fir <profesor.fir@gmail.com> writes:
>
> well im not sure, could you tell maybe how i can check if
> there was an out of range, or non numer , and number but with 'dirty'
> input provided?
Seriously? OK.
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
void checknum(const char *nptr, int base)
{
long int val;
char *endptr;
fprintf(stderr, "testing string \"%s\" in base %d\n", nptr, base);
errno = 0;
val = strtol(nptr, &endptr, base);
if (val == LONG_MAX)
fputs("value returned is LONG_MAX\n", stderr);
else if (val == LONG_MIN)
fputs("value returned is LONG_MIN\n", stderr);
else
fprintf(stderr, "value returned is %ld\n", val);
if (errno != 0)
perror("Error in input");
if (endptr == nptr)
fputs("string does not start with whitespace followed by number\n", stderr);
else if (*endptr != '\0')
fprintf(stderr, "more characters in string after number \"%s\"\n", endptr);
else fputs("no characters in string after number\n", stderr);
fputc('\n', stderr);
}
int main(void)
{
char *str1 = " 0034aaa"; // may or may not be a single number
char *str2 = "fffffffffffffffff"; // won't fit in 64 bit long
checknum(str1, 16); // 'a' is a digit in base 16
checknum(str1, 10); // 'a' is not a digit in base 10
checknum(str2, 16); // too big
checknum(str2, 10); // no number in string in base 10
}
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-11 09:26 -0800 |
| Message-ID | <40e9bbbe-3307-4850-bda8-39c40da1e0d1@googlegroups.com> |
| In reply to | #103683 |
W dniu sobota, 11 lutego 2017 18:10:57 UTC+1 użytkownik Joe Pfeiffer napisał:
> fir <profesor.fir@gmail.com> writes:
> >
> > well im not sure, could you tell maybe how i can check if
> > there was an out of range, or non numer , and number but with 'dirty'
> > input provided?
>
> Seriously? OK.
>
> #include <errno.h>
> #include <limits.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> void checknum(const char *nptr, int base)
> {
> long int val;
> char *endptr;
>
> fprintf(stderr, "testing string \"%s\" in base %d\n", nptr, base);
> errno = 0;
> val = strtol(nptr, &endptr, base);
>
> if (val == LONG_MAX)
> fputs("value returned is LONG_MAX\n", stderr);
> else if (val == LONG_MIN)
> fputs("value returned is LONG_MIN\n", stderr);
> else
> fprintf(stderr, "value returned is %ld\n", val);
>
> if (errno != 0)
> perror("Error in input");
>
> if (endptr == nptr)
> fputs("string does not start with whitespace followed by number\n", stderr);
> else if (*endptr != '\0')
> fprintf(stderr, "more characters in string after number \"%s\"\n", endptr);
> else fputs("no characters in string after number\n", stderr);
>
> fputc('\n', stderr);
>
> }
>
> int main(void)
> {
> char *str1 = " 0034aaa"; // may or may not be a single number
> char *str2 = "fffffffffffffffff"; // won't fit in 64 bit long
>
> checknum(str1, 16); // 'a' is a digit in base 16
> checknum(str1, 10); // 'a' is not a digit in base 10
> checknum(str2, 16); // too big
> checknum(str2, 10); // no number in string in base 10
> }
but could you maybe put
fprintf("err - no number");
fprintf("err - out of range");
fprintf("ok - pure number ");
fprintf("ok - pure number with whitespaces ");
fprintf("ok - dirty number ");
in adaquate places ?
(im working on some other things and cannot
switch much easily)
can you tell if if i get some like xxx before number
(like "xxx 100") it will also work (return value) or not
(i understand if i put xx after a number ("100xxx") it will work? (i both this cases xx10 znad 10xx understand as "dirty number") those with whitespaces before and after
i name "slightly dirty number" and just number i call "pure number")
if this allow to distinguish those cases its ok though
way of reporting it still seem a bit 'nonstrightforward',
tnx for answer
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2017-02-11 15:36 -0700 |
| Message-ID | <1bd1eo5p87.fsf@pfeifferfamily.net> |
| In reply to | #103684 |
fir <profesor.fir@gmail.com> writes:
>
> but could you maybe put
> fprintf("err - no number");
> fprintf("err - out of range");
> fprintf("ok - pure number ");
> fprintf("ok - pure number with whitespaces ");
> fprintf("ok - dirty number ");
>
> in adaquate places ?
> (im working on some other things and cannot
> switch much easily)
>
> can you tell if if i get some like xxx before number
> (like "xxx 100") it will also work (return value) or not
> (i understand if i put xx after a number ("100xxx") it will work? (i
> both this cases xx10 znad 10xx understand as "dirty number") those
> with whitespaces before and after
> i name "slightly dirty number" and just number i call "pure number")
>
>
>
> if this allow to distinguish those cases its ok though
> way of reporting it still seem a bit 'nonstrightforward',
> tnx for answer
The code isn't that complicated, and demonstrates how the results of
strtol() can be checked as you wanted (note that Ben spotted a slight
error, which is corrected in my followup to him). You can read it.
--
"Erwin, have you seen the cat?" -- Mrs. Shrödinger
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 00:18 -0800 |
| Message-ID | <b863e108-98c2-4f21-9945-22794eb454ce@googlegroups.com> |
| In reply to | #103700 |
W dniu sobota, 11 lutego 2017 23:37:07 UTC+1 użytkownik Joe Pfeiffer napisał:
> fir <profesor.fir@gmail.com> writes:
> >
> > but could you maybe put
> > fprintf("err - no number");
> > fprintf("err - out of range");
> > fprintf("ok - pure number ");
> > fprintf("ok - pure number with whitespaces ");
> > fprintf("ok - dirty number ");
> >
> > in adaquate places ?
> > (im working on some other things and cannot
> > switch much easily)
> >
> > can you tell if if i get some like xxx before number
> > (like "xxx 100") it will also work (return value) or not
> > (i understand if i put xx after a number ("100xxx") it will work? (i
> > both this cases xx10 znad 10xx understand as "dirty number") those
> > with whitespaces before and after
> > i name "slightly dirty number" and just number i call "pure number")
> >
> >
> >
> > if this allow to distinguish those cases its ok though
> > way of reporting it still seem a bit 'nonstrightforward',
> > tnx for answer
>
> The code isn't that complicated, and demonstrates how the results of
> strtol() can be checked as you wanted (note that Ben spotted a slight
> error, which is corrected in my followup to him). You can read it.
i know but you could also put the messages,
it would be faster
im coding other things and if im deeply involved
in X i got weird effect i cant switch to Y (without
some harm for X ) so if i read it i will read it
only after i end what i do
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 00:31 -0800 |
| Message-ID | <72094b29-94cc-4187-88ea-c9b59341d1db@googlegroups.com> |
| In reply to | #103717 |
W dniu niedziela, 12 lutego 2017 09:19:15 UTC+1 użytkownik fir napisał:
> W dniu sobota, 11 lutego 2017 23:37:07 UTC+1 użytkownik Joe Pfeiffer napisał:
> > fir <profesor.fir@gmail.com> writes:
> > >
> > > but could you maybe put
> > > fprintf("err - no number");
> > > fprintf("err - out of range");
> > > fprintf("ok - pure number ");
> > > fprintf("ok - pure number with whitespaces ");
> > > fprintf("ok - dirty number ");
> > >
> > > in adaquate places ?
> > > (im working on some other things and cannot
> > > switch much easily)
> > >
> > > can you tell if if i get some like xxx before number
> > > (like "xxx 100") it will also work (return value) or not
> > > (i understand if i put xx after a number ("100xxx") it will work? (i
> > > both this cases xx10 znad 10xx understand as "dirty number") those
> > > with whitespaces before and after
> > > i name "slightly dirty number" and just number i call "pure number")
> > >
> > >
> > >
> > > if this allow to distinguish those cases its ok though
> > > way of reporting it still seem a bit 'nonstrightforward',
> > > tnx for answer
> >
> > The code isn't that complicated, and demonstrates how the results of
> > strtol() can be checked as you wanted (note that Ben spotted a slight
> > error, which is corrected in my followup to him). You can read it.
>
> i know but you could also put the messages,
> it would be faster
>
> im coding other things and if im deeply involved
> in X i got weird effect i cant switch to Y (without
> some harm for X ) so if i read it i will read it
> only after i end what i do
i dont think this is uncommon..
besides as i decripted how good test should look like
(by providing those 5 messages) you could do the tests
and work is 50/50 (and both users end up with better understanding here)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 03:47 -0800 |
| Message-ID | <5a7a3a8f-7dd3-4f32-b676-d46b76ed05b9@googlegroups.com> |
| In reply to | #103718 |
W dniu niedziela, 12 lutego 2017 09:31:50 UTC+1 użytkownik fir napisał:
> W dniu niedziela, 12 lutego 2017 09:19:15 UTC+1 użytkownik fir napisał:
> > W dniu sobota, 11 lutego 2017 23:37:07 UTC+1 użytkownik Joe Pfeiffer napisał:
> > > fir <profesor.fir@gmail.com> writes:
> > > >
> > > > but could you maybe put
> > > > fprintf("err - no number");
> > > > fprintf("err - out of range");
> > > > fprintf("ok - pure number ");
> > > > fprintf("ok - pure number with whitespaces ");
> > > > fprintf("ok - dirty number ");
> > > >
> > > > in adaquate places ?
> > > > (im working on some other things and cannot
> > > > switch much easily)
> > > >
> > > > can you tell if if i get some like xxx before number
> > > > (like "xxx 100") it will also work (return value) or not
> > > > (i understand if i put xx after a number ("100xxx") it will work? (i
> > > > both this cases xx10 znad 10xx understand as "dirty number") those
> > > > with whitespaces before and after
> > > > i name "slightly dirty number" and just number i call "pure number")
> > > >
> > > >
> > > >
> > > > if this allow to distinguish those cases its ok though
> > > > way of reporting it still seem a bit 'nonstrightforward',
> > > > tnx for answer
> > >
> > > The code isn't that complicated, and demonstrates how the results of
> > > strtol() can be checked as you wanted (note that Ben spotted a slight
> > > error, which is corrected in my followup to him). You can read it.
> >
> > i know but you could also put the messages,
> > it would be faster
> >
> > im coding other things and if im deeply involved
> > in X i got weird effect i cant switch to Y (without
> > some harm for X ) so if i read it i will read it
> > only after i end what i do
>
> i dont think this is uncommon..
> besides as i decripted how good test should look like
> (by providing those 5 messages) you could do the tests
> and work is 50/50 (and both users end up with better understanding here)
ok i broke work on other projects temporarily, execyted your test for clarity
btw you used 4 different ways to flush communicates to stderr o_O i only use printfs normally to console
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
void checknum(const char *str)
{
long int val;
char *endptr= NULL;
fprintf(stderr, ">>> testing string \"%s\" \n", str);
errno = 0;
val = strtol(str, &endptr, 10);
if (errno != 0) perror(" Error in input\n");
if (val == LONG_MAX) fputs(" value returned is LONG_MAX\n", stderr);
else if (val == LONG_MIN) fputs(" value returned is LONG_MIN\n", stderr);
else fprintf(stderr, " value returned is %ld\n", val);
if (endptr == str) fputs(" endptr == str \n", stderr);
else if (*endptr != '\0') fputs(" *endptr != 0 \n", stderr);
else if (*endptr == '\0') fputs(" *endptr == 0 \n", stderr);
else fputs(" other case endptr \n", stderr);
fputc('\n', stderr);
}
int main(void)
{
checknum("34");
checknum("00034");
checknum(" 34");
checknum("34 ");
checknum(" 34 ");
checknum(" 0034");
checknum("ss34");
checknum("34ss");
checknum("ss34ss");
checknum("ss 34ss");
checknum("ss34 ss");
checknum("ss 34 ss");
}
the output is
>>> testing string "34"
value returned is 34
*endptr == 0
>>> testing string "00034"
value returned is 34
*endptr == 0
>>> testing string " 34"
value returned is 34
*endptr == 0
>>> testing string "34 "
value returned is 34
*endptr != 0
>>> testing string " 34 "
value returned is 34
*endptr != 0
>>> testing string " 0034"
value returned is 34
*endptr == 0
>>> testing string "ss34"
value returned is 0
endptr == str
>>> testing string "34ss"
value returned is 34
*endptr != 0
>>> testing string "ss34ss"
value returned is 0
endptr == str
>>> testing string "ss 34ss"
value returned is 0
endptr == str
>>> testing string "ss34 ss"
value returned is 0
endptr == str
>>> testing string "ss 34 ss"
value returned is 0
endptr == str
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 04:22 -0800 |
| Message-ID | <662d4a14-8712-44a0-bc44-bda2f016861b@googlegroups.com> |
| In reply to | #103720 |
W dniu niedziela, 12 lutego 2017 12:47:37 UTC+1 użytkownik fir napisał:
> W dniu niedziela, 12 lutego 2017 09:31:50 UTC+1 użytkownik fir napisał:
> > W dniu niedziela, 12 lutego 2017 09:19:15 UTC+1 użytkownik fir napisał:
> > > W dniu sobota, 11 lutego 2017 23:37:07 UTC+1 użytkownik Joe Pfeiffer napisał:
> > > > fir <profesor.fir@gmail.com> writes:
> > > > >
> > > > > but could you maybe put
> > > > > fprintf("err - no number");
> > > > > fprintf("err - out of range");
> > > > > fprintf("ok - pure number ");
> > > > > fprintf("ok - pure number with whitespaces ");
> > > > > fprintf("ok - dirty number ");
> > > > >
> > > > > in adaquate places ?
> > > > > (im working on some other things and cannot
> > > > > switch much easily)
> > > > >
> > > > > can you tell if if i get some like xxx before number
> > > > > (like "xxx 100") it will also work (return value) or not
> > > > > (i understand if i put xx after a number ("100xxx") it will work? (i
> > > > > both this cases xx10 znad 10xx understand as "dirty number") those
> > > > > with whitespaces before and after
> > > > > i name "slightly dirty number" and just number i call "pure number")
> > > > >
> > > > >
> > > > >
> > > > > if this allow to distinguish those cases its ok though
> > > > > way of reporting it still seem a bit 'nonstrightforward',
> > > > > tnx for answer
> > > >
> > > > The code isn't that complicated, and demonstrates how the results of
> > > > strtol() can be checked as you wanted (note that Ben spotted a slight
> > > > error, which is corrected in my followup to him). You can read it.
> > >
> > > i know but you could also put the messages,
> > > it would be faster
> > >
> > > im coding other things and if im deeply involved
> > > in X i got weird effect i cant switch to Y (without
> > > some harm for X ) so if i read it i will read it
> > > only after i end what i do
> >
> > i dont think this is uncommon..
> > besides as i decripted how good test should look like
> > (by providing those 5 messages) you could do the tests
> > and work is 50/50 (and both users end up with better understanding here)
>
> ok i broke work on other projects temporarily, execyted your test for clarity
>
> btw you used 4 different ways to flush communicates to stderr o_O i only use printfs normally to console
>
> #include <errno.h>
> #include <limits.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> void checknum(const char *str)
> {
>
> long int val;
> char *endptr= NULL;
>
> fprintf(stderr, ">>> testing string \"%s\" \n", str);
>
> errno = 0;
>
> val = strtol(str, &endptr, 10);
>
> if (errno != 0) perror(" Error in input\n");
>
> if (val == LONG_MAX) fputs(" value returned is LONG_MAX\n", stderr);
> else if (val == LONG_MIN) fputs(" value returned is LONG_MIN\n", stderr);
> else fprintf(stderr, " value returned is %ld\n", val);
>
> if (endptr == str) fputs(" endptr == str \n", stderr);
> else if (*endptr != '\0') fputs(" *endptr != 0 \n", stderr);
> else if (*endptr == '\0') fputs(" *endptr == 0 \n", stderr);
> else fputs(" other case endptr \n", stderr);
>
> fputc('\n', stderr);
>
> }
>
> int main(void)
> {
> checknum("34");
> checknum("00034");
> checknum(" 34");
> checknum("34 ");
> checknum(" 34 ");
> checknum(" 0034");
> checknum("ss34");
> checknum("34ss");
> checknum("ss34ss");
> checknum("ss 34ss");
> checknum("ss34 ss");
> checknum("ss 34 ss");
>
>
> }
>
> the output is
>
>
> >>> testing string "34"
> value returned is 34
> *endptr == 0
>
> >>> testing string "00034"
> value returned is 34
> *endptr == 0
>
> >>> testing string " 34"
> value returned is 34
> *endptr == 0
>
> >>> testing string "34 "
> value returned is 34
> *endptr != 0
>
> >>> testing string " 34 "
> value returned is 34
> *endptr != 0
>
> >>> testing string " 0034"
> value returned is 34
> *endptr == 0
>
> >>> testing string "ss34"
> value returned is 0
> endptr == str
>
> >>> testing string "34ss"
> value returned is 34
> *endptr != 0
>
> >>> testing string "ss34ss"
> value returned is 0
> endptr == str
>
> >>> testing string "ss 34ss"
> value returned is 0
> endptr == str
>
> >>> testing string "ss34 ss"
> value returned is 0
> endptr == str
>
> >>> testing string "ss 34 ss"
> value returned is 0
> endptr == str
it shows how it works - is able to skip whitespaces
but breaks on leading nonwhitespaces (non breaks on
trailing nonwhitespaces)
im not sure if this is good design - maybe the one tht would not break at all only provides info ("dirty", "left dirty" "right dirty" "two side dirty" "slight dirty" "left slightdirty"... etc ) would be better - though it seems
not such bad, it could be used to find sequence of numbers though those trailing would have to be skipped in hand (with other function) if it would not break on it maybe one functions would suffice so such function would be more general
other interesting is it not sets errno at all,
(i forget to check "" "ZZ" cases, wil check it now,
>>> testing string ""
value returned is 0
endptr == str
>>> testing string " "
value returned is 0
endptr == str
>>> testing string "zz"
value returned is 0
endptr == str
also not modifies errno (do i have error in test?) - if so why is this errno mentioned at all ?
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 06:08 -0800 |
| Message-ID | <399014bc-5273-48fc-b7d4-3d3494bbb396@googlegroups.com> |
| In reply to | #103721 |
for example how it would look like a code
needed to mine (dig) all integers form given
string like
char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; "
i will consider some simple version that will come to my mind ->
if such function will take pointer to "current char" in string it could move it to another char after digged int
(if no int digged it could set it to last char (it is string trailing null))
char* p = txt;
for(;;)
{
int value = dig(&p);
if(*p!=0) add(value); //adds to array
else
break;
}
this is simpler (dig would need not only skip
whitespaces but all nonnumbers) though not perfect as
it returns value even if fails to dig
also maybe other form would be more loop friendly
char* p = txt;
int value;
while(dig(&p, &value)) add(value);
(this in turn is more loop friendly but possibly
a bit uglier (?)
in improved c it could be designed in yet other things
but this above probably should work if they would redesign strtol slightly different (or if to write a wrapper on it)
(wonder if i should implement it in icl and how)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 06:13 -0800 |
| Message-ID | <b3c6f844-8629-4f74-9871-a0b6f3c15842@googlegroups.com> |
| In reply to | #103724 |
W dniu niedziela, 12 lutego 2017 15:08:25 UTC+1 użytkownik fir napisał:
> for example how it would look like a code
> needed to mine (dig) all integers form given
> string like
>
> char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; "
>
> i will consider some simple version that will come to my mind ->
>
> if such function will take pointer to "current char" in string it could move it to another char after digged int
> (if no int digged it could set it to last char (it is string trailing null))
>
> char* p = txt;
>
> for(;;)
> {
> int value = dig(&p);
>
> if(*p!=0) add(value); //adds to array
> else
> break;
> }
>
> this is simpler (dig would need not only skip
> whitespaces but all nonnumbers) though not perfect as
> it returns value even if fails to dig
> also maybe other form would be more loop friendly
>
> char* p = txt;
> int value;
>
> while(dig(&p, &value)) add(value);
>
> (this in turn is more loop friendly but possibly
> a bit uglier (?)
>
> in improved c it could be designed in yet other things
> but this above probably should work if they would redesign strtol slightly different (or if to write a wrapper on it)
>
> (wonder if i should implement it in icl and how)
well, first can be also write shorter so maybe first is better
char* p = txt;
while(*p) add(dig(&p));
or
for( char* p = txt; *p ; add(dig(&p)) )
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 07:31 -0800 |
| Message-ID | <07531319-297d-4377-98b1-404d2f678cc1@googlegroups.com> |
| In reply to | #103725 |
W dniu niedziela, 12 lutego 2017 15:14:04 UTC+1 użytkownik fir napisał:
> W dniu niedziela, 12 lutego 2017 15:08:25 UTC+1 użytkownik fir napisał:
> > for example how it would look like a code
> > needed to mine (dig) all integers form given
> > string like
> >
> > char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; "
> >
> > i will consider some simple version that will come to my mind ->
> >
> > if such function will take pointer to "current char" in string it could move it to another char after digged int
> > (if no int digged it could set it to last char (it is string trailing null))
> >
> > char* p = txt;
> >
> > for(;;)
> > {
> > int value = dig(&p);
> >
> > if(*p!=0) add(value); //adds to array
> > else
> > break;
> > }
> >
> > this is simpler (dig would need not only skip
> > whitespaces but all nonnumbers) though not perfect as
> > it returns value even if fails to dig
> > also maybe other form would be more loop friendly
> >
> > char* p = txt;
> > int value;
> >
> > while(dig(&p, &value)) add(value);
> >
> > (this in turn is more loop friendly but possibly
> > a bit uglier (?)
> >
> > in improved c it could be designed in yet other things
> > but this above probably should work if they would redesign strtol slightly different (or if to write a wrapper on it)
> >
> > (wonder if i should implement it in icl and how)
>
> well, first can be also write shorter so maybe first is better
>
> char* p = txt;
>
> while(*p) add(dig(&p));
>
> or
>
> for( char* p = txt; *p ; add(dig(&p)) )
ps i was curious how it would dig that string in present state of thing and it would be like that
inline int IsDigit(char p)
{
if(p>='0' && p<='9') return 1;
return 0;
}
void SkipCharsTillNumber(char** p)
{
for(;;)
{
if( **p==0 ) break;
if(IsDigit(**p)) break;
if((**p)=='-' && IsDigit( *((*p)+1) ) ) break;
printf("\n skipped char: %c", **p);
(*p)++;
}
}
void AddInt(int val)
{
printf("\n int digged: %d", val);
}
int main(void)
{
char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; ";
char* p = txt;
for(;;)
{
SkipCharsTillNumber(&p);
if(*p==0) break;
char* s = p;
int val = strtol(s, &p, 10);
AddInt(val);
}
}
the output is
skipped char: s
skipped char: o
skipped char: m
skipped char: e
skipped char: :
skipped char:
int digged: 100
skipped char: ,
skipped char:
int digged: 200
skipped char: ,
skipped char:
int digged: 300
skipped char: ;
skipped char:
skipped char: t
skipped char: h
skipped char: e
skipped char: n
skipped char: :
skipped char:
int digged: 3
skipped char: ,
skipped char:
int digged: 4
skipped char: ,
skipped char:
int digged: 5
skipped char: ,
skipped char:
int digged: 6
skipped char: ,
skipped char:
int digged: 7
skipped char: ,
skipped char:
int digged: 8
skipped char: ,
skipped char:
int digged: 9
skipped char: ;
skipped char:
skipped char: a
skipped char: n
skipped char: d
skipped char:
skipped char: t
skipped char: h
skipped char: e
skipped char: n
skipped char: :
skipped char:
int digged: -10
skipped char: ,
skipped char:
int digged: -20
skipped char: ,
skipped char:
int digged: -30
skipped char: ;
skipped char:
i noticed yet that strtol break on "- 10" where the space is between - and digits (im not sure if this is correct behaviour)
i could yet wrap this code above in "int DigInt(char**ptr)" function and maybe use it sometimes
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 08:02 -0800 |
| Message-ID | <32789f8d-ac0d-4da3-93bb-b4c741a4872d@googlegroups.com> |
| In reply to | #103730 |
i tested a wrapper
inline int IsDigit(char p)
{
if(p>='0' && p<='9') return 1;
return 0;
}
void SkipCharsTillNumber(char** p)
{
for(;;)
{
if( **p==0 ) break;
if(IsDigit(**p)) break;
if((**p)=='-' && IsDigit( *((*p)+1) ) ) break;
printf("\n skipped char: %c", **p);
(*p)++;
}
}
int DigInt(char**p)
{
for(;;)
{
SkipCharsTillNumber(p);
if(**p==0) break;
char* s = *p;
int val = strtol(s, p, 10);
return val;
}
return 666666666;
}
but it showed to be a problem, if dig is called on the tail of the string where no number is present tge dig must return something, so in such syntax
int main(void)
{
char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
char* p = txt;
while(*p) AddInt(DigInt(&p));
}
the result is wrong:
skipped char: s
skipped char: o
skipped char: m
skipped char: e
skipped char: :
skipped char:
int digged: 100
skipped char: ,
skipped char:
int digged: 200
skipped char: ,
skipped char:
int digged: 300
skipped char: ;
skipped char:
skipped char: t
skipped char: h
skipped char: e
skipped char: n
skipped char: :
skipped char:
int digged: 3
skipped char: ,
skipped char:
int digged: 4
skipped char: ,
skipped char:
int digged: 5
skipped char: ,
skipped char:
int digged: 6
skipped char: ,
skipped char:
int digged: 7
skipped char: ,
skipped char:
int digged: 8
skipped char: ,
skipped char:
int digged: 9
skipped char: ;
skipped char:
skipped char: a
skipped char: n
skipped char: d
skipped char:
skipped char: t
skipped char: h
skipped char: e
skipped char: n
skipped char: :
skipped char:
int digged: -10
skipped char: ,
skipped char:
int digged: -20
skipped char: ,
skipped char:
int digged: -30
skipped char: ;
skipped char:
int digged: 1
skipped char:
int digged: 2
skipped char:
int digged: 3
skipped char:
int digged: 4
skipped char:
int digged: 5
skipped char:
int digged: 6
skipped char:
int digged: 666666666
same with "for(char* p = txt; *p; AddInt(DigInt(&p)));" btw, so longer here must be used
int main(void)
{
char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
char* p = txt;
while(*p)
{
int v = DigInt(&p);
if(*p) AddInt(v);
}
}
this work ok, (though not tested very much, maybe some bugs hidden)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 08:08 -0800 |
| Message-ID | <0760bfde-f09b-4edd-b6f6-25b6a3d655b7@googlegroups.com> |
| In reply to | #103731 |
W dniu niedziela, 12 lutego 2017 17:02:33 UTC+1 użytkownik fir napisał:
> i tested a wrapper
>
>
>
> inline int IsDigit(char p)
> {
> if(p>='0' && p<='9') return 1;
> return 0;
> }
>
> void SkipCharsTillNumber(char** p)
> {
> for(;;)
> {
> if( **p==0 ) break;
> if(IsDigit(**p)) break;
>
> if((**p)=='-' && IsDigit( *((*p)+1) ) ) break;
>
> printf("\n skipped char: %c", **p);
>
> (*p)++;
> }
>
> }
>
>
>
> int DigInt(char**p)
> {
>
> for(;;)
> {
>
> SkipCharsTillNumber(p);
> if(**p==0) break;
>
> char* s = *p;
> int val = strtol(s, p, 10);
>
> return val;
> }
> return 666666666;
> }
>
> but it showed to be a problem, if dig is called on the tail of the string where no number is present tge dig must return something, so in such syntax
>
> int main(void)
> {
> char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
>
> char* p = txt;
>
> while(*p) AddInt(DigInt(&p));
> }
>
> the result is wrong:
>
>
> skipped char: s
> skipped char: o
> skipped char: m
> skipped char: e
> skipped char: :
> skipped char:
> int digged: 100
> skipped char: ,
> skipped char:
> int digged: 200
> skipped char: ,
> skipped char:
> int digged: 300
> skipped char: ;
> skipped char:
> skipped char: t
> skipped char: h
> skipped char: e
> skipped char: n
> skipped char: :
> skipped char:
> int digged: 3
> skipped char: ,
> skipped char:
> int digged: 4
> skipped char: ,
> skipped char:
> int digged: 5
> skipped char: ,
> skipped char:
> int digged: 6
> skipped char: ,
> skipped char:
> int digged: 7
> skipped char: ,
> skipped char:
> int digged: 8
> skipped char: ,
> skipped char:
> int digged: 9
> skipped char: ;
> skipped char:
> skipped char: a
> skipped char: n
> skipped char: d
> skipped char:
> skipped char: t
> skipped char: h
> skipped char: e
> skipped char: n
> skipped char: :
> skipped char:
> int digged: -10
> skipped char: ,
> skipped char:
> int digged: -20
> skipped char: ,
> skipped char:
> int digged: -30
> skipped char: ;
> skipped char:
> int digged: 1
> skipped char:
> int digged: 2
> skipped char:
> int digged: 3
> skipped char:
> int digged: 4
> skipped char:
> int digged: 5
> skipped char:
> int digged: 6
> skipped char:
> int digged: 666666666
>
> same with "for(char* p = txt; *p; AddInt(DigInt(&p)));" btw, so longer here must be used
>
> int main(void)
> {
> char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
>
> char* p = txt;
>
> while(*p)
> {
> int v = DigInt(&p);
>
> if(*p) AddInt(v);
> }
> }
>
> this work ok, (though not tested very much, maybe some bugs hidden)
there seems to be yet one problem i didnt noticed if number is last in the string the p also will be set to
talning zero (so no way to distinguish if that digged and int or no) so maybe i must revrite to this longer form
int digInt(char**p, int* value);
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 08:17 -0800 |
| Message-ID | <a1ca56f1-54c8-496d-88d9-cffb35ce3b61@googlegroups.com> |
| In reply to | #103732 |
W dniu niedziela, 12 lutego 2017 17:08:11 UTC+1 użytkownik fir napisał:
> W dniu niedziela, 12 lutego 2017 17:02:33 UTC+1 użytkownik fir napisał:
> > i tested a wrapper
> >
> >
> >
> > inline int IsDigit(char p)
> > {
> > if(p>='0' && p<='9') return 1;
> > return 0;
> > }
> >
> > void SkipCharsTillNumber(char** p)
> > {
> > for(;;)
> > {
> > if( **p==0 ) break;
> > if(IsDigit(**p)) break;
> >
> > if((**p)=='-' && IsDigit( *((*p)+1) ) ) break;
> >
> > printf("\n skipped char: %c", **p);
> >
> > (*p)++;
> > }
> >
> > }
> >
> >
> >
> > int DigInt(char**p)
> > {
> >
> > for(;;)
> > {
> >
> > SkipCharsTillNumber(p);
> > if(**p==0) break;
> >
> > char* s = *p;
> > int val = strtol(s, p, 10);
> >
> > return val;
> > }
> > return 666666666;
> > }
> >
> > but it showed to be a problem, if dig is called on the tail of the string where no number is present tge dig must return something, so in such syntax
> >
> > int main(void)
> > {
> > char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
> >
> > char* p = txt;
> >
> > while(*p) AddInt(DigInt(&p));
> > }
> >
> > the result is wrong:
> >
> >
> > skipped char: s
> > skipped char: o
> > skipped char: m
> > skipped char: e
> > skipped char: :
> > skipped char:
> > int digged: 100
> > skipped char: ,
> > skipped char:
> > int digged: 200
> > skipped char: ,
> > skipped char:
> > int digged: 300
> > skipped char: ;
> > skipped char:
> > skipped char: t
> > skipped char: h
> > skipped char: e
> > skipped char: n
> > skipped char: :
> > skipped char:
> > int digged: 3
> > skipped char: ,
> > skipped char:
> > int digged: 4
> > skipped char: ,
> > skipped char:
> > int digged: 5
> > skipped char: ,
> > skipped char:
> > int digged: 6
> > skipped char: ,
> > skipped char:
> > int digged: 7
> > skipped char: ,
> > skipped char:
> > int digged: 8
> > skipped char: ,
> > skipped char:
> > int digged: 9
> > skipped char: ;
> > skipped char:
> > skipped char: a
> > skipped char: n
> > skipped char: d
> > skipped char:
> > skipped char: t
> > skipped char: h
> > skipped char: e
> > skipped char: n
> > skipped char: :
> > skipped char:
> > int digged: -10
> > skipped char: ,
> > skipped char:
> > int digged: -20
> > skipped char: ,
> > skipped char:
> > int digged: -30
> > skipped char: ;
> > skipped char:
> > int digged: 1
> > skipped char:
> > int digged: 2
> > skipped char:
> > int digged: 3
> > skipped char:
> > int digged: 4
> > skipped char:
> > int digged: 5
> > skipped char:
> > int digged: 6
> > skipped char:
> > int digged: 666666666
> >
> > same with "for(char* p = txt; *p; AddInt(DigInt(&p)));" btw, so longer here must be used
> >
> > int main(void)
> > {
> > char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
> >
> > char* p = txt;
> >
> > while(*p)
> > {
> > int v = DigInt(&p);
> >
> > if(*p) AddInt(v);
> > }
> > }
> >
> > this work ok, (though not tested very much, maybe some bugs hidden)
>
> there seems to be yet one problem i didnt noticed if number is last in the string the p also will be set to
> talning zero (so no way to distinguish if that digged and int or no) so maybe i must revrite to this longer form
>
> int digInt(char**p, int* value);
ok this seem to work
void AddInt(int val)
{
printf("\n int digged: %d", val);
}
int main(void)
{
char* txt = "some: 100, 200, 300; then: 3, 4, 5, 6, 7, 8, 9; and then: -10, -20, -30; 1 2 3 4 5 6 ";
char* p = txt;
int value;
while(DigInt(&p, &value)) AddInt(value);
}
where
inline int IsDigit(char p)
{
if(p>='0' && p<='9') return 1;
return 0;
}
void SkipCharsTillNumber(char** p)
{
for(;;)
{
if( **p==0 ) break;
if(IsDigit(**p)) break;
if((**p)=='-' && IsDigit( *((*p)+1) ) ) break;
printf("\n skipped char: %c", **p);
(*p)++;
}
}
int DigInt(char**p, int* value)
{
for(;;)
{
SkipCharsTillNumber(p);
if(**p==0) break;
char* s = *p;
*value = strtol(s, p, 10);
return 1;
}
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-02-12 08:23 -0800 |
| Message-ID | <66862d4e-8abe-4199-b41f-4480c8267190@googlegroups.com> |
| In reply to | #103733 |
> int DigInt(char**p, int* value) as i said if they will make strtol this form it could be more general and easy to use maybe i will add it to my initial chunk library though still not quite sure if it is best 'interfacing' for such things
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.c
csiph-web