Skip to content

Commit

Permalink
substr should return undef if start outside string
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jun 29, 2011
1 parent 836a89a commit b6dc939
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/Builtins.cs
Expand Up @@ -126,7 +126,9 @@ class SubstrLValue: Variable {

public override P6any Fetch() {
string str = backing.Fetch().mo.mro_raw_Str.Get(backing);
return Kernel.BoxRaw<string>(Builtins.LaxSubstring2(str, from, length), Kernel.StrMO);
string sub = Builtins.LaxSubstring2(str, from, length);
return sub == null ? Kernel.StrMO.typeObject :
Kernel.BoxRaw(sub,Kernel.StrMO);
}

public override void Store(P6any v) {
Expand All @@ -146,18 +148,15 @@ class SubstrLValue: Variable {
}

public static string LaxSubstring(string str, int from) {
if (from <= 0)
return str;
if (from >= str.Length)
return "";
if (from < 0 || from > str.Length)
return null;
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 (from < 0 || from > str.Length) return null;
if (l >= str.Length - from) l = str.Length - from;
if (l < 0) l = 0;
if (l < 0) return null;
return str.Substring(from, l);
}

Expand Down

0 comments on commit b6dc939

Please sign in to comment.