Skip to content

Commit

Permalink
Added replace regex functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Jan 9, 2017
1 parent 708dd71 commit 2eb642f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 15 additions & 3 deletions base.c
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,7 @@ ctr_object* ctr_string_find_pattern_options_do( ctr_object* myself, ctr_argument
}
char* haystack = ctr_heap_allocate_cstring(myself);
size_t offset = 0;
ctr_object* newString = ctr_build_empty_string();
while( !regex_error && !flagIgnore ) {
regex_error = regexec(&pattern, haystack + offset , n, matches, REG_NOTBOL );
if ( regex_error ) break;
Expand All @@ -1682,17 +1683,29 @@ ctr_object* ctr_string_find_pattern_options_do( ctr_object* myself, ctr_argument
ctr_heap_free( tmp );
}
if (matches[0].rm_eo != -1) {
ctr_argument* arg = ctr_heap_allocate( sizeof( ctr_argument ) );
arg->object = ctr_build_string( haystack + offset, matches[0].rm_so );
ctr_string_append( newString, arg );
offset += matches[0].rm_eo;
ctr_heap_free( arg );
}
ctr_block_run( block, blockArguments, block );
ctr_object* replacement = ctr_block_run( block, blockArguments, block );
ctr_argument* arg = ctr_heap_allocate( sizeof( ctr_argument ) );
arg->object = replacement;
ctr_string_append( newString, arg );
ctr_heap_free( arg );
ctr_heap_free(blockArguments);
ctr_heap_free(arrayConstructorArgument);
}
ctr_argument* arg = ctr_heap_allocate( sizeof( ctr_argument ) );
arg->object = ctr_build_string( haystack + offset, strlen( haystack + offset ) );
ctr_string_append( newString, arg );
ctr_heap_free( arg );
ctr_heap_free( needle );
ctr_heap_free( haystack );
ctr_heap_free( options );
regfree( &pattern );
return myself;
return newString;
}

/**
Expand All @@ -1706,7 +1719,6 @@ ctr_object* ctr_string_find_pattern_do( ctr_object* myself, ctr_argument* argume
argumentList->next->next->object = ctr_build_empty_string();
ctr_object* answer;
answer = ctr_string_find_pattern_options_do( myself, argumentList );
answer = myself;
ctr_heap_free( no_options );
return answer;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/test0222.ctr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Replace the sequences ell and rl with a * --> h*o wo*d
var newString := ('hello world' findPattern: '([eor])([l]+)' do: { :match
^ '*'.
}).
Pen write: newString,brk.
1 change: 1 addition & 0 deletions tests/test0222.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h*o wo*d

0 comments on commit 2eb642f

Please sign in to comment.