Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add nqp::eqat to jvm and parrot.
  • Loading branch information
timo committed Oct 30, 2013
1 parent d7151b3 commit c726cd5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -2094,6 +2094,7 @@ QAST::OperationsJAST.add_core_op('substr', -> $qastcomp, $op {
!! QAST::Op.new( :op('substr3'), |@operands ));
});

QAST::OperationsJAST.map_classlib_core_op('eqat', $TYPE_OPS, 'string_equal_at', [$RT_STR, $RT_STR, $RT_INT], $RT_INT);
# ord can be on a the first char in a string or at a particular char.
QAST::OperationsJAST.map_classlib_core_op('ordfirst', $TYPE_OPS, 'ordfirst', [$RT_STR], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('ordat', $TYPE_OPS, 'ordat', [$RT_STR, $RT_INT], $RT_INT);
Expand Down
17 changes: 17 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -3095,6 +3095,23 @@ public static String substr3(String val, long offset, long length) {
return val.substring((int)offset, end);
}

// does a have b as a substring at offset?
public static long string_equal_at(String haystack, String needle, long offset) {
long haylen = haystack.length();
long needlelen = needle.length();

if (offset < 0) {
offset += haylen;
if (offset < 0) {
offset = 0;
}
}
if (haylen - offset < needlelen) {
return 0;
}
return haystack.regionMatches((int)offset, needle, 0, (int)needlelen) ? 1 : 0;
}

public static long ordfirst(String str) {
return str.codePointAt(0);
}
Expand Down
2 changes: 2 additions & 0 deletions src/vm/parrot/QAST/Operations.nqp
Expand Up @@ -1983,6 +1983,8 @@ QAST::Operations.add_core_op('substr', :inlinable(1), -> $qastcomp, $op {
!! QAST::Op.new( :op('substr3'), |@operands ));
});

QAST::Operations.add_core_pirop_mapping('eqat', 'nqp_string_equal_at', 'Issi', :inlinable(1));

# ord can be on a the first char in a string or at a particular char.
QAST::Operations.add_core_pirop_mapping('ordfirst', 'ord', 'Is', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('ordat', 'ord', 'Isi', :inlinable(1));
Expand Down
48 changes: 46 additions & 2 deletions src/vm/parrot/ops/nqp.ops
Expand Up @@ -3246,8 +3246,6 @@ inline op is_uprop(out INT, in STR, in STR, in INT) :base_core {
goto ADDRESS(handler);
#endif
}


/*

=item nqp_serialize_sc
Expand Down Expand Up @@ -3941,3 +3939,49 @@ inline op nqp_decode(out STR, invar PMC, in STR) {
if (elems > 0)
free(cbuffer);
}

/*

=item nqp_string_equal_at

Check if the String in $2 has the string in $3 at its offset $4.

=cut

*/
inline op nqp_string_equal_at(out INT, in STR, in STR, in INT) {
int32_t offset;
uint32_t cmp_index;
char done;

String_iter hay_iter;
String_iter needle_iter;

offset = (INTVAL)($4);

if (offset < 0) {
offset += ($2)->strlen;
if (offset < 0) {
offset = 0;
}
}

if (($2)->strlen - offset < ($3)->strlen || offset > ($2)->strlen) {
$1 = 0;
} else {
done = 0;
$1 = 1;

STRING_ITER_INIT(interp, &hay_iter);
STRING_iter_skip(interp, $2, &hay_iter, offset);
STRING_ITER_INIT(interp, &needle_iter);

for (cmp_index = 0; cmp_index < $3->strlen && !done; cmp_index++) {
if (STRING_iter_get_and_advance(interp, $2, &hay_iter)
!= STRING_iter_get_and_advance(interp, $3, &needle_iter)) {
$1 = 0;
done = 1;
}
}
}
}

0 comments on commit c726cd5

Please sign in to comment.