This commit is contained in:
Andrew Morris
2023-03-14 11:49:58 +11:00
parent f2edd6f866
commit a9e52e564f
3 changed files with 32 additions and 0 deletions

View File

@@ -56,6 +56,7 @@ pub fn get_string_method(method: &str) -> Val {
"normalize" => Val::Static(&NORMALIZE), // (TODO)
"padEnd" => Val::Static(&PAD_END),
"padStart" => Val::Static(&PAD_START),
"repeat" => Val::Static(&REPEAT),
_ => Val::Undefined,
}
}
@@ -405,6 +406,31 @@ static PAD_START: NativeFunction = NativeFunction {
},
};
static REPEAT: NativeFunction = NativeFunction {
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
match this {
Val::String(string_data) => {
let count = match params.get(0) {
Some(p) => match p.to_index() {
Some(i) => i,
None => return Val::String(string_data.clone()),
},
_ => return Val::String(string_data.clone()),
};
let mut result = String::new();
for _ in 0..count {
result.push_str(string_data);
}
Val::String(Rc::new(result))
}
_ => panic!("TODO: exceptions/string indirection"),
}
},
};
fn index_of(string_bytes: &[u8], search_bytes: &[u8], start_pos: usize) -> Option<usize> {
let search_length = search_bytes.len();