Created
March 21, 2025 11:49
-
-
Save gwenn/2a164d67c6097b0863e6375906836f03 to your computer and use it in GitHub Desktop.
regex_replace
This file contains hidden or 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
CFLAGS=-Wall -g | |
all: | |
cc -fPIC -o test.exe icu_replace.c test.c -licuin -licuuc -licudt -lsqlite3 | |
# cc -fPIC -o test_glib.exe `pkg-config --cflags glib-2.0` glib_replace.c test_glib.c -lglib-2.0 -lintl -lsqlite3 | |
# cc -fPIC -o test.exe icu_replace.c test.c $(pkg-config --libs icu-i18n) $(pkg-config --libs sqlite3) | |
# cc -fPIC -o test_glib.exe `pkg-config --cflags glib-2.0` glib_replace.c test_glib.c `pkg-config --libs glib-2.0` `pkg-config --libs sqlite3` | |
clean: | |
rm -f test.exe | |
rm -f test_glib.exe |
This file contains hidden or 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
#include <stdio.h> | |
#include <sqlite3.h> | |
extern void icuReplaceAllFunc( | |
sqlite3_context *context, | |
int argc, | |
sqlite3_value **argv | |
); | |
static int init( | |
sqlite3 *db, | |
char **pzErrMsg, | |
const sqlite3_api_routines *pApi | |
){ | |
sqlite3_create_function_v2(db, "regex_replace", 3, SQLITE_ANY, 0, icuReplaceAllFunc, NULL, NULL, NULL); | |
return SQLITE_OK; | |
} | |
int main() { | |
int rc = sqlite3_initialize(); | |
if (rc != SQLITE_OK) { | |
printf("sqlite3_initialize: %d\n", rc); | |
return rc; | |
} | |
rc = sqlite3_auto_extension((void(*)(void))init); | |
if (rc != SQLITE_OK) { | |
printf("sqlite3_auto_extension: %d\n", rc); | |
return rc; | |
} | |
sqlite3 *db = NULL; | |
rc = sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXRESCODE, NULL); | |
if (rc != SQLITE_OK || db == NULL) { | |
printf("sqlite3_open_v2: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
rc = sqlite3_exec(db, "CREATE TABLE test (id INT, name TEXT); \ | |
INSERT INTO test VALUES (1, 'Laurent'); \ | |
INSERT INTO test VALUES (2, 'Sergiu'); \ | |
", NULL, NULL, NULL); | |
if (rc != SQLITE_OK) { | |
printf("sqlite3_exec: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
sqlite3_stmt *stmt = NULL; | |
rc = sqlite3_prepare_v2(db, "select regex_replace('aur', name, 'asdfasdfasdf') from test;", -1, &stmt, NULL); | |
//rc = sqlite3_prepare_v2(db, "select name from test;", -1, &stmt, NULL); | |
if (stmt == NULL || SQLITE_OK != rc) { | |
printf("sqlite3_prepare_v2: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
rc = sqlite3_step(stmt); | |
while (rc == SQLITE_ROW) { | |
printf("%s\n", sqlite3_column_text(stmt, 0)); | |
rc = sqlite3_step(stmt); | |
} | |
if (SQLITE_OK != rc && SQLITE_DONE != rc) { | |
printf("sqlite3_step: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
sqlite3_finalize(stmt); | |
sqlite3_close(db); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
#include <sqlite3.h> | |
extern void glibReplaceAllFunc( | |
sqlite3_context *ctx, | |
int argc, | |
sqlite3_value **argv | |
); | |
static int init( | |
sqlite3 *db, | |
char **pzErrMsg, | |
const sqlite3_api_routines *pApi | |
){ | |
sqlite3_create_function_v2(db, "regex_replace", 3, SQLITE_UTF8, 0, glibReplaceAllFunc, NULL, NULL, NULL); | |
return SQLITE_OK; | |
} | |
int main() { | |
int rc = sqlite3_initialize(); | |
if (rc != SQLITE_OK) { | |
printf("sqlite3_initialize: %d\n", rc); | |
return rc; | |
} | |
rc = sqlite3_auto_extension((void(*)(void))init); | |
if (rc != SQLITE_OK) { | |
printf("sqlite3_auto_extension: %d\n", rc); | |
return rc; | |
} | |
sqlite3 *db = NULL; | |
rc = sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXRESCODE, NULL); | |
if (rc != SQLITE_OK || db == NULL) { | |
printf("sqlite3_open_v2: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
rc = sqlite3_exec(db, "CREATE TABLE test (id INT, name TEXT); \ | |
INSERT INTO test VALUES (1, 'Laurent'); \ | |
INSERT INTO test VALUES (2, 'Sergiu'); \ | |
", NULL, NULL, NULL); | |
if (rc != SQLITE_OK) { | |
printf("sqlite3_exec: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
sqlite3_stmt *stmt = NULL; | |
//rc = sqlite3_prepare_v2(db, "select regex_replace('aur', name, 'asdfasdfasdf') from test;", -1, &stmt, NULL); | |
rc = sqlite3_prepare_v2(db, "select regex_replace('aur', 'Laurent', '');", -1, &stmt, NULL); | |
if (stmt == NULL || SQLITE_OK != rc) { | |
printf("sqlite3_prepare_v2: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
rc = sqlite3_step(stmt); | |
while (rc == SQLITE_ROW) { | |
printf("%s\n", sqlite3_column_text(stmt, 0)); | |
rc = sqlite3_step(stmt); | |
} | |
if (SQLITE_OK != rc && SQLITE_DONE != rc) { | |
printf("sqlite3_step: %d, %s\n", rc, sqlite3_errmsg(db)); | |
return rc; | |
} | |
sqlite3_finalize(stmt); | |
sqlite3_close(db); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment