-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cregex_replace*() [implemented in utf8code.c]. Added examples/r…
…egex_replace.c. Docs not ready, i.e. API not fixed. Some other refactoring and minor fixes/improvements. cstr_assign_sv() now returns char* like the other cstr_assign*().
- Loading branch information
Tyge Lovset
committed
Jul 20, 2022
1 parent
78cb613
commit 3f89c29
Showing
9 changed files
with
181 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#define i_implement | ||
#include <stc/cstr.h> | ||
#include <stc/cregex.h> | ||
#include <stc/csview.h> | ||
|
||
cstr sub_20y(int i, csview m) { | ||
if (i == 1) { // year | ||
int year; | ||
sscanf(m.str, "%4d", &year); | ||
return cstr_from_fmt("%04d", year - 20); | ||
} | ||
return cstr_from_sv(m); | ||
} | ||
|
||
int main() | ||
{ | ||
const char* pattern = "\\b(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])\\b"; | ||
const char* input = "start date: 2015-12-31, end date: 2022-02-28"; | ||
|
||
c_auto (cregex, re) | ||
c_auto (cstr, str1, str2) | ||
{ | ||
printf("input: %s\n", input); | ||
/* European date format */ | ||
str1 = cregex_replace(input, pattern, "\\3.\\2.\\1"); | ||
printf("euros: %s\n", cstr_str(&str1)); | ||
|
||
/* US date format, and subtract 20 years: */ | ||
str2 = cregex_replace_fn(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0); | ||
printf("us-20: %s\n", cstr_str(&str2)); | ||
} | ||
} | ||
|
||
#include "../src/cregex.c" | ||
#include "../src/utf8code.c" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.