diff options
79 files changed, 1222 insertions, 534 deletions
@@ -1,44 +1,20 @@ CC = clang -CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Iinclude -march=native -mtune=native -O3 -fPIC +CFLAGS = -std=c17 -Wall -Wextra -pedantic-errors -Iinclude -march=native -mtune=native -fPIC ifneq ($(thrdsafe),0) CFLAGS += -Du8c_bethrdsafe endif ifeq ($(debug),1) CFLAGS += -g else -CFLAGS += -DNDEBUG +CFLAGS += -O3 -DNDEBUG endif LDFLAGS = -shared -lpthread -SRCS = \ - src/u8c/debug.c \ - src/u8c/end.c \ - src/u8c/err.c \ - src/u8c/errlock.c \ - src/u8c/fmt.c \ - src/u8c/geterr.c \ - src/u8c/init.c \ - src/u8c/isalnum.c \ - src/u8c/isalpha.c \ - src/u8c/iscntrl.c \ - src/u8c/isdigit.c \ - src/u8c/isspace.c \ - src/u8c/ispunct.c \ - src/u8c/print.c \ - src/u8c/println.c \ - src/u8c/seterr.c \ - src/u8c/stat.c \ - src/u8c/thrdsafe.c \ - src/u8c/u32cmp.c \ - src/u8c/u32cp.c \ - src/u8c/u32sz.c \ - src/u8c/u8dec.c \ - src/u8c/u8enc.c \ - src/u8c/vfmt.c \ - src/u8c/vprint.c HDRS = \ + include/u8c/SIZE_C.h \ + include/u8c/abrt.h \ include/u8c/col.h \ + include/u8c/dbg.h \ include/u8c/dbgprint.h \ - include/u8c/debug.h \ include/u8c/end.h \ include/u8c/fmt.h \ include/u8c/fmttyp.h \ @@ -48,28 +24,69 @@ HDRS = \ include/u8c/isalpha.h \ include/u8c/iscntrl.h \ include/u8c/isdigit.h \ - include/u8c/isspace.h \ include/u8c/ispunct.h \ + include/u8c/isspace.h \ + include/u8c/isxdigit.h \ include/u8c/print.h \ include/u8c/println.h \ include/u8c/seterr.h \ - include/u8c/SIZE_C.h \ + include/u8c/setfmt.h \ include/u8c/thrdsafe.h \ - include/u8c/txt.h \ + include/u8c/u32alloc.h \ + include/u8c/u32cat.h \ include/u8c/u32cmp.h \ include/u8c/u32cp.h \ + include/u8c/u32fndchr.h \ + include/u8c/u32fndpat.h \ include/u8c/u32free.h \ + include/u8c/u32substr.h \ include/u8c/u32sz.h \ + include/u8c/u8alloc.h \ include/u8c/u8dec.h \ include/u8c/u8enc.h \ include/u8c/u8free.h \ + include/u8c/unimax.h \ include/u8c/ver.h \ include/u8c/vfmt.h \ include/u8c/vprint.h HDRS_PRIV = \ - src/u8c/err.h \ - src/u8c/errlock.h \ - src/u8c/stat.h + src/u8c/dat.h \ + src/u8c/dattyp.h +SRCS = \ + src/u8c/abrt.c \ + src/u8c/dat.c \ + src/u8c/debug.c \ + src/u8c/end.c \ + src/u8c/fmt.c \ + src/u8c/geterr.c \ + src/u8c/init.c \ + src/u8c/isalnum.c \ + src/u8c/isalpha.c \ + src/u8c/iscntrl.c \ + src/u8c/isdigit.c \ + src/u8c/isspace.c \ + src/u8c/ispunct.c \ + src/u8c/isxdigit.c \ + src/u8c/print.c \ + src/u8c/println.c \ + src/u8c/seterr.c \ + src/u8c/setfmt.c \ + src/u8c/thrdsafe.c \ + src/u8c/u32alloc.c \ + src/u8c/u32cat.c \ + src/u8c/u32cmp.c \ + src/u8c/u32cp.c \ + src/u8c/u32fndchr.c \ + src/u8c/u32fndpat.c \ + src/u8c/u32free.c \ + src/u8c/u32substr.c \ + src/u8c/u32sz.c \ + src/u8c/u8alloc.c \ + src/u8c/u8dec.c \ + src/u8c/u8enc.c \ + src/u8c/u8free.c \ + src/u8c/vfmt.c \ + src/u8c/vprint.c OBJS = $(SRCS:.c=.o) LIB = libu8c.so $(LIB): $(OBJS) @@ -89,9 +106,6 @@ runtest: test export LD_LIBRARY_PATH=$(CURDIR) && ./$^ test: $(LIB) test.c $(CC) -std=c17 -Wall -Wextra -Wpedantic -Iinclude -march=native -mtune=native -O3 -g -L. -lu8c -o $@ [email protected] -txttolit: $(LIB) txttolit.c - $(CC) -std=c17 -Wall -Wextra -Wpedantic -Iinclude -march=native -mtune=native -O3 -g -L. -lu8c -o $@ [email protected] - export LD_LIBRARY_PATH=$(CURDIR) && ./$@ uninstall: rm --force --recursive $(DESTDIR)/include/u8c/ rm --force --recursive $(DESTDIR)/lib/$(LIB) @@ -1,6 +1,6 @@ # u8c -[*u8c*](https://mandelbrot.dk/delta/u8c) is a cross-platform, free, and open-source library for transforming Unicode codepoints, as well as encoding them into UTF-8, even on implementations that use a different 32 bit encoding. +[*u8c*](https://mandelbrot.dk/delta/u8c) is a cross-platform, free, and open-source library for transforming Unicode codepoints, as well as encoding them into UTF-8. ## Installing diff --git a/changelog.md b/changelog.md index edc7569..d8fb6a4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,27 @@ +# 16 + +* Add function for concatenating UTF-32 strings; `u8c_u32cat`. +* Add functions for allocating UTF-32 and UTF-8 strings; `u8c_u32alloc` and `u8c_u8alloc`. +* Add function for finding a given codepoint in an UTF-32 string; `u8c_u32fndchr`. +* Add function for finding a given pattern (string) in an UTF-32 string; `u8c_u32fndpat`. +* Update `SIZE_C`. +* Add function for aborting; `u8c_abrt`. +* Rename `u8c_debug` to `u8c_dbg`. +* Use `bool` (`_Bool`) for return values instead of `uint_least8_t`. +* Add more format types. +* Fix incorrect unabbreviated names in headers. +* Add another function from `ctypes.h`; `u8c_isxdigit`. +* Use `char32_t` (from `stdbool.h`) instead of `uint_least32_t` in UTF-32. +* Use `unsigned char` instead of `uint_least8_t` in UTF-8. +* Move all data into `u8c_dat` (of type `struct u8c_dattyp`). +* Add function for setting the format (base and endian) of `u8c_fmt` and company; `u8c_setfmt`. +* Remove `u8c_txt` in favour of Unicode string literals (much clearer code, but less portable). +* Add function for getting a sub-string of an UTF-32 string; `u8c_u32substr`. +* Don't count the null-terminator in string sizes. +* Add macro for maximum valid Unicode codepoint; `u8c_unimax`. +* Remove `txttolit`. +* Add function for deallocating UTF-8 strings; `u8c_u8free`. + # 15 * Add missing include directives to `include/u8c/u32free.h` and `include/u8c/u8free.h`. diff --git a/include/u8c/SIZE_C.h b/include/u8c/SIZE_C.h index 1591252..4a8d513 100644 --- a/include/u8c/SIZE_C.h +++ b/include/u8c/SIZE_C.h @@ -15,31 +15,17 @@ */ /* Size constant */ # if !defined(SIZE_C) -# if !defined(__cplusplus) # include <limits.h> -# endif -# if defined(__cplusplus) && __cplusplus > 0x31512l -/* C++23 has a size_t suffix. */ -# define SIZE_C(val) val ## uz +# include <stdint.h> +# if SIZE_MAX == USHRT_MAX +# define SIZE_C(val) ((unsigned short)val) # elif SIZE_MAX == UINT_MAX # define SIZE_C(val) val # elif SIZE_MAX == ULONG_MAX -# define SIZE_C(val) val ## ul +# define SIZE_C(val) val ## lu # elif SIZE_MAX == ULLONG_MAX -# define SIZE_C(val) val ## ull -# elif SIZE_MAX == UINTMAX_MAX -# include <stdint.h> -# define SIZE_C(val) (UINTMAX_C(val)) +# define SIZE_C(val) val ## llu # else -/* Cannot match width; construct new element of type "size_t" */ -# if defined(__cplusplus) -/* C++ has value initialisation. */ -# include <cstddef> -# define SIZE_C(val) (std::size_t{val}) -# else -/* C has compound literals. */ -# include <stddef.h> -# define SIZE_C(val) ((size_t){val}) -# endif +# define SIZE_C(val) ((size_t)UINTMAX_C(val)) # endif # endif diff --git a/src/u8c/errlock.h b/include/u8c/abrt.h index a77ade5..a068ff2 100644 --- a/src/u8c/errlock.h +++ b/include/u8c/abrt.h @@ -13,11 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Error lock */ -# if !defined(u8c_sym_errlock) -# define u8c_sym_errlock -# if defined(u8c_bethrdsafe) -# include <threads.h> -extern mtx_t u8c_errlock; -# endif +/* Abort */ +# if !defined(u8c_sym_abrt) +# define u8c_sym_abrt +# include <stdbool.h> +# include <stdnoreturn.h> +# include <uchar.h> +extern noreturn bool u8c_abrt(char const * const fl,long long const ln,char const * const fn,char const * const why); # endif diff --git a/src/u8c/errlock.c b/include/u8c/dbg.h index b63a435..e389195 100644 --- a/src/u8c/errlock.c +++ b/include/u8c/dbg.h @@ -13,8 +13,9 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "errlock.h" -# if defined(u8c_bethrdsafe) -# include <threads.h> -mtx_t u8c_errlock; +/* Debug */ +# if !defined(u8c_sym_debug) +# define u8c_sym_debug +# include <stdbool.h> +extern bool const u8c_dbg; # endif diff --git a/include/u8c/dbgprint.h b/include/u8c/dbgprint.h index 42fcb16..2f974d2 100644 --- a/include/u8c/dbgprint.h +++ b/include/u8c/dbgprint.h @@ -16,11 +16,7 @@ /* Debug print */ # if !defined(u8c_dbgprint) # if defined(NDEBUG) -# if defined(__cplusplus) -# define u8c_dbgprint(...) (static_cast<void>(0x0)); -# else # define u8c_dbgprint(...) ((void)0x0) -# endif # else # include <u8c/println.h> # include <stdio.h> diff --git a/include/u8c/end.h b/include/u8c/end.h index 1322496..01c8c24 100644 --- a/include/u8c/end.h +++ b/include/u8c/end.h @@ -16,12 +16,6 @@ /* End */ # if !defined(u8c_sym_end) # define u8c_sym_end -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_end(void); -# if defined(__cplusplus) -} -# endif +# include <stdbool.h> +extern bool u8c_end(void); # endif diff --git a/include/u8c/fmt.h b/include/u8c/fmt.h index 61ee8b2..4f7ed1b 100644 --- a/include/u8c/fmt.h +++ b/include/u8c/fmt.h @@ -17,12 +17,6 @@ # if !defined(u8c_sym_fmt) # define u8c_sym_fmt # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_fmt(size_t * const outsz,uint_least32_t const * * const out,uint_least32_t const * const in,...); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_fmt(size_t * const outsz,char32_t const * * const out,char32_t const * const in,...); # endif diff --git a/include/u8c/fmttyp.h b/include/u8c/fmttyp.h index d9b27de..cce9a2f 100644 --- a/include/u8c/fmttyp.h +++ b/include/u8c/fmttyp.h @@ -17,13 +17,33 @@ # if !defined(u8c_sym_fmttyp) # define u8c_sym_fmttyp enum u8c_fmttyp { - u8c_fmttyp_bgcol, /* Background colour */ + u8c_fmttyp_bgcol, /* Background colour (uint_least32_t) */ u8c_fmttyp_bgcol0, /* Background colour #0 */ - u8c_fmttyp_chr, /* Character */ - u8c_fmttyp_fgcol, /* Foreground colour */ + u8c_fmttyp_bool, /* Boolean (bool) */ + u8c_fmttyp_byt, /* Byte (char) */ + u8c_fmttyp_chr, /* Character (uint_least32_t) */ + u8c_fmttyp_fgcol, /* Foreground colour (uint_least32_t) */ u8c_fmttyp_fgcol0, /* Foreground colour #0 */ - u8c_fmttyp_int, /* Integer */ - u8c_fmttyp_str, /* String */ - u8c_fmttyp_uint, /* Unsigned integer */ + u8c_fmttyp_int, /* Integer (int) */ + u8c_fmttyp_int16, /* Integer 16 bit (int_least16_t) */ + u8c_fmttyp_int32, /* Integer 32 bit (int_least32_t) */ + u8c_fmttyp_int64, /* Integer 64 bit (int_least64_t) */ + u8c_fmttyp_int8, /* Integer 8 bit (int_least8_t) */ + u8c_fmttyp_llong, /* Long long (long long) */ + u8c_fmttyp_long, /* Long (long) */ + u8c_fmttyp_sbyt, /* Signed byte (signed char) */ + u8c_fmttyp_shrt, /* Short (short) */ + u8c_fmttyp_str, /* String (char32_t const *) */ + u8c_fmttyp_sz, /* Size (size_t) */ + u8c_fmttyp_tm, /* Time (uint_least64_t) */ + u8c_fmttyp_ubyt, /* Unsigned byte (unsigned char) */ + u8c_fmttyp_uint, /* Unsigned integer (unsigned int) */ + u8c_fmttyp_uint16, /* Unsigned integer 16 bit (uint_least16_t) */ + u8c_fmttyp_uint32, /* Unsigned integer 32 bit (uint_least32_t) */ + u8c_fmttyp_uint64, /* Unsigned integer 64 bit (uint_least64_t) */ + u8c_fmttyp_uint8, /* Unsigned integer 8 bit (uint_least8_t) */ + u8c_fmttyp_ulong, /* Unsigned long (unsigned long) */ + u8c_fmttyp_ullong, /* Unsigned long long (unsigned long long) */ + u8c_fmttyp_ushrt, /* Unsigned short (unsigned short) */ }; # endif diff --git a/include/u8c/geterr.h b/include/u8c/geterr.h index 45a25ac..70acc4f 100644 --- a/include/u8c/geterr.h +++ b/include/u8c/geterr.h @@ -16,13 +16,8 @@ /* Get error */ # if !defined(u8c_sym_geterr) # define u8c_sym_geterr +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_geterr(size_t * const sz,uint_least32_t const * * const out); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_geterr(size_t * const sz,char32_t const * * const out); # endif diff --git a/include/u8c/init.h b/include/u8c/init.h index 823c1fb..1aa4804 100644 --- a/include/u8c/init.h +++ b/include/u8c/init.h @@ -16,12 +16,6 @@ /* Initialise */ # if !defined(u8c_sym_init) # define u8c_sym_init -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_init(void); -# if defined(__cplusplus) -} -# endif +# include <stdbool.h> +extern bool u8c_init(void); # endif diff --git a/include/u8c/isalnum.h b/include/u8c/isalnum.h index 0b61bfd..b951f5e 100644 --- a/include/u8c/isalnum.h +++ b/include/u8c/isalnum.h @@ -13,15 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Is digit */ +/* Is alphanumeric */ # if !defined(luma_sym_isalnum) # define luma_sym_isalnum +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_isalnum(uint_least8_t * const res,uint_least32_t const chr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_isalnum(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/isalpha.h b/include/u8c/isalpha.h index 3493dc0..a43363f 100644 --- a/include/u8c/isalpha.h +++ b/include/u8c/isalpha.h @@ -13,15 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Is digit */ +/* Is alphabetic */ # if !defined(luma_sym_isalpha) # define luma_sym_isalpha +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_isalpha(uint_least8_t * const res,uint_least32_t const chr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_isalpha(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/iscntrl.h b/include/u8c/iscntrl.h index 0f52911..5ffe157 100644 --- a/include/u8c/iscntrl.h +++ b/include/u8c/iscntrl.h @@ -13,15 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Is digit */ +/* Is control */ # if !defined(luma_sym_iscntrl) # define luma_sym_iscntrl +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_iscntrl(uint_least8_t * const res,uint_least32_t const chr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_iscntrl(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/isdigit.h b/include/u8c/isdigit.h index c2053c7..a28dd9a 100644 --- a/include/u8c/isdigit.h +++ b/include/u8c/isdigit.h @@ -16,12 +16,8 @@ /* Is digit */ # if !defined(luma_sym_isdigit) # define luma_sym_isdigit +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_isdigit(uint_least8_t * const res,uint_least32_t const chr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_isdigit(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/ispunct.h b/include/u8c/ispunct.h index a4da5c9..9834491 100644 --- a/include/u8c/ispunct.h +++ b/include/u8c/ispunct.h @@ -13,15 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Is digit */ +/* Is punctuation */ # if !defined(luma_sym_ispunct) # define luma_sym_ispunct +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_ispunct(uint_least8_t * const res,uint_least32_t const chr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_ispunct(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/isspace.h b/include/u8c/isspace.h index 35f9d4f..78cafea 100644 --- a/include/u8c/isspace.h +++ b/include/u8c/isspace.h @@ -13,15 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Is digit */ +/* Is space */ # if !defined(luma_sym_isspace) # define luma_sym_isspace +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_isspace(uint_least8_t * const res,uint_least32_t const chr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_isspace(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/debug.h b/include/u8c/isxdigit.h index 3bd2dfd..57e1392 100644 --- a/include/u8c/debug.h +++ b/include/u8c/isxdigit.h @@ -13,15 +13,11 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Debug */ -# if !defined(u8c_sym_debug) -# define u8c_sym_debug +/* Is hexadecimal digit */ +# if !defined(luma_sym_isxdigit) +# define luma_sym_isxdigit +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t const u8c_debug; -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_isxdigit(uint_least8_t * const res,char32_t const chr); # endif diff --git a/include/u8c/print.h b/include/u8c/print.h index f77875d..f76c942 100644 --- a/include/u8c/print.h +++ b/include/u8c/print.h @@ -16,13 +16,8 @@ /* Print */ # if !defined(u8c_sym_print) # define u8c_sym_print -# include <stdint.h> +# include <stdbool.h> # include <stdio.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_print(FILE * fp,uint_least32_t const * const msg,...); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_print(FILE * fp,char32_t const * const msg,...); # endif diff --git a/include/u8c/println.h b/include/u8c/println.h index d6874de..8d80353 100644 --- a/include/u8c/println.h +++ b/include/u8c/println.h @@ -16,13 +16,8 @@ /* Print line */ # if !defined(u8c_sym_println) # define u8c_sym_println -# include <stdint.h> +# include <stdbool.h> # include <stdio.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_println(FILE * fp,uint_least32_t const * const msg,...); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_println(FILE * fp,char32_t const * const msg,...); # endif diff --git a/include/u8c/seterr.h b/include/u8c/seterr.h index ae2f534..3f3e793 100644 --- a/include/u8c/seterr.h +++ b/include/u8c/seterr.h @@ -16,6 +16,7 @@ /* Set error */ # if !defined(u8c_sym_seterr) # define u8c_sym_seterr -# include <stdint.h> -extern uint_least8_t u8c_seterr(uint_least32_t const * const msg); +# include <stdbool.h> +# include <uchar.h> +extern bool u8c_seterr(char32_t const * const msg); # endif diff --git a/include/u8c/setfmt.h b/include/u8c/setfmt.h new file mode 100644 index 0000000..f742bea --- /dev/null +++ b/include/u8c/setfmt.h @@ -0,0 +1,22 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +/* Set format */ +# if !defined(u8c_sym_setfmt) +# define u8c_sym_setfmt +# include <stdbool.h> +# include <stdint.h> +extern bool u8c_setfmt(uint_least8_t const base,uint_least8_t const endian); +# endif diff --git a/include/u8c/thrdsafe.h b/include/u8c/thrdsafe.h index 1de6a31..2ab6709 100644 --- a/include/u8c/thrdsafe.h +++ b/include/u8c/thrdsafe.h @@ -16,12 +16,6 @@ /* Thread-safe */ # if !defined(u8c_sym_thrdsafe) # define u8c_sym_thrdsafe -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t const u8c_thrdsafe; -# if defined(__cplusplus) -} -# endif +# include <stdbool.h> +extern bool const u8c_thrdsafe; # endif diff --git a/include/u8c/u32alloc.h b/include/u8c/u32alloc.h new file mode 100644 index 0000000..5241ee6 --- /dev/null +++ b/include/u8c/u32alloc.h @@ -0,0 +1,23 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +/* UTF-32 allocate */ +# if !defined(u8c_sym_u32alloc) +# define u8c_sym_u32alloc +# include <stdbool.h> +# include <stddef.h> +# include <uchar.h> +extern bool u8c_u32alloc(char32_t * * const u32,size_t const sz); +# endif diff --git a/include/u8c/u32cat.h b/include/u8c/u32cat.h new file mode 100644 index 0000000..7672cb7 --- /dev/null +++ b/include/u8c/u32cat.h @@ -0,0 +1,23 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +/* UTF-32 concatenate */ +# if !defined(luma_sym_u32cat) +# define luma_sym_u32cat +# include <stdbool.h> +# include <stddef.h> +# include <uchar.h> +extern bool u8c_u32cat(size_t * const sz,char32_t const * * const out,char32_t const * const lstr,char32_t const * const rstr); +# endif diff --git a/include/u8c/u32cmp.h b/include/u8c/u32cmp.h index 5ca3d5d..ffc2729 100644 --- a/include/u8c/u32cmp.h +++ b/include/u8c/u32cmp.h @@ -16,12 +16,8 @@ /* UTF-32 compare */ # if !defined(luma_sym_u32cmp) # define luma_sym_u32cmp +# include <stdbool.h> # include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_u32cmp(uint_least8_t * const res,uint_least32_t const * const lstr,uint_least32_t const * const rstr); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_u32cmp(uint_least8_t * const res,char32_t const * const lstr,char32_t const * const rstr); # endif diff --git a/include/u8c/u32cp.h b/include/u8c/u32cp.h index 66cc870..01e2ae1 100644 --- a/include/u8c/u32cp.h +++ b/include/u8c/u32cp.h @@ -16,13 +16,8 @@ /* UTF-32 copy */ # if !defined(luma_sym_u32cp) # define luma_sym_u32cp +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_u32cp(size_t * const sz,uint_least32_t const * * const out,uint_least32_t const * const in); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_u32cp(size_t * const sz,char32_t const * * const out,char32_t const * const in); # endif diff --git a/include/u8c/u32fndchr.h b/include/u8c/u32fndchr.h new file mode 100644 index 0000000..d52682d --- /dev/null +++ b/include/u8c/u32fndchr.h @@ -0,0 +1,23 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +/* UTF-32 find character */ +# if !defined(luma_sym_u32fndchr) +# define luma_sym_u32fndchr +# include <stdbool.h> +# include <stddef.h> +# include <uchar.h> +extern bool u8c_u32fndchr(size_t * const pos,char32_t const * const in,char32_t const chr); +# endif diff --git a/include/u8c/u32fndpat.h b/include/u8c/u32fndpat.h new file mode 100644 index 0000000..a17948b --- /dev/null +++ b/include/u8c/u32fndpat.h @@ -0,0 +1,23 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +/* UTF-32 find pattern */ +# if !defined(luma_sym_u32fndpat) +# define luma_sym_u32fndpat +# include <stdbool.h> +# include <stddef.h> +# include <uchar.h> +extern bool u8c_u32fndpat(size_t * const pos,char32_t const * const in,char32_t const * const pat); +# endif diff --git a/include/u8c/u32free.h b/include/u8c/u32free.h index fd431e0..4525b22 100644 --- a/include/u8c/u32free.h +++ b/include/u8c/u32free.h @@ -13,15 +13,10 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Format */ -# if !defined(u8c_u32free) -# if defined(__cplusplus) -# include <cstdint> -# include <cstdlib> -# define u8c_u32free(u32) (std::free(const_cast<uint_least32_t *>(u32))) -# else -# include <stdint.h> -# include <stdlib.h> -# define u8c_u32free(u32) (free((uint_least32_t *)u32)) -# endif +/* UTF-32 free */ +# if !defined(u8c_sym_u32free) +# define u8c_sym_u32free +# include <stdbool.h> +# include <uchar.h> +extern bool u8c_u32free(char32_t const * * const u32); # endif diff --git a/include/u8c/u32substr.h b/include/u8c/u32substr.h new file mode 100644 index 0000000..e9258a4 --- /dev/null +++ b/include/u8c/u32substr.h @@ -0,0 +1,23 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +/* UTF-32 sub-string */ +# if !defined(luma_sym_u32substr) +# define luma_sym_u32substr +# include <stdbool.h> +# include <stddef.h> +# include <uchar.h> +extern bool u8c_u32substr(char32_t const * * const out,size_t const start,size_t const len,char32_t const * const in); +# endif diff --git a/include/u8c/u32sz.h b/include/u8c/u32sz.h index b97ccdb..f182e92 100644 --- a/include/u8c/u32sz.h +++ b/include/u8c/u32sz.h @@ -16,13 +16,8 @@ /* UTF-32 size */ # if !defined(u8c_sym_u32sz) # define u8c_sym_u32sz +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_u32sz(size_t * sz,uint_least32_t const * in); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_u32sz(size_t * sz,char32_t const * in); # endif diff --git a/src/u8c/err.c b/include/u8c/u8alloc.h index 77a8221..b5c2516 100644 --- a/src/u8c/err.c +++ b/include/u8c/u8alloc.h @@ -13,7 +13,10 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "err.h" +/* UTF-8 allocate */ +# if !defined(u8c_sym_u8alloc) +# define u8c_sym_u8alloc +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -uint_least32_t const * u8c_err = NULL; +extern bool u8c_u8alloc(unsigned char * * const u32,size_t const sz); +# endif diff --git a/include/u8c/u8dec.h b/include/u8c/u8dec.h index eed43ae..2b76df8 100644 --- a/include/u8c/u8dec.h +++ b/include/u8c/u8dec.h @@ -16,13 +16,8 @@ /* UTF-8 decode */ # if !defined(u8c_sym_dec) # define u8c_sym_dec +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_u8dec(size_t * const sz,uint_least32_t const * * const out,uint_least8_t const * const in); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_u8dec(size_t * const sz,char32_t const * * const out,unsigned char const * const in); # endif diff --git a/include/u8c/u8enc.h b/include/u8c/u8enc.h index 8040f8d..096e65a 100644 --- a/include/u8c/u8enc.h +++ b/include/u8c/u8enc.h @@ -16,13 +16,8 @@ /* UTF-8 encode */ # if !defined(u8c_sym_enc) # define u8c_sym_enc +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_u8enc(size_t * const sz,uint_least8_t const * * const out,uint_least32_t const * const in); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_u8enc(size_t * const sz,unsigned char const * * const out,char32_t const * const in); # endif diff --git a/include/u8c/u8free.h b/include/u8c/u8free.h index 1ae3242..f1994dd 100644 --- a/include/u8c/u8free.h +++ b/include/u8c/u8free.h @@ -13,15 +13,9 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Format */ -# if !defined(u8c_u8free) -# if defined(__cplusplus) -# include <cstdint> -# include <cstdlib> -# define u8c_u8free(u8) (std::free(const_cast<std::uint_least8_t *>(u8))) -# else -# include <stdint.h> -# include <stdlib.h> -# define u8c_u8free(u8) (free((uint_least8_t *)u8)) -# endif +/* UTF-8 free */ +# if !defined(u8c_sym_u8free) +# define u8c_sym_u8free +# include <stdbool.h> +extern bool u8c_u8free(unsigned char const * * const u8); # endif diff --git a/src/u8c/stat.h b/include/u8c/unimax.h index 8ca005e..10c8be6 100644 --- a/src/u8c/stat.h +++ b/include/u8c/unimax.h @@ -13,9 +13,9 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Status */ -# if !defined(u8c_sym_stat) -# define u8c_sym_stat +/* Unicode maximum */ +# if !defined(u8c_unimax) # include <stdint.h> -extern uint_least8_t u8c_stat; +# include <uchar.h> +# define u8c_unimax ((char32_t)UINT32_C(0x10FFFF)) # endif diff --git a/include/u8c/ver.h b/include/u8c/ver.h index f10872c..054052d 100644 --- a/include/u8c/ver.h +++ b/include/u8c/ver.h @@ -16,5 +16,5 @@ /* Version */ # if !defined(u8c_ver) # include <stdint.h> -# define u8c_ver (UINT64_C(0x11)) +# define u8c_ver (UINT64_C(0x12)) # endif diff --git a/include/u8c/vfmt.h b/include/u8c/vfmt.h index d4ae9b0..8db9e05 100644 --- a/include/u8c/vfmt.h +++ b/include/u8c/vfmt.h @@ -17,13 +17,8 @@ # if !defined(u8c_sym_vfmt) # define u8c_sym_vfmt # include <stdarg.h> +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_vfmt(size_t * const sz,uint_least32_t const * * const out,uint_least32_t const * const in,va_list args); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_vfmt(size_t * const sz,char32_t const * * const out,char32_t const * const in,va_list args); # endif diff --git a/include/u8c/vprint.h b/include/u8c/vprint.h index ace9612..41e7e41 100644 --- a/include/u8c/vprint.h +++ b/include/u8c/vprint.h @@ -17,13 +17,8 @@ # if !defined(u8c_sym_vprint) # define u8c_sym_vprint # include <stdarg.h> -# include <stdint.h> +# include <stdbool.h> # include <stdio.h> -# if defined(__cplusplus) -extern "C" { -# endif -extern uint_least8_t u8c_vprint(FILE * fp,uint_least32_t const * const msg,va_list args); -# if defined(__cplusplus) -} -# endif +# include <uchar.h> +extern bool u8c_vprint(FILE * fp,char32_t const * const msg,va_list args); # endif diff --git a/src/u8c/abrt.c b/src/u8c/abrt.c new file mode 100644 index 0000000..28361ec --- /dev/null +++ b/src/u8c/abrt.c @@ -0,0 +1,35 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include "dat.h" +# include <inttypes.h> +# include <stdbool.h> +# include <stdint.h> +# include <stdio.h> +# include <stdlib.h> +# include <stdnoreturn.h> +# include <time.h> +# include <u8c/abrt.h> +# include <u8c/dbg.h> +# include <u8c/end.h> +# include <u8c/thrdsafe.h> +# include <u8c/ver.h> +noreturn bool u8c_abrt(char const * const _fl,long long const _ln,char const * const _fn,char const * const _why) { + fprintf(stderr,"u8c: *** Aborted (\"%s\":%lld in function \"%s\": \"%s\" @ %" PRIuMAX ") ***\nLibrary diagnostics:\n debug:%s\n status:%" PRIuLEAST8 "\n thread-safe:%s\n version:%" PRIuLEAST64 "\n",_fl,_ln,_fn,_why,(intmax_t)time(NULL),u8c_dbg ? "true" : "false",u8c_dat.stat,u8c_thrdsafe ? "true" : "false",u8c_ver); + fprintf(stderr,"Trying to clean up...\n"); + u8c_end(); + fprintf(stderr,"Aborting...\n"); + abort(); +} diff --git a/src/u8c/dat.c b/src/u8c/dat.c new file mode 100644 index 0000000..0fcf492 --- /dev/null +++ b/src/u8c/dat.c @@ -0,0 +1,26 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include "dat.h" +# include "dattyp.h" +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <u8c/SIZE_C.h> +struct u8c_dattyp u8c_dat = (struct u8c_dattyp){ + .err = NULL, + .fmtendian = false, + .stat = UINT8_C(0x0), +}; diff --git a/src/u8c/err.h b/src/u8c/dat.h index 1537701..fdda8c7 100644 --- a/src/u8c/err.h +++ b/src/u8c/dat.h @@ -13,8 +13,9 @@ If not, see <https://www.gnu.org/licenses/>. */ -# if !defined(u8c_sym_err) -# define u8c_sym_err -# include <stdint.h> -extern uint_least32_t const * u8c_err; +/* Data */ +# if !defined(u8c_sym_dat) +# define u8c_sym_dat +# include "dattyp.h" +extern struct u8c_dattyp u8c_dat; # endif diff --git a/src/u8c/dattyp.h b/src/u8c/dattyp.h new file mode 100644 index 0000000..d094151 --- /dev/null +++ b/src/u8c/dattyp.h @@ -0,0 +1,36 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# if !defined(u8c_sym_dattyp) +# define u8c_sym_dattyp +# include <stdbool.h> +# include <stdint.h> +# include <u8c/SIZE_C.h> +# include <uchar.h> +# if defined(u8c_bethrdsafe) +# include <threads.h> +# endif +struct u8c_dattyp { + bool fmtendian; + char32_t const * err; + unsigned char pad0[sizeof(void(*)(void)) - SIZE_C(0x1)]; + uint_least8_t fmtbase; + uint_least8_t stat; +# if defined(u8c_bethrdsafe) + mtx_t errlock; + mtx_t fmtlock; +# endif +}; +# endif diff --git a/src/u8c/debug.c b/src/u8c/debug.c index eb926eb..faa668e 100644 --- a/src/u8c/debug.c +++ b/src/u8c/debug.c @@ -13,11 +13,12 @@ If not, see <https://www.gnu.org/licenses/>. */ +# include <stdbool.h> # include <stdint.h> -# include <u8c/debug.h> -uint_least8_t const u8c_debug = +# include <u8c/dbg.h> +bool const u8c_dbg = # if defined(NDEBUG) - UINT8_C(0x0); + false; # else - UINT8_C(0x1); + true; # endif diff --git a/src/u8c/end.c b/src/u8c/end.c index af9661c..633ced7 100644 --- a/src/u8c/end.c +++ b/src/u8c/end.c @@ -13,24 +13,31 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "err.h" -# include "errlock.h" -# include "stat.h" +# include "dat.h" +# include <stdbool.h> # include <stdint.h> # include <stdlib.h> # include <u8c/end.h> +# include <u8c/SIZE_C.h> # include <u8c/u32free.h> # if defined(u8c_bethrdsafe) # include <threads.h> # endif -uint_least8_t u8c_end(void) { - if(u8c_stat) { - return UINT8_C(0x0); +bool u8c_end(void) { + if(u8c_dat.stat) { + return false; } # if defined(u8c_bethrdsafe) - mtx_destroy(&u8c_errlock); + /* Destroy mutexes */ + mtx_destroy(&u8c_dat.errlock); + mtx_destroy(&u8c_dat.fmtlock); # endif - u8c_u32free(u8c_err); - u8c_stat = UINT8_C(0x1); - return UINT8_C(0x0); + /* Free error message: */ + u8c_u32free(&u8c_dat.err); + /* Set default formatting options: */ + u8c_dat.fmtbase = UINT8_C(0xC); + u8c_dat.fmtendian = UINT8_C(0x0); + /* Set status: */ + u8c_dat.stat = UINT8_C(0x1); + return false; } diff --git a/src/u8c/fmt.c b/src/u8c/fmt.c index 67015da..7ea690b 100644 --- a/src/u8c/fmt.c +++ b/src/u8c/fmt.c @@ -14,11 +14,13 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <stdarg.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/fmt.h> +# include <u8c/u32cp.h> # include <u8c/vfmt.h> -uint_least8_t u8c_fmt(size_t * const _outsz,uint_least32_t const * * const _out,uint_least32_t const * const _in,...) { +bool u8c_fmt(size_t * const _outsz,char32_t const * * const _out,char32_t const * const _in,...) { va_list args; va_start(args,_in); uint_least8_t val = u8c_vfmt(_outsz,_out,_in,args); diff --git a/src/u8c/geterr.c b/src/u8c/geterr.c index 9f09327..a63ae20 100644 --- a/src/u8c/geterr.c +++ b/src/u8c/geterr.c @@ -13,26 +13,23 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "err.h" -# include "errlock.h" +# include "dat.h" +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/geterr.h> # include <u8c/seterr.h> # include <u8c/u32cp.h> -# include <u8c/u8free.h> -uint_least8_t u8c_geterr(size_t * const _sz,uint_least32_t const * * const _out) { - if(u8c_err == NULL) { - u8c_seterr((uint_least32_t[]){UINT32_C(0x0)}); - } +# include <u8c/u32free.h> +bool u8c_geterr(size_t * const _sz,char32_t const * * const _out) { # if defined(u8c_bethrdsafe) - mtx_lock(&u8c_errlock); + mtx_lock(&u8c_dat.errlock); # endif - u8c_u32cp(_sz,_out,u8c_err); - u8c_u8free(u8c_err); - u8c_err = NULL; + u8c_u32cp(_sz,_out,u8c_dat.err); + u8c_u32free(&u8c_dat.err); # if defined(u8c_bethrdsafe) - mtx_unlock(&u8c_errlock); + mtx_unlock(&u8c_dat.errlock); # endif - return UINT8_C(0x0); + u8c_seterr((uint_least32_t[]){UINT32_C(0x0),}); + return false; } diff --git a/src/u8c/init.c b/src/u8c/init.c index e13f15a..3c15a1d 100644 --- a/src/u8c/init.c +++ b/src/u8c/init.c @@ -13,23 +13,29 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "err.h" -# include "errlock.h" -# include "stat.h" +# include "dat.h" +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/init.h> +# include <u8c/seterr.h> # include <u8c/u32cp.h> # if defined(u8c_bethrdsafe) # include <threads.h> # endif -uint_least8_t u8c_init(void) { +bool u8c_init(void) { # if defined(u8c_bethrdsafe) - if(mtx_init(&u8c_errlock,mtx_plain) == thrd_error) { - return UINT8_C(0x2); + if(mtx_init(&u8c_dat.errlock,mtx_plain) == thrd_error) { + return true; + } + if(mtx_init(&u8c_dat.fmtlock,mtx_plain) == thrd_error) { + return true; } # endif + /* Set default error message: */ + u8c_dat.err = NULL; + u8c_seterr((uint_least32_t[]){UINT32_C(0x0),}); /* Set status: */ - u8c_stat = UINT8_C(0x0); - return UINT8_C(0x0); + u8c_dat.stat = UINT8_C(0x0); + return false; } diff --git a/src/u8c/isalnum.c b/src/u8c/isalnum.c index ae2e4c8..f8b5686 100644 --- a/src/u8c/isalnum.c +++ b/src/u8c/isalnum.c @@ -14,20 +14,21 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/isalnum.h> # include <u8c/isalpha.h> # include <u8c/isdigit.h> -uint_least8_t u8c_isalnum(uint_least8_t * const _res,uint_least32_t const _chr) { +bool u8c_isalnum(uint_least8_t * const _res,char32_t const _chr) { assert(_res != NULL); uint_least8_t res = UINT8_C(0x0); u8c_isalpha(&res,_chr); if(res) { *_res = res; - return UINT8_C(0x0); + return false; } u8c_isdigit(&res,_chr); *_res = res; - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/isalpha.c b/src/u8c/isalpha.c index bc6d5c0..b960bf5 100644 --- a/src/u8c/isalpha.c +++ b/src/u8c/isalpha.c @@ -14,10 +14,11 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/isalpha.h> -uint_least8_t u8c_isalpha(uint_least8_t * const _res,uint_least32_t const _chr) { +bool u8c_isalpha(uint_least8_t * const _res,char32_t const _chr) { assert(_res != NULL); switch(_chr) { default: @@ -65,13 +66,30 @@ uint_least8_t u8c_isalpha(uint_least8_t * const _res,uint_least32_t const _chr) case UINT32_C(0xFC): /* U WITH TWO DOTS */ case UINT32_C(0xFD): /* LATIN SMALL LETTER Y WITH ACUTE */ case UINT32_C(0xFE): /* LATIN SMALL LETTER THORN */ + case UINT32_C(0x105): /* LATIN SMALL LETTER A WITH OGONEK */ + case UINT32_C(0x107): /* LATIN SMALL LETTER C WITH ACUTE */ + case UINT32_C(0x10D): /* LATIN SMALL LETTER C WITH CARON */ + case UINT32_C(0x10F): /* LATIN SMALL LETTER D WITH CARON */ + case UINT32_C(0x119): /* LATIN SMALL LETTER E WITH OGONEK */ + case UINT32_C(0x11B): /* LATIN SMALL LETTER E WITH CARON */ case UINT32_C(0x11F): /* LATIN SMALL LETTER G WITH BREVE */ case UINT32_C(0x131): /* LATIN SMALL LETTER DOTLESS I */ case UINT32_C(0x133): /* LATIN SMALL LIGATURE LJ */ case UINT32_C(0x138): /* LATIN SMALL LETTER KRA */ + case UINT32_C(0x142): /* LATIN SMALL LETTER L WITH STROKE */ + case UINT32_C(0x144): /* LATIN SMALL LETTER N WITH ACUTE */ + case UINT32_C(0x148): /* LATIN SMALL LETTER N WITH CARON */ case UINT32_C(0x14B): /* LATIN SMALL LETTER ENG */ case UINT32_C(0x153): /* LATIN SMALL LIGATURE OE */ + case UINT32_C(0x159): /* LATIN SMALL LETTER R WITH CARON */ + case UINT32_C(0x15B): /* LATIN SMALL LETTER S WITH ACUTE */ case UINT32_C(0x15F): /* LATIN SMALL LETTER S WITH CEDILLA */ + case UINT32_C(0x161): /* LATIN SMALL LETTER S WITH CARON */ + case UINT32_C(0x165): /* LATIN SMALL LETTER T WITH CARON */ + case UINT32_C(0x16F): /* LATIN SMALL LETTER U WITH RING ABOVE */ + case UINT32_C(0x17A): /* LATIN SMALL LETTER Z WITH ACUTE */ + case UINT32_C(0x17C): /* LATIN SMALL LETTER Z WITH DOT ABOVE */ + case UINT32_C(0x17E): /* LATIN SMALL LETTER Z WITH CARON */ case UINT32_C(0x1BF): /* LATIN LETTER WYNN */ case UINT32_C(0x1DD): /* LATIN SMALL LETTER TURNED E */ case UINT32_C(0x21D): /* LATIN SMALL LETTER YOGH */ @@ -86,10 +104,36 @@ uint_least8_t u8c_isalpha(uint_least8_t * const _res,uint_least32_t const _chr) case UINT32_C(0x28B): /* LATIN SMALL LETTER V WITH HOOK */ case UINT32_C(0x292): /* LATIN SMALL LETTER EZH */ case UINT32_C(0x294): /* LATIN SMALL LETTER GLOTTAL STOP */ + case UINT32_C(0x3B1): /* GREEK SMALL LETTER ALPHA */ + case UINT32_C(0x3B2): /* GREEK SMALL LETTER BETA */ + case UINT32_C(0x3B3): /* GREEK SMALL LETTER GAMMA */ + case UINT32_C(0x3B4): /* GREEK SMALL LETTER DELTA */ + case UINT32_C(0x3B5): /* GREEK SMALL LETTER EPSILON */ + case UINT32_C(0x3B6): /* GREEK SMALL LETTER ZETA */ + case UINT32_C(0x3B7): /* GREEK SMALL LETTER ETA */ + case UINT32_C(0x3B8): /* GREEK SMALL LETTER THETA */ + case UINT32_C(0x3B9): /* GREEK SMALL LETTER IOTA */ + case UINT32_C(0x3BA): /* GREEK SMALL LETTER KAPPA */ + case UINT32_C(0x3BB): /* GREEK SMALL LETTER LAMBDA */ + case UINT32_C(0x3BC): /* GREEK SMALL LETTER MU */ + case UINT32_C(0x3BD): /* GREEK SMALL LETTER NU */ + case UINT32_C(0x3BE): /* GREEK SMALL LETTER XI */ + case UINT32_C(0x3BF): /* GREEK SMALL LETTER OMICRON */ + case UINT32_C(0x3C0): /* GREEK SMALL LETTER PI */ + case UINT32_C(0x3C1): /* GREEK SMALL LETTER RHO */ + case UINT32_C(0x3C2): /* GREEK SMALL LETTER FINAL SIGMA */ + case UINT32_C(0x3C3): /* GREEK SMALL LETTER SIGMA */ + case UINT32_C(0x3C4): /* GREEK SMALL LETTER TAU */ + case UINT32_C(0x3C5): /* GREEK SMALL LETTER UPSILON */ + case UINT32_C(0x3C6): /* GREEK SMALL LETTER PHI */ + case UINT32_C(0x3C7): /* GREEK SMALL LETTER CHI */ + case UINT32_C(0x3C8): /* GREEK SMALL LETTER PSI */ + case UINT32_C(0x3C9): /* GREEK SMALL LETTER OMEGA */ case UINT32_C(0x1D79): /* LATIN SMALL LETTER INSULAR G */ case UINT32_C(0xA7B7): /* LATIN SMALL LETTER OMEGA */ + case UINT32_C(0xFB00): /* LATIN SMALL LIGATURE FF */ *_res = UINT8_C(0x1); break; } - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/iscntrl.c b/src/u8c/iscntrl.c index 75d3fa5..4d698b2 100644 --- a/src/u8c/iscntrl.c +++ b/src/u8c/iscntrl.c @@ -14,10 +14,11 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/iscntrl.h> -uint_least8_t u8c_iscntrl(uint_least8_t * const _res,uint_least32_t const _chr) { +bool u8c_iscntrl(uint_least8_t * const _res,char32_t const _chr) { assert(_res != NULL); switch(_chr) { default: @@ -58,5 +59,5 @@ uint_least8_t u8c_iscntrl(uint_least8_t * const _res,uint_least32_t const _chr) *_res = UINT8_C(0x1); break; } - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/isdigit.c b/src/u8c/isdigit.c index ebfd7c2..e4c6f78 100644 --- a/src/u8c/isdigit.c +++ b/src/u8c/isdigit.c @@ -14,10 +14,11 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/isdigit.h> -uint_least8_t u8c_isdigit(uint_least8_t * const _res,uint_least32_t const _chr) { +bool u8c_isdigit(uint_least8_t * const _res,char32_t const _chr) { assert(_res != NULL); switch(_chr) { default: @@ -38,5 +39,5 @@ uint_least8_t u8c_isdigit(uint_least8_t * const _res,uint_least32_t const _chr) *_res = UINT8_C(0x1); break; } - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/ispunct.c b/src/u8c/ispunct.c index abdd72b..0298518 100644 --- a/src/u8c/ispunct.c +++ b/src/u8c/ispunct.c @@ -14,10 +14,11 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/ispunct.h> -uint_least8_t u8c_ispunct(uint_least8_t * const _res,uint_least32_t const _chr) { +bool u8c_ispunct(uint_least8_t * const _res,char32_t const _chr) { assert(_res != NULL); switch(_chr) { default: @@ -89,13 +90,51 @@ uint_least8_t u8c_ispunct(uint_least8_t * const _res,uint_least32_t const _chr) case UINT32_C(0x20AC): /* EURO SIGN */ case UINT32_C(0x20B9): /* INDIAN RUPEE SIGN */ case UINT32_C(0x20BF): /* BITCOIN SIGN */ + case UINT32_C(0x2103): /* DEGREE CELSIUS */ + case UINT32_C(0x2107): /* EULER CONSTANT */ + case UINT32_C(0x2109): /* DEGREE FAHRENHEIT */ + case UINT32_C(0x210E): /* PLANCK CONSTANT */ + case UINT32_C(0x2117): /* SOUND RECORDING COPYRIGHT */ + case UINT32_C(0x2122): /* TRADE MARK SIGN */ + case UINT32_C(0x2125): /* OUNCE SIGN */ + case UINT32_C(0x2126): /* OHM SIGN */ + case UINT32_C(0x212A): /* KELVIN SIGN */ + case UINT32_C(0x214D): /* AKTIESELSKAB */ case UINT32_C(0x2205): /* EMPTY SET */ case UINT32_C(0x2212): /* MINUS SIGN */ + case UINT32_C(0x221A): /* SQUARE ROOT */ + case UINT32_C(0x221B): /* CUBE ROOT */ + case UINT32_C(0x221C): /* FOURTH ROOT */ case UINT32_C(0x221E): /* INFINITY */ case UINT32_C(0x2248): /* ALMOST EQUAL TO */ case UINT32_C(0x2260): /* NOT EQUAL TO */ case UINT32_C(0x2264): /* LESS-THAN OR EQUAL TO */ case UINT32_C(0x2265): /* GREATER-THAN OR EQUAL TO */ + case UINT32_C(0x2609): /* SUN */ + case UINT32_C(0x263F): /* MERCURY */ + case UINT32_C(0x2640): /* FEMALE SIGN */ + case UINT32_C(0x2641): /* EARTH */ + case UINT32_C(0x2642): /* MALE SIGN */ + case UINT32_C(0x2643): /* JUPITER */ + case UINT32_C(0x2644): /* SATURN */ + case UINT32_C(0x2645): /* URANUS */ + case UINT32_C(0x2646): /* NEPTUNE */ + case UINT32_C(0x2647): /* PLUTO */ + case UINT32_C(0x26A2): /* DOUBLED FEMALE SIGN */ + case UINT32_C(0x26A3): /* DOUBLED MALE SIGN */ + case UINT32_C(0x26A4): /* INTERLOCKED FEMALE AND MALE SIGN */ + case UINT32_C(0x26A5): /* MALE AND FEMALE SIGN */ + case UINT32_C(0x26B3): /* CERES */ + case UINT32_C(0x26B4): /* PALLAS */ + case UINT32_C(0x26B5): /* JUNO */ + case UINT32_C(0x26B6): /* VESTA */ + case UINT32_C(0x26B7): /* CHIRON */ + case UINT32_C(0x2BD8): /* PROSERPINA */ + case UINT32_C(0x2BD9): /* ASTRAEA */ + case UINT32_C(0x2BDA): /* HYGIEA */ + case UINT32_C(0x2BDB): /* PHOLOS */ + case UINT32_C(0x2BDC): /* NESSUS */ + case UINT32_C(0x33D7): /* SQUARE PH */ case UINT32_C(0xFDFC): /* RIAL SIGN */ case UINT32_C(0x1F10D): /* CIRCLED ZERO WITH SLASH */ case UINT32_C(0x1F10E): /* CIRCLED ANTICKLOCKWISE ARROW */ @@ -107,5 +146,5 @@ uint_least8_t u8c_ispunct(uint_least8_t * const _res,uint_least32_t const _chr) *_res = UINT8_C(0x1); break; } - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/isspace.c b/src/u8c/isspace.c index 8b1ad9b..deca03c 100644 --- a/src/u8c/isspace.c +++ b/src/u8c/isspace.c @@ -14,10 +14,11 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/isspace.h> -uint_least8_t u8c_isspace(uint_least8_t * const _res,uint_least32_t const _chr) { +bool u8c_isspace(uint_least8_t * const _res,char32_t const _chr) { assert(_res != NULL); switch(_chr) { default: @@ -32,5 +33,5 @@ uint_least8_t u8c_isspace(uint_least8_t * const _res,uint_least32_t const _chr) *_res = UINT8_C(0x1); break; } - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/isxdigit.c b/src/u8c/isxdigit.c new file mode 100644 index 0000000..ae12ae3 --- /dev/null +++ b/src/u8c/isxdigit.c @@ -0,0 +1,47 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <u8c/isxdigit.h> +bool u8c_isxdigit(uint_least8_t * const _res,char32_t const _chr) { + assert(_res != NULL); + switch(_chr) { + default: + *_res = UINT8_C(0x0); + break; + case UINT32_C(0x30): /* DIGIT ZERO */ + case UINT32_C(0x31): /* DIGIT ONE */ + case UINT32_C(0x32): /* DIGIT TWO */ + case UINT32_C(0x33): /* DIGIT THREE */ + case UINT32_C(0x34): /* DIGIT FOUR */ + case UINT32_C(0x35): /* DIGIT FIVE */ + case UINT32_C(0x36): /* DIGIT SIX */ + case UINT32_C(0x37): /* DIGIT SEVEN */ + case UINT32_C(0x38): /* DIGIT EIGHT */ + case UINT32_C(0x39): /* DIGIT NINE */ + case UINT32_C(0x41): /* LATIN CAPITAL LETTER A */ + case UINT32_C(0x42): /* LATIN CAPITAL LETTER B */ + case UINT32_C(0x43): /* LATIN CAPITAL LETTER C */ + case UINT32_C(0x44): /* LATIN CAPITAL LETTER D */ + case UINT32_C(0x45): /* LATIN CAPITAL LETTER E */ + case UINT32_C(0x46): /* LATIN CAPITAL LETTER F */ + *_res = UINT8_C(0x1); + break; + } + return false; +} diff --git a/src/u8c/print.c b/src/u8c/print.c index d8fb7d7..5c34749 100644 --- a/src/u8c/print.c +++ b/src/u8c/print.c @@ -14,10 +14,11 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <stdarg.h> +# include <stdbool.h> # include <stdint.h> # include <u8c/print.h> # include <u8c/vprint.h> -uint_least8_t u8c_print(FILE * _fp,uint_least32_t const * const _msg,...) { +bool u8c_print(FILE * _fp,char32_t const * const _msg,...) { va_list args; va_start(args,_msg); uint_least8_t val = u8c_vprint(_fp,_msg,args); diff --git a/src/u8c/println.c b/src/u8c/println.c index dcf592d..fbfd046 100644 --- a/src/u8c/println.c +++ b/src/u8c/println.c @@ -15,22 +15,23 @@ */ # include <assert.h> # include <stdarg.h> +# include <stdbool.h> # include <stdint.h> # include <stdio.h> # include <u8c/println.h> # include <u8c/seterr.h> # include <u8c/vprint.h> -uint_least8_t u8c_println(FILE * _fp,uint_least32_t const * const _msg,...) { +bool u8c_println(FILE * _fp,char32_t const * const _msg,...) { assert(_fp != NULL); va_list args; va_start(args,_msg); if(u8c_vprint(_fp,_msg,args)) { - return UINT8_C(0x1); + return true; } if(fputc(0xA,stdout) == EOF) { u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x70),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x6C),UINT32_C(0x6E),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x66),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x62),UINT32_C(0x6C),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x77),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x73),UINT32_C(0x74),UINT32_C(0x64),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_println: fputc: Unable to write to stdout. */ - return UINT8_C(0x0); + return false; } va_end(args); - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/seterr.c b/src/u8c/seterr.c index c500ff4..7c7fb12 100644 --- a/src/u8c/seterr.c +++ b/src/u8c/seterr.c @@ -13,9 +13,9 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "err.h" -# include "errlock.h" +# include "dat.h" # include <assert.h> +# include <stdbool.h> # include <stdint.h> # include <stdlib.h> # include <u8c/dbgprint.h> @@ -25,16 +25,16 @@ # if defined(u8c_bethrdsafe) # include <threads.h> # endif -uint_least8_t u8c_seterr(uint_least32_t const * const _msg) { +bool u8c_seterr(char32_t const * const _msg) { assert(_msg != NULL); - u8c_dbgprint(_msg); + //u8c_dbgprint(_msg); # if defined(u8c_bethrdsafe) - mtx_lock(&u8c_errlock); + mtx_lock(&u8c_dat.errlock); # endif - u8c_u32free(u8c_err); - u8c_u32cp(NULL,&u8c_err,_msg); + u8c_u32free(&u8c_dat.err); + u8c_u32cp(NULL,&u8c_dat.err,_msg); # if defined(u8c_bethrdsafe) - mtx_unlock(&u8c_errlock); + mtx_unlock(&u8c_dat.errlock); # endif - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/setfmt.c b/src/u8c/setfmt.c new file mode 100644 index 0000000..70b5c93 --- /dev/null +++ b/src/u8c/setfmt.c @@ -0,0 +1,42 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include "dat.h" +# include <stdbool.h> +# include <stdint.h> +# include <u8c/seterr.h> +# include <u8c/setfmt.h> +# if defined(u8c_bethrdsafe) +# include <threads.h> +# endif +bool u8c_setfmt(unsigned char const _base,unsigned char const _endian) { + uint_least8_t base = _base; + uint_least8_t endian = _endian; + if(_base > UINT8_C(0x20)) { + base = UINT8_C(0xC); + } + if(_endian > UINT8_C(0x1)) { + endian = UINT8_C(0x0); + } +# if defined(u8c_bethrdsafe) + mtx_lock(&u8c_dat.fmtlock); +# endif + u8c_dat.fmtbase = base; + u8c_dat.fmtendian = endian; +# if defined(u8c_bethrdsafe) + mtx_unlock(&u8c_dat.fmtlock); +# endif + return false; +} diff --git a/src/u8c/thrdsafe.c b/src/u8c/thrdsafe.c index 3cb4c4d..c32a2e7 100644 --- a/src/u8c/thrdsafe.c +++ b/src/u8c/thrdsafe.c @@ -13,11 +13,12 @@ If not, see <https://www.gnu.org/licenses/>. */ +# include <stdbool.h> # include <stdint.h> # include <u8c/thrdsafe.h> -uint_least8_t const u8c_thrdsafe = +bool const u8c_thrdsafe = # if defined(u8c_bethrdsafe) - UINT8_C(0x1); + true; # else - UINT8_C(0x0); + false; # endif diff --git a/src/u8c/u32alloc.c b/src/u8c/u32alloc.c new file mode 100644 index 0000000..804d564 --- /dev/null +++ b/src/u8c/u32alloc.c @@ -0,0 +1,29 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <stdbool.h> +# include <stdlib.h> +# include <u8c/seterr.h> +# include <u8c/u32alloc.h> +# include <uchar.h> +bool u8c_u32alloc(char32_t * * const _u32,size_t const _sz) { + char32_t * arr = NULL; + if((arr = calloc(sizeof *arr,_sz)) == NULL) { + u8c_seterr(U"u8c_u32alloc: Unable to allocate resources (not enough memory?)."); + return true; + } + *_u32 = arr; + return false; +} diff --git a/src/u8c/u32cat.c b/src/u8c/u32cat.c new file mode 100644 index 0000000..409030e --- /dev/null +++ b/src/u8c/u32cat.c @@ -0,0 +1,53 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stdlib.h> +# include <u8c/SIZE_C.h> +# include <u8c/seterr.h> +# include <u8c/u32alloc.h> +# include <u8c/u32cat.h> +# include <u8c/u32cp.h> +# include <u8c/u32free.h> +# include <u8c/u32sz.h> +# include <uchar.h> +bool u8c_u32cat(size_t * const _sz,char32_t const * * const _out,char32_t const * const _lstr,char32_t const * const _rstr) { + assert(_out != NULL); + assert(_lstr != NULL); + assert(_rstr != NULL); + size_t sz = SIZE_C(0x0); + size_t lsz = SIZE_C(0x0); + size_t rsz = SIZE_C(0x0); + u8c_u32sz(&lsz,_lstr); + u8c_u32sz(&rsz,_rstr); + sz = lsz + rsz - SIZE_C(0x1); + if(_sz != NULL) { + *_sz = sz; + } + char32_t * out = NULL; + if(u8c_u32alloc(&out,sz + SIZE_C(0x1))) { + return false; + } + for(register size_t n = SIZE_C(0x0);n < lsz - SIZE_C(0x1);n += SIZE_C(0x1)) { + out[n] = _lstr[n]; + } + for(register size_t n = SIZE_C(0x0);n < rsz;n += SIZE_C(0x1)) { + out[n + lsz - SIZE_C(0x1)] = _rstr[n]; + } + u8c_u32free(_out); + *_out = out; + return false; +} diff --git a/src/u8c/u32cmp.c b/src/u8c/u32cmp.c index 3f8e405..1dabfef 100644 --- a/src/u8c/u32cmp.c +++ b/src/u8c/u32cmp.c @@ -14,31 +14,32 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> # include <stdint.h> # include <u8c/seterr.h> # include <u8c/SIZE_C.h> # include <u8c/u32cmp.h> -uint_least8_t u8c_u32cmp(uint_least8_t * const _res,uint_least32_t const * const _lstr,uint_least32_t const * const _rstr) { +bool u8c_u32cmp(uint_least8_t * const _res,char32_t const * const _lstr,char32_t const * const _rstr) { assert(_res != NULL); assert(_lstr != NULL); assert(_rstr != NULL); for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { - register uint_least32_t const lchr = _lstr[n]; - register uint_least32_t const rchr = _rstr[n]; + register char32_t const lchr = _lstr[n]; + register char32_t const rchr = _rstr[n]; if(lchr != rchr) { if(lchr < rchr) { *_res = UINT8_C(0x0); - return UINT8_C(0x0); + return false; } *_res = UINT8_C(0x2); - return UINT8_C(0x0); + return false; } if(lchr == UINT32_C(0x0)) { *_res = UINT8_C(0x1); - return UINT8_C(0x0); + return false; } } u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x33),UINT32_C(0x32),UINT32_C(0x63),UINT32_C(0x6D),UINT32_C(0x70),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u32cmp: Unterminated input. */ - return UINT8_C(0x1); + return true; } diff --git a/src/u8c/u32cp.c b/src/u8c/u32cp.c index 35e8940..979686f 100644 --- a/src/u8c/u32cp.c +++ b/src/u8c/u32cp.c @@ -14,28 +14,32 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> -# include <stdint.h> +# include <stdbool.h> # include <stdlib.h> -# include <u8c/seterr.h> # include <u8c/SIZE_C.h> +# include <u8c/seterr.h> +# include <u8c/u32alloc.h> # include <u8c/u32cp.h> +# include <u8c/u32free.h> # include <u8c/u32sz.h> -uint_least8_t u8c_u32cp(size_t * const _sz,uint_least32_t const * * const _out,uint_least32_t const * const _in) { +bool u8c_u32cp(size_t * const _sz,char32_t const * * const _out,char32_t const * const _in) { + assert(_out != NULL); assert(_in != NULL); + if(*_out != NULL) { + u8c_u32free(&*_out); + } size_t insz = SIZE_C(0x0); u8c_u32sz(&insz,_in); - assert(insz > SIZE_C(0x0)); if(_sz != NULL) { *_sz = insz; } - uint_least32_t * out; - if((out = calloc(sizeof(uint_least32_t),insz)) == NULL) { - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x33),UINT32_C(0x32),UINT32_C(0x63),UINT32_C(0x70),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x62),UINT32_C(0x6C),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x61),UINT32_C(0x6C),UINT32_C(0x6C),UINT32_C(0x6F),UINT32_C(0x63),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0x73),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x72),UINT32_C(0x63),UINT32_C(0x65),UINT32_C(0x73),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u32cp: Unable to allocate resources. */ - return UINT8_C(0x1); + uint_least32_t * out = NULL; + if(u8c_u32alloc(&out,insz + SIZE_C(0x1))) { + return false; } for(register size_t n = SIZE_C(0x0);n < insz;n += SIZE_C(0x1)) { out[n] = _in[n]; } *_out = out; - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/u32fndchr.c b/src/u8c/u32fndchr.c new file mode 100644 index 0000000..ca83998 --- /dev/null +++ b/src/u8c/u32fndchr.c @@ -0,0 +1,40 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <u8c/SIZE_C.h> +# include <u8c/seterr.h> +# include <u8c/u32fndchr.h> +# include <u8c/u32sz.h> +bool u8c_u32fndchr(size_t * const _pos,char32_t const * const _in,char32_t const _chr) { + assert(_pos != NULL); + assert(_in != NULL); + for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { + if(_in[n] == UINT32_C(0x0) && _chr != UINT32_C(0x0)) { + *_pos = SIZE_C(-0x1); + return true; + } + if(_in[n] == _chr) { + *_pos = n; + return false; + } + } + u8c_seterr(U"u8c_u32fndchr: Unterminated input."); + *_pos = SIZE_C(-0x1); + return true; +} diff --git a/src/u8c/u32fndpat.c b/src/u8c/u32fndpat.c new file mode 100644 index 0000000..05071e5 --- /dev/null +++ b/src/u8c/u32fndpat.c @@ -0,0 +1,51 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <u8c/SIZE_C.h> +# include <u8c/seterr.h> +# include <u8c/u32cmp.h> +# include <u8c/u32fndpat.h> +# include <u8c/u32free.h> +# include <u8c/u32substr.h> +# include <u8c/u32sz.h> +bool u8c_u32fndpat(size_t * const _pos,char32_t const * const _in,char32_t const * const _pat) { + assert(_pos != NULL); + assert(_in != NULL); + size_t insz = SIZE_C(0x0); + size_t patsz = SIZE_C(0x0); + u8c_u32sz(&insz,_in); + u8c_u32sz(&patsz,_pat); + if(insz == SIZE_C(0x1) || insz < patsz) { + *_pos = SIZE_C(-0x1); + return false; + } + for(register size_t n = SIZE_C(0x0);n < insz - patsz;n += SIZE_C(0x1)) { + char32_t const * str = NULL; + u8c_u32substr(&str,n,patsz - SIZE_C(0x1),_in); + uint_least8_t cmpres = UINT8_C(0x0); + u8c_u32cmp(&cmpres,str,_pat); + u8c_u32free(&str); + if(cmpres == UINT8_C(0x1)) { + *_pos = n; + return false; + } + } + *_pos = SIZE_C(-0x1); + return false; +} diff --git a/src/u8c/stat.c b/src/u8c/u32free.c index 63214ec..62b43ef 100644 --- a/src/u8c/stat.c +++ b/src/u8c/u32free.c @@ -13,10 +13,12 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "stat.h" +# include <stdbool.h> # include <stdint.h> -/* - 0x0 = Finished. - 0x1 = Initialised. -*/ -uint_least8_t u8c_stat = UINT8_C(0x0); +# include <stdlib.h> +# include <u8c/u32free.h> +bool u8c_u32free(char32_t const * * const _u32) { + free((char32_t *)*_u32); + *_u32 = NULL; + return false; +} diff --git a/src/u8c/u32substr.c b/src/u8c/u32substr.c new file mode 100644 index 0000000..2a30b3b --- /dev/null +++ b/src/u8c/u32substr.c @@ -0,0 +1,48 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stdlib.h> +# include <u8c/SIZE_C.h> +# include <u8c/u32alloc.h> +# include <u8c/u32free.h> +# include <u8c/u32substr.h> +# include <u8c/u32sz.h> +# include <uchar.h> +bool u8c_u32substr(char32_t const * * const _out,size_t const _start,size_t const _len,char32_t const * const _in) { + assert(_out != NULL); + assert(_in != NULL); + u8c_u32free(_out); + size_t insz = SIZE_C(0x0); + u8c_u32sz(&insz,_in); + size_t len = _len; + if(_len == SIZE_C(0x0)) { + len = insz - _start; + } + if(insz < _start + len) { + return false; + } + size_t const outsz = len + SIZE_C(0x2); + char32_t * out = NULL; + if(u8c_u32alloc(&out,outsz)) { + return false; + } + for(register size_t n = SIZE_C(0x0);n <= len;n += SIZE_C(0x1)) { + out[n] = _in[n + _start]; + } + *_out = out; + return false; +} diff --git a/src/u8c/u32sz.c b/src/u8c/u32sz.c index ca8b1be..9dc2108 100644 --- a/src/u8c/u32sz.c +++ b/src/u8c/u32sz.c @@ -14,20 +14,18 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> # include <stddef.h> -# include <stdint.h> -# include <u8c/seterr.h> # include <u8c/SIZE_C.h> +# include <u8c/u32fndchr.h> # include <u8c/u32sz.h> -uint_least8_t u8c_u32sz(size_t * const _sz,uint_least32_t const * const _u32) { +# include <uchar.h> +bool u8c_u32sz(size_t * const _sz,char32_t const * const _in) { assert(_sz != NULL); - assert(_u32 != NULL); - for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { - if(_u32[n] == UINT32_C(0x0)) { - *_sz = n + SIZE_C(0x1); - return UINT8_C(0x0); - } + size_t sz = SIZE_C(0x0); + if(u8c_u32fndchr(&sz,_in,U'\0')) { + return true; } - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x33),UINT32_C(0x32),UINT32_C(0x73),UINT32_C(0x7A),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u32sz: Unterminated input. */ - return UINT8_C(0x1); + *_sz = sz; + return true; } diff --git a/include/u8c/txt.h b/src/u8c/u8alloc.c index 4959e81..a364acf 100644 --- a/include/u8c/txt.h +++ b/src/u8c/u8alloc.c @@ -13,17 +13,16 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Text */ -# if !defined(__STDC_UTF_32__) -# error UTF-32 is required to use u8c_txt. -# else -# if !defined(u8c_txt) -# if defined(__cplusplus) -# include <cstdint> -# define u8c_txt(txt) (reinterpret_cast<std::uint_least32_t const *>(U ## txt)) -# else -# include <stdint.h> -# define u8c_txt(txt) (uint_least32_t const *)U ## txt -# endif -# endif -# endif +# include <stdbool.h> +# include <stdlib.h> +# include <u8c/seterr.h> +# include <u8c/u8alloc.h> +bool u8c_u8alloc(unsigned char * * const _u8,size_t const _sz) { + unsigned char * arr = NULL; + if((arr = calloc(sizeof *arr,_sz)) == NULL) { + u8c_seterr(U"u8c_u8alloc: Unable to allocate resources (not enough memory?)."); + return true; + } + *_u8 = arr; + return false; +} diff --git a/src/u8c/u8dec.c b/src/u8c/u8dec.c index dfbfeaf..bbc6e94 100644 --- a/src/u8c/u8dec.c +++ b/src/u8c/u8dec.c @@ -14,24 +14,29 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> +# include <stddef.h> # include <stdint.h> -# include <stdlib.h> +# include <u8c/SIZE_C.h> # include <u8c/seterr.h> +# include <u8c/u32alloc.h> +# include <u8c/u32free.h> # include <u8c/u8dec.h> -# include <u8c/SIZE_C.h> -uint_least8_t u8c_u8dec(size_t * const _sz,uint_least32_t const * * const _out,uint_least8_t const * const _in) { +# include <uchar.h> +bool u8c_u8dec(size_t * const _sz,char32_t const * * const _out,unsigned char const * const _in) { + assert(_out != NULL); assert(_in != NULL); register size_t insz = SIZE_C(0x0); register size_t outsz = SIZE_C(0x1); for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;outsz += SIZE_C(0x1)) { /* First pass: get size of input array and determine size of output array. */ - register uint_least8_t const tmp = _in[n]; + register unsigned char const tmp = _in[n]; if(tmp == UINT8_C(0x0)) { /* Null-terminator: end of string has been reached. */ - insz = n + SIZE_C(0x1); + insz = n; goto nottoobig; } if(tmp >= UINT8_C(0xF8)) { /* Too big. */ - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x64),UINT32_C(0x65),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x43),UINT32_C(0x68),UINT32_C(0x61),UINT32_C(0x72),UINT32_C(0x61),UINT32_C(0x63),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x20),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x20),UINT32_C(0x6F),UINT32_C(0x66),UINT32_C(0x20),UINT32_C(0x72),UINT32_C(0x61),UINT32_C(0x6E),UINT32_C(0x67),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x28),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x62),UINT32_C(0x69),UINT32_C(0x67),UINT32_C(0x29),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u8dec: Character out of range (too big). */ - return UINT8_C(0x1); + u8c_seterr(U"u8c_u8dec: Character out of range (too big)."); + return true; } if(tmp >= UINT8_C(0xF0)) { /* Four byte. */ n += SIZE_C(0x4); @@ -49,13 +54,16 @@ uint_least8_t u8c_u8dec(size_t * const _sz,uint_least32_t const * * const _out,u n += SIZE_C(0x1); } /* Input is not null-terminated. */ - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x64),UINT32_C(0x65),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u8dec: Unterminated input. */ - return UINT8_C(0x1); + u8c_seterr(U"u8c_u8dec: Unterminated input."); + return true; nottoobig:; if(_sz != NULL) { *_sz = outsz; } - uint_least32_t * out = calloc(sizeof(uint_least32_t),outsz); + uint_least32_t * out = NULL; + if(u8c_u32alloc(&out,outsz + SIZE_C(0x1))) { + return false; + } for(register size_t n = SIZE_C(0x0),outn = SIZE_C(0x0);n < insz;outn += SIZE_C(0x1)) { /* Second pass: decode UTF-8. */ if(_in[n] >= UINT8_C(0xF0)) { /* Four byte. */ uint_least32_t codep = (_in[n] ^ UINT32_C(0xF0)) << UINT32_C(0x12); @@ -92,6 +100,7 @@ nottoobig:; n += SIZE_C(0x1); continue; } + u8c_u32free(_out); *_out = out; - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/u8enc.c b/src/u8c/u8enc.c index dda62d3..60bc724 100644 --- a/src/u8c/u8enc.c +++ b/src/u8c/u8enc.c @@ -14,20 +14,26 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <assert.h> +# include <stdbool.h> +# include <stddef.h> # include <stdint.h> -# include <stdlib.h> +# include <u8c/SIZE_C.h> # include <u8c/seterr.h> +# include <u8c/u8alloc.h> # include <u8c/u8enc.h> -# include <u8c/SIZE_C.h> -uint_least8_t u8c_u8enc(size_t * const _sz,uint_least8_t const * * const _out,uint_least32_t const * const _in) { +# include <u8c/u8free.h> +# include <u8c/unimax.h> +# include <uchar.h> +bool u8c_u8enc(size_t * const _sz,unsigned char const * * const _out,char32_t const * const _in) { + assert(_out != NULL); assert(_in != NULL); size_t insz = SIZE_C(0x0); /* Size of input array (bytes). */ size_t outsz = SIZE_C(0x0); /* Size of output array /bytes). */ for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { /* First pass: get size of input array, and determine size of output array. */ - register uint_least32_t const tmp = _in[n]; - if(tmp >= UINT32_C(0x110000)) { /* Codepoint out of range. */ - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x65),UINT32_C(0x6E),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x43),UINT32_C(0x6F),UINT32_C(0x64),UINT32_C(0x65),UINT32_C(0x70),UINT32_C(0x6F),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x20),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x20),UINT32_C(0x6F),UINT32_C(0x66),UINT32_C(0x20),UINT32_C(0x72),UINT32_C(0x61),UINT32_C(0x6E),UINT32_C(0x67),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x28),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x62),UINT32_C(0x69),UINT32_C(0x67),UINT32_C(0x29),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u8enc: Codepoint out of range (too big). */ - return UINT8_C(0x1); + register char32_t const tmp = _in[n]; + if(tmp > u8c_unimax) { /* Codepoint out of range. */ + u8c_seterr(U"u8c_u8enc: Codepoint out of range (too big)."); + return true; } if(tmp >= UINT32_C(0x10000)) { /* 4 bytes. */ outsz += SIZE_C(0x4); @@ -48,15 +54,19 @@ uint_least8_t u8c_u8enc(size_t * const _sz,uint_least8_t const * * const _out,ui goto nottoobig; } } - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x65),UINT32_C(0x6E),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u8enc: Unterminated input. */ - return UINT8_C(0x1); + u8c_seterr(U"u8c_u8enc: Unterminated input."); + return true; nottoobig:; if(_sz != NULL) { *_sz = outsz; } - uint_least8_t * out = calloc(sizeof(uint_least8_t),outsz); /* Allocate space for output array. */ + unsigned char * out = NULL; + if(u8c_u8alloc(&out,outsz + SIZE_C(0x1))) { + u8c_seterr(U"u8c_u32enc: Unable to allocate resources (not enough memory?)."); + return true; + } for(register size_t n = SIZE_C(0x0), outn = SIZE_C(0x0);n < insz;n += SIZE_C(0x1),outn += SIZE_C(0x1)) { /* Second pass: encode each codepoint into UTF-8. */ - register uint_least32_t const tmp = _in[n]; + register char32_t const tmp = _in[n]; if(tmp >= UINT32_C(0x10000)) { // Four bytes. out[outn] = UINT8_C(0xF0) + (uint_least8_t)(tmp >> UINT32_C(0x12)); outn += SIZE_C(0x1); @@ -84,6 +94,7 @@ nottoobig:; /* One byte. */ out[outn] = (uint_least8_t)tmp; } + u8c_u8free(_out); *_out = out; - return UINT8_C(0x0); + return false; } diff --git a/src/u8c/u8free.c b/src/u8c/u8free.c new file mode 100644 index 0000000..4e3a026 --- /dev/null +++ b/src/u8c/u8free.c @@ -0,0 +1,24 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <stdbool.h> +# include <stdint.h> +# include <stdlib.h> +# include <u8c/u8free.h> +bool u8c_u8free(unsigned char const * * const _u8) { + free((unsigned char *)*_u8); + *_u8 = NULL; + return false; +} diff --git a/src/u8c/vfmt.c b/src/u8c/vfmt.c index e32b6fa..292db80 100644 --- a/src/u8c/vfmt.c +++ b/src/u8c/vfmt.c @@ -14,10 +14,13 @@ If not, see <https://www.gnu.org/licenses/>. */ # include <stdarg.h> -# include <stdint.h> +# include <stdbool.h> # include <u8c/u32cp.h> # include <u8c/vfmt.h> -uint_least8_t u8c_vfmt(size_t * const _sz,uint_least32_t const * * const _out,uint_least32_t const * const _in,va_list _args) { - /* To be added. */ +# include <uchar.h> +# if defined(u8c_bethrdsafe) +# include <threads.h> +# endif +bool u8c_vfmt(size_t * const _sz,char32_t const * * const _out,char32_t const * const _in,va_list _args) { return u8c_u32cp(_sz,_out,_in); } diff --git a/src/u8c/vprint.c b/src/u8c/vprint.c index 503ca88..6dc30ae 100644 --- a/src/u8c/vprint.c +++ b/src/u8c/vprint.c @@ -15,29 +15,31 @@ */ # include <assert.h> # include <stdarg.h> +# include <stdbool.h> # include <stdint.h> # include <stdio.h> # include <stdlib.h> +# include <u8c/SIZE_C.h> # include <u8c/seterr.h> # include <u8c/u32free.h> # include <u8c/u8enc.h> # include <u8c/u8free.h> -# include <u8c/SIZE_C.h> # include <u8c/vfmt.h> # include <u8c/vprint.h> -uint_least8_t u8c_vprint(FILE * _fp,uint_least32_t const * const _msg,va_list _args) { +# include <uchar.h> +bool u8c_vprint(FILE * _fp,char32_t const * const _msg,va_list _args) { assert(_msg != NULL); - uint_least32_t const * str0 = NULL; + char32_t const * str0 = NULL; u8c_vfmt(NULL,&str0,_msg,_args); - size_t str1sz = SIZE_C(0x0); - uint_least8_t const * str1 = NULL; + size_t str1sz = SIZE_C(0x0); + unsigned char const * str1 = NULL; u8c_u8enc(&str1sz,&str1,str0); assert(str1sz > SIZE_C(0x0)); if(fwrite(str1,sizeof(uint_least8_t),str1sz - SIZE_C(0x1),_fp) < str1sz - SIZE_C(0x1)) { - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x76),UINT32_C(0x70),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x66),UINT32_C(0x77),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x62),UINT32_C(0x6C),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x77),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x73),UINT32_C(0x74),UINT32_C(0x64),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_vprint: fwrite: Unable to write to stdout. */ - return UINT8_C(0x1); + u8c_seterr(U"u8c_vprint: fwrite: Unable to write to stdout."); + return true; } - u8c_u32free(str0); - u8c_u8free(str1); - return UINT8_C(0x0); + u8c_u32free(&str0); + u8c_u8free(&str1); + return false; } @@ -3,9 +3,11 @@ # include <stdint.h> # include <stdio.h> # include <stdlib.h> +# include <u8c/SIZE_C.h> +# include <u8c/abrt.h> # include <u8c/col.h> # include <u8c/dbgprint.h> -# include <u8c/debug.h> +# include <u8c/dbg.h> # include <u8c/end.h> # include <u8c/fmt.h> # include <u8c/fmttyp.h> @@ -15,16 +17,21 @@ # include <u8c/isalpha.h> # include <u8c/iscntrl.h> # include <u8c/isdigit.h> +# include <u8c/isxdigit.h> # include <u8c/ispunct.h> # include <u8c/isspace.h> # include <u8c/print.h> # include <u8c/println.h> # include <u8c/seterr.h> -# include <u8c/SIZE_C.h> +# include <u8c/setfmt.h> # include <u8c/thrdsafe.h> +# include <u8c/u32cat.h> # include <u8c/u32cmp.h> # include <u8c/u32cp.h> +# include <u8c/u32fndchr.h> +# include <u8c/u32fndpat.h> # include <u8c/u32free.h> +# include <u8c/u32substr.h> # include <u8c/u32sz.h> # include <u8c/u8dec.h> # include <u8c/u8enc.h> @@ -32,9 +39,6 @@ # include <u8c/ver.h> # include <u8c/vfmt.h> # include <u8c/vprint.h> -# if defined(__STDC_UTF_32__) -# include <u8c/txt.h> -# endif static void testmsg(char const * fmt,...) { va_list args; va_start(args,fmt); @@ -43,76 +47,82 @@ static void testmsg(char const * fmt,...) { printf("\"...\n+->\n\n"); va_end(args); } -static void testmsgdone(uint_least32_t * num0,uint_least8_t * num1) { - *num0 += (uint_least32_t)(*num1); - printf("\n+->\n| \x1b[38:2::61:225:169mDone\x1b[0m! (\x1b[38:2::225:61:61mErrors\x1b[0m: %" PRIuLEAST32 ")\n+->\n",*num1); - *num1 = UINT8_C(0x0); +static void testmsgdone() { + printf("\n+->\n| \x1b[38:2::61:225:169mDone\x1b[0m!\n+->\n"); } int main(void) { - u8c_init(); + if(u8c_init()) { + printf("Unable to initialise u8c!\n"); + exit(EXIT_FAILURE); + } + atexit((void (*)(void))&u8c_end); + u8c_setfmt(UINT8_C(0xC),UINT8_C(0x1)); printf("u8c version: %" PRIXLEAST64 ".\n",u8c_ver); - printf("Debug build: %" PRIXLEAST8 ".\n",u8c_debug); + printf("Debug build: %" PRIXLEAST8 ".\n",u8c_dbg); printf("Thread safe: %" PRIXLEAST8 ".\n",u8c_thrdsafe); - uint_least32_t errcount0 = UINT32_C(0x0); - uint_least8_t errcount1 = UINT8_C(0x0); testmsg("Error messages"); { - uint_least32_t const * err = NULL; - errcount1 += u8c_geterr(NULL,&err); - errcount1 += u8c_println(stdout,err); - u8c_u32free(err); + char32_t const * err = NULL; + u8c_geterr(NULL,&err); + printf("default error message: "); + u8c_println(stdout,err); + u8c_seterr(U"Gluchwein!"); + u8c_geterr(NULL,&err); + printf(" set error message: "); + u8c_println(stdout,err); + u8c_u32free(&err); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("UTF-8 encoding/decoding"); { - uint_least32_t const * msg0 = (uint_least32_t[]){UINT32_C(0xA2),UINT32_C(0x2C),UINT32_C(0x939),UINT32_C(0x2C),UINT32_C(0x10348),UINT32_C(0x2C),UINT32_C(0x20AC),UINT32_C(0x2C),UINT32_C(0x218A),UINT32_C(0x2C),UINT32_C(0x1F44B),UINT32_C(0x0)}; - uint_least8_t const * msg1 = NULL; - errcount1 += u8c_u8enc(NULL,&msg1,msg0); + char32_t const * msg0 = U"¢,ह,𐍈,€,↊,👋"; + unsigned char const * msg1 = NULL; + u8c_u8enc(NULL,&msg1,msg0); + msg0 = NULL; printf("Encoded: %s\n",msg1); - errcount1 += u8c_u8dec(NULL,&msg0,msg1); - u8c_u8free(msg1); - errcount1 += u8c_u8enc(NULL,&msg1,msg0); + u8c_u8dec(NULL,&msg0,msg1); + u8c_u8enc(NULL,&msg1,msg0); printf("Encoded -> Decoded -> Encoded: %s\n",msg1); - u8c_u32free(msg0); - u8c_u8free(msg1); + u8c_u32free(&msg0); + u8c_u8free(&msg1); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("Printing (u8c_print)"); { - errcount1 += u8c_print(stdout,(uint_least32_t[]){UINT32_C(0x48),UINT32_C(0x65),UINT32_C(0x6C),UINT32_C(0x6C),UINT32_C(0x6F),UINT32_C(0x0),}); - errcount1 += u8c_print(stdout,(uint_least32_t[]){UINT32_C(0x20),UINT32_C(0xF0),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0x21),UINT32_C(0xA),UINT32_C(0x0),}); + u8c_print(stdout,U"Hello"); + u8c_print(stdout,U" ðere!"); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("Printing (u8c_println)"); { - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x48),UINT32_C(0x65),UINT32_C(0x6C),UINT32_C(0x6C),UINT32_C(0x6F),UINT32_C(0x0),}); - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x20),UINT32_C(0xF0),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0x21),UINT32_C(0x0),}); + u8c_println(stdout,U"Hello"); + u8c_println(stdout,U" ðere!"); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("Text formatting"); { - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x54),UINT32_C(0x68),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0xFFFD),UINT32_C(0x6E),UINT32_C(0x75),UINT32_C(0x6D),UINT32_C(0x62),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0xFFFD),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x73),UINT32_C(0x20),UINT32_C(0xFFFD),UINT32_C(0x2E),UINT32_C(0x0),},u8c_fmttyp_fgcol,u8c_col_mint,u8c_fmttyp_fgcol0,u8c_fmttyp_int,(int_least64_t){-0x10}); + u8c_println(stdout,U"The number is .",u8c_fmttyp_fgcol,u8c_col_mint,u8c_fmttyp_fgcol0,u8c_fmttyp_int,(int_least64_t){-0x10}); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("Colour text"); { - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0xFFFD),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0xFFFD),UINT32_C(0x6F),UINT32_C(0x72),UINT32_C(0x61),UINT32_C(0x6E),UINT32_C(0x67),UINT32_C(0x65),UINT32_C(0xFFFD),UINT32_C(0x79),UINT32_C(0x65),UINT32_C(0x6C),UINT32_C(0x6C),UINT32_C(0x6F),UINT32_C(0x77),UINT32_C(0xFFFD),UINT32_C(0x63),UINT32_C(0x68),UINT32_C(0x61),UINT32_C(0x72),UINT32_C(0x74),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0x75),UINT32_C(0x73),UINT32_C(0x65),UINT32_C(0xFFFD),UINT32_C(0x67),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0x65),UINT32_C(0x6E),UINT32_C(0xFFFD),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0xFFFD),UINT32_C(0x63),UINT32_C(0x79),UINT32_C(0x61),UINT32_C(0x6E),UINT32_C(0xFFFD),UINT32_C(0x61),UINT32_C(0x7A),UINT32_C(0x75),UINT32_C(0x72),UINT32_C(0x65),UINT32_C(0xFFFD),UINT32_C(0x62),UINT32_C(0x6C),UINT32_C(0x75),UINT32_C(0x65),UINT32_C(0xFFFD),UINT32_C(0x76),UINT32_C(0x69),UINT32_C(0x6F),UINT32_C(0x6C),UINT32_C(0x65),UINT32_C(0x74),UINT32_C(0xFFFD),UINT32_C(0x6D),UINT32_C(0x61),UINT32_C(0x67),UINT32_C(0x65),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x61),UINT32_C(0xFFFD),UINT32_C(0x72),UINT32_C(0x6F),UINT32_C(0x73),UINT32_C(0x65),UINT32_C(0xFFFD),UINT32_C(0x0),},u8c_fmttyp_fgcol,u8c_col_red,u8c_fmttyp_fgcol,u8c_col_orange,u8c_fmttyp_fgcol,u8c_col_yellow,u8c_fmttyp_fgcol,u8c_col_chartreuse,u8c_fmttyp_fgcol,u8c_col_green,u8c_fmttyp_fgcol,u8c_col_mint,u8c_fmttyp_fgcol,u8c_col_cyan,u8c_fmttyp_fgcol,u8c_col_azure,u8c_fmttyp_fgcol,u8c_col_blue,u8c_fmttyp_fgcol,u8c_col_violet,u8c_fmttyp_fgcol,u8c_col_magenta,u8c_fmttyp_fgcol,u8c_col_rose,u8c_fmttyp_fgcol0); + u8c_println(stdout,U"redorangeyellowchartreusegreenmintcyanazurebluevioletmagentarose",u8c_fmttyp_fgcol,u8c_col_red,u8c_fmttyp_fgcol,u8c_col_orange,u8c_fmttyp_fgcol,u8c_col_yellow,u8c_fmttyp_fgcol,u8c_col_chartreuse,u8c_fmttyp_fgcol,u8c_col_green,u8c_fmttyp_fgcol,u8c_col_mint,u8c_fmttyp_fgcol,u8c_col_cyan,u8c_fmttyp_fgcol,u8c_col_azure,u8c_fmttyp_fgcol,u8c_col_blue,u8c_fmttyp_fgcol,u8c_col_violet,u8c_fmttyp_fgcol,u8c_col_magenta,u8c_fmttyp_fgcol,u8c_col_rose,u8c_fmttyp_fgcol0); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("Combining characters"); { for(register uint_least32_t n = UINT32_C(0x300);n <= UINT32_C(0x36F);n += UINT32_C(0x1)) { - errcount1 += u8c_print(stdout,(uint_least32_t[]){UINT32_C(0x41),n,UINT32_C(0x20),UINT32_C(0x0),}); + u8c_print(stdout,(uint_least32_t[]){UINT32_C(0x61),n,UINT32_C(0x20),UINT32_C(0x0),}); fflush(stdout); } - errcount1 += u8c_print(stdout,(uint_least32_t[]){UINT32_C(0xA),UINT32_C(0x0),}); + u8c_print(stdout,U"\n"); } - testmsgdone(&errcount0,&errcount1); - testmsg("String comparison (UTF-32)"); + testmsgdone(); + testmsg("String comparison"); { - uint_least32_t const * str0 = (uint_least32_t[]){UINT32_C(0x48),UINT32_C(0x65),UINT32_C(0x6C),UINT32_C(0x6C),UINT32_C(0x6F),UINT32_C(0x0),}; - uint_least32_t const * str1 = (uint_least32_t[]){UINT32_C(0x48),UINT32_C(0x65),UINT32_C(0x6C),UINT32_C(0x6C),UINT32_C(0x6F),UINT32_C(0x0),}; - uint_least32_t const * str2 = (uint_least32_t[]){UINT32_C(0x47),UINT32_C(0x6F),UINT32_C(0x6F),UINT32_C(0x64),UINT32_C(0x62),UINT32_C(0x79),UINT32_C(0x65),UINT32_C(0x0),}; + char32_t const * str0 = U"Hello"; + char32_t const * str1 = U"Hello"; + char32_t const * str2 = U"Goodbye"; printf("str0: "); u8c_println(stdout,str0); printf("str1: "); @@ -120,72 +130,143 @@ int main(void) { printf("str2: "); u8c_println(stdout,str2); uint_least8_t res = UINT8_C(0x0); - errcount1 += u8c_u32cmp(&res,str0,str1); + u8c_u32cmp(&res,str0,str1); printf("str0,str1: %" PRIXLEAST8 ".\n",res); - errcount1 += u8c_u32cmp(&res,str1,str2); + u8c_u32cmp(&res,str1,str2); printf("str0,str2: %" PRIXLEAST8 ".\n",res); - errcount1 += u8c_u32cmp(&res,str2,str1); + u8c_u32cmp(&res,str2,str1); printf("str2,str1: %" PRIXLEAST8 ".\n",res); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("u8c_isalnum"); { for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) { uint_least8_t res = UINT8_C(0x0); - errcount1 += u8c_isalnum(&res,n); + u8c_isalnum(&res,n); if(res) { - errcount1 += u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); + u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); } } - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x0),}); + u8c_println(stdout,U""); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("u8c_isalpha"); { for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) { uint_least8_t res = UINT8_C(0x0); - errcount1 += u8c_isalpha(&res,n); + u8c_isalpha(&res,n); if(res) { - errcount1 += u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); + u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); } } - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x0),}); + u8c_println(stdout,U""); } - testmsgdone(&errcount0,&errcount1); + testmsgdone(); testmsg("u8c_isdigit"); { for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) { uint_least8_t res = UINT8_C(0x0); - errcount1 += u8c_isdigit(&res,n); + u8c_isdigit(&res,n); if(res) { - errcount1 += u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); + u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); } } - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x0),}); + u8c_println(stdout,U""); } - testmsgdone(&errcount0,&errcount1); - testmsg("u8c_ispunct"); /* This test appears broken on some fonts. */ + testmsgdone(); + testmsg("u8c_ispunct"); /* This test appears broken on some incomplete fonts, altough it is not. */ { for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) { uint_least8_t res = UINT8_C(0x0); - errcount1 += u8c_ispunct(&res,n); + u8c_ispunct(&res,n); if(res) { - errcount1 += u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); + u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); } } - errcount1 += u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x0),}); + u8c_println(stdout,U""); } - testmsgdone(&errcount0,&errcount1); -# if defined(__STDC_UTF_32__) + testmsgdone(); testmsg("UTF-32 string literals"); { - u8c_println(stdout,u8c_txt("Can you see ðis?")); + u8c_println(stdout,U"Can you see ðis?"); + } + testmsgdone(); + testmsg("string concatenation"); + { + char32_t const * str0 = U"Free as in freedom!"; + char32_t const * str1 = U" GNU"; + char32_t const * str2 = NULL; + u8c_u32cat(NULL,&str2,str0,str1); + printf("string #0: "); + u8c_println(stdout,str0); + printf("string #1: "); + u8c_println(stdout,str1); + printf("string #2: "); + u8c_println(stdout,str2); + u8c_u32free(&str2); + + } + testmsgdone(); + testmsg("sub-strings"); + { + char32_t const * str0 = U"I wish to suck big duck."; + char32_t const * str1 = NULL; + u8c_u32substr(&str1,SIZE_C(0x0),SIZE_C(0xE),str0); + char32_t const * str2 = NULL; + u8c_u32substr(&str2,SIZE_C(0xF),SIZE_C(0x0),str0); + printf("string #0: "); + u8c_println(stdout,str0); + printf("string #1: "); + u8c_println(stdout,str1); + printf("string #2: "); + u8c_println(stdout,str2); + u8c_u32free(&str1); + u8c_u32free(&str2); + } + testmsgdone(); + testmsg("u8c_isxdigit"); + { + for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) { + uint_least8_t res = UINT8_C(0x0); + u8c_isxdigit(&res,n); + if(res) { + u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),}); + } + } + u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x0),}); + } + testmsgdone(); + testmsg("Printing (u8c_dbgprint)"); + { + u8c_dbgprint(U"Hello"); + u8c_dbgprint(U" ðere!"); + } + testmsgdone(); + testmsg("u8c_u32fndchr"); + { + char32_t const * str = U"Proprietary as in Microsoft."; + size_t pos0 = SIZE_C(0x0); + size_t pos1 = SIZE_C(0x0); + u8c_u32fndchr(&pos0,str,U'M'); + u8c_u32fndchr(&pos1,str,U'ŋ'); + printf("string: "); + u8c_println(stdout,str); + printf("Position of 'M': %zu\n",pos0); + printf("Position of 'ŋ': %zu\n",pos1); + } + testmsgdone(); + testmsg("u8c_u32fndpat"); + { + char32_t const * str = U"Proprietary as in Microsoft."; + size_t pos0 = SIZE_C(0x0); + size_t pos1 = SIZE_C(0x0); + u8c_u32fndpat(&pos0,str,U"as in"); + u8c_u32fndpat(&pos1,str,U"forever"); + printf("string: "); + u8c_println(stdout,str); + printf("Position of \"as in\": %zu\n",pos0); + printf("Position of \"forever\": %zu\n",pos1); } - testmsgdone(&errcount0,&errcount1); -# endif - printf("\n"); - printf("Test done!\n"); - printf("Total number of errors: %" PRIuLEAST32 ".\n",errcount0); - u8c_end(); - exit(EXIT_SUCCESS); + testmsgdone(); + u8c_abrt(__FILE__,(long long)__LINE__,__func__,"Testing u8c_abort"); } diff --git a/txttolit.c b/txttolit.c deleted file mode 100644 index 24f196b..0000000 --- a/txttolit.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - Use this program to convert Unicode message into C-arrays. - Just copy the output and paste it into the source. -*/ -# if !defined(__STDC_UTF_32__) -# error UTF-32 support is needed. -# endif -# include <inttypes.h> -# include <stdio.h> -# include <stdlib.h> -# include <u8c/end.h> -# include <u8c/init.h> -# include <u8c/SIZE_C.h> -# include <u8c/u32sz.h> -int main(void) { - u8c_init(); - size_t u32sz = SIZE_C(0x0); - uint_least32_t const * u32 = U"Hello ðere!"; /* Place string here. */ - u8c_u32sz(&u32sz,u32); - printf("Arrray:\n{"); - for(size_t n = SIZE_C(0x0);n < u32sz;n += SIZE_C(0x1)) { - printf("UINT32_C(0x%" PRIXLEAST32 "),",u32[n]); - } - printf("}\n"); - u8c_end(); - exit(EXIT_SUCCESS); -} |