summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-05-30 14:22:44 +0200
committerTyge Løvset <[email protected]>2022-05-30 14:22:44 +0200
commit58bb58e7980d1eae175ed66dbe873893a05ab81e (patch)
tree2756b2d1b62264c34a6d40265dc85f07eb2f1112
parentb28d3fa7c3b9233ca485014744bf84e6c4f5a1d3 (diff)
downloadSTC-modified-58bb58e7980d1eae175ed66dbe873893a05ab81e.tar.gz
STC-modified-58bb58e7980d1eae175ed66dbe873893a05ab81e.zip
Done refactoring:
- Non-templated types (cstr, csview, cbits, crandom) have no longer default static linking. Now i_header is defined, i.e. files are as headers only. ==> Define `i_implement` before file inclusion. Still possible to do static linkage by defining `i_static` before inclusion or global STC_STATIC. - Templated containers still have static linkage by default. - Renamed csview_substr(), csview_slice() to csview_substr_ex(), csview_slice_ex(). Added simpler inlined csview_substr(), csview_slice().
-rw-r--r--docs/csview_api.md4
-rw-r--r--examples/arc_containers.c1
-rw-r--r--examples/astar.c2
-rw-r--r--examples/birthday.c1
-rw-r--r--examples/bits.c1
-rw-r--r--examples/bits2.c1
-rw-r--r--examples/books.c2
-rw-r--r--examples/box.c1
-rw-r--r--examples/city.c1
-rw-r--r--examples/complex.c1
-rw-r--r--examples/convert.c1
-rw-r--r--examples/csmap_erase.c1
-rw-r--r--examples/csmap_find.c1
-rw-r--r--examples/csmap_insert.c2
-rw-r--r--examples/cstr_match.c1
-rw-r--r--examples/demos.c1
-rw-r--r--examples/gauss1.c3
-rw-r--r--examples/gauss2.c2
-rw-r--r--examples/hashmap.c2
-rw-r--r--examples/inits.c2
-rw-r--r--examples/list.c1
-rw-r--r--examples/mapmap.c2
-rw-r--r--examples/mmap.c1
-rw-r--r--examples/multimap.c2
-rw-r--r--examples/music_arc.c2
-rw-r--r--examples/new_arr.c2
-rw-r--r--examples/new_deq.c1
-rw-r--r--examples/new_list.c2
-rw-r--r--examples/new_map.c1
-rw-r--r--examples/new_queue.c1
-rw-r--r--examples/new_smap.c1
-rw-r--r--examples/new_sptr.c1
-rw-r--r--examples/new_vec.c1
-rw-r--r--examples/person_arc.c1
-rw-r--r--examples/phonebook.c2
-rw-r--r--examples/prime.c2
-rw-r--r--examples/priority.c1
-rw-r--r--examples/queue.c1
-rw-r--r--examples/random.c1
-rw-r--r--examples/rawptr_elements.c1
-rw-r--r--examples/read.c1
-rw-r--r--examples/regex1.c1
-rw-r--r--examples/regex2.c3
-rw-r--r--examples/regex_match.c6
-rw-r--r--examples/replace.c2
-rw-r--r--examples/splitstr.c4
-rw-r--r--examples/sso_map.c2
-rw-r--r--examples/sso_substr.c14
-rw-r--r--examples/sview_split.c2
-rw-r--r--examples/unordered_set.c1
-rw-r--r--examples/utf8replace_c.c1
-rw-r--r--examples/vikings.c2
-rw-r--r--examples/words.c1
-rw-r--r--include/stc/cbits.h1
-rw-r--r--include/stc/ccommon.h4
-rw-r--r--include/stc/crandom.h35
-rw-r--r--include/stc/cstr.h2
-rw-r--r--include/stc/csview.h38
-rw-r--r--src/cregex.c2
59 files changed, 121 insertions, 59 deletions
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 55ac017e..543173d3 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -40,8 +40,8 @@ char csview_back(csview sv);
void csview_clear(csview* self);
-csview csview_substr(csview sv, intptr_t pos, size_t n); // negative pos count from end
-csview csview_slice(csview sv, intptr_t p1, intptr_t p2); // negative p1, p2 count from end
+csview csview_substr_ex(csview sv, intptr_t pos, size_t n); // negative pos count from end
+csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); // negative p1, p2 count from end
csview csview_token(csview sv, csview sep, size_t* start); // see split example below.
bool csview_equals(csview sv, csview sv2);
diff --git a/examples/arc_containers.c b/examples/arc_containers.c
index 0ee2cbeb..1fe227f7 100644
--- a/examples/arc_containers.c
+++ b/examples/arc_containers.c
@@ -1,5 +1,6 @@
// Create a stack and a list of shared pointers to maps,
// and demonstrate sharing and cloning of maps.
+#define i_static
#include <stc/cstr.h>
#define i_type Map
#define i_key_str // strings
diff --git a/examples/astar.c b/examples/astar.c
index bce03716..5bcbe7e9 100644
--- a/examples/astar.c
+++ b/examples/astar.c
@@ -4,7 +4,7 @@
// This is a reimplementation of the CTL example to STC:
// https://github.com/glouw/ctl/blob/master/examples/astar.c
// https://www.redblobgames.com/pathfinding/a-star/introduction.html
-
+#define i_implement
#include <stc/cstr.h>
#include <stdio.h>
diff --git a/examples/birthday.c b/examples/birthday.c
index 7c95f961..15d85e0c 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -1,6 +1,7 @@
#include <math.h>
#include <stdio.h>
#include <time.h>
+#define i_implement
#include <stc/crandom.h>
#define i_tag ic
diff --git a/examples/bits.c b/examples/bits.c
index 8888f73e..051beb02 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#define i_implement
#include <stc/cbits.h>
int main()
diff --git a/examples/bits2.c b/examples/bits2.c
index 81e28d60..af87df42 100644
--- a/examples/bits2.c
+++ b/examples/bits2.c
@@ -3,6 +3,7 @@
#define i_type Bits
#define i_len 80 // enable fixed bitset on the stack
+#define i_implement
#include <stc/cbits.h>
int main()
diff --git a/examples/books.c b/examples/books.c
index eb644ba7..de724a19 100644
--- a/examples/books.c
+++ b/examples/books.c
@@ -1,5 +1,5 @@
// https://doc.rust-lang.org/std/collections/struct.HashMap.html
-
+#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
diff --git a/examples/box.c b/examples/box.c
index 2f82a694..4502f479 100644
--- a/examples/box.c
+++ b/examples/box.c
@@ -1,4 +1,5 @@
/* cbox: heap allocated boxed type */
+#define i_implement
#include <stc/cstr.h>
typedef struct { cstr name, last; } Person;
diff --git a/examples/city.c b/examples/city.c
index fb863e3c..8ee1576f 100644
--- a/examples/city.c
+++ b/examples/city.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
typedef struct {
diff --git a/examples/complex.c b/examples/complex.c
index 584343be..c5ac9882 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
void check_drop(float* v) {printf("destroy %g\n", *v);}
diff --git a/examples/convert.c b/examples/convert.c
index a17e41f6..48ba2ecf 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#define i_key_str
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 09d28d78..b1dbaada 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -1,5 +1,6 @@
// map_erase.c
// From C++ example: https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16
+#define i_implement
#include <stc/cstr.h>
#include <stdio.h>
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index c3047110..7fac5bc2 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -1,5 +1,6 @@
// This implements the c++ std::map::find example at:
// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
+#define i_implement
#include <stc/cstr.h>
#define i_key int
diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c
index 07fba9ed..24f536e9 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -1,5 +1,5 @@
+#define i_implement
#include <stc/cstr.h>
-#include <stdio.h>
// This implements the std::map insert c++ example at:
// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-19
diff --git a/examples/cstr_match.c b/examples/cstr_match.c
index 637fa7f9..a110e49a 100644
--- a/examples/cstr_match.c
+++ b/examples/cstr_match.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/csview.h>
#include <stdio.h>
diff --git a/examples/demos.c b/examples/demos.c
index c0cecac2..29bfba66 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
void stringdemo1()
diff --git a/examples/gauss1.c b/examples/gauss1.c
index 081a4a73..da58c86e 100644
--- a/examples/gauss1.c
+++ b/examples/gauss1.c
@@ -1,7 +1,8 @@
-#include <stdio.h>
#include <time.h>
#include <math.h>
+#define i_implement
#include <stc/crandom.h>
+#define i_implement
#include <stc/cstr.h>
// Declare int -> int hashmap. Uses typetag 'ii' for ints.
diff --git a/examples/gauss2.c b/examples/gauss2.c
index 5ded4714..496c43e1 100644
--- a/examples/gauss2.c
+++ b/examples/gauss2.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <time.h>
+#define i_implement
#include <stc/crandom.h>
+#define i_implement
#include <stc/cstr.h>
// Declare int -> int sorted map.
diff --git a/examples/hashmap.c b/examples/hashmap.c
index 9ff019e6..62f20079 100644
--- a/examples/hashmap.c
+++ b/examples/hashmap.c
@@ -1,5 +1,5 @@
// https://doc.rust-lang.org/rust-by-example/std/hash.html
-
+#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
diff --git a/examples/inits.c b/examples/inits.c
index ae64f6ec..1c904dda 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#define i_implement
#include <stc/cstr.h>
#define i_key int
diff --git a/examples/list.c b/examples/list.c
index 1b3ccdba..a0dbd49f 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <time.h>
+#define i_implement
#include <stc/crandom.h>
#define i_val double
diff --git a/examples/mapmap.c b/examples/mapmap.c
index ccf09314..9ac22371 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -1,5 +1,5 @@
// unordered_map<string, unordered_map<string, string>>:
-
+#define i_implement
#include <stc/cstr.h>
#define i_type People
#define i_key_str
diff --git a/examples/mmap.c b/examples/mmap.c
index 16b5d2df..4754656f 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -2,6 +2,7 @@
// https://en.cppreference.com/w/cpp/container/multimap/insert
// Multimap entries
+#define i_implement
#include <stc/cstr.h>
#define i_val_str
#include <stc/clist.h>
diff --git a/examples/multimap.c b/examples/multimap.c
index 17181d70..ee118dde 100644
--- a/examples/multimap.c
+++ b/examples/multimap.c
@@ -1,5 +1,5 @@
+#define i_implement
#include <stc/cstr.h>
-#include <stdio.h>
// Olympics multimap example
diff --git a/examples/music_arc.c b/examples/music_arc.c
index 489a136f..b6b59489 100644
--- a/examples/music_arc.c
+++ b/examples/music_arc.c
@@ -1,6 +1,6 @@
// shared_ptr-examples.cpp
// based on https://docs.microsoft.com/en-us/cpp/cpp/how-to-create-and-use-shared-ptr-instances?view=msvc-160
-
+#define i_implement
#include <stc/cstr.h>
struct Song
diff --git a/examples/new_arr.c b/examples/new_arr.c
index 0561764a..d046c835 100644
--- a/examples/new_arr.c
+++ b/examples/new_arr.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#define i_implement
#include <stc/cstr.h>
#define i_val int
diff --git a/examples/new_deq.c b/examples/new_deq.c
index 9022c377..f1e872f0 100644
--- a/examples/new_deq.c
+++ b/examples/new_deq.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/new_list.c b/examples/new_list.c
index 20a64ad3..43556aca 100644
--- a/examples/new_list.c
+++ b/examples/new_list.c
@@ -1,5 +1,5 @@
+#define i_implement
#include <stc/cstr.h>
-#include <stc/forward.h>
forward_clist(clist_i32, int);
forward_clist(clist_pnt, struct Point);
diff --git a/examples/new_map.c b/examples/new_map.c
index 32a4ce68..0882d02f 100644
--- a/examples/new_map.c
+++ b/examples/new_map.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/new_queue.c b/examples/new_queue.c
index 3dde97f5..86f4227c 100644
--- a/examples/new_queue.c
+++ b/examples/new_queue.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/crandom.h>
#include <stc/forward.h>
#include <stdio.h>
diff --git a/examples/new_smap.c b/examples/new_smap.c
index 38822585..2a885d03 100644
--- a/examples/new_smap.c
+++ b/examples/new_smap.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index 84f7e075..58f68dae 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
struct Person { cstr name, last; } typedef Person;
diff --git a/examples/new_vec.c b/examples/new_vec.c
index d1fe8728..3f3d5de8 100644
--- a/examples/new_vec.c
+++ b/examples/new_vec.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/person_arc.c b/examples/person_arc.c
index c77ca315..5ac1e9c2 100644
--- a/examples/person_arc.c
+++ b/examples/person_arc.c
@@ -1,4 +1,5 @@
/* cbox: heap allocated boxed type */
+#define i_implement
#include <stc/cstr.h>
typedef struct { cstr name, last; } Person;
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 26f4ff9e..ab782653 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -21,7 +21,7 @@
// Program to emulates the phone book.
-#include <stdio.h>
+#define i_implement
#include <stc/cstr.h>
#define i_key_str
diff --git a/examples/prime.c b/examples/prime.c
index cc16603b..92fafde4 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <math.h>
#include <time.h>
-
+#define i_implement
#include <stc/cbits.h>
cbits sieveOfEratosthenes(size_t n)
diff --git a/examples/priority.c b/examples/priority.c
index 66e400dd..65543590 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <time.h>
+#define i_implement
#include <stc/crandom.h>
#define i_val int64_t
diff --git a/examples/queue.c b/examples/queue.c
index 5f2fa03d..1b56d96a 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/crandom.h>
#include <stdio.h>
diff --git a/examples/random.c b/examples/random.c
index 9ffcf0e2..39fd7fa1 100644
--- a/examples/random.c
+++ b/examples/random.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <time.h>
+#define i_implement
#include <stc/crandom.h>
int main()
diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c
index c438727b..64d73843 100644
--- a/examples/rawptr_elements.c
+++ b/examples/rawptr_elements.c
@@ -13,6 +13,7 @@ struct { double x, y; } typedef Point;
#define i_tag pnt
#include <stc/cset.h>
+#define i_implement
#include <stc/cstr.h>
// Map of int64 pointers: Define i_valraw as int64_t for easy emplace calls!
typedef int64_t inttype;
diff --git a/examples/read.c b/examples/read.c
index 5f31e357..67b7e67e 100644
--- a/examples/read.c
+++ b/examples/read.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#define i_val_str
#include <stc/cvec.h>
diff --git a/examples/regex1.c b/examples/regex1.c
index 7481fbb1..84a5e28b 100644
--- a/examples/regex1.c
+++ b/examples/regex1.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
diff --git a/examples/regex2.c b/examples/regex2.c
index 7089956f..19c49ec2 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -1,6 +1,7 @@
+#define i_implement
+#include <stc/cstr.h>
#include <stc/cregex.h>
#include <stc/csview.h>
-#include <stc/cstr.h>
int main()
{
diff --git a/examples/regex_match.c b/examples/regex_match.c
index 0aa740d4..c2499733 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -1,12 +1,6 @@
-#include <stdio.h>
#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
-#include <stc/crandom.h>
-#include <stc/csview.h>
-#define i_val double
-#include <time.h>
-
int main()
{
diff --git a/examples/replace.c b/examples/replace.c
index f658fb3c..57018618 100644
--- a/examples/replace.c
+++ b/examples/replace.c
@@ -1,4 +1,4 @@
-
+#define i_implement
#include <stc/cstr.h>
int main ()
diff --git a/examples/splitstr.c b/examples/splitstr.c
index 390a6c6f..0cd48067 100644
--- a/examples/splitstr.c
+++ b/examples/splitstr.c
@@ -1,7 +1,9 @@
+#define i_implement
#include <stc/cstr.h>
+#define i_implement
+#include <stc/csview.h>
#define i_val_str
#include <stc/cvec.h>
-#include <stc/csview.h>
void print_split(csview str, csview sep)
{
diff --git a/examples/sso_map.c b/examples/sso_map.c
index bab182e3..53fac3e3 100644
--- a/examples/sso_map.c
+++ b/examples/sso_map.c
@@ -1,9 +1,9 @@
+#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
#include <stc/cmap.h>
-
int main()
{
c_auto (cmap_str, m) {
diff --git a/examples/sso_substr.c b/examples/sso_substr.c
index 8e21b7fe..288d0b85 100644
--- a/examples/sso_substr.c
+++ b/examples/sso_substr.c
@@ -1,19 +1,21 @@
+#define i_implement
#include <stc/cstr.h>
+#define i_implement
#include <stc/csview.h>
int main ()
{
cstr str = cstr_new("We think in generalities, but we live in details.");
- csview sv1 = cstr_substr(&str, 3, 5); // "think"
- size_t pos = cstr_find(str, "live"); // position of "live"
- csview sv2 = cstr_substr(&str, pos, 4); // "live"
- csview sv3 = cstr_slice(&str, -8, -1); // "details"
+ csview sv1 = cstr_substr_ex(&str, 3, 5); // "think"
+ size_t pos = cstr_find(str, "live"); // position of "live"
+ csview sv2 = cstr_substr_ex(&str, pos, 4); // "live"
+ csview sv3 = cstr_slice_ex(&str, -8, -1); // "details"
printf("%" c_PRIsv ", %" c_PRIsv ", %" c_PRIsv "\n",
c_ARGsv(sv1), c_ARGsv(sv2), c_ARGsv(sv3));
cstr_assign(&str, "apples are green or red");
- cstr s2 = cstr_from_sv(cstr_substr(&str, -3, 3)); // "red"
- cstr s3 = cstr_from_sv(cstr_substr(&str, 0, 6)); // "apples"
+ cstr s2 = cstr_from_sv(cstr_substr_ex(&str, -3, 3)); // "red"
+ cstr s3 = cstr_from_sv(cstr_substr_ex(&str, 0, 6)); // "apples"
#ifndef STC_CSTR_V1
printf("%s %s: %d, %d\n", cstr_str(&s2), cstr_str(&s3),
cstr_is_long(&str), cstr_is_long(&s2));
diff --git a/examples/sview_split.c b/examples/sview_split.c
index 3594a599..ec664ba0 100644
--- a/examples/sview_split.c
+++ b/examples/sview_split.c
@@ -1,4 +1,6 @@
+#define i_implement
#include <stc/cstr.h>
+#define i_implement
#include <stc/csview.h>
int main()
diff --git a/examples/unordered_set.c b/examples/unordered_set.c
index 11ffea80..9ecd59fe 100644
--- a/examples/unordered_set.c
+++ b/examples/unordered_set.c
@@ -1,5 +1,6 @@
// https://iq.opengenus.org/containers-cpp-stl/
// C program to demonstrate various function of stc cset
+#define i_implement
#include <stc/cstr.h>
#define i_key_str
#include <stc/cset.h>
diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c
index 89d5375f..733245e5 100644
--- a/examples/utf8replace_c.c
+++ b/examples/utf8replace_c.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/csview.h>
diff --git a/examples/vikings.c b/examples/vikings.c
index 623cd172..c63b7ffc 100644
--- a/examples/vikings.c
+++ b/examples/vikings.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#define i_implement
#include <stc/cstr.h>
typedef struct Viking {
diff --git a/examples/words.c b/examples/words.c
index 8a86ba7f..ed800ae7 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -1,4 +1,5 @@
#include <math.h>
+#define i_implement
#include <stc/cstr.h>
#define i_val_str
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index e35e4225..9c7081f7 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -51,6 +51,7 @@ int main() {
*/
#ifndef CBITS_H_INCLUDED
+#define i_header
#include "ccommon.h"
#include <stdlib.h>
#include <string.h>
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index f27190af..9c8ba414 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -252,8 +252,8 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
#undef STC_API
#undef STC_DEF
-#if !defined i_static && (defined(i_header) || defined(i_implement) || \
- defined(STC_HEADER) || defined(STC_IMPLEMENT))
+#if !defined(i_static) && !defined(STC_STATIC) && (defined(i_header) || defined(STC_HEADER) || \
+ defined(i_implement) || defined(STC_IMPLEMENT))
# define STC_API extern
# define STC_DEF
#else
diff --git a/include/stc/crandom.h b/include/stc/crandom.h
index a9877b50..a3f68279 100644
--- a/include/stc/crandom.h
+++ b/include/stc/crandom.h
@@ -20,6 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#define i_header
#include "ccommon.h"
#ifndef CRANDOM_H_INCLUDED
@@ -73,6 +74,10 @@ STC_INLINE stc64_t stc64_new(uint64_t seed)
STC_API stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high);
STC_API int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* dist);
+/* Normal distribution PRNG */
+STC_API double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist);
+
+
/* Main stc64 prng */
STC_INLINE uint64_t stc64_rand(stc64_t* rng) {
uint64_t *s = rng->state; enum {LR=24, RS=11, LS=3};
@@ -104,21 +109,6 @@ STC_INLINE stc64_normalf_t stc64_normalf_new(double mean, double stddev) {
return c_make(stc64_normalf_t){mean, stddev, 0.0, 0};
}
-/* Normal distribution PRNG */
-STC_INLINE double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
- double u1, u2, s, m;
- if (dist->has_next++ & 1)
- return dist->next * dist->stddev + dist->mean;
- do {
- u1 = 2.0 * stc64_randf(rng) - 1.0;
- u2 = 2.0 * stc64_randf(rng) - 1.0;
- s = u1*u1 + u2*u2;
- } while (s >= 1.0 || s == 0.0);
- m = sqrt(-2.0 * log(s) / s);
- dist->next = u2 * m;
- return (u1 * m) * dist->stddev + dist->mean;
-}
-
/* Following functions are deprecated (will be removed in the future): */
STC_INLINE void stc64_srandom(uint64_t seed) { csrandom(seed); }
STC_INLINE uint64_t stc64_random() { return crandom(); }
@@ -181,6 +171,21 @@ STC_DEF int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) {
#endif
}
+/* Normal distribution PRNG */
+STC_DEF double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
+ double u1, u2, s, m;
+ if (dist->has_next++ & 1)
+ return dist->next * dist->stddev + dist->mean;
+ do {
+ u1 = 2.0 * stc64_randf(rng) - 1.0;
+ u2 = 2.0 * stc64_randf(rng) - 1.0;
+ s = u1*u1 + u2*u2;
+ } while (s >= 1.0 || s == 0.0);
+ m = sqrt(-2.0 * log(s) / s);
+ dist->next = u2 * m;
+ return (u1 * m) * dist->stddev + dist->mean;
+}
+
#endif
#endif
#undef i_opt
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 561bb1ca..47dff3cd 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -30,6 +30,7 @@
#ifndef CSTR_H_INCLUDED
#define CSTR_H_INCLUDED
+#define i_header
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -537,5 +538,4 @@ STC_DEF int cstr_printf(cstr* self, const char* fmt, ...) {
#undef i_header
#undef i_static
#undef i_implement
-//#undef i_implement
#endif // !STC_CSTR_V1
diff --git a/include/stc/csview.h b/include/stc/csview.h
index ba0a7568..eb79c482 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -23,6 +23,7 @@
#ifndef CSVIEW_H_INCLUDED
#define CSVIEW_H_INCLUDED
+#define i_header
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -31,8 +32,8 @@
#define csview_new(literal) c_sv(literal)
#define csview_npos (SIZE_MAX >> 1)
-STC_API csview csview_substr(csview sv, intptr_t pos, size_t n);
-STC_API csview csview_slice(csview sv, intptr_t p1, intptr_t p2);
+STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n);
+STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2);
STC_API csview csview_token(csview sv, csview sep, size_t* start);
STC_INLINE csview csview_init() { return csview_null; }
@@ -69,6 +70,18 @@ STC_INLINE bool csview_ends_with(csview sv, csview sub) {
return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size);
}
+STC_INLINE csview csview_substr(csview sv, size_t pos, size_t n) {
+ if (pos + n > sv.size) n = sv.size - pos;
+ sv.str += pos, sv.size = n;
+ return sv;
+}
+
+STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) {
+ if (p2 > sv.size) p2 = sv.size;
+ sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0;
+ return sv;
+}
+
/* iterator */
STC_INLINE csview_iter csview_begin(const csview* self)
{ return c_make(csview_iter){.chr = {self->str, utf8_codep_size(self->str)}}; }
@@ -80,9 +93,6 @@ STC_INLINE void csview_next(csview_iter* it)
{ it->ref += it->chr.size; it->chr.size = utf8_codep_size(it->ref); }
/* utf8 */
-STC_INLINE bool csview_valid_u8(csview sv) // depends on src/utf8utils.c
- { return utf8_valid_n(sv.str, sv.size); }
-
STC_INLINE size_t csview_size_u8(csview sv)
{ return utf8_size_n(sv.str, sv.size); }
@@ -92,6 +102,10 @@ STC_INLINE csview csview_substr_u8(csview sv, size_t u8pos, size_t u8len) {
return sv;
}
+STC_INLINE bool csview_valid_u8(csview sv) // depends on src/utf8utils.c
+ { return utf8_valid_n(sv.str, sv.size); }
+
+
/* csview interaction with cstr: */
#ifdef CSTR_H_INCLUDED
@@ -101,12 +115,18 @@ STC_INLINE csview csview_from_s(const cstr* self)
STC_INLINE cstr cstr_from_sv(csview sv)
{ return cstr_from_n(sv.str, sv.size); }
-STC_INLINE csview cstr_substr(const cstr* self, intptr_t pos, size_t n)
+STC_INLINE csview cstr_substr(const cstr* self, size_t pos, size_t n)
{ return csview_substr(csview_from_s(self), pos, n); }
-STC_INLINE csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2)
+STC_INLINE csview cstr_slice(const cstr* self, size_t p1, size_t p2)
{ return csview_slice(csview_from_s(self), p1, p2); }
+STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, size_t n)
+ { return csview_substr_ex(csview_from_s(self), pos, n); }
+
+STC_INLINE csview cstr_slice_ex(const cstr* self, intptr_t p1, intptr_t p2)
+ { return csview_slice_ex(csview_from_s(self), p1, p2); }
+
STC_INLINE csview cstr_assign_sv(cstr* self, csview sv)
{ return c_make(csview){cstr_assign_n(self, sv.str, sv.size), sv.size}; }
@@ -155,7 +175,7 @@ STC_INLINE uint64_t csview_hash(const csview *self)
#if defined(i_implement)
STC_DEF csview
-csview_substr(csview sv, intptr_t pos, size_t n) {
+csview_substr_ex(csview sv, intptr_t pos, size_t n) {
if (pos < 0) {
pos += sv.size;
if (pos < 0) pos = 0;
@@ -167,7 +187,7 @@ csview_substr(csview sv, intptr_t pos, size_t n) {
}
STC_DEF csview
-csview_slice(csview sv, intptr_t p1, intptr_t p2) {
+csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) {
if (p1 < 0) {
p1 += sv.size;
if (p1 < 0) p1 = 0;
diff --git a/src/cregex.c b/src/cregex.c
index 34c78090..99b0fa03 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -31,7 +31,9 @@ THE SOFTWARE.
#include <setjmp.h>
#include <string.h>
#include <ctype.h>
+#include <stdio.h>
#include <stc/cregex.h>
+#include <stc/utf8.h>
typedef uint32_t Rune; /* Utf8 code point */
typedef int32_t Token;