Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement lax substr()
  • Loading branch information
sorear committed Nov 21, 2010
1 parent 04e4b93 commit 2a5333e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -3,6 +3,22 @@
using System.Collections.Generic;

public class Builtins {
public static string LaxSubstring(string str, int from) {
if (from <= 0)
return str;
if (from >= str.Length)
return "";
return str.Substring(from);
}

public static string LaxSubstring2(string str, int from, int l) {
if (from <= 0) from = 0;
if (from >= str.Length) from = str.Length;
if (l >= str.Length - from) l = str.Length - from;
if (l < 0) l = 0;
return str.Substring(from, l);
}

public static Variable NumericEq(Variable v1, Variable v2) {
IP6 o1 = v1.Fetch();
IP6 o2 = v2.Fetch();
Expand Down
2 changes: 1 addition & 1 deletion src/CgOp.pm
Expand Up @@ -163,7 +163,7 @@ use warnings;

sub num_to_string { rawcall($_[0], 'ToString') }
sub str_length { getfield('Length', $_[0]) }
sub str_substring { rawcall($_[0], 'Substring', $_[1], $_[2]) }
sub str_substring { rawscall('Builtins.LaxSubstring2:m,String', @_) }
sub str_chr { rawnew('str', cast('clr:System.Char', $_[0]), CgOp::int(1)) }
sub strcmp { rawscall('String.CompareOrdinal', $_[0], $_[1]) }
sub str_tolower { rawcall($_[0], 'ToLowerInvariant:m,String') }
Expand Down
3 changes: 3 additions & 0 deletions test2.pl
Expand Up @@ -19,6 +19,9 @@
my $died = 1;
try { $*NONEX = 1; $died = 0; }
ok $died, "Assignment to non-existing dynvar fails";

is "foo".substr(5,2), "", "substr starting off end works";
is "foo".substr(1,10), "oo", "substr ending off end works";
}

#is $?FILE, 'test.pl', '$?FILE works';
Expand Down

0 comments on commit 2a5333e

Please sign in to comment.