Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
naively implement nqp::replace
  • Loading branch information
timo committed Nov 26, 2013
1 parent 4867b27 commit b2da0b7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 11 deletions.
19 changes: 14 additions & 5 deletions lib/MAST/Ops.nqp
Expand Up @@ -544,7 +544,8 @@ BEGIN {
1283,
1286,
1288,
1291);
1291,
1293);
MAST::Ops.WHO<@counts> := nqp::list_i(0,
2,
2,
Expand Down Expand Up @@ -1086,7 +1087,8 @@ BEGIN {
3,
2,
3,
2);
2,
5);
MAST::Ops.WHO<@values> := nqp::list_i(10,
8,
18,
Expand Down Expand Up @@ -2379,7 +2381,12 @@ BEGIN {
65,
33,
65,
65);
65,
58,
57,
33,
33,
57);
MAST::Ops.WHO<%codes> := nqp::hash('no_op', 0,
'const_i8', 1,
'const_i16', 2,
Expand Down Expand Up @@ -2921,7 +2928,8 @@ BEGIN {
'say_fhs', 538,
'capturenamedshash', 539,
'read_fhb', 540,
'write_fhb', 541);
'write_fhb', 541,
'replace', 542);
MAST::Ops.WHO<@names> := nqp::list('no_op',
'const_i8',
'const_i16',
Expand Down Expand Up @@ -3463,5 +3471,6 @@ BEGIN {
'say_fhs',
'capturenamedshash',
'read_fhb',
'write_fhb');
'write_fhb',
'replace');
}
8 changes: 4 additions & 4 deletions src/core/interp.c
Expand Up @@ -1389,11 +1389,11 @@ void MVM_interp_run(MVMThreadContext *tc, void (*initial_invoke)(MVMThreadContex
GET_REG(cur_op, 2).s, GET_REG(cur_op, 4).o);
cur_op += 6;
goto NEXT;
/*OP(replace):
OP(replace):
GET_REG(cur_op, 0).s = MVM_string_replace(tc,
GET_REG(cur_op, 2).s, GET_REG(cur_op, 4).s, GET_REG(cur_op, 6).s);
cur_op += 8;
goto NEXT;*/
GET_REG(cur_op, 2).s, GET_REG(cur_op, 4).i64, GET_REG(cur_op, 6).i64, GET_REG(cur_op, 8).s);
cur_op += 10;
goto NEXT;
OP(getcpbyname):
GET_REG(cur_op, 0).i64 = MVM_unicode_lookup_by_name(tc,
GET_REG(cur_op, 2).s);
Expand Down
2 changes: 1 addition & 1 deletion src/core/oplabels.h
Expand Up @@ -543,7 +543,7 @@ static const void * const LABELS[] = {
&&OP_capturenamedshash,
&&OP_read_fhb,
&&OP_write_fhb,
NULL,
&&OP_replace,
NULL,
NULL,
NULL,
Expand Down
1 change: 1 addition & 0 deletions src/core/oplist
Expand Up @@ -571,3 +571,4 @@ say_fhs w(int64) r(obj) r(str)
capturenamedshash w(obj) r(obj)
read_fhb r(obj) r(obj) r(int64)
write_fhb r(obj) r(obj)
replace w(str) r(str) r(int64) r(int64) r(str)
9 changes: 8 additions & 1 deletion src/core/ops.c
Expand Up @@ -3800,9 +3800,16 @@ static MVMOpInfo MVM_op_infos[] = {
2,
{ MVM_operand_read_reg | MVM_operand_obj, MVM_operand_read_reg | MVM_operand_obj }
},
{
MVM_OP_replace,
"replace",
" ",
5,
{ MVM_operand_write_reg | MVM_operand_str, MVM_operand_read_reg | MVM_operand_str, MVM_operand_read_reg | MVM_operand_int64, MVM_operand_read_reg | MVM_operand_int64, MVM_operand_read_reg | MVM_operand_str }
},
};

static unsigned short MVM_op_counts = 542;
static unsigned short MVM_op_counts = 543;

MVMOpInfo * MVM_op_get_op(unsigned short op) {
if (op >= MVM_op_counts)
Expand Down
1 change: 1 addition & 0 deletions src/core/ops.h
Expand Up @@ -543,6 +543,7 @@
#define MVM_OP_capturenamedshash 539
#define MVM_OP_read_fhb 540
#define MVM_OP_write_fhb 541
#define MVM_OP_replace 542

#define MVM_OP_EXT_BASE 1024
#define MVM_OP_EXT_CU_LIMIT 1024
Expand Down
19 changes: 19 additions & 0 deletions src/strings/ops.c
Expand Up @@ -401,6 +401,25 @@ MVMString * MVM_string_substring(MVMThreadContext *tc, MVMString *a, MVMint64 of
return result;
}

MVMString * MVM_string_replace(MVMThreadContext *tc, MVMString *original, MVMint64 start, MVMint64 count, MVMString *replacement) {
/* XXX this could probably be done more efficiently directly. */
MVMString *first_part;
MVMString *rest_part;
MVMString *result;

MVM_gc_root_temp_push(tc, (MVMCollectable **)&replacement);
first_part = MVM_string_substring(tc, original, 0, start);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&first_part);

rest_part = MVM_string_substring(tc, original, start + count, -1);
rest_part = MVM_string_concatenate(tc, replacement, rest_part);
result = MVM_string_concatenate(tc, first_part, rest_part);

MVM_gc_root_temp_pop_n(tc, 2);

return result;
}

/* Append one string to another. */
/* XXX inline parent's strands if it's a rope too? */
MVMString * MVM_string_concatenate(MVMThreadContext *tc, MVMString *a, MVMString *b) {
Expand Down
1 change: 1 addition & 0 deletions src/strings/ops.h
Expand Up @@ -67,6 +67,7 @@ MVMint64 MVM_string_index_from_end(MVMThreadContext *tc, MVMString *haystack, MV
MVMString * MVM_string_concatenate(MVMThreadContext *tc, MVMString *a, MVMString *b);
MVMString * MVM_string_repeat(MVMThreadContext *tc, MVMString *a, MVMint64 count);
MVMString * MVM_string_substring(MVMThreadContext *tc, MVMString *a, MVMint64 start, MVMint64 length);
MVMString * MVM_string_replace(MVMThreadContext *tc, MVMString *a, MVMint64 start, MVMint64 length, MVMString *replacement);
void MVM_string_say(MVMThreadContext *tc, MVMString *a);
void MVM_string_print(MVMThreadContext *tc, MVMString *a);
MVMint64 MVM_string_equal_at(MVMThreadContext *tc, MVMString *a, MVMString *b, MVMint64 offset);
Expand Down

0 comments on commit b2da0b7

Please sign in to comment.