src/sqlite3-re.c
/*
 * Returns source substring matching pattern.
 * regexp_replace(source, pattern, replacement)
 * E.g.: select regexp_replace('abcdef', 'b.d', '...') = 'a...ef';
 */
Returns source substring matching pattern.
Returns source substring matching pattern .
static void regexp_replace(
    sqlite3_context *context,
    int argc,
    sqlite3_value **argv) {
    regexp *r;
    char *source;
    char *pattern;
    char *replacement;
    char *result;
    int is_match = 0;
    assert(argc == 3);
    source = (char *)sqlite3_value_text(argv[0]);
    if (!source) {
        return;
    }
    pattern = (char *)sqlite3_value_text(argv[1]);
    if (!pattern) {
        sqlite3_result_error(context, "missing regexp pattern", -1);
        return;
    }
    r = re_compile(pattern);
    if (r == NULL) {
        sqlite3_result_error(context, "invalid regexp pattern", -1);
        return;
    }
    replacement = (char *)sqlite3_value_text(argv[2]);
    if (!replacement) {
        sqlite3_result_value(context, argv[0]);
        return;
    }
    is_match = re_execute(r, source);
    if (!is_match) {
        sqlite3_result_value(context, argv[0]);
        return;
    }
    int len = r->endp[0] - r->startp[0];
    char *matched_str = sqlite3_malloc(len + 1);
    (void)strncpy(matched_str, r->startp[0], len);
    matched_str[len] = '\0';
#ifdef DEBUG
    fprintf(stderr, "len = %d\n", len);
    fprintf(stderr, "matched_str = '%s'\n", matched_str);
#endif
    char replacement_str[BUFSIZ];
    int err = re_substitute(r, replacement, replacement_str);
#ifdef DEBUG
    fprintf(stderr, "replacement_str = '%s'\n", replacement_str);
#endif
    if (err) {
        sqlite3_result_error(context, "invalid replacement pattern", -1);
        return;
    }
    result = str_replace(source, matched_str, replacement_str);
#ifdef DEBUG
    fprintf(stderr, "replace('%s', '%s', '%s') = '%s'\n", source, matched_str, replacement_str, result);
#endif
    sqlite3_result_text(context, (char *)result, -1, sqlite3_free);
    free((char *)r);
}
static void regexp_replace ( sqlite3_context * context , int argc , sqlite3_value * * argv ) { regexp * r ; char * source ; char * pattern ; char * replacement ; char * result ; int is_match = 0 ; assert ( argc == 3 ) ; source = ( char * ) sqlite3_value_text ( argv [ 0 ] ) ; if ( ! source ) { return ; } pattern = ( char * ) sqlite3_value_text ( argv [ 1 ] ) ; if ( ! pattern ) { sqlite3_result_error ( context , " " , -1 ) ; return ; } r = re_compile ( pattern ) ; if ( r == NULL ) { sqlite3_result_error ( context , " " , -1 ) ; return ; } replacement = ( char * ) sqlite3_value_text ( argv [ 2 ] ) ; if ( ! replacement ) { sqlite3_result_value ( context , argv [ 0 ] ) ; return ; } is_match = re_execute ( r , source ) ; if ( ! is_match ) { sqlite3_result_value ( context , argv [ 0 ] ) ; return ; } int len = r -> endp [ 0 ] - r -> startp [ 0 ] ; char * matched_str = sqlite3_malloc ( len + 1 ) ; ( void ) strncpy ( matched_str , r -> startp [ 0 ] , len ) ; matched_str [ len ] = ' \0 ' ; #ifdef DEBUG fprintf ( stderr , " \n " , len ) ; fprintf ( stderr , " \n " , matched_str ) ; #endif char replacement_str [ BUFSIZ ] ; int err = re_substitute ( r , replacement , replacement_str ) ; #ifdef DEBUG fprintf ( stderr , " \n " , replacement_str ) ; #endif if ( err ) { sqlite3_result_error ( context , " " , -1 ) ; return ; } result = str_replace ( source , matched_str , replacement_str ) ; #ifdef DEBUG fprintf ( stderr , " \n " , source , matched_str , replacement_str , result ) ; #endif sqlite3_result_text ( context , ( char * ) result , -1 , sqlite3_free ) ; free ( ( char * ) r ) ; }
Returns source substring matching pattern.
Returns source substring matching pattern .