summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/ccommon.h2
-rw-r--r--include/stc/cregex.h60
-rw-r--r--include/stc/cstr.h10
-rw-r--r--include/stc/csview.h4
4 files changed, 45 insertions, 31 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 3a6d8f4e..e87e7678 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -112,7 +112,7 @@ typedef const char* crawstr;
#define crawstr_cmp(xp, yp) strcmp(*(xp), *(yp))
#define crawstr_hash(p) c_strhash(*(p))
#define c_strlen_lit(literal) (sizeof "" literal - 1U)
-#define c_sv(lit) c_make(csview){lit, c_strlen_lit(lit)}
+#define c_sv(lit) (c_make(csview){lit, c_strlen_lit(lit)})
#define c_PRIsv ".*s"
#define c_ARGsv(sv) (int)(sv).size, (sv).str
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index 448f9405..11e21b06 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -34,32 +34,33 @@ THE SOFTWARE.
#include "forward.h" // csview
typedef enum {
- creg_nomatch = -1,
- creg_matcherror = -2,
- creg_outofmemory = -3,
- creg_unmatchedleftparenthesis = -4,
- creg_unmatchedrightparenthesis = -5,
- creg_toomanysubexpressions = -6,
- creg_toomanycharacterclasses = -7,
- creg_malformedcharacterclass = -8,
- creg_missingoperand = -9,
- creg_unknownoperator = -10,
- creg_operandstackoverflow = -11,
- creg_operatorstackoverflow = -12,
- creg_operatorstackunderflow = -13,
+ creg_success = 1,
+ creg_nomatch = 0,
+ creg_matcherror = -1,
+ creg_outofmemory = -2,
+ creg_unmatchedleftparenthesis = -3,
+ creg_unmatchedrightparenthesis = -4,
+ creg_toomanysubexpressions = -5,
+ creg_toomanycharacterclasses = -6,
+ creg_malformedcharacterclass = -7,
+ creg_missingoperand = -8,
+ creg_unknownoperator = -9,
+ creg_operandstackoverflow = -10,
+ creg_operatorstackoverflow = -11,
+ creg_operatorstackunderflow = -12,
} cregex_error_t;
enum {
/* compile flags */
- creg_dotall = 1<<0,
- creg_caseless = 1<<1,
+ cregex_DOTALL = 1<<0,
+ cregex_CASELESS = 1<<1,
/* execution flags */
- creg_fullmatch = 1<<2,
- creg_next = 1<<3,
- creg_startend = 1<<4,
+ cregex_FULLMATCH = 1<<2,
+ cregex_NEXT = 1<<3,
+ cregex_STARTEND = 1<<4,
/* limits */
- creg_max_classes = 16,
- creg_max_captures = 32,
+ cregex_MAXCLASSES = 16,
+ cregex_MAXCAPTURES = 32,
};
typedef struct {
@@ -76,15 +77,26 @@ static inline cregex cregex_init(void) {
int cregex_compile(cregex *self, const char* pattern, int cflags);
/* number of capture groups in a regex pattern */
-int cregex_captures(cregex rx);
+int cregex_captures(const cregex* self);
/* return number of capture groups on success, or (negative) error code on failure. */
int cregex_match(const cregex *self, const char* string,
- size_t nmatch, cregmatch match[], int mflags);
+ unsigned nmatch, csview match[], int mflags);
-void cregex_replace(const char* src, char* dst, int dsize,
- int nmatch, const cregmatch match[]);
+/* replace regular expression */
+void cregex_build_replace(const char* repl, unsigned nmatch, const csview match[],
+ cstr (*mfun)(int i, csview match), cstr* out);
+cstr cregex_replace_re(const char* input, const cregex* re, const char* repl,
+ cstr (*mfun)(int i, csview match), int cflags, unsigned count);
+
+cstr cregex_replace_fn(const char* input, const char* pattern, const char* replace,
+ cstr (*mfun)(int i, csview match), int cflags, unsigned count);
+static inline
+cstr cregex_replace(const char* input, const char* pattern, const char* replace)
+ { return cregex_replace_fn(input, pattern, replace, NULL, 0, 0); }
+
+/* destroy regex */
void cregex_drop(cregex* self);
#endif
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 441fe94a..8395f127 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -404,9 +404,9 @@ STC_DEF char* cstr_reserve(cstr* self, const size_t cap) {
if (cap > cstr_s_cap) {
char* data = (char *)c_malloc(cap + 1);
const size_t len = cstr_s_size(self);
- memcpy(data, self->sml.data, len);
+ memcpy(data, self->sml.data, cstr_s_cap + 1);
self->lon.data = data;
- cstr_l_set_size(self, len);
+ self->lon.size = len;
cstr_l_set_cap(self, cap);
return data;
}
@@ -525,7 +525,8 @@ STC_DEF int cstr_vfmt(cstr* self, const char* fmt, va_list args) {
STC_DEF cstr cstr_from_fmt(const char* fmt, ...) {
cstr s = cstr_null;
- va_list args; va_start(args, fmt);
+ va_list args;
+ va_start(args, fmt);
cstr_vfmt(&s, fmt, args);
va_end(args);
return s;
@@ -533,7 +534,8 @@ STC_DEF cstr cstr_from_fmt(const char* fmt, ...) {
STC_DEF int cstr_printf(cstr* self, const char* fmt, ...) {
cstr s = cstr_null;
- va_list args; va_start(args, fmt);
+ va_list args;
+ va_start(args, fmt);
const int n = cstr_vfmt(&s, fmt, args);
va_end(args);
cstr_drop(self); *self = s;
diff --git a/include/stc/csview.h b/include/stc/csview.h
index e74ce844..39bfa354 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -120,8 +120,8 @@ STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, size_t 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}; }
+STC_INLINE char* cstr_assign_sv(cstr* self, csview sv)
+ { return cstr_assign_n(self, sv.str, sv.size); }
STC_INLINE void cstr_append_sv(cstr* self, csview sv)
{ cstr_append_n(self, sv.str, sv.size); }