diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/u8c/abrt.c | 35 | ||||
-rw-r--r-- | src/u8c/dat.c (renamed from src/u8c/stat.h) | 15 | ||||
-rw-r--r-- | src/u8c/dat.h (renamed from src/u8c/err.h) | 9 | ||||
-rw-r--r-- | src/u8c/dattyp.h (renamed from src/u8c/errlock.h) | 21 | ||||
-rw-r--r-- | src/u8c/debug.c | 9 | ||||
-rw-r--r-- | src/u8c/end.c | 27 | ||||
-rw-r--r-- | src/u8c/fmt.c | 4 | ||||
-rw-r--r-- | src/u8c/geterr.c | 23 | ||||
-rw-r--r-- | src/u8c/init.c | 22 | ||||
-rw-r--r-- | src/u8c/isalnum.c | 7 | ||||
-rw-r--r-- | src/u8c/isalpha.c | 48 | ||||
-rw-r--r-- | src/u8c/iscntrl.c | 5 | ||||
-rw-r--r-- | src/u8c/isdigit.c | 5 | ||||
-rw-r--r-- | src/u8c/ispunct.c | 43 | ||||
-rw-r--r-- | src/u8c/isspace.c | 5 | ||||
-rw-r--r-- | src/u8c/isxdigit.c | 47 | ||||
-rw-r--r-- | src/u8c/print.c | 3 | ||||
-rw-r--r-- | src/u8c/println.c | 9 | ||||
-rw-r--r-- | src/u8c/seterr.c | 18 | ||||
-rw-r--r-- | src/u8c/setfmt.c | 42 | ||||
-rw-r--r-- | src/u8c/thrdsafe.c | 7 | ||||
-rw-r--r-- | src/u8c/u32alloc.c | 29 | ||||
-rw-r--r-- | src/u8c/u32cat.c | 53 | ||||
-rw-r--r-- | src/u8c/u32cmp.c | 15 | ||||
-rw-r--r-- | src/u8c/u32cp.c | 22 | ||||
-rw-r--r-- | src/u8c/u32fndchr.c | 40 | ||||
-rw-r--r-- | src/u8c/u32fndpat.c | 51 | ||||
-rw-r--r-- | src/u8c/u32free.c (renamed from src/u8c/err.c) | 11 | ||||
-rw-r--r-- | src/u8c/u32substr.c | 48 | ||||
-rw-r--r-- | src/u8c/u32sz.c | 20 | ||||
-rw-r--r-- | src/u8c/u8alloc.c (renamed from src/u8c/errlock.c) | 18 | ||||
-rw-r--r-- | src/u8c/u8dec.c | 31 | ||||
-rw-r--r-- | src/u8c/u8enc.c | 35 | ||||
-rw-r--r-- | src/u8c/u8free.c (renamed from src/u8c/stat.c) | 14 | ||||
-rw-r--r-- | src/u8c/vfmt.c | 9 | ||||
-rw-r--r-- | src/u8c/vprint.c | 22 |
36 files changed, 666 insertions, 156 deletions
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/stat.h b/src/u8c/dat.c index 8ca005e..0fcf492 100644 --- a/src/u8c/stat.h +++ b/src/u8c/dat.c @@ -13,9 +13,14 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Status */ -# if !defined(u8c_sym_stat) -# define u8c_sym_stat +# include "dat.h" +# include "dattyp.h" +# include <stdbool.h> +# include <stddef.h> # include <stdint.h> -extern uint_least8_t u8c_stat; -# endif +# 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/errlock.h b/src/u8c/dattyp.h index a77ade5..d094151 100644 --- a/src/u8c/errlock.h +++ b/src/u8c/dattyp.h @@ -13,11 +13,24 @@ If not, see <https://www.gnu.org/licenses/>. */ -/* Error lock */ -# if !defined(u8c_sym_errlock) -# define u8c_sym_errlock +# 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> -extern mtx_t u8c_errlock; # 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/err.c b/src/u8c/u32free.c index 77a8221..62b43ef 100644 --- a/src/u8c/err.c +++ b/src/u8c/u32free.c @@ -13,7 +13,12 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "err.h" -# include <stddef.h> +# include <stdbool.h> # include <stdint.h> -uint_least32_t const * u8c_err = NULL; +# 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/src/u8c/errlock.c b/src/u8c/u8alloc.c index b63a435..a364acf 100644 --- a/src/u8c/errlock.c +++ b/src/u8c/u8alloc.c @@ -13,8 +13,16 @@ If not, see <https://www.gnu.org/licenses/>. */ -# include "errlock.h" -# if defined(u8c_bethrdsafe) -# include <threads.h> -mtx_t u8c_errlock; -# 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/stat.c b/src/u8c/u8free.c index 63214ec..4e3a026 100644 --- a/src/u8c/stat.c +++ b/src/u8c/u8free.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/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; } |