Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: code for 32/64 bit strtoul strtol strtoull strtoll |
| Date | 2015-03-29 12:15 -0700 |
| Organization | None to speak of |
| Message-ID | <87mw2vlinn.fsf@kst-u.example.com> (permalink) |
| References | <mf9605$7oq$1@speranza.aioe.org> <lj9gha5v5adefueiavvtbk5ap4207khp8b@4ax.com> <mf9bcd$nr5$1@speranza.aioe.org> |
jacob navia <jacob@jacob.remcomp.fr> writes:
> Le 29/03/2015 18:44, Barry Schwarz a écrit :
>> The if evaluates to true and 0 is correct return value but *endptr
>> still points to '0' when it should point to 'x'. Since 0x is not a
>> valid hex constant, the string "0x" should be treated the same as
>> "0a".
>>
>
> That would be the case if the standard was logical but it is
> explicitely stated that 0x should NOT advance the pointer.
>
> <quote>
> C99:
> 7 If the subject sequence is empty or does not have the expected form,
> no conversion is performed; the value of nptr is stored in the object
> pointed to by endptr, provided that endptr is not a null pointer.
> <end quote>
>
> In this case the sequence has not the expected form, i.e. an
> hexadecimal number
"0x" does not have the expected form. "0" does.
strtol("0x", endptr, 0) should return 0 and set *endptr to point to the
'x'. The expected form is not a hexadecimal number; it's a number (with
optional sign) that may or may not have a leading 0x.
You use the identifier _Strtoull, which is reserved. That's ok for code
that's part of the implementation, which this is intended to be. But
since the function is static, it's not necessary to use a reserved
identifier.
In your main program (enabled if TEST is #defined), you assign the
result of strtoll() to an unsigned long long object. When I run your
program with an argument of "-1", I get:
strtoull(-1):
18446744073709551615 2 errno = Success
strtoll(-1):
18446744073709551615 2 errno = Success
The functions work correctly for this case, but the result of
strtoll(-1) is displayed in a misleading form.
I get several warnings when I compile your program. The assignments
*endptr = input;
discard the "const" qualifier. The solution, in this case, is to add a
cast. That's unfortunate, but the strto*() functions, via endptr, can
give you a non-const pointer to const data. (I'm surprised your
compiler didn't warn about this.)
You use "%lld" to print the value end-s, which is of type ptrdiff_t.
The correct format is "%td". (Yes, I know it happens to work on your
system, but "%td" is both correct and portable.)
At the bottom of this article I've included a patch that corrects some
but not all of these problems. (It doesn't correct the "0x" and
"-9223372036854775808" problems.) It also shows the results of both
your functions and the implementation's standard functions.
After applying these changes, when I run your program (compiled with
-DTEST) with an argument of "0x" I get:
Strtoull(0x):
0 0 errno = Success
Strtoll(0x):
0 0 errno = Success
strtoull(0x):
0 1 errno = Success
strtoll(0x):
0 1 errno = Success
The endptr-s value is inconsistent.
With an argument of "-9223372036854775808" I get:
Strtoull(-9223372036854775808):
9223372036854775808 20 errno = Success
Strtoll(-9223372036854775808):
-9223372036854775808 20 errno = Numerical result out of range
strtoull(-9223372036854775808):
9223372036854775808 20 errno = Success
strtoll(-9223372036854775808):
-9223372036854775808 20 errno = Success
This is the ambiguous case we discussed before.
strtoll(-9223372036854775808) should, I think, return
-9223372036854775808 and set errno to 0. (glibc's version does this.)
Here's the patch:
diff --git navia-strto.c navia-strto.c
index 664e13c..4016ba0 100644
--- navia-strto.c
+++ navia-strto.c
@@ -14,3 +14,3 @@
-static unsigned long long _Strtoull(const char *input,
+static unsigned long long Strtoull_(const char *input,
char **endptr, int base, int Flags)
@@ -32,3 +32,3 @@ static unsigned long long _Strtoull(const char *input,
since we are going to reuse it */
- if (endptr) *endptr = input;
+ if (endptr) *endptr = (char*)input;
@@ -180,3 +180,3 @@ static unsigned long long _Strtoull(const char *input,
if (endptr != NULL && ReadSomething > 0) {
- *endptr = input;
+ *endptr = (char*)input;
}
@@ -200,3 +200,3 @@ unsigned long Strtol(const char *input, char **endptr, int base)
{
- return(_Strtoull(input, endptr, base, SIGNED32));
+ return(Strtoull_(input, endptr, base, SIGNED32));
}
@@ -208,3 +208,3 @@ long Strtoul(const char *input, char **endptr, int base)
{
- return(_Strtoull(input, endptr, base, UNSIGNED32));
+ return(Strtoull_(input, endptr, base, UNSIGNED32));
}
@@ -216,3 +216,3 @@ unsigned long long Strtoull(const char *input, char **endptr, int base)
{
- return(_Strtoull(input, endptr, base, UNSIGNED64));
+ return(Strtoull_(input, endptr, base, UNSIGNED64));
}
@@ -224,3 +224,3 @@ long long Strtoll(const char *input, char **endptr, int base)
{
- return(_Strtoull(input, endptr, base, SIGNED64));
+ return(Strtoull_(input, endptr, base, SIGNED64));
}
@@ -235,3 +235,3 @@ int main(int argc,char *argv[])
char *end;
- char *s = "-2147483647";
+ char *s;
int save_errno;
@@ -246,12 +246,29 @@ int main(int argc,char *argv[])
base = atoi(argv[2]);
- errno = 0;
- unsigned long long m = Strtoull(s,&end,0);
- save_errno = errno;
- printf("strtoull(%s):\n %llu %lld",argv[1],m,end-s);
- printf(" errno = %s\n",strerror(save_errno));
- errno = 0;
- m = Strtoll(s,&end,0);
- save_errno=errno;
- printf("strtoll(%s):\n %llu %lld",argv[1],m,end-s);
- printf(" errno = %s\n",strerror(save_errno));
+
+ {
+ errno = 0;
+ unsigned long long ull = Strtoull(s,&end,0);
+ save_errno = errno;
+ printf("Strtoull(%s):\n %llu %td",argv[1],ull,end-s);
+ printf(" errno = %s\n",strerror(save_errno));
+ errno = 0;
+ long long ll = Strtoll(s,&end,0);
+ save_errno=errno;
+ printf("Strtoll(%s):\n %lld %td",argv[1],ll,end-s);
+ printf(" errno = %s\n",strerror(save_errno));
+ }
+
+ {
+ errno = 0;
+ unsigned long long ull = strtoull(s,&end,0);
+ save_errno = errno;
+ printf("strtoull(%s):\n %llu %td",argv[1],ull,end-s);
+ printf(" errno = %s\n",strerror(save_errno));
+ errno = 0;
+ long long ll = strtoll(s,&end,0);
+ save_errno=errno;
+ printf("strtoll(%s):\n %lld %td",argv[1],ll,end-s);
+ printf(" errno = %s\n",strerror(save_errno));
+ }
+
}
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
code for 32/64 bit strtoul strtol strtoull strtoll jacob navia <jacob@jacob.remcomp.fr> - 2015-03-29 17:33 +0200
code for 32/64 bit strtoul strtol strtoull strtoll asetofsymbols@gmail.com - 2015-03-29 09:22 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Barry Schwarz <schwarzb@dqel.com> - 2015-03-29 15:12 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll asetofsymbols@gmail.com - 2015-03-29 23:07 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Barry Schwarz <schwarzb@dqel.com> - 2015-03-29 09:44 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll jacob navia <jacob@jacob.remcomp.fr> - 2015-03-29 19:05 +0200
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-29 12:15 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-30 02:01 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-29 19:48 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Barry Schwarz <schwarzb@dqel.com> - 2015-03-29 22:08 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-30 12:44 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Barry Schwarz <schwarzb@dqel.com> - 2015-03-30 09:04 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-30 21:00 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-30 08:24 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-30 07:21 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ian Collins <ian-news@hotmail.com> - 2015-03-30 18:02 +1300
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-03-30 08:03 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ian Collins <ian-news@hotmail.com> - 2015-03-30 20:33 +1300
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ian Collins <ian-news@hotmail.com> - 2015-03-30 20:35 +1300
Re: code for 32/64 bit strtoul strtol strtoull strtoll Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-30 07:27 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-30 09:08 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-30 09:29 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-03-30 15:16 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll jacob navia <jacob@jacob.remcomp.fr> - 2015-03-31 00:15 +0200
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-03-31 01:42 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-03-30 23:39 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-03-31 01:44 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-03-31 13:01 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-31 05:13 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-31 08:06 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-31 08:26 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-03-31 16:40 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-31 09:39 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Kaz Kylheku <kaz@kylheku.com> - 2015-03-31 18:22 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-03-31 19:36 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-31 12:11 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Kaz Kylheku <kaz@kylheku.com> - 2015-03-31 19:23 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-03-31 16:52 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-31 17:54 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll raltbos@xs4all.nl (Richard Bos) - 2015-03-31 18:51 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-03-31 20:05 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll James Kuyper <jameskuyper@verizon.net> - 2015-03-31 15:18 -0400
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-03-31 20:49 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-31 13:06 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll raltbos@xs4all.nl (Richard Bos) - 2015-04-06 13:03 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-04-06 14:27 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-04-06 17:01 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-06 11:25 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll James Kuyper <jameskuyper@verizon.net> - 2015-04-06 23:34 -0400
Re: code for 32/64 bit strtoul strtol strtoull strtoll glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-07 03:46 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll James Kuyper <jameskuyper@verizon.net> - 2015-04-08 11:05 -0400
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-08 08:31 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-31 19:54 +0000
Re: code for 32/64 bit strtoul strtol strtoull strtoll James Kuyper <jameskuyper@verizon.net> - 2015-03-31 16:55 -0400
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-03-31 22:20 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-31 14:36 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-31 14:55 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-03-31 17:00 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-04-01 01:24 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll James Kuyper <jameskuyper@verizon.net> - 2015-03-31 23:33 -0400
Re: code for 32/64 bit strtoul strtol strtoull strtoll Richard Heathfield <rjh@cpax.org.uk> - 2015-04-01 04:42 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-03-31 20:26 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Ian Collins <ian-news@hotmail.com> - 2015-04-01 10:33 +1300
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-04-01 04:33 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll Bartc <bc@freeuk.com> - 2015-04-01 12:43 +0100
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-04-02 01:02 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll asetofsymbols@gmail.com - 2015-04-01 23:57 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-04-03 00:29 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-02 09:01 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Keith Thompson <kst-u@mib.org> - 2015-04-02 14:30 -0700
Re: code for 32/64 bit strtoul strtol strtoull strtoll Robert Wessel <robertwessel2@yahoo.com> - 2015-04-03 00:39 -0500
Re: code for 32/64 bit strtoul strtol strtoull strtoll glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-01 15:55 +0000
csiph-web