This commit is contained in:
Andrew Morris
2023-03-13 14:02:35 +11:00
parent 22eb55a915
commit 11e77f36bd
2 changed files with 59 additions and 0 deletions

View File

@@ -48,6 +48,7 @@ pub fn get_string_method(method: &str) -> Val {
//
"codePointAt" => Val::Static(&CODE_POINT_AT),
"concat" => Val::Static(&CONCAT),
"endsWith" => Val::Static(&ENDS_WITH),
_ => Val::Undefined,
}
}
@@ -114,6 +115,48 @@ static CONCAT: NativeFunction = NativeFunction {
},
};
static ENDS_WITH: NativeFunction = NativeFunction {
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
match this {
Val::String(string_data) => {
let string_bytes = string_data.as_bytes();
let search_string = match params.get(0) {
Some(s) => s.val_to_string(),
_ => return Val::Bool(false),
};
let end_pos = match params.get(1) {
Some(p) => match p.to_index() {
None => return Val::Bool(false),
Some(i) => std::cmp::min(i, string_bytes.len()),
},
_ => string_bytes.len(),
};
let search_bytes = search_string.as_bytes();
let search_length = search_bytes.len();
if search_length > end_pos {
return Val::Bool(false);
}
let start_index = end_pos - search_length;
for i in 0..search_length {
if string_bytes[start_index + i] != search_bytes[i] {
return Val::Bool(false);
}
}
Val::Bool(true)
}
_ => std::panic!("Not implemented: exceptions/string indirection"),
}
},
};
fn unicode_at(bytes: &[u8], index: usize) -> Option<String> {
match code_point_at(bytes, index) {
Some(code_point) => Some(