| 90 | */ |
| 91 | |
| 92 | LIST * regex_replace( FRAME * frame, int flags ) |
| 93 | { |
| 94 | LIST * args = lol_get( frame->args, 0 ); |
| 95 | OBJECT * s; |
| 96 | OBJECT * match; |
| 97 | OBJECT * replacement; |
| 98 | regexp * re; |
| 99 | const char * pos; |
| 100 | string buf[ 1 ]; |
| 101 | LIST * result; |
| 102 | LISTITER iter = list_begin( args ); |
| 103 | s = list_item( iter ); |
| 104 | iter = list_next( iter ); |
| 105 | match = list_item( iter ); |
| 106 | iter = list_next( iter ); |
| 107 | replacement = list_item(iter ); |
| 108 | |
| 109 | re = regex_compile( match ); |
| 110 | |
| 111 | string_new( buf ); |
| 112 | |
| 113 | pos = object_str( s ); |
| 114 | while ( regexec( re, pos ) ) |
| 115 | { |
| 116 | string_append_range( buf, pos, re->startp[ 0 ] ); |
| 117 | string_append( buf, object_str( replacement ) ); |
| 118 | /* Handle empty matches */ |
| 119 | if ( *pos == '\0' ) |
| 120 | break; |
| 121 | else if ( pos == re->endp[ 0 ] ) |
| 122 | string_push_back( buf, *pos++ ); |
| 123 | else |
| 124 | pos = re->endp[ 0 ]; |
| 125 | } |
| 126 | string_append( buf, pos ); |
| 127 | |
| 128 | result = list_new( object_new( buf->value ) ); |
| 129 | |
| 130 | string_free( buf ); |
| 131 | |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | rule transform ( list * : pattern : indices * ) |
nothing calls this directly
no test coverage detected