summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/u8c/debug.c23
-rw-r--r--src/u8c/end.c36
-rw-r--r--src/u8c/err.c (renamed from src/u8c/free.c)13
-rw-r--r--src/u8c/err.h20
-rw-r--r--src/u8c/errlock.c20
-rw-r--r--src/u8c/errlock.h22
-rw-r--r--src/u8c/fmt.c29
-rw-r--r--src/u8c/geterr.c31
-rw-r--r--src/u8c/init.c40
-rw-r--r--src/u8c/print.c86
-rw-r--r--src/u8c/println.c35
-rw-r--r--src/u8c/seterr.c39
-rw-r--r--src/u8c/seterr.h20
-rw-r--r--src/u8c/stat.c22
-rw-r--r--src/u8c/stat.h20
-rw-r--r--src/u8c/thrdsafe.c23
-rw-r--r--src/u8c/u32cp.c40
-rw-r--r--src/u8c/u32sz.c32
-rw-r--r--src/u8c/u8dec.c122
-rw-r--r--src/u8c/u8enc.c104
-rw-r--r--src/u8c/vfmt.c30
-rw-r--r--src/u8c/vprint.c42
22 files changed, 653 insertions, 196 deletions
diff --git a/src/u8c/debug.c b/src/u8c/debug.c
new file mode 100644
index 0000000..eb926eb
--- /dev/null
+++ b/src/u8c/debug.c
@@ -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/>.
+*/
+# include <stdint.h>
+# include <u8c/debug.h>
+uint_least8_t const u8c_debug =
+# if defined(NDEBUG)
+ UINT8_C(0x0);
+# else
+ UINT8_C(0x1);
+# endif
diff --git a/src/u8c/end.c b/src/u8c/end.c
new file mode 100644
index 0000000..b4e79f2
--- /dev/null
+++ b/src/u8c/end.c
@@ -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/>.
+*/
+# include "err.h"
+# include "errlock.h"
+# include "stat.h"
+# include <stdint.h>
+# include <stdlib.h>
+# include <u8c/end.h>
+# include <u8c/print.h>
+# if defined(u8c_bethrdsafe)
+# include <threads.h>
+# endif
+uint_least8_t u8c_end(void) {
+ if(u8c_stat > UINT8_C(0x0)) {
+ return UINT8_C(0x0);
+ }
+# if defined(u8c_bethrdsafe)
+ mtx_destroy(&u8c_errlock);
+# endif
+ free(u8c_err);
+ u8c_stat = UINT8_C(0x1);
+ return UINT8_C(0x0);
+}
diff --git a/src/u8c/free.c b/src/u8c/err.c
index f58f667..6c7a940 100644
--- a/src/u8c/free.c
+++ b/src/u8c/err.c
@@ -3,9 +3,9 @@
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 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.
+ 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.
@@ -13,10 +13,7 @@
If not, see <https://www.gnu.org/licenses/>.
*/
-# include <u8c/free.h>
+# include "err.h"
+# include <stddef.h>
# include <stdint.h>
-# include <stdlib.h>
-uint_least8_t u8c_free(void * ptr) {
- free(ptr);
- return 0x0;
-}
+uint_least32_t * u8c_err = NULL;
diff --git a/src/u8c/err.h b/src/u8c/err.h
new file mode 100644
index 0000000..895710d
--- /dev/null
+++ b/src/u8c/err.h
@@ -0,0 +1,20 @@
+/*
+ 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_err)
+# define u8c_sym_err
+# include <stdint.h>
+extern uint_least32_t * u8c_err;
+# endif
diff --git a/src/u8c/errlock.c b/src/u8c/errlock.c
new file mode 100644
index 0000000..b63a435
--- /dev/null
+++ b/src/u8c/errlock.c
@@ -0,0 +1,20 @@
+/*
+ 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 "errlock.h"
+# if defined(u8c_bethrdsafe)
+# include <threads.h>
+mtx_t u8c_errlock;
+# endif
diff --git a/src/u8c/errlock.h b/src/u8c/errlock.h
new file mode 100644
index 0000000..d1d380c
--- /dev/null
+++ b/src/u8c/errlock.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/>.
+*/
+# if !defined(u8c_sym_errlock)
+# define u8c_sym_errlock
+# if defined(u8c_bethrdsafe)
+# include <threads.h>
+extern mtx_t u8c_errlock;
+# endif
+# endif
diff --git a/src/u8c/fmt.c b/src/u8c/fmt.c
new file mode 100644
index 0000000..dabf376
--- /dev/null
+++ b/src/u8c/fmt.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 <u8c/fmt.h>
+# include <u8c/fmttyp.h>
+# include <u8c/u8enc.h>
+# include <u8c/vfmt.h>
+# include <stdarg.h>
+# include <stddef.h>
+# include <stdint.h>
+uint_least8_t u8c_fmt(size_t * _outsz,uint_least32_t * * _out,uint_least32_t * _in,...) {
+ va_list args;
+ va_start(args,_in);
+ uint_least8_t val = u8c_vfmt(_outsz,_out,_in,args);
+ va_end(args);
+ return val;
+}
diff --git a/src/u8c/geterr.c b/src/u8c/geterr.c
new file mode 100644
index 0000000..113281e
--- /dev/null
+++ b/src/u8c/geterr.c
@@ -0,0 +1,31 @@
+/*
+ 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 "err.h"
+# include "errlock.h"
+# include <stdint.h>
+# include <stdlib.h>
+# include <u8c/u32cp.h>
+# include <u8c/geterr.h>
+uint_least8_t u8c_geterr(size_t * _sz,uint_least32_t * * _u32) {
+ # if defined(u8c_bethrdsafe)
+ mtx_lock(&u8c_errlock);
+ # endif
+ u8c_u32cp(_sz,_u32,u8c_err);
+ # if defined(u8c_bethrdsafe)
+ mtx_unlock(&u8c_errlock);
+ # endif
+ return UINT8_C(0x0);
+ }
diff --git a/src/u8c/init.c b/src/u8c/init.c
new file mode 100644
index 0000000..0f752ce
--- /dev/null
+++ b/src/u8c/init.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 "err.h"
+# include "errlock.h"
+# include "stat.h"
+# include <assert.h>
+# include <stdint.h>
+# include <stdlib.h>
+# include <u8c/end.h>
+# include <u8c/init.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/u32cp.h>
+# if defined(u8c_bethrdsafe)
+# include <threads.h>
+# endif
+extern uint_least8_t u8c_init(void) {
+# if defined(u8c_bethrdsafe)
+ if(mtx_init(&u8c_errlock,mtx_plain) == thrd_error) {
+ return UINT8_C(0x2);
+ }
+# endif
+ /* Set default error message: */
+ u8c_u32cp(NULL,&u8c_err,(uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x69),UINT32_C(0x74),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x44),UINT32_C(0x65),UINT32_C(0x66),UINT32_C(0x61),UINT32_C(0x75),UINT32_C(0x6C),UINT32_C(0x74),UINT32_C(0x20),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x72),UINT32_C(0x6F),UINT32_C(0x72),UINT32_C(0x20),UINT32_C(0x6D),UINT32_C(0x65),UINT32_C(0x73),UINT32_C(0x73),UINT32_C(0x61),UINT32_C(0x67),UINT32_C(0x65),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_init: Default error message. */
+ /* Set status: */
+ u8c_stat = UINT8_C(0x0);
+ return UINT8_C(0x0);
+}
diff --git a/src/u8c/print.c b/src/u8c/print.c
index 741d25a..5308f18 100644
--- a/src/u8c/print.c
+++ b/src/u8c/print.c
@@ -3,9 +3,9 @@
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 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.
+ 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.
@@ -13,84 +13,14 @@
If not, see <https://www.gnu.org/licenses/>.
*/
-# include <u8c/fmttyp.h>
-# include <u8c/u8enc.h>
# include <stdarg.h>
# include <stdint.h>
-# include <stdio.h>
-# include <stdlib.h>
-uint_least8_t u8c_print(uint_least32_t * msg,...) {
+# include <u8c/print.h>
+# include <u8c/vprint.h>
+uint_least8_t u8c_print(FILE * _fp,uint_least32_t * _msg,...) {
va_list args;
- va_start(args,msg);
- for(size_t n = (size_t){0x0};;n += (size_t){0x1}) {
- if(msg[n] == (uint_least32_t){0x0}) {
- fwrite(&(uint_least8_t){0xA},(size_t){0x1},(size_t){0x1},stdout);
- break;
- }
- if(msg[n] == (uint_least32_t){0xFFFD}) {
- enum u8c_fmttyp fmttyp = va_arg(args,enum u8c_fmttyp);
- switch(fmttyp) {
- case u8c_fmttyp_chr:
- {
- uint_least32_t const chr = va_arg(args,uint_least32_t);
- if(chr == (uint_least32_t){0x0}) {
- continue;
- }
- size_t strsz = (size_t){0x0};
- uint_least8_t * str = NULL;
- u8c_u8enc((uint_least32_t[]){chr,0x0},&strsz,&str);
- fwrite(str,(size_t){0x1},strsz - (size_t){0x1},stdout);
- free(str);
- break;
- }
- case u8c_fmttyp_int:
- {
- int_least64_t n = va_arg(args,int_least64_t);
- if(n < 0x0) {
- size_t chrsz = (size_t){0x0};
- uint_least8_t * chr = NULL;
- u8c_u8enc((uint_least32_t[]){0x2212,0x0},&chrsz,&chr);
- fwrite(chr,(size_t){0x1},chrsz - (size_t){0x1},stdout);
- free(chr);
- }
- for(;n != 0x0;n /= (int_least64_t){0xB}) {
-
- }
- break;
- }
- case u8c_fmttyp_str:
- {
- size_t strsz = (size_t){0x0};
- uint_least8_t * str = NULL;
- u8c_u8enc(va_arg(args,uint_least32_t *),&strsz,&str);
- fwrite(str,(size_t){0x1},strsz - (size_t){0x1},stdout);
- free((void *)str);
- break;
- }
- case u8c_fmttyp_uint:
- {
- break;
- }
- }
- continue;
- }
- size_t chrsz = (size_t){0x0};
- uint_least8_t * chr = NULL;
- u8c_u8enc((uint_least32_t[]){msg[n],0x0,0x0},&chrsz,&chr);
- fwrite(chr,(size_t){0x1},chrsz - (size_t){0x1},stdout);
- free((void *)chr);
- }
+ va_start(args,_msg);
+ uint_least8_t val = u8c_vprint(_fp,_msg,args);
va_end(args);
- return 0x0;
+ return val;
}
-/*
- #######################
- # #
- # ## ## ### # # ### #
- # # # # # # ### # #
- # ## ## # ### # #
- # # # # # ### # #
- # # # # ### # # # #
- # #
- #######################
-*/
diff --git a/src/u8c/println.c b/src/u8c/println.c
new file mode 100644
index 0000000..1b1830d
--- /dev/null
+++ b/src/u8c/println.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 "seterr.h"
+# include <stdarg.h>
+# include <stdint.h>
+# include <stdio.h>
+# include <u8c/println.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/vprint.h>
+uint_least8_t u8c_println(FILE * _fp,uint_least32_t * _msg,...) {
+ va_list args;
+ va_start(args,_msg);
+ if(u8c_vprint(_fp,_msg,args)) {
+ return UINT8_C(0x1);
+ }
+ 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);
+ }
+ va_end(args);
+ return UINT8_C(0x0);
+}
diff --git a/src/u8c/seterr.c b/src/u8c/seterr.c
new file mode 100644
index 0000000..4692348
--- /dev/null
+++ b/src/u8c/seterr.c
@@ -0,0 +1,39 @@
+/*
+ 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 "err.h"
+# include "errlock.h"
+# include "seterr.h"
+# include <assert.h>
+# include <stdint.h>
+# include <stdlib.h>
+# include <u8c/dbgprint.h>
+# include <u8c/u32cp.h>
+# if defined(u8c_bethrdsafe)
+# include <threads.h>
+# endif
+uint_least8_t u8c_seterr(uint_least32_t * _msg) {
+ assert(_msg != NULL);
+ u8c_dbgprint(_msg);
+# if defined(u8c_bethrdsafe)
+ mtx_lock(&u8c_errlock);
+# endif
+ free(u8c_err);
+ u8c_u32cp(NULL,&u8c_err,_msg);
+# if defined(u8c_bethrdsafe)
+ mtx_unlock(&u8c_errlock);
+# endif
+ return UINT8_C(0x0);
+}
diff --git a/src/u8c/seterr.h b/src/u8c/seterr.h
new file mode 100644
index 0000000..c7832c2
--- /dev/null
+++ b/src/u8c/seterr.h
@@ -0,0 +1,20 @@
+/*
+ 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_seterr)
+# define u8c_sym_seterr
+# include <stdint.h>
+extern uint_least8_t u8c_seterr(uint_least32_t * msg);
+# endif
diff --git a/src/u8c/stat.c b/src/u8c/stat.c
new file mode 100644
index 0000000..63214ec
--- /dev/null
+++ b/src/u8c/stat.c
@@ -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/>.
+*/
+# include "stat.h"
+# include <stdint.h>
+/*
+ 0x0 = Finished.
+ 0x1 = Initialised.
+*/
+uint_least8_t u8c_stat = UINT8_C(0x0);
diff --git a/src/u8c/stat.h b/src/u8c/stat.h
new file mode 100644
index 0000000..92b2063
--- /dev/null
+++ b/src/u8c/stat.h
@@ -0,0 +1,20 @@
+/*
+ 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_done)
+# define u8c_sym_done
+# include <stdint.h>
+extern uint_least8_t u8c_stat;
+# endif
diff --git a/src/u8c/thrdsafe.c b/src/u8c/thrdsafe.c
new file mode 100644
index 0000000..3cb4c4d
--- /dev/null
+++ b/src/u8c/thrdsafe.c
@@ -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/>.
+*/
+# include <stdint.h>
+# include <u8c/thrdsafe.h>
+uint_least8_t const u8c_thrdsafe =
+# if defined(u8c_bethrdsafe)
+ UINT8_C(0x1);
+# else
+ UINT8_C(0x0);
+# endif
diff --git a/src/u8c/u32cp.c b/src/u8c/u32cp.c
new file mode 100644
index 0000000..2462d85
--- /dev/null
+++ b/src/u8c/u32cp.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 "seterr.h"
+# include <assert.h>
+# include <stddef.h>
+# include <stdint.h>
+# include <stdlib.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/u32cp.h>
+# include <u8c/u32sz.h>
+uint_least8_t u8c_u32cp(size_t * _sz,uint_least32_t * * _out,uint_least32_t * _in) {
+ assert(_in != NULL);
+ size_t insz = SIZE_C(0x0);
+ u8c_u32sz(&insz,_in);
+ assert(insz > SIZE_C(0x0));
+ if(_sz != NULL) {
+ *_sz = insz;
+ }
+ 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);
+ }
+ for(size_t n = SIZE_C(0x0);n < insz;n += SIZE_C(0x1)) {
+ (*_out)[n] = _in[n];
+ }
+ return UINT8_C(0x0);
+}
diff --git a/src/u8c/u32sz.c b/src/u8c/u32sz.c
new file mode 100644
index 0000000..614557c
--- /dev/null
+++ b/src/u8c/u32sz.c
@@ -0,0 +1,32 @@
+/*
+ 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 <stddef.h>
+# include <stdint.h>
+# include <stdio.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/u32sz.h>
+uint_least8_t u8c_u32sz(size_t * _sz,uint_least32_t * _u32) {
+ assert(_sz != NULL);
+ assert(_u32 != NULL);
+ for(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);
+ }
+ }
+ return UINT8_C(0x1);
+}
diff --git a/src/u8c/u8dec.c b/src/u8c/u8dec.c
index 60f4c3d..d7ddb05 100644
--- a/src/u8c/u8dec.c
+++ b/src/u8c/u8dec.c
@@ -3,9 +3,9 @@
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 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.
+ 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.
@@ -13,83 +13,83 @@
If not, see <https://www.gnu.org/licenses/>.
*/
-# include <u8c/dbgprint.h>
-# include <u8c/u8dec.h>
+# include "seterr.h"
+# include <assert.h>
# include <stdint.h>
-# include <stdio.h>
# include <stdlib.h>
-uint_least8_t u8c_u8dec(uint_least8_t * utf,size_t * codepssz,uint_least32_t * * codeps) {
- size_t insz = (size_t){0x0};
- size_t outsz = (size_t){0x0};
- for(size_t n = (size_t){0x0};;n += (size_t){0x1}) { // First pass: get size of input array and determine size of output array.
- outsz += (size_t){0x1};
- if(utf[n] == (uint_least8_t){0x0}) { // Null-terminator.
+# include <u8c/dbgprint.h>
+# include <u8c/u8dec.h>
+# include <u8c/SIZE_C.h>
+uint_least8_t u8c_u8dec(size_t * _outsz,uint_least32_t * * _out,uint_least8_t * _in) {
+ assert(_in != NULL);
+ size_t insz = SIZE_C(0x0);
+ size_t outsz = SIZE_C(0x0);
+ for(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. */
+ outsz += SIZE_C(0x1);
+ if(_in[n] == UINT8_C(0x0)) { /* Null-terminator: end of string has been reached. */
insz = n;
- break;
+ goto nottoobig;
+ }
+ if(_in[n] >= 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);
}
- if(utf[n] >= (uint_least8_t){0xF0}) { // Four byte.
- n += (size_t){0x4};
+ if(_in[n] >= UINT8_C(0xF0)) { /* Four byte. */
+ n += SIZE_C(0x4);
continue;
}
- if(utf[n] >= (uint_least8_t){0xE0}) { // Three bytes.
- n += (size_t){0x3};
+ if(_in[n] >= UINT8_C(0xE0)) { /* Three bytes. */
+ n += SIZE_C(0x3);
continue;
}
- if(utf[n] >= (uint_least8_t){0xC0}) { // Two bytes.
- n += (size_t){0x2};
+ if(_in[n] >= UINT8_C(0xC0)) { /* Two bytes. */
+ n += SIZE_C(0x2);
continue;
}
+ /* One byte. */
+ n += SIZE_C(0x1);
}
- if(codepssz != NULL) {
- *codepssz = outsz;
+ 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);
+nottoobig:;
+ if(_outsz != NULL) {
+ *_outsz = outsz;
}
- *codeps = malloc(outsz);
- (*codeps)[outsz - (size_t){0x1}] = (uint_least32_t){0x0}; // Create null-terminator on output array.
- for(size_t n = (size_t){0x0}, outn = (size_t){0x0};n < insz;n += (size_t){0x1},outn += (size_t){0x1}) { // Second pass: decode UTF-8.
- uint_least8_t chr = utf[n];
- if(chr >= (uint_least8_t){0xF7}) { // Out of range.
- u8c_dbgprint(U"Out of range (in character decoding: byte too big).");
- return 0x1;
- }
- if(chr >= (uint_least8_t){0xF0}) { // Four byte.
- uint_least32_t codep = (uint_least32_t){(chr ^ 0xF0) << 0x12};
- n += (size_t){0x1};
- chr = utf[n];
- codep += (uint_least32_t){(chr ^ 0x80) << 0xC};
- n += (size_t){0x1};
- chr = utf[n];
- codep += (uint_least32_t){(chr ^ 0x80) << 0x6};
- n += (size_t){0x1};
- chr = utf[n];
- codep += (uint_least32_t){(chr ^ 0x80)};
- (*codeps)[outn] = codep;
+ *_out = calloc(sizeof(uint_least8_t),outsz);
+ (*_out)[outsz - SIZE_C(0x1)] = (uint_least32_t){0x0}; /* Create null-terminator on output array. */
+ for(size_t n = SIZE_C(0x0), outn = SIZE_C(0x0);n < insz;n += SIZE_C(0x1),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);
+ n += SIZE_C(0x1);
+ codep += (_in[n] ^ UINT32_C(0x80)) << UINT32_C(0xC);
+ n += SIZE_C(0x1);
+ codep += (_in[n] ^ UINT32_C(0x80)) << UINT32_C(0x6);
+ n += SIZE_C(0x1);
+ codep += (uint_least32_t)(_in[n]) ^ SIZE_C(0x80);
+ (*_out)[outn] = codep;
continue;
}
- if(chr >= (uint_least8_t){0xE0}) { // Three bytes.
- uint_least32_t codep = (uint_least32_t){(chr ^ 0xE0) << 0xC};
- n += (size_t){0x1};
- chr = utf[n];
- codep += (uint_least32_t){(chr ^ 0x80) << 0x6};
- n += (size_t){0x1};
- chr = utf[n];
- codep += (uint_least32_t){(chr ^ 0x80)};
- n += (size_t){0x1};
- (*codeps)[outn] = codep;
+ if(_in[n] >= UINT8_C(0xE0)) { /* Three bytes. */
+ uint_least32_t codep = (_in[n] ^ UINT32_C(0xE0)) << UINT32_C(0xC);
+ n += SIZE_C(0x1);
+ codep += (_in[n] ^ UINT32_C(0x80)) << UINT32_C(0x6);
+ n += SIZE_C(0x1);
+ codep += _in[n] ^ UINT32_C(0x80);
+ n += SIZE_C(0x1);
+ (*_out)[outn] = codep;
continue;
}
- if(chr >= (uint_least8_t){0xC0}) { // Two bytes.
- uint_least32_t codep = (uint_least32_t){(chr ^ 0xC0) << 0x6};
- n += (size_t){0x1};
- chr = utf[n];
- codep += (uint_least32_t){(chr ^ 0x80)};
- n += (size_t){0x1};
- (*codeps)[outn] = codep;
+ if(_in[n] >= UINT8_C(0xC0)) { /* Two bytes. */
+ uint_least32_t codep = (_in[n] ^ UINT32_C(0xC0)) << UINT32_C(0x6);
+ n += SIZE_C(0x1);
+ codep += _in[n] ^ UINT32_C(0x80);
+ n += SIZE_C(0x1);
+ (*_out)[outn] = codep;
continue;
}
- // One byte.
- uint_least32_t codep = (uint_least32_t){chr};
- (*codeps)[outn] = codep;
+ /* One byte. */
+ (*_out)[outn] = (uint_least32_t)(_in[n]);
continue;
}
- return 0x0;
+ return UINT8_C(0x0);
}
diff --git a/src/u8c/u8enc.c b/src/u8c/u8enc.c
index 3ea6cdc..f37e9cf 100644
--- a/src/u8c/u8enc.c
+++ b/src/u8c/u8enc.c
@@ -3,9 +3,9 @@
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 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.
+ 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.
@@ -13,70 +13,76 @@
If not, see <https://www.gnu.org/licenses/>.
*/
-# include <u8c/u8enc.h>
+# include "seterr.h"
+# include <assert.h>
# include <stdint.h>
-# include <stdio.h>
# include <stdlib.h>
-uint_least8_t u8c_u8enc(uint_least32_t * codeps,size_t * utfsz,uint_least8_t * * utf) {
- size_t sz = (size_t){0x0}; // Size of input array (bytes).
- size_t outsz = (size_t){0x0}; // Size of output array /bytes).
- for(size_t n = (size_t){0x0};;n += (size_t){0x1}) { // First pass: get size of input array, and determine size of output array.
- uint_least32_t codep = codeps[n]; // Current Unicode codepoint.
- if(codep == (uint_least32_t){0x0}) { // U+0000 is Null.
- sz = n;
- break;
+# include <u8c/u8enc.h>
+# include <u8c/SIZE_C.h>
+uint_least8_t u8c_u8enc(size_t * _sz,uint_least8_t * * _out,uint_least32_t * _in) {
+ 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(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. */
+ if(_in[n] == UINT32_C(0x0)) { /* U+0000 is Null. */
+ insz = n;
+ goto nottoobig;
}
- if(codep >= (uint_least32_t){0x110000}) { // Codepoint out of range.
- return 0x1;
+ if(_in[n] >= 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);
}
- if(codep >= (uint_least32_t){0x10000}) { // 4 bytes.
- outsz += (size_t){0x4};
+ if(_in[n] >= UINT32_C(0x10000)) { /* 4 bytes. */
+ outsz += SIZE_C(0x4);
continue;
}
- if(codep >= (uint_least32_t){0x800}) { // 3 bytes.
- outsz += (size_t){0x3};
+ if(_in[n] >= UINT32_C(0x800)) { /* 3 bytes. */
+ outsz += SIZE_C(0x3);
continue;
}
- if(codep >= (uint_least32_t){0x80}) { // 2 bytes.
- outsz += (size_t){0x2};
+ if(_in[n] >= UINT32_C(0x80)) { /* 2 bytes. */
+ outsz += SIZE_C(0x2);
continue;
}
- // 1 byte.
- outsz += (size_t){0x1};
+ /* 1 byte. */
+ outsz += SIZE_C(0x1);
}
- outsz += (size_t){0x1}; // Add space for null-terminator.
- if(utfsz != NULL) {
- *utfsz = outsz;
+ 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);
+nottoobig:;
+ outsz += SIZE_C(0x1); /* Reserve space for null-terminator. */
+ if(_sz != NULL) {
+ *_sz = outsz;
}
- *utf = malloc(outsz); // Allocate space for output array.
- (*utf)[outsz - (size_t){0x1}] = (uint_least8_t){0x0}; // Create null-terminator on output array.
- for(size_t n = (size_t){0x0}, outn = (size_t){0x0};n < sz;n += (size_t){0x1},outn += (size_t){0x1}) { // Second pass: encode each codepoint into UTF-8.
- if(codeps[n] >= 0x10000) { // Four bytes.
- (*utf)[outn] = (uint_least8_t){0xF0 + (codeps[n] >> 0x12)};
- outn += (size_t){0x1};
- (*utf)[outn] = (uint_least8_t){0x80 + ((codeps[n] >> 0xC) & 0x3F)};
- outn += (size_t){0x1};
- (*utf)[outn] = (uint_least8_t){0x80 + ((codeps[n] >> 0x6) & 0x3F)};
- outn += (size_t){0x1};
- (*utf)[outn] = (uint_least8_t){0x80 + (codeps[n] & 0x3F)};
+ *_out = calloc(sizeof(uint_least8_t),outsz); /* Allocate space for output array. */
+ (*_out)[outsz - SIZE_C(0x1)] = UINT8_C(0x0); /* Create null-terminator on output array. */
+ for(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. */
+ if(_in[n] >= UINT32_C(0x10000)) { // Four bytes.
+ (*_out)[outn] = UINT8_C(0xF0) + (uint_least8_t)(_in[n] >> UINT32_C(0x12));
+ outn += SIZE_C(0x1);
+ (*_out)[outn] = UINT8_C(0x80) + (uint_least8_t)(_in[n] >> UINT32_C(0xC) & UINT8_C(0x3F));
+ outn += SIZE_C(0x1);
+ (*_out)[outn] = UINT8_C(0x80) + (uint_least8_t)(_in[n] >> UINT32_C(0x6) & UINT8_C(0x3F));
+ outn += SIZE_C(0x1);
+ (*_out)[outn] = UINT8_C(0x80) + (uint_least8_t)(_in[n] & UINT32_C(0x3F));
continue;
}
- if(codeps[n] >= 0x800) { // Three bytes.
- (*utf)[outn] = (uint_least8_t){0xE0 + (codeps[n] >> 0xC)};
- outn += (size_t){0x1};
- (*utf)[outn] = (uint_least8_t){0x80 + ((codeps[n] >> 0x6) & 0x3F)};
- outn += (size_t){0x1};
- (*utf)[outn] = (uint_least8_t){0x80 + (codeps[n] & 0x3F)};
+ if(_in[n] >= UINT32_C(0x800)) { /* Three bytes. */
+ (*_out)[outn] = UINT8_C(0xE0) + (uint_least8_t)(_in[n] >> UINT32_C(0xC));
+ outn += SIZE_C(0x1);
+ (*_out)[outn] = UINT8_C(0x80) + (uint_least8_t)(_in[n] >> UINT32_C(0x6) & UINT8_C(0x3F));
+ outn += SIZE_C(0x1);
+ (*_out)[outn] = UINT8_C(0x80) + (uint_least8_t)(_in[n] & UINT32_C(0x3F));
continue;
}
- if(codeps[n] >= 0x80) { // Two bytes.
- (*utf)[outn] = (uint_least8_t){0xC0 + (codeps[n] >> 0x6)};
- outn += (size_t){0x1};
- (*utf)[outn] = (uint_least8_t){0x80 + (codeps[n] & 0x3F)};
+ if(_in[n] >= UINT32_C(0x80)) { /* Two bytes. */
+ (*_out)[outn] = UINT8_C(0xC0) + (uint_least8_t)(_in[n] >> UINT8_C(0x6));
+ outn += SIZE_C(0x1);
+ (*_out)[outn] = UINT8_C(0x80) + (uint_least8_t)(_in[n] & UINT8_C(0x3F));
continue;
}
- // One byte.
- (*utf)[outn] = codeps[n];
+ /* One byte. */
+ (*_out)[outn] = (uint_least8_t)_in[n];
}
- return 0x0;
+ return UINT8_C(0x0);
}
diff --git a/src/u8c/vfmt.c b/src/u8c/vfmt.c
new file mode 100644
index 0000000..c84aca2
--- /dev/null
+++ b/src/u8c/vfmt.c
@@ -0,0 +1,30 @@
+/*
+ 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 <stdarg.h>
+# include <stdint.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+# include <u8c/fmttyp.h>
+# include <u8c/u32cp.h>
+# include <u8c/u8enc.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/vfmt.h>
+uint_least8_t u8c_vfmt(size_t * _outsz,uint_least32_t * * _out,uint_least32_t * _in,va_list _args) {
+ /* To be added. */
+ return u8c_u32cp(_outsz,_out,_in);
+}
diff --git a/src/u8c/vprint.c b/src/u8c/vprint.c
new file mode 100644
index 0000000..fcbafe4
--- /dev/null
+++ b/src/u8c/vprint.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 "seterr.h"
+# include <assert.h>
+# include <stdarg.h>
+# include <stdint.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <u8c/col.h>
+# include <u8c/u8enc.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/vfmt.h>
+# include <u8c/vprint.h>
+uint_least8_t u8c_vprint(FILE * _fp,uint_least32_t * _msg,va_list _args) {
+ assert(_msg != NULL);
+ uint_least32_t * str0 = NULL;
+ u8c_vfmt(NULL,&str0,_msg,_args);
+ size_t str1sz = SIZE_C(0x0);
+ uint_least8_t * 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(0x70),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x76),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_printv: fwrite: Unable to write to stdout. */
+ return UINT8_C(0x1);
+ }
+ free(str0);
+ free(str1);
+ return UINT8_C(0x0);
+}