Browse Source

Fixed `String.prototype.indexOf()` bug, when the search string is empty (#599)

pull/598/head
HalidOdat 4 years ago committed by GitHub
parent
commit
9fdadf85d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      boa/src/builtins/string/mod.rs
  2. 12
      boa/src/builtins/string/tests.rs

4
boa/src/builtins/string/mod.rs

@ -556,6 +556,10 @@ impl String {
.transpose()? .transpose()?
.map_or(0, |position| position.max(0.0).min(length as f64) as usize); .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 start < length {
if let Some(position) = string.find(search_string.as_str()) { if let Some(position) = string.find(search_string.as_str()) {
return Ok(string[..position].chars().count().into()); return Ok(string[..position].chars().count().into());

12
boa/src/builtins/string/tests.rs

@ -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");
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");
}

Loading…
Cancel
Save