mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
repeat
This commit is contained in:
5
inputs/passing/stringMethods/repeat.ts
Normal file
5
inputs/passing/stringMethods/repeat.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
// test_output! "foofoofoo"
|
||||
|
||||
export default function () {
|
||||
return "foo".repeat(3);
|
||||
}
|
||||
@@ -62,6 +62,7 @@ impl ValTrait for StringBuiltin {
|
||||
match key.val_to_string().as_str() {
|
||||
"fromCodePoint" => Val::Static(&FROM_CODE_POINT),
|
||||
// "fromCharCode" => Val::Static(&FROM_CHAR_CODE),
|
||||
// "raw" => Val::Static(&RAW), // TODO
|
||||
_ => Val::Undefined,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user