summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-12-04 20:52:36 +0100
committerTyge Løvset <[email protected]>2022-12-04 20:52:36 +0100
commit20400977b40599fd1c6637dd659e72b900794d0f (patch)
treed4c60a2af4b21d6a6da4e79829254602e5f7c16c
parent1e0f229b0b044f6df0d0084674f3cf0dc4f4d31d (diff)
downloadSTC-modified-20400977b40599fd1c6637dd659e72b900794d0f.tar.gz
STC-modified-20400977b40599fd1c6637dd659e72b900794d0f.zip
Support for linking dependant UTF8 and cstr functions and/or cregex.c by defining i_extern before including <stc/cstr.h> and <stc/cregex.h>. I.e.
#define i_extern #define i_implement // define cstr functions as shared symbols (static if not defined). #include <stc/cregex.h> // include cstr.h, utf8code.c, cregex.c, unless already included.
-rw-r--r--examples/forfilter.c4
-rw-r--r--examples/regex1.c6
-rw-r--r--examples/regex2.c6
-rw-r--r--examples/regex_match.c8
-rw-r--r--examples/regex_replace.c8
-rw-r--r--examples/splitstr.c8
-rw-r--r--examples/utf8replace_c.c6
-rw-r--r--include/stc/cregex.h13
-rw-r--r--include/stc/cstr.h37
-rw-r--r--include/stc/csview.h2
-rw-r--r--include/stc/utf8.h8
-rw-r--r--src/cregex.c8
-rw-r--r--src/utf8code.c35
13 files changed, 74 insertions, 75 deletions
diff --git a/examples/forfilter.c b/examples/forfilter.c
index 25ed07cd..bf486ea4 100644
--- a/examples/forfilter.c
+++ b/examples/forfilter.c
@@ -1,5 +1,5 @@
#include <stdio.h>
-#define i_implement
+#define i_extern
#include <stc/cstr.h>
#include <stc/csview.h>
#include <stc/views.h>
@@ -142,5 +142,3 @@ int main(void)
demo4();
demo5();
}
-
-#include "../src/utf8code.c"
diff --git a/examples/regex1.c b/examples/regex1.c
index d1f061fb..47cce4cf 100644
--- a/examples/regex1.c
+++ b/examples/regex1.c
@@ -1,5 +1,4 @@
-#define i_implement
-#include <stc/cstr.h>
+#define i_extern
#include <stc/cregex.h>
int main(int argc, char* argv[])
@@ -29,6 +28,3 @@ int main(int argc, char* argv[])
}
}
}
-
-#include "../src/cregex.c"
-#include "../src/utf8code.c"
diff --git a/examples/regex2.c b/examples/regex2.c
index 6dffc8c4..8c4df420 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -1,5 +1,4 @@
-#define i_implement
-#include <stc/cstr.h>
+#define i_extern
#include <stc/cregex.h>
int main()
@@ -31,6 +30,3 @@ int main()
}
}
}
-
-#include "../src/cregex.c"
-#include "../src/utf8code.c"
diff --git a/examples/regex_match.c b/examples/regex_match.c
index da23b3a2..fd4315ad 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -1,7 +1,6 @@
-#define i_implement
-#include <stc/cstr.h>
-#include <stc/csview.h>
+#define i_extern
#include <stc/cregex.h>
+#include <stc/csview.h>
#define i_val float
#include <stc/cstack.h>
@@ -33,6 +32,3 @@ int main()
printf("\n%s\n", cstr_str(&nums));
}
}
-
-#include "../src/cregex.c"
-#include "../src/utf8code.c"
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
index 13bc9bf0..4091a59e 100644
--- a/examples/regex_replace.c
+++ b/examples/regex_replace.c
@@ -1,7 +1,6 @@
-#define i_implement
-#include <stc/cstr.h>
-#include <stc/csview.h>
+#define i_extern
#include <stc/cregex.h>
+#include <stc/csview.h>
bool add_10_years(int i, csview m, cstr* mstr) {
if (i == 1) { // group 1 matches year
@@ -52,6 +51,3 @@ int main()
printf("curly: %s\n", cstr_str(&str));
}
}
-
-#include "../src/cregex.c"
-#include "../src/utf8code.c"
diff --git a/examples/splitstr.c b/examples/splitstr.c
index 27dd308c..a7b386f3 100644
--- a/examples/splitstr.c
+++ b/examples/splitstr.c
@@ -1,8 +1,7 @@
#include <stdio.h>
-#define i_implement // cstr functions
-#include <stc/cstr.h>
-#include <stc/csview.h>
+#define i_extern // cstr + utf8 functions
#include <stc/cregex.h>
+#include <stc/csview.h>
int main()
{
@@ -18,6 +17,3 @@ int main()
c_formatch (i, &re, " Hello World C99! ")
printf("'%.*s'\n", c_ARGsv(i.match[0]));
}
-
-#include "../src/cregex.c"
-#include "../src/utf8code.c"
diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c
index b43aab6f..be77d615 100644
--- a/examples/utf8replace_c.c
+++ b/examples/utf8replace_c.c
@@ -1,9 +1,9 @@
-#define i_implement
+#define i_extern // add utf8 dependencies
#include <stc/cstr.h>
#include <stc/csview.h>
int main() {
- c_auto (cstr, hello) {
+ c_auto (cstr, hello, upper) {
hello = cstr_new("hell😀 w😀rld");
printf("%s\n", cstr_str(&hello));
@@ -14,6 +14,8 @@ int main() {
cstr_replace(&hello, "🐨", "ø", 1);
printf("%s\n", cstr_str(&hello));
+ upper = cstr_toupper_sv(cstr_sv(&hello));
+
c_foreach (c, cstr, hello)
printf("%.*s,", c_ARGsv(c.u8.chr));
puts("");
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index 43dc7ac0..877f4052 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -22,8 +22,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
-#ifndef CREGEX_H_
-#define CREGEX_H_
+#ifndef CREGEX_H_INCLUDED
+#define CREGEX_H_INCLUDED
/*
* cregex.h
*
@@ -135,4 +135,13 @@ cstr cregex_replace_pattern(const char* pattern, const char* input, const char*
/* destroy regex */
void cregex_drop(cregex* self);
+#endif // CREGEX_H_INCLUDED
+#if defined i_extern || defined STC_EXTERN
+# include "../src/cregex.c"
+# include "../src/utf8code.c"
#endif
+#undef i_opt
+#undef i_header
+#undef i_static
+#undef i_implement
+#undef i_extern
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index bf58444c..8a351d02 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -27,6 +27,9 @@
#ifndef CSTR_H_INCLUDED
#define CSTR_H_INCLUDED
+#if defined i_extern || defined STC_EXTERN
+# define _i_extern
+#endif
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -405,8 +408,38 @@ STC_INLINE bool cstr_getline(cstr *self, FILE *fp)
STC_API uint64_t cstr_hash(const cstr *self);
+#ifdef _i_extern
+static struct {
+ int (*conv_asc)(int);
+ uint32_t (*conv_utf)(uint32_t);
+}
+fn_tocase[] = {{tolower, utf8_casefold},
+ {tolower, utf8_tolower},
+ {toupper, utf8_toupper}};
+
+cstr cstr_tocase(csview sv, int k) {
+ cstr out = cstr_init();
+ char *buf = cstr_reserve(&out, sv.size*3/2);
+ uint32_t cp; size_t sz = 0;
+ utf8_decode_t d = {.state=0};
+
+ while (*sv.str) {
+ do { utf8_decode(&d, (uint8_t)*sv.str++); } while (d.state);
+ if (d.codep < 128)
+ buf[sz++] = (char)fn_tocase[k].conv_asc((int)d.codep);
+ else {
+ cp = fn_tocase[k].conv_utf(d.codep);
+ sz += utf8_encode(buf + sz, cp);
+ }
+ }
+ _cstr_set_size(&out, sz);
+ cstr_shrink_to_fit(&out);
+ return out;
+}
+#endif
+
/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement) || defined(i_extern)
+#if defined(i_implement)
STC_DEF uint64_t cstr_hash(const cstr *self) {
csview sv = cstr_sv(self);
@@ -627,4 +660,4 @@ STC_DEF size_t cstr_printf(cstr* self, const char* fmt, ...) {
#undef i_header
#undef i_static
#undef i_implement
-#undef i_extern
+#undef _i_extern
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 149759ce..4bd37173 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -160,7 +160,7 @@ STC_INLINE bool csview_eq(const csview* x, const csview* y)
STC_API uint64_t csview_hash(const csview *self);
/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement) || defined(i_extern)
+#if defined(i_implement)
STC_DEF size_t csview_find_sv(csview sv, csview search) {
char* res = cstrnstrn(sv.str, search.str, sv.size, search.size);
diff --git a/include/stc/utf8.h b/include/stc/utf8.h
index 0ff79fb9..ab39f3ba 100644
--- a/include/stc/utf8.h
+++ b/include/stc/utf8.h
@@ -1,8 +1,8 @@
#ifndef UTF8_H_INCLUDED
#define UTF8_H_INCLUDED
+#include "forward.h"
#include "ccommon.h"
-#include <ctype.h>
// utf8 methods defined in src/utf8code.c:
@@ -88,4 +88,8 @@ STC_INLINE const char* utf8_at(const char *s, size_t index) {
STC_INLINE size_t utf8_pos(const char* s, size_t index)
{ return (size_t)(utf8_at(s, index) - s); }
-#endif
+#endif // UTF8_H_INCLUDED
+#if defined i_extern || defined STC_EXTERN
+# include "../src/utf8code.c"
+# undef i_extern
+#endif \ No newline at end of file
diff --git a/src/cregex.c b/src/cregex.c
index 358e299d..1747ed3f 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -23,11 +23,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
-
-#include <stc/cregex.h>
-#include <stc/utf8.h>
-#define i_header
+#ifndef CREGEX_C_INCLUDED
+#define CREGEX_C_INCLUDED
#include <stc/cstr.h>
+#include <stc/cregex.h> // header only
#include <setjmp.h>
#include <ctype.h>
@@ -1260,3 +1259,4 @@ void
cregex_drop(cregex* self) {
c_free(self->prog);
}
+#endif \ No newline at end of file
diff --git a/src/utf8code.c b/src/utf8code.c
index b1534230..5dfb7d30 100644
--- a/src/utf8code.c
+++ b/src/utf8code.c
@@ -1,7 +1,7 @@
+#ifndef UTF8_C_INCLUDED
+#define UTF8_C_INCLUDED
#include <ctype.h>
-#define i_header
-#include <stc/cstr.h>
-#include <stc/cregex.h>
+#include <stc/utf8.h> // header only
#include "utf8tabs.inc"
const uint8_t utf8_dtab[] = {
@@ -143,31 +143,4 @@ bool utf8_isalpha(uint32_t c) {
if (c < 128) return isalpha(c) != 0;
return utf8_islower(c) || utf8_isupper(c);
}
-
-static struct {
- int (*conv_asc)(int);
- uint32_t (*conv_utf)(uint32_t);
-}
-fn_tocase[] = {{tolower, utf8_casefold},
- {tolower, utf8_tolower},
- {toupper, utf8_toupper}};
-
-cstr cstr_tocase(csview sv, int k) {
- cstr out = cstr_null;
- char *buf = cstr_reserve(&out, sv.size*3/2);
- uint32_t cp; size_t sz = 0;
- utf8_decode_t d = {.state=0};
-
- while (*sv.str) {
- do { utf8_decode(&d, (uint8_t)*sv.str++); } while (d.state);
- if (d.codep < 128)
- buf[sz++] = (char)fn_tocase[k].conv_asc((int)d.codep);
- else {
- cp = fn_tocase[k].conv_utf(d.codep);
- sz += utf8_encode(buf + sz, cp);
- }
- }
- _cstr_set_size(&out, sz);
- cstr_shrink_to_fit(&out);
- return out;
-}
+#endif