Skip to content

Commit

Permalink
Fixed String.prototype.indexOf() when the search string is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jul 30, 2020
1 parent 9a3b69f commit d3017d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ impl String {
.transpose()?
.map_or(0, |position| position.max(0.0).min(length as f64) as usize);

if search_string.is_empty() {
return Ok(start.min(length).into());
}

if start < length {
if let Some(position) = string.find(search_string.as_str()) {
return Ok(string[..position].chars().count().into());
Expand Down
12 changes: 12 additions & 0 deletions boa/src/builtins/string/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,15 @@ fn generic_index_of() {
assert_eq!(forward(&mut engine, "(10).indexOf(0)"), "1");
assert_eq!(forward(&mut engine, "(10).indexOf('0')"), "1");
}

#[test]
fn index_of_empty_search_string() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

assert_eq!(forward(&mut engine, "''.indexOf('')"), "0");
assert_eq!(forward(&mut engine, "''.indexOf('', 10)"), "0");
assert_eq!(forward(&mut engine, "'ABC'.indexOf('', 1)"), "1");
assert_eq!(forward(&mut engine, "'ABC'.indexOf('', 2)"), "2");
assert_eq!(forward(&mut engine, "'ABC'.indexOf('', 10)"), "3");
}

0 comments on commit d3017d8

Please sign in to comment.