Skip to content

Commit

Permalink
Fix String.prototype.substr for zero length strings
Browse files Browse the repository at this point in the history
  • Loading branch information
lance committed Sep 25, 2014
1 parent 5025633 commit fffbe0c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
Expand Up @@ -25,6 +25,7 @@ public Object call(ExecutionContext context, Object self, Object... args) {
start = Math.max(chars+start, 0);
}
length = Math.min(Math.max(length, 0), chars-start);
if (length <= 0) return "";
return s.substring(start, start+length);
}

Expand Down
Expand Up @@ -359,6 +359,11 @@ public void testSubstr() {
assertThat(eval("'boblanceqmx'.substr(0, 3)")).isEqualTo("bob");
}

@Test
public void testSubstrZeroLength() {
assertThat(eval("''.substr(2, 0)")).isEqualTo("");
}

@Test
public void testSubstrFromEnd() {
assertThat(eval("'boblanceqmx'.substr(-3)")).isEqualTo("qmx");
Expand Down

0 comments on commit fffbe0c

Please sign in to comment.