summaryrefslogtreecommitdiff
path: root/src/u8c/str.h.d
diff options
context:
space:
mode:
Diffstat (limited to 'src/u8c/str.h.d')
-rw-r--r--src/u8c/str.h.d/stralloc.c33
-rw-r--r--src/u8c/str.h.d/strcat.c46
-rw-r--r--src/u8c/str.h.d/strcmp.c45
-rw-r--r--src/u8c/str.h.d/strcp.c40
-rw-r--r--src/u8c/str.h.d/strfndchr.c45
-rw-r--r--src/u8c/str.h.d/strfndpat.c43
-rw-r--r--src/u8c/str.h.d/strfree.c26
-rw-r--r--src/u8c/str.h.d/strins.c38
-rw-r--r--src/u8c/str.h.d/strsubstr.c48
-rw-r--r--src/u8c/str.h.d/strsz.c35
10 files changed, 399 insertions, 0 deletions
diff --git a/src/u8c/str.h.d/stralloc.c b/src/u8c/str.h.d/stralloc.c
new file mode 100644
index 0000000..f9addcd
--- /dev/null
+++ b/src/u8c/str.h.d/stralloc.c
@@ -0,0 +1,33 @@
+/*
+ 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/err.h>
+# include <u8c/str.h>
+# include <uchar.h>
+struct u8c_stralloc_tuple u8c_stralloc(size_t const _sz) {
+ struct u8c_stralloc_tuple ret = {
+ .stat = false,
+ };
+ char32_t * arr = NULL;
+ if((arr = calloc(sizeof *arr,_sz)) == NULL) {
+ u8c_seterr(u8c_errtyp_badalloc,U"u8c_stralloc: Unable to allocate resources (not enough memory?).");
+ ret.stat = true;
+ return ret;
+ }
+ ret.str = arr;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strcat.c b/src/u8c/str.h.d/strcat.c
new file mode 100644
index 0000000..5e5f693
--- /dev/null
+++ b/src/u8c/str.h.d/strcat.c
@@ -0,0 +1,46 @@
+/*
+ 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/SIZE_C.h>
+# include <u8c/err.h>
+# include <u8c/str.h>
+# include <uchar.h>
+struct u8c_strcat_tuple u8c_strcat(char32_t const * const restrict _lstr,char32_t const * const restrict _rstr) {
+ struct u8c_strcat_tuple ret = {
+ .stat = false,
+ };
+ size_t lsz = u8c_strsz(_lstr).sz;
+ size_t rsz = u8c_strsz(_rstr).sz;
+ ret.strsz = lsz + rsz;
+ char32_t * out = NULL;
+ {
+ struct u8c_stralloc_tuple const tuple = u8c_stralloc(ret.strsz + SIZE_C(0x1));
+ if(tuple.stat) {
+ ret.stat = true;
+ return ret;
+ }
+ out = tuple.str;
+ }
+ for(register size_t n = SIZE_C(0x0);n < lsz;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] = _rstr[n];
+ }
+ ret.str = out;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strcmp.c b/src/u8c/str.h.d/strcmp.c
new file mode 100644
index 0000000..31654d0
--- /dev/null
+++ b/src/u8c/str.h.d/strcmp.c
@@ -0,0 +1,45 @@
+/*
+ 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 <stddef.h>
+# include <stdint.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/err.h>
+# include <u8c/str.h>
+struct u8c_strcmp_tuple u8c_strcmp(char32_t const * const restrict _lstr,char32_t const * const restrict _rstr) {
+ struct u8c_strcmp_tuple ret = {
+ .stat = false,
+ };
+ for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) {
+ register char32_t const lchr = _lstr[n];
+ register char32_t const rchr = _rstr[n];
+ if(lchr != rchr) {
+ if(lchr < rchr) {
+ ret.res = UINT8_C(0x0);
+ return ret;
+ }
+ ret.res = UINT8_C(0x2);
+ return ret;
+ }
+ if(lchr == U'\x0') {
+ ret.res = UINT8_C(0x1);
+ return ret;
+ }
+ }
+ u8c_seterr(u8c_errtyp_untermin,U"u8c_strcmp: Unterminated input.");
+ ret.stat = true;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strcp.c b/src/u8c/str.h.d/strcp.c
new file mode 100644
index 0000000..1343bf1
--- /dev/null
+++ b/src/u8c/str.h.d/strcp.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 <stdbool.h>
+# include <stdlib.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/err.h>
+# include <u8c/str.h>
+struct u8c_strcp_tuple u8c_strcp(char32_t const * const restrict _in) {
+ struct u8c_strcp_tuple ret = {
+ .stat = false,
+ };
+ ret.strsz = u8c_strsz(_in).sz;
+ uint_least32_t * out = NULL;
+ {
+ struct u8c_stralloc_tuple const tuple = u8c_stralloc(ret.strsz + SIZE_C(0x1));
+ if(tuple.stat) {
+ ret.stat = true;
+ return ret;
+ }
+ out = tuple.str;
+ }
+ for(register size_t n = SIZE_C(0x0);n < ret.strsz;n += SIZE_C(0x1)) {
+ out[n] = _in[n];
+ }
+ ret.str = out;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strfndchr.c b/src/u8c/str.h.d/strfndchr.c
new file mode 100644
index 0000000..93bb77c
--- /dev/null
+++ b/src/u8c/str.h.d/strfndchr.c
@@ -0,0 +1,45 @@
+/*
+ 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 <stddef.h>
+# include <stdint.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/err.h>
+# include <u8c/str.h>
+struct u8c_strfndchr_tuple u8c_strfndchr(char32_t const * const restrict _in,char32_t const _chr) {
+ struct u8c_strfndchr_tuple ret = {
+ .stat = false,
+ };
+ for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) {
+ register uint_least32_t const tmp = _in[n];
+ if(tmp == U'\x0') {
+ if(_chr == U'\x0') {
+ ret.pos = n;
+ return ret;
+ }
+ ret.pos = SIZE_C(-0x1);
+ return ret;
+ }
+ if(tmp == _chr) {
+ ret.pos = n;
+ return ret;
+ }
+ }
+ u8c_seterr(u8c_errtyp_untermin,U"u8c_strfndchr: Unterminated input.");
+ ret.pos = SIZE_C(-0x1);
+ ret.stat = true;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strfndpat.c b/src/u8c/str.h.d/strfndpat.c
new file mode 100644
index 0000000..1091238
--- /dev/null
+++ b/src/u8c/str.h.d/strfndpat.c
@@ -0,0 +1,43 @@
+/*
+ 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 <stddef.h>
+# include <stdint.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/err.h>
+# include <u8c/str.h>
+struct u8c_strfndpat_tuple u8c_strfndpat(char32_t const * const restrict _in,char32_t const * const restrict _pat) {
+ struct u8c_strfndpat_tuple ret = {
+ .stat = false,
+ };
+ size_t insz = u8c_strsz(_in).sz;
+ size_t patsz = u8c_strsz(_pat).sz;
+ if(insz == SIZE_C(0x1) || insz < patsz) {
+ ret.pos = SIZE_C(-0x1);
+ return ret;
+ }
+ for(register size_t n = SIZE_C(0x0);n < insz - patsz;n += SIZE_C(0x1)) {
+ char32_t const * str = u8c_strsubstr(n,patsz - SIZE_C(0x1),_in).str;
+ uint_least8_t const cmpres = u8c_strcmp(str,_pat).res;
+ u8c_strfree(str);
+ if(cmpres == UINT8_C(0x1)) {
+ ret.pos = n;
+ return ret;
+ }
+ }
+ ret.pos = SIZE_C(-0x1);
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strfree.c b/src/u8c/str.h.d/strfree.c
new file mode 100644
index 0000000..bf6d477
--- /dev/null
+++ b/src/u8c/str.h.d/strfree.c
@@ -0,0 +1,26 @@
+/*
+ Copyright 2021 Gabriel Jensen
+
+ This file is part of u8c.
+
+ u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with u8c.
+
+ If not, see <https://www.gnu.org/licenses/>.
+*/
+# include <stdbool.h>
+# include <stdint.h>
+# include <stdlib.h>
+# include <u8c/str.h>
+struct u8c_strfree_tuple u8c_strfree(char32_t const * const restrict _str) {
+ struct u8c_strfree_tuple ret = {
+ .stat = false,
+ };
+ free((char32_t *)_str); /* This cast does indeed discard a const-qualifier, but it is not undefined behaviour, as the array must have been allocated by calloc or malloc, meaning it's original type is not const-qualified. */
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strins.c b/src/u8c/str.h.d/strins.c
new file mode 100644
index 0000000..89173ae
--- /dev/null
+++ b/src/u8c/str.h.d/strins.c
@@ -0,0 +1,38 @@
+/*
+ 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 <stddef.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/str.h>
+# include <uchar.h>
+struct u8c_strins_tuple u8c_strins(size_t const _pos,char32_t const * const restrict _str0,char32_t const * const restrict _str1) {
+ struct u8c_strins_tuple ret = {
+ .stat = false,
+ };
+ char32_t const * lstr = u8c_strsubstr(SIZE_C(0x0),_pos - SIZE_C(0x1),_str0).str;
+ char32_t const * rstr = u8c_strsubstr(_pos,SIZE_C(0x0),_str0).str;
+ ret.strsz = SIZE_C(0x0);
+ char32_t const * out = NULL;
+ {
+ char32_t const * tmp = u8c_strcat(lstr,_str1).str;
+ u8c_strfree(lstr);
+ out = u8c_strcat(tmp,rstr).str;
+ u8c_strfree(rstr);
+ u8c_strfree(tmp);
+ }
+ ret.str = out;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strsubstr.c b/src/u8c/str.h.d/strsubstr.c
new file mode 100644
index 0000000..b9daac5
--- /dev/null
+++ b/src/u8c/str.h.d/strsubstr.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 <stdbool.h>
+# include <stdlib.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/str.h>
+# include <uchar.h>
+struct u8c_strsubstr_tuple u8c_strsubstr(size_t const _start,size_t const _len,char32_t const * const restrict _in) {
+ struct u8c_strsubstr_tuple ret = {
+ .stat = false,
+ };
+ size_t insz = u8c_strsz(_in).sz;
+ size_t len = _len;
+ if(_len == SIZE_C(0x0)) {
+ len = insz - _start;
+ }
+ if(insz < _start + len) {
+ return ret;
+ }
+ size_t const outsz = len + SIZE_C(0x2);
+ char32_t * out = NULL;
+ {
+ struct u8c_stralloc_tuple const tuple = u8c_stralloc(outsz);
+ if(tuple.stat) {
+ ret.stat = true;
+ return ret;
+ }
+ out = tuple.str;
+ }
+ for(register size_t n = SIZE_C(0x0);n <= len;n += SIZE_C(0x1)) {
+ out[n] = _in[n + _start];
+ }
+ ret.str = out;
+ return ret;
+}
diff --git a/src/u8c/str.h.d/strsz.c b/src/u8c/str.h.d/strsz.c
new file mode 100644
index 0000000..f1b348a
--- /dev/null
+++ b/src/u8c/str.h.d/strsz.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 <stdbool.h>
+# include <stddef.h>
+# include <stdint.h>
+# include <u8c/SIZE_C.h>
+# include <u8c/str.h>
+# include <uchar.h>
+struct u8c_strsz_tuple u8c_strsz(char32_t const * const restrict _in) {
+ struct u8c_strsz_tuple ret = {
+ .stat = false,
+ };
+ {
+ struct u8c_strfndchr_tuple const tuple = u8c_strfndchr(_in,UINT8_C(0x0));
+ if(tuple.stat) {
+ ret.stat = true;
+ return ret;
+ }
+ ret.sz = tuple.pos;
+ }
+ return ret;
+}