diff options
author | Mattias Andrée <maandree@kth.se> | 2023-04-09 20:05:12 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-04-09 20:05:12 +0200 |
commit | bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2 (patch) | |
tree | 1bdaf0b894cf933f7d96951a516b037cde2f32c1 | |
parent | Improve makefile and fix a bug (diff) | |
download | libsimple-bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2.tar.gz libsimple-bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2.tar.bz2 libsimple-bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2.tar.xz |
Add tests and man pages
Signed-off-by: Mattias Andrée <maandree@kth.se>
49 files changed, 927 insertions, 53 deletions
diff --git a/libsimple/strtoint.h b/libsimple/strtoint.h index a342045..d7fc292 100644 --- a/libsimple/strtoint.h +++ b/libsimple/strtoint.h @@ -17,7 +17,7 @@ * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) signed char libsimple_strtohh(const char *restrict, char **restrict, int); #ifndef strtohh # define strtohh libsimple_strtohh @@ -40,7 +40,7 @@ signed char libsimple_strtohh(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) unsigned char libsimple_strtouhh(const char *restrict, char **restrict, int); #ifndef strtouhh # define strtouhh libsimple_strtouhh @@ -63,7 +63,7 @@ unsigned char libsimple_strtouhh(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) signed short int libsimple_strtoh(const char *restrict, char **restrict, int); #ifndef strtoh # define strtoh libsimple_strtoh @@ -86,7 +86,7 @@ signed short int libsimple_strtoh(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) unsigned short int libsimple_strtouh(const char *restrict, char **restrict, int); #ifndef strtouh # define strtouh libsimple_strtouh @@ -109,7 +109,7 @@ unsigned short int libsimple_strtouh(const char *restrict, char **restrict, int) * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) signed int libsimple_strtoi(const char *restrict, char **restrict, int); #ifndef strtoi # define strtoi libsimple_strtoi @@ -132,7 +132,7 @@ signed int libsimple_strtoi(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) unsigned int libsimple_strtou(const char *restrict, char **restrict, int); #ifndef strtou # define strtou libsimple_strtou @@ -155,7 +155,7 @@ unsigned int libsimple_strtou(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) inline ssize_t libsimple_strtoz(const char *restrict nptr__, char **restrict endptr__, int base__) { @@ -182,7 +182,7 @@ libsimple_strtoz(const char *restrict nptr__, char **restrict endptr__, int base * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) inline size_t libsimple_strtouz(const char *restrict nptr__, char **restrict endptr__, int base__) { @@ -209,7 +209,7 @@ libsimple_strtouz(const char *restrict nptr__, char **restrict endptr__, int bas * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) int_least8_t libsimple_strtoi8(const char *restrict, char **restrict, int); #ifndef strtoi8 # define strtoi8 libsimple_strtoi8 @@ -232,7 +232,7 @@ int_least8_t libsimple_strtoi8(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) uint_least8_t libsimple_strtou8(const char *restrict, char **restrict, int); #ifndef strtou8 # define strtou8 libsimple_strtou8 @@ -255,7 +255,7 @@ uint_least8_t libsimple_strtou8(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) int_least16_t libsimple_strtoi16(const char *restrict, char **restrict, int); #ifndef strtoi16 # define strtoi16 libsimple_strtoi16 @@ -278,7 +278,7 @@ int_least16_t libsimple_strtoi16(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) uint_least16_t libsimple_strtou16(const char *restrict, char **restrict, int); #ifndef strtou16 # define strtou16 libsimple_strtou16 @@ -301,7 +301,7 @@ uint_least16_t libsimple_strtou16(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) int_least32_t libsimple_strtoi32(const char *restrict, char **restrict, int); #ifndef strtoi32 # define strtoi32 libsimple_strtoi32 @@ -324,7 +324,7 @@ int_least32_t libsimple_strtoi32(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) uint_least32_t libsimple_strtou32(const char *restrict, char **restrict, int); #ifndef strtou32 # define strtou32 libsimple_strtou32 @@ -347,7 +347,7 @@ uint_least32_t libsimple_strtou32(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) int_least64_t libsimple_strtoi64(const char *restrict, char **restrict, int); #ifndef strtoi64 # define strtoi64 libsimple_strtoi64 @@ -370,7 +370,7 @@ int_least64_t libsimple_strtoi64(const char *restrict, char **restrict, int); * closed value that can be represented; 0 on * failure (unless `errno` not set to `ERANGE`) */ -LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(2), __warn_unused_result__))) +LIBSIMPLE_GCC_ONLY__(__attribute__((__nonnull__(1), __warn_unused_result__))) uint_least64_t libsimple_strtou64(const char *restrict, char **restrict, int); #ifndef strtou64 # define strtou64 libsimple_strtou64 diff --git a/man3/libsimple_strtoh.3 b/man3/libsimple_strtoh.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtoh.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/libsimple_strtohh.3 b/man3/libsimple_strtohh.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtohh.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/libsimple_strtoi.3 b/man3/libsimple_strtoi.3 new file mode 100644 index 0000000..3def194 --- /dev/null +++ b/man3/libsimple_strtoi.3 @@ -0,0 +1,202 @@ +.TH LIBSIMPLE_STRTOI 3 libsimple +.SH NAME +libsimple_strto{i,h,hh,z,i8,i16,i32,i64} \- parse a string as a signed integer + +.SH SYNOPSIS +.nf +#include <libsimple.h> + +signed int libsimple_strtoi(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +signed short int libsimple_strtoh(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +signed char libsimple_strtohh(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +inline ssize_t libsimple_strtoz(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +int_least8_t libsimple_strtoi8(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +int_least16_t libsimple_strtoi16(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +int_least32_t libsimple_strtoi32(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +int_least64_t libsimple_strtoi64(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); + +#ifndef strtoi +# define strtoi libsimple_strtoi +#endif +#ifndef strtoh +# define strtoh libsimple_strtoh +#endif +#ifndef strtohh +# define strtohh libsimple_strtohh +#endif +#ifndef strtoz +# define strtoz libsimple_strtoz +#endif +#ifndef strtoi8 +# define strtoi8 libsimple_strtoi8 +#endif +#ifndef strtoi16 +# define strtoi16 libsimple_strtoi16 +#endif +#ifndef strtoi32 +# define strtoi32 libsimple_strtoi32 +#endif +#ifndef strtoi64 +# define strtoi64 libsimple_strtoi64 +#endif +.fi +.PP +Link with +.IR \-lsimple . + +.SH DESCRIPTION +The +.BR libsimple_strtoi () +function is a version of the +.BR strtol (3) +function, that returns a +.BR "signed int" . +.PP +The +.BR libsimple_strtoh () +function is a version of the +.BR strtol (3) +function, that returns a +.BR "signed short int" . +.PP +The +.BR libsimple_strtohh () +function is a version of the +.BR strtol (3) +function, that returns a +.BR "signed char" . +.PP +The +.BR libsimple_strtoz () +function is a version of the +.BR strtol (3) +function, that returns a +.BR ssize_t . +.PP +The +.BR libsimple_strtoi8 () +function is a version of the +.BR strtol (3) +function, that returns an +.B int_least8_t +restricted to the range of +.BR int8_t . +.PP +The +.BR libsimple_strtoi16 () +function is a version of the +.BR strtol (3) +function, that returns an +.B int_least16_t +restricted to the range of +.BR int16_t . +.PP +The +.BR libsimple_strtoi32 () +function is a version of the +.BR strtol (3) +function, that returns an +.B int_least32_t +restricted to the range of +.BR int32_t . +.PP +The +.BR libsimple_strtoi64 () +function is a version of the +.BR strtol (3) +function, that returns an +.B int_least64_t +restricted to the range of +.BR int64_t . + +.SH RETURN VALUE +See +.BR strtol (3). + +.SH ERRORS +See +.BR strtol (3). + +.SH ATTRIBUTES +For an explanation of the terms used in this section, see +.BR attributes (7). +.TS +allbox; +lb lb lb +l l l. +Interface Attribute Value +T{ +.BR libsimple_strtoi (), +.br +.BR libsimple_strtoh (), +.br +.BR libsimple_strtohh (), +.br +.BR libsimple_strtoz (), +.br +.BR libsimple_strtoi8 (), +.br +.BR libsimple_strtoi16 (), +.br +.BR libsimple_strtoi32 (), +.br +.BR libsimple_strtoi64 () +T} Thread safety MT-Safe +T{ +.BR libsimple_strtoi (), +.br +.BR libsimple_strtoh (), +.br +.BR libsimple_strtohh (), +.br +.BR libsimple_strtoz (), +.br +.BR libsimple_strtoi8 (), +.br +.BR libsimple_strtoi16 (), +.br +.BR libsimple_strtoi32 (), +.br +.BR libsimple_strtoi64 () +T} Async-signal safety AS-Safe +T{ +.BR libsimple_strtoi (), +.br +.BR libsimple_strtoh (), +.br +.BR libsimple_strtohh (), +.br +.BR libsimple_strtoz (), +.br +.BR libsimple_strtoi8 (), +.br +.BR libsimple_strtoi16 (), +.br +.BR libsimple_strtoi32 (), +.br +.BR libsimple_strtoi64 () +T} Async-cancel safety AC-Safe +.TE + +.SH EXAMPLES +None. + +.SH APPLICATION USAGE +None. + +.SH RATIONALE +None. + +.SH FUTURE DIRECTIONS +None. + +.SH NOTES +None. + +.SH BUGS +None. + +.SH SEE ALSO +.BR libsimple_strtou (3), +.BR strtol (3), +.BR strtoll (3) diff --git a/man3/libsimple_strtoi16.3 b/man3/libsimple_strtoi16.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtoi16.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/libsimple_strtoi32.3 b/man3/libsimple_strtoi32.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtoi32.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/libsimple_strtoi64.3 b/man3/libsimple_strtoi64.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtoi64.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/libsimple_strtoi8.3 b/man3/libsimple_strtoi8.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtoi8.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/libsimple_strtou.3 b/man3/libsimple_strtou.3 new file mode 100644 index 0000000..4e4bb33 --- /dev/null +++ b/man3/libsimple_strtou.3 @@ -0,0 +1,202 @@ +.TH LIBSIMPLE_STRTOU 3 libsimple +.SH NAME +libsimple_strto{u,uh,uhh,uz,u8,u16,u32,u64} \- parse a string as an unsigned integer + +.SH SYNOPSIS +.nf +#include <libsimple.h> + +unsigned int libsimple_strtou(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +unsigned short int libsimple_strtouh(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +unsigned char libsimple_strtouhh(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +inline size_t libsimple_strtouz(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +uint_least8_t libsimple_strtou8(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +uint_least16_t libsimple_strtou16(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +uint_least32_t libsimple_strtou32(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); +uint_least64_t libsimple_strtou64(const char *\fInptr\fP, char **\fIend\fP, int \fIbase\fP); + +#ifndef strtou +# define strtou libsimple_strtou +#endif +#ifndef strtouh +# define strtouh libsimple_strtouh +#endif +#ifndef strtouhh +# define strtouhh libsimple_strtouhh +#endif +#ifndef strtouz +# define strtouz libsimple_strtouz +#endif +#ifndef strtou8 +# define strtou8 libsimple_strtou8 +#endif +#ifndef strtou16 +# define strtou16 libsimple_strtou16 +#endif +#ifndef strtou32 +# define strtou32 libsimple_strtou32 +#endif +#ifndef strtou64 +# define strtou64 libsimple_strtou64 +#endif +.fi +.PP +Link with +.IR \-lsimple . + +.SH DESCRIPTION +The +.BR libsimple_strtou () +function is a version of the +.BR strtoul (3) +function, that returns au +.BR "unsigned int" . +.PP +The +.BR libsimple_strtouh () +function is a version of the +.BR strtoul (3) +function, that returns au +.BR "unsigned short int" . +.PP +The +.BR libsimple_strtouhh () +function is a version of the +.BR strtoul (3) +function, that returns au +.BR "unsigned char" . +.PP +The +.BR libsimple_strtouz () +function is a version of the +.BR strtoul (3) +function, that returns a +.BR size_t . +.PP +The +.BR libsimple_strtou8 () +function is a version of the +.BR strtoul (3) +function, that returns a +.B uint_least8_t +restricted to the range of +.BR uint8_t . +.PP +The +.BR libsimple_strtou16 () +function is a version of the +.BR strtoul (3) +function, that returns a +.B uint_least16_t +restricted to the range of +.BR uint16_t . +.PP +The +.BR libsimple_strtou32 () +function is a version of the +.BR strtoul (3) +function, that returns a +.B uint_least32_t +restricted to the range of +.BR uint32_t . +.PP +The +.BR libsimple_strtou64 () +function is a version of the +.BR strtoul (3) +function, that returns a +.B uint_least64_t +restricted to the range of +.BR uint64_t . + +.SH RETURN VALUE +See +.BR strtoul (3). + +.SH ERRORS +See +.BR strtoul (3). + +.SH ATTRIBUTES +For an explanation of the terms used in this section, see +.BR attributes (7). +.TS +allbox; +lb lb lb +l l l. +Interface Attribute Value +T{ +.BR libsimple_strtou (), +.br +.BR libsimple_strtouh (), +.br +.BR libsimple_strtouhh (), +.br +.BR libsimple_strtouz (), +.br +.BR libsimple_strtou8 (), +.br +.BR libsimple_strtou16 (), +.br +.BR libsimple_strtou32 (), +.br +.BR libsimple_strtou64 () +T} Thread safety MT-Safe +T{ +.BR libsimple_strtou (), +.br +.BR libsimple_strtouh (), +.br +.BR libsimple_strtouhh (), +.br +.BR libsimple_strtouz (), +.br +.BR libsimple_strtou8 (), +.br +.BR libsimple_strtou16 (), +.br +.BR libsimple_strtou32 (), +.br +.BR libsimple_strtou64 () +T} Async-signal safety AS-Safe +T{ +.BR libsimple_strtou (), +.br +.BR libsimple_strtouh (), +.br +.BR libsimple_strtouhh (), +.br +.BR libsimple_strtouz (), +.br +.BR libsimple_strtou8 (), +.br +.BR libsimple_strtou16 (), +.br +.BR libsimple_strtou32 (), +.br +.BR libsimple_strtou64 () +T} Async-cancel safety AC-Safe +.TE + +.SH EXAMPLES +None. + +.SH APPLICATION USAGE +None. + +.SH RATIONALE +None. + +.SH FUTURE DIRECTIONS +None. + +.SH NOTES +None. + +.SH BUGS +None. + +.SH SEE ALSO +.BR libsimple_strtoi (3), +.BR strtoul (3), +.BR strtoull (3) diff --git a/man3/libsimple_strtou16.3 b/man3/libsimple_strtou16.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtou16.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtou32.3 b/man3/libsimple_strtou32.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtou32.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtou64.3 b/man3/libsimple_strtou64.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtou64.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtou8.3 b/man3/libsimple_strtou8.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtou8.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtouh.3 b/man3/libsimple_strtouh.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtouh.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtouhh.3 b/man3/libsimple_strtouhh.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtouhh.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtouz.3 b/man3/libsimple_strtouz.3 new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/libsimple_strtouz.3 @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/libsimple_strtoz.3 b/man3/libsimple_strtoz.3 new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/libsimple_strtoz.3 @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/strtoh.3libsimple b/man3/strtoh.3libsimple new file mode 120000 index 0000000..58751cb --- /dev/null +++ b/man3/strtoh.3libsimple @@ -0,0 +1 @@ +libsimple_strtoh.3
\ No newline at end of file diff --git a/man3/strtohh.3libsimple b/man3/strtohh.3libsimple new file mode 120000 index 0000000..890ee2c --- /dev/null +++ b/man3/strtohh.3libsimple @@ -0,0 +1 @@ +libsimple_strtohh.3
\ No newline at end of file diff --git a/man3/strtoi.3libsimple b/man3/strtoi.3libsimple new file mode 120000 index 0000000..debbfef --- /dev/null +++ b/man3/strtoi.3libsimple @@ -0,0 +1 @@ +libsimple_strtoi.3
\ No newline at end of file diff --git a/man3/strtoi16.3libsimple b/man3/strtoi16.3libsimple new file mode 120000 index 0000000..de1c0dc --- /dev/null +++ b/man3/strtoi16.3libsimple @@ -0,0 +1 @@ +libsimple_strtoi16.3
\ No newline at end of file diff --git a/man3/strtoi32.3libsimple b/man3/strtoi32.3libsimple new file mode 120000 index 0000000..c2256df --- /dev/null +++ b/man3/strtoi32.3libsimple @@ -0,0 +1 @@ +libsimple_strtoi32.3
\ No newline at end of file diff --git a/man3/strtoi64.3libsimple b/man3/strtoi64.3libsimple new file mode 120000 index 0000000..051af78 --- /dev/null +++ b/man3/strtoi64.3libsimple @@ -0,0 +1 @@ +libsimple_strtoi64.3
\ No newline at end of file diff --git a/man3/strtoi8.3libsimple b/man3/strtoi8.3libsimple new file mode 120000 index 0000000..7a10a91 --- /dev/null +++ b/man3/strtoi8.3libsimple @@ -0,0 +1 @@ +libsimple_strtoi8.3
\ No newline at end of file diff --git a/man3/strtou.3libsimple b/man3/strtou.3libsimple new file mode 120000 index 0000000..513cee9 --- /dev/null +++ b/man3/strtou.3libsimple @@ -0,0 +1 @@ +libsimple_strtou.3
\ No newline at end of file diff --git a/man3/strtou16.3libsimple b/man3/strtou16.3libsimple new file mode 120000 index 0000000..4265fbd --- /dev/null +++ b/man3/strtou16.3libsimple @@ -0,0 +1 @@ +libsimple_strtou16.3
\ No newline at end of file diff --git a/man3/strtou32.3libsimple b/man3/strtou32.3libsimple new file mode 120000 index 0000000..38ae14a --- /dev/null +++ b/man3/strtou32.3libsimple @@ -0,0 +1 @@ +libsimple_strtou32.3
\ No newline at end of file diff --git a/man3/strtou64.3libsimple b/man3/strtou64.3libsimple new file mode 120000 index 0000000..e71f91d --- /dev/null +++ b/man3/strtou64.3libsimple @@ -0,0 +1 @@ +libsimple_strtou64.3
\ No newline at end of file diff --git a/man3/strtou8.3libsimple b/man3/strtou8.3libsimple new file mode 120000 index 0000000..0554888 --- /dev/null +++ b/man3/strtou8.3libsimple @@ -0,0 +1 @@ +libsimple_strtou8.3
\ No newline at end of file diff --git a/man3/strtouh.3libsimple b/man3/strtouh.3libsimple new file mode 120000 index 0000000..fac2341 --- /dev/null +++ b/man3/strtouh.3libsimple @@ -0,0 +1 @@ +libsimple_strtouh.3
\ No newline at end of file diff --git a/man3/strtouhh.3libsimple b/man3/strtouhh.3libsimple new file mode 120000 index 0000000..9933519 --- /dev/null +++ b/man3/strtouhh.3libsimple @@ -0,0 +1 @@ +libsimple_strtouhh.3
\ No newline at end of file diff --git a/man3/strtouz.3libsimple b/man3/strtouz.3libsimple new file mode 120000 index 0000000..af00600 --- /dev/null +++ b/man3/strtouz.3libsimple @@ -0,0 +1 @@ +libsimple_strtouz.3
\ No newline at end of file diff --git a/man3/strtoz.3libsimple b/man3/strtoz.3libsimple new file mode 120000 index 0000000..157c5c8 --- /dev/null +++ b/man3/strtoz.3libsimple @@ -0,0 +1 @@ +libsimple_strtoz.3
\ No newline at end of file @@ -4,13 +4,13 @@ signed short int -libsimple_strtoh(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoh(const char *restrict nptr, char **restrict end, int base) { signed long int r = strtol(nptr, end, base); - if(r < SHRT_MIN) { + if (r < SHRT_MIN) { r = SHRT_MIN; errno = ERANGE; - } else if(r > SHRT_MAX) { + } else if (r > SHRT_MAX) { r = SHRT_MAX; errno = ERANGE; } @@ -21,9 +21,48 @@ libsimple_strtoh(const char *restrict nptr, char **restrict end, int base) /* TO #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", (unsigned int)SHRT_MAX); + errno = 0; + assert(strtoh(str, NULL, 0) == SHRT_MAX && !errno); + assert(strtoh(str, NULL, 16) == SHRT_MAX && !errno); + assert(strtoh(&str[2], NULL, 16) == SHRT_MAX && !errno); + assert(strtoh(str, NULL, 10) == 0 && !errno); + assert(strtoh(str, &e, 0) == SHRT_MAX && !*e && !errno); + assert(strtoh(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", (unsigned int)SHRT_MAX); + assert(strtoh(str, &e, 16) == SHRT_MAX && *e == ' ' && !errno); + sprintf(str, "-%i", SHRT_MAX); + assert(strtoh(&str[1], &e, 10) == SHRT_MAX && !*e && !errno); + assert(strtoh(str, &e, 10) == -SHRT_MAX && !*e && !errno); + sprintf(str, "%i", SHRT_MIN); + assert(strtoh(str, &e, 10) == SHRT_MIN && !*e && !errno); + assert(strtoh("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoh("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoh("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%i", SHRT_MAX); + add_one(str); + assert(strtoh(str, &e, 10) == SHRT_MAX && !*e && errno == ERANGE); + errno = 0; + sprintf(str, "%i", SHRT_MIN); + add_one(str); + assert(strtoh(str, &e, 10) == SHRT_MIN && !*e && errno == ERANGE); + errno = 0; + assert(!strtoh("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -4,13 +4,13 @@ signed char -libsimple_strtohh(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtohh(const char *restrict nptr, char **restrict end, int base) { signed long int r = strtol(nptr, end, base); - if(r < SCHAR_MIN) { + if (r < SCHAR_MIN) { r = SCHAR_MIN; errno = ERANGE; - } else if(r > SCHAR_MAX) { + } else if (r > SCHAR_MAX) { r = SCHAR_MAX; errno = ERANGE; } @@ -21,9 +21,48 @@ libsimple_strtohh(const char *restrict nptr, char **restrict end, int base) /* T #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", (unsigned int)SCHAR_MAX); + errno = 0; + assert(strtohh(str, NULL, 0) == SCHAR_MAX && !errno); + assert(strtohh(str, NULL, 16) == SCHAR_MAX && !errno); + assert(strtohh(&str[2], NULL, 16) == SCHAR_MAX && !errno); + assert(strtohh(str, NULL, 10) == 0 && !errno); + assert(strtohh(str, &e, 0) == SCHAR_MAX && !*e && !errno); + assert(strtohh(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", (unsigned int)SCHAR_MAX); + assert(strtohh(str, &e, 16) == SCHAR_MAX && *e == ' ' && !errno); + sprintf(str, "-%i", SCHAR_MAX); + assert(strtohh(&str[1], &e, 10) == SCHAR_MAX && !*e && !errno); + assert(strtohh(str, &e, 10) == -SCHAR_MAX && !*e && !errno); + sprintf(str, "%i", SCHAR_MIN); + assert(strtohh(str, &e, 10) == SCHAR_MIN && !*e && !errno); + assert(strtohh("12", &e, 10) == 12 && !*e && !errno); + assert(strtohh("12", &e, 8) == 012 && !*e && !errno); + assert(strtohh("012", &e, 0) == 012 && !*e && !errno); + sprintf(str, "%i", SCHAR_MAX); + add_one(str); + assert(strtohh(str, &e, 10) == SCHAR_MAX && !*e && errno == ERANGE); + errno = 0; + sprintf(str, "%i", SCHAR_MIN); + add_one(str); + assert(strtohh(str, &e, 10) == SCHAR_MIN && !*e && errno == ERANGE); + errno = 0; + assert(!strtohh("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -4,13 +4,13 @@ signed int -libsimple_strtoi(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoi(const char *restrict nptr, char **restrict end, int base) { signed long int r = strtol(nptr, end, base); - if(r < INT_MIN) { + if (r < INT_MIN) { r = INT_MIN; errno = ERANGE; - } else if(r > INT_MAX) { + } else if (r > INT_MAX) { r = INT_MAX; errno = ERANGE; } @@ -21,9 +21,48 @@ libsimple_strtoi(const char *restrict nptr, char **restrict end, int base) /* TO #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", INT_MAX); + errno = 0; + assert(strtoi(str, NULL, 0) == INT_MAX && !errno); + assert(strtoi(str, NULL, 16) == INT_MAX && !errno); + assert(strtoi(&str[2], NULL, 16) == INT_MAX && !errno); + assert(strtoi(str, NULL, 10) == 0 && !errno); + assert(strtoi(str, &e, 0) == INT_MAX && !*e && !errno); + assert(strtoi(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", INT_MAX); + assert(strtoi(str, &e, 16) == INT_MAX && *e == ' ' && !errno); + sprintf(str, "-%i", INT_MAX); + assert(strtoi(&str[1], &e, 10) == INT_MAX && !*e && !errno); + assert(strtoi(str, &e, 10) == -INT_MAX && !*e && !errno); + sprintf(str, "%i", INT_MIN); + assert(strtoi(str, &e, 10) == INT_MIN && !*e && !errno); + assert(strtoi("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoi("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoi("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%i", INT_MAX); + add_one(str); + assert(strtoi(str, &e, 10) == INT_MAX && !*e && errno == ERANGE); + errno = 0; + sprintf(str, "%i", INT_MIN); + add_one(str); + assert(strtoi(str, &e, 10) == INT_MIN && !*e && errno == ERANGE); + errno = 0; + assert(!strtoi("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -7,13 +7,13 @@ int_least16_t -libsimple_strtoi16(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoi16(const char *restrict nptr, char **restrict end, int base) { intmax_t r = strtoimax(nptr, end, base); - if(r < RET_MIN) { + if (r < RET_MIN) { r = RET_MIN; errno = ERANGE; - } else if(r > RET_MAX) { + } else if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -27,6 +27,27 @@ libsimple_strtoi16(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtoi16("0x7FFF", NULL, 0) == INT16_C(0x7FFF) && !errno); + assert(strtoi16("0x7FFF", NULL, 16) == INT16_C(0x7FFF) && !errno); + assert(strtoi16("7FFF", NULL, 16) == INT16_C(0x7FFF) && !errno); + assert(strtoi16("0x7FFF", NULL, 10) == 0 && !errno); + assert(strtoi16("0x7FFF", &e, 0) == INT16_C(0x7FFF) && !*e && !errno); + assert(strtoi16("0x7FFF ", &e, 16) == INT16_C(0x7FFF) && *e == ' ' && !errno); + assert(strtoi16("0x7FFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtoi16("32767", &e, 10) == INT16_C(0x7FFF) && !*e && !errno); + assert(strtoi16("-32767", &e, 10) == INT16_C(-32767) && !*e && !errno); + assert(strtoi16("-32768", &e, 10) == INT16_C(-32768) && !*e && !errno); + assert(strtoi16("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoi16("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoi16("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtoi16("32768", &e, 10) == INT16_C(32767) && !*e && errno == ERANGE); + errno = 0; + assert(strtoi16("-32769", &e, 10) == INT16_C(-32768) && !*e && errno == ERANGE); + errno = 0; + assert(!strtoi16("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -7,13 +7,13 @@ int_least32_t -libsimple_strtoi32(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoi32(const char *restrict nptr, char **restrict end, int base) { intmax_t r = strtoimax(nptr, end, base); - if(r < RET_MIN) { + if (r < RET_MIN) { r = RET_MIN; errno = ERANGE; - } else if(r > RET_MAX) { + } else if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -27,6 +27,27 @@ libsimple_strtoi32(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtoi32("0x7FFFFFFF", NULL, 0) == INT32_C(0x7FFFFFFF) && !errno); + assert(strtoi32("0x7FFFFFFF", NULL, 16) == INT32_C(0x7FFFFFFF) && !errno); + assert(strtoi32("7FFFFFFF", NULL, 16) == INT32_C(0x7FFFFFFF) && !errno); + assert(strtoi32("0x7FFFFFFF", NULL, 10) == 0 && !errno); + assert(strtoi32("0x7FFFFFFF", &e, 0) == INT32_C(0x7FFFFFFF) && !*e && !errno); + assert(strtoi32("0x7FFFFFFF ", &e, 16) == INT32_C(0x7FFFFFFF) && *e == ' ' && !errno); + assert(strtoi32("0x7FFFFFFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtoi32("2147483647", &e, 10) == INT32_C(0x7FFFFFFF) && !*e && !errno); + assert(strtoi32("-2147483647", &e, 10) == INT32_C(-2147483647) && !*e && !errno); + assert(strtoi32("-2147483648", &e, 10) == INT32_C(-2147483648) && !*e && !errno); + assert(strtoi32("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoi32("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoi32("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtoi32("2147483648", &e, 10) == INT32_C(2147483647) && !*e && errno == ERANGE); + errno = 0; + assert(strtoi32("-2147483649", &e, 10) == INT32_C(-2147483648) && !*e && errno == ERANGE); + errno = 0; + assert(!strtoi32("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -7,13 +7,13 @@ int_least64_t -libsimple_strtoi64(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoi64(const char *restrict nptr, char **restrict end, int base) { intmax_t r = strtoimax(nptr, end, base); - if(r < RET_MIN) { + if (r < RET_MIN) { r = RET_MIN; errno = ERANGE; - } else if(r > RET_MAX) { + } else if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -27,6 +27,27 @@ libsimple_strtoi64(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtoi64("0x7FFFFFFFFFFFFFFF", NULL, 0) == INT64_C(0x7FFFFFFFFFFFFFFF) && !errno); + assert(strtoi64("0x7FFFFFFFFFFFFFFF", NULL, 16) == INT64_C(0x7FFFFFFFFFFFFFFF) && !errno); + assert(strtoi64("7FFFFFFFFFFFFFFF", NULL, 16) == INT64_C(0x7FFFFFFFFFFFFFFF) && !errno); + assert(strtoi64("0x7FFFFFFFFFFFFFFF", NULL, 10) == 0 && !errno); + assert(strtoi64("0x7FFFFFFFFFFFFFFF", &e, 0) == INT64_C(0x7FFFFFFFFFFFFFFF) && !*e && !errno); + assert(strtoi64("0x7FFFFFFFFFFFFFFF ", &e, 16) == INT64_C(0x7FFFFFFFFFFFFFFF) && *e == ' ' && !errno); + assert(strtoi64("0x7FFFFFFFFFFFFFFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtoi64("9223372036854775807", &e, 10) == INT64_C(0x7FFFFFFFFFFFFFFF) && !*e && !errno); + assert(strtoi64("-9223372036854775807", &e, 10) == INT64_C(-9223372036854775807) && !*e && !errno); + assert(strtoi64("-9223372036854775808", &e, 10) == INT64_C(-9223372036854775808) && !*e && !errno); + assert(strtoi64("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoi64("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoi64("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtoi64("9223372036854775808", &e, 10) == INT64_C(9223372036854775807) && !*e && errno == ERANGE); + errno = 0; + assert(strtoi64("-9223372036854775809", &e, 10) == INT64_C(-9223372036854775808) && !*e && errno == ERANGE); + errno = 0; + assert(!strtoi64("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -7,13 +7,13 @@ int_least8_t -libsimple_strtoi8(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoi8(const char *restrict nptr, char **restrict end, int base) { intmax_t r = strtoimax(nptr, end, base); - if(r < RET_MIN) { + if (r < RET_MIN) { r = RET_MIN; errno = ERANGE; - } else if(r > RET_MAX) { + } else if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -27,6 +27,27 @@ libsimple_strtoi8(const char *restrict nptr, char **restrict end, int base) /* T int main(void) { + char *e; + errno = 0; + assert(strtoi8("0x7F", NULL, 0) == INT8_C(0x7F) && !errno); + assert(strtoi8("0x7F", NULL, 16) == INT8_C(0x7F) && !errno); + assert(strtoi8("7F", NULL, 16) == INT8_C(0x7F) && !errno); + assert(strtoi8("0x7F", NULL, 10) == 0 && !errno); + assert(strtoi8("0x7F", &e, 0) == INT8_C(0x7F) && !*e && !errno); + assert(strtoi8("0x7F ", &e, 16) == INT8_C(0x7F) && *e == ' ' && !errno); + assert(strtoi8("0x7F", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtoi8("127", &e, 10) == INT8_C(0x7F) && !*e && !errno); + assert(strtoi8("-127", &e, 10) == INT8_C(-127) && !*e && !errno); + assert(strtoi8("-128", &e, 10) == INT8_C(-128) && !*e && !errno); + assert(strtoi8("12", &e, 10) == 12 && !*e && !errno); + assert(strtoi8("12", &e, 8) == 012 && !*e && !errno); + assert(strtoi8("012", &e, 0) == 012 && !*e && !errno); + assert(strtoi8("128", &e, 10) == INT8_C(127) && !*e && errno == ERANGE); + errno = 0; + assert(strtoi8("-129", &e, 10) == INT8_C(-128) && !*e && errno == ERANGE); + errno = 0; + assert(!strtoi8("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -4,10 +4,10 @@ unsigned int -libsimple_strtou(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtou(const char *restrict nptr, char **restrict end, int base) { unsigned long int r = strtoul(nptr, end, base); - if(r > UINT_MAX) { + if (r > UINT_MAX) { r = UINT_MAX; errno = ERANGE; } @@ -18,9 +18,41 @@ libsimple_strtou(const char *restrict nptr, char **restrict end, int base) /* TO #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", UINT_MAX); + errno = 0; + assert(strtou(str, NULL, 0) == UINT_MAX && !errno); + assert(strtou(str, NULL, 16) == UINT_MAX && !errno); + assert(strtou(&str[2], NULL, 16) == UINT_MAX && !errno); + assert(strtou(str, NULL, 10) == 0 && !errno); + assert(strtou(str, &e, 0) == UINT_MAX && !*e && !errno); + assert(strtou(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", UINT_MAX); + assert(strtou(str, &e, 16) == UINT_MAX && *e == ' ' && !errno); + sprintf(str, "%u", UINT_MAX); + assert(strtou(str, &e, 10) == UINT_MAX && !*e && !errno); + assert(strtou("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtou("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtou("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%u", UINT_MAX); + add_one(str); + assert(strtou(str, &e, 10) == UINT_MAX && !*e && errno == ERANGE); + errno = 0; + assert(!strtou("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -6,10 +6,10 @@ uint_least16_t -libsimple_strtou16(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtou16(const char *restrict nptr, char **restrict end, int base) { uintmax_t r = strtoumax(nptr, end, base); - if(r > RET_MAX) { + if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -23,6 +23,23 @@ libsimple_strtou16(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtou16("0xFFFF", NULL, 0) == UINT16_C(0xFFFF) && !errno); + assert(strtou16("0xFFFF", NULL, 16) == UINT16_C(0xFFFF) && !errno); + assert(strtou16("FFFF", NULL, 16) == UINT16_C(0xFFFF) && !errno); + assert(strtou16("0xFFFF", NULL, 10) == 0 && !errno); + assert(strtou16("0xFFFF", &e, 0) == UINT16_C(0xFFFF) && !*e && !errno); + assert(strtou16("0xFFFF ", &e, 16) == UINT16_C(0xFFFF) && *e == ' ' && !errno); + assert(strtou16("0xFFFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtou16("65535", &e, 10) == UINT16_C(0xFFFF) && !*e && !errno); + assert(strtou16("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtou16("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtou16("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtou16("65536", &e, 10) == UINT16_C(65535) && !*e && errno == ERANGE); + errno = 0; + assert(!strtou16("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -6,10 +6,10 @@ uint_least32_t -libsimple_strtou32(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtou32(const char *restrict nptr, char **restrict end, int base) { uintmax_t r = strtoumax(nptr, end, base); - if(r > RET_MAX) { + if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -23,6 +23,23 @@ libsimple_strtou32(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtou32("0xFFFFFFFF", NULL, 0) == UINT32_C(0xFFFFFFFF) && !errno); + assert(strtou32("0xFFFFFFFF", NULL, 16) == UINT32_C(0xFFFFFFFF) && !errno); + assert(strtou32("FFFFFFFF", NULL, 16) == UINT32_C(0xFFFFFFFF) && !errno); + assert(strtou32("0xFFFFFFFF", NULL, 10) == 0 && !errno); + assert(strtou32("0xFFFFFFFF", &e, 0) == UINT32_C(0xFFFFFFFF) && !*e && !errno); + assert(strtou32("0xFFFFFFFF ", &e, 16) == UINT32_C(0xFFFFFFFF) && *e == ' ' && !errno); + assert(strtou32("0xFFFFFFFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtou32("4294967295", &e, 10) == UINT32_C(0xFFFFFFFF) && !*e && !errno); + assert(strtou32("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtou32("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtou32("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtou32("4294967296", &e, 10) == UINT32_C(4294967295) && !*e && errno == ERANGE); + errno = 0; + assert(!strtou32("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -6,11 +6,11 @@ uint_least64_t -libsimple_strtou64(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtou64(const char *restrict nptr, char **restrict end, int base) { uintmax_t r = strtoumax(nptr, end, base); #if UINTMAX_MAX > RET_MAX - if(r > RET_MAX) { + if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -25,6 +25,23 @@ libsimple_strtou64(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtou64("0xFFFFFFFFFFFFFFFF", NULL, 0) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !errno); + assert(strtou64("0xFFFFFFFFFFFFFFFF", NULL, 16) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !errno); + assert(strtou64("FFFFFFFFFFFFFFFF", NULL, 16) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !errno); + assert(strtou64("0xFFFFFFFFFFFFFFFF", NULL, 10) == 0 && !errno); + assert(strtou64("0xFFFFFFFFFFFFFFFF", &e, 0) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !*e && !errno); + assert(strtou64("0xFFFFFFFFFFFFFFFF ", &e, 16) == UINT64_C(0xFFFFFFFFFFFFFFFF) && *e == ' ' && !errno); + assert(strtou64("0xFFFFFFFFFFFFFFFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtou64("18446744073709551615", &e, 10) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !*e && !errno); + assert(strtou64("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtou64("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtou64("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtou64("18446744073709551616", &e, 10) == UINT64_C(18446744073709551615) && !*e && errno == ERANGE); + errno = 0; + assert(!strtou64("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -6,10 +6,10 @@ uint_least8_t -libsimple_strtou8(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtou8(const char *restrict nptr, char **restrict end, int base) { uintmax_t r = strtoumax(nptr, end, base); - if(r > RET_MAX) { + if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -23,6 +23,23 @@ libsimple_strtou8(const char *restrict nptr, char **restrict end, int base) /* T int main(void) { + char *e; + errno = 0; + assert(strtou8("0xFF", NULL, 0) == UINT8_C(0xFF) && !errno); + assert(strtou8("0xFF", NULL, 16) == UINT8_C(0xFF) && !errno); + assert(strtou8("FF", NULL, 16) == UINT8_C(0xFF) && !errno); + assert(strtou8("0xFF", NULL, 10) == 0 && !errno); + assert(strtou8("0xFF", &e, 0) == UINT8_C(0xFF) && !*e && !errno); + assert(strtou8("0xFF ", &e, 16) == UINT8_C(0xFF) && *e == ' ' && !errno); + assert(strtou8("0xFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtou8("255", &e, 10) == UINT8_C(0xFF) && !*e && !errno); + assert(strtou8("12", &e, 10) == 12 && !*e && !errno); + assert(strtou8("12", &e, 8) == 012 && !*e && !errno); + assert(strtou8("012", &e, 0) == 012 && !*e && !errno); + assert(strtou8("256", &e, 10) == UINT8_C(255) && !*e && errno == ERANGE); + errno = 0; + assert(!strtou8("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -4,10 +4,10 @@ unsigned short int -libsimple_strtouh(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtouh(const char *restrict nptr, char **restrict end, int base) { unsigned long int r = strtoul(nptr, end, base); - if(r > USHRT_MAX) { + if (r > USHRT_MAX) { r = USHRT_MAX; errno = ERANGE; } @@ -18,9 +18,41 @@ libsimple_strtouh(const char *restrict nptr, char **restrict end, int base) /* T #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", (unsigned int)USHRT_MAX); + errno = 0; + assert(strtouh(str, NULL, 0) == USHRT_MAX && !errno); + assert(strtouh(str, NULL, 16) == USHRT_MAX && !errno); + assert(strtouh(&str[2], NULL, 16) == USHRT_MAX && !errno); + assert(strtouh(str, NULL, 10) == 0 && !errno); + assert(strtouh(str, &e, 0) == USHRT_MAX && !*e && !errno); + assert(strtouh(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", (unsigned int)USHRT_MAX); + assert(strtouh(str, &e, 16) == USHRT_MAX && *e == ' ' && !errno); + sprintf(str, "%u", (unsigned int)USHRT_MAX); + assert(strtouh(str, &e, 10) == USHRT_MAX && !*e && !errno); + assert(strtouh("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtouh("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtouh("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%u", (unsigned int)USHRT_MAX); + add_one(str); + assert(strtouh(str, &e, 10) == USHRT_MAX && !*e && errno == ERANGE); + errno = 0; + assert(!strtouh("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -4,10 +4,10 @@ unsigned char -libsimple_strtouhh(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtouhh(const char *restrict nptr, char **restrict end, int base) { unsigned long int r = strtoul(nptr, end, base); - if(r > UCHAR_MAX) { + if (r > UCHAR_MAX) { r = UCHAR_MAX; errno = ERANGE; } @@ -18,9 +18,41 @@ libsimple_strtouhh(const char *restrict nptr, char **restrict end, int base) /* #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", (unsigned int)UCHAR_MAX); + errno = 0; + assert(strtouhh(str, NULL, 0) == UCHAR_MAX && !errno); + assert(strtouhh(str, NULL, 16) == UCHAR_MAX && !errno); + assert(strtouhh(&str[2], NULL, 16) == UCHAR_MAX && !errno); + assert(strtouhh(str, NULL, 10) == 0 && !errno); + assert(strtouhh(str, &e, 0) == UCHAR_MAX && !*e && !errno); + assert(strtouhh(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", (unsigned int)UCHAR_MAX); + assert(strtouhh(str, &e, 16) == UCHAR_MAX && *e == ' ' && !errno); + sprintf(str, "%u", (unsigned int)UCHAR_MAX); + assert(strtouhh(str, &e, 10) == UCHAR_MAX && !*e && !errno); + assert(strtouhh("12", &e, 10) == 12 && !*e && !errno); + assert(strtouhh("12", &e, 8) == 012 && !*e && !errno); + assert(strtouhh("012", &e, 0) == 012 && !*e && !errno); + sprintf(str, "%u", (unsigned int)UCHAR_MAX); + add_one(str); + assert(strtouhh(str, &e, 10) == UCHAR_MAX && !*e && errno == ERANGE); + errno = 0; + assert(!strtouhh("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -3,15 +3,47 @@ #ifndef TEST -extern inline size_t libsimple_strtouz(const char *restrict, char **restrict, int); /* TODO test, man */ +extern inline size_t libsimple_strtouz(const char *restrict, char **restrict, int); #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%zx", SIZE_MAX); + errno = 0; + assert(strtouz(str, NULL, 0) == SIZE_MAX && !errno); + assert(strtouz(str, NULL, 16) == SIZE_MAX && !errno); + assert(strtouz(&str[2], NULL, 16) == SIZE_MAX && !errno); + assert(strtouz(str, NULL, 10) == 0 && !errno); + assert(strtouz(str, &e, 0) == SIZE_MAX && !*e && !errno); + assert(strtouz(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%zx ", SIZE_MAX); + assert(strtouz(str, &e, 16) == SIZE_MAX && *e == ' ' && !errno); + sprintf(str, "%zu", SIZE_MAX); + assert(strtouz(str, &e, 10) == SIZE_MAX && !*e && !errno); + assert(strtouz("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtouz("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtouz("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%zu", SIZE_MAX); + add_one(str); + assert(strtouz(str, &e, 10) == SIZE_MAX && !*e && errno == ERANGE); + errno = 0; + assert(!strtouz("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } @@ -3,15 +3,58 @@ #ifndef TEST -extern inline ssize_t libsimple_strtoz(const char *restrict, char **restrict, int); /* TODO test, man */ +extern inline ssize_t libsimple_strtoz(const char *restrict, char **restrict, int); #else #include "test.h" +#ifndef SSIZE_MIN +#define SSIZE_MIN (-SSIZE_MAX - (-LONG_MAX != LONG_MIN)) +#endif + +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%zx", SSIZE_MAX); + errno = 0; + assert(strtoz(str, NULL, 0) == SSIZE_MAX && !errno); + assert(strtoz(str, NULL, 16) == SSIZE_MAX && !errno); + assert(strtoz(&str[2], NULL, 16) == SSIZE_MAX && !errno); + assert(strtoz(str, NULL, 10) == 0 && !errno); + assert(strtoz(str, &e, 0) == SSIZE_MAX && !*e && !errno); + assert(strtoz(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%zx ", SSIZE_MAX); + assert(strtoz(str, &e, 16) == SSIZE_MAX && *e == ' ' && !errno); + sprintf(str, "-%zi", SSIZE_MAX); + assert(strtoz(&str[1], &e, 10) == SSIZE_MAX && !*e && !errno); + assert(strtoz(str, &e, 10) == -SSIZE_MAX && !*e && !errno); + sprintf(str, "%zi", SSIZE_MIN); + assert(strtoz(str, &e, 10) == SSIZE_MIN && !*e && !errno); + assert(strtoz("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoz("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoz("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%zi", SSIZE_MAX); + add_one(str); + assert(strtoz(str, &e, 10) == SSIZE_MAX && !*e && errno == ERANGE); + errno = 0; + sprintf(str, "%zi", SSIZE_MIN); + add_one(str); + assert(strtoz(str, &e, 10) == SSIZE_MIN && !*e && errno == ERANGE); + errno = 0; + assert(!strtoz("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } |