|
|
|
@ -203,7 +203,12 @@ pub fn slice(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue
|
|
|
|
|
|
|
|
|
|
let mut new_str = String::new(); |
|
|
|
|
for i in from..from.wrapping_add(span) { |
|
|
|
|
new_str.push(primitive_val.chars().nth(i as usize).expect("Could not get nth char")); |
|
|
|
|
new_str.push( |
|
|
|
|
primitive_val |
|
|
|
|
.chars() |
|
|
|
|
.nth(i as usize) |
|
|
|
|
.expect("Could not get nth char"), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
Ok(to_value(new_str)) |
|
|
|
|
} |
|
|
|
@ -272,7 +277,8 @@ pub fn ends_with(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultV
|
|
|
|
|
let end_position: i32 = if args.len() < 2 { |
|
|
|
|
length |
|
|
|
|
} else { |
|
|
|
|
from_value(args.get(1).expect("Could not get argumetn").clone()).expect("Could not convert value to i32") |
|
|
|
|
from_value(args.get(1).expect("Could not get argumetn").clone()) |
|
|
|
|
.expect("Could not convert value to i32") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let end = min(max(end_position, 0), length); |
|
|
|
@ -311,7 +317,8 @@ pub fn includes(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVa
|
|
|
|
|
let position: i32 = if args.len() < 2 { |
|
|
|
|
0 |
|
|
|
|
} else { |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()).expect("Could not convert value to i32") |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("Could not convert value to i32") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let start = min(max(position, 0), length); |
|
|
|
@ -347,7 +354,8 @@ pub fn index_of(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVa
|
|
|
|
|
let position: i32 = if args.len() < 2 { |
|
|
|
|
0 |
|
|
|
|
} else { |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()).expect("Could not convert value to i32") |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("Could not convert value to i32") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let start = min(max(position, 0), length); |
|
|
|
@ -392,7 +400,8 @@ pub fn last_index_of(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Res
|
|
|
|
|
let position: i32 = if args.len() < 2 { |
|
|
|
|
0 |
|
|
|
|
} else { |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()).expect("Could not convert value to i32") |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("Could not convert value to i32") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let start = min(max(position, 0), length); |
|
|
|
@ -471,7 +480,10 @@ pub fn pad_end(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVal
|
|
|
|
|
.expect("failed to parse argument for String method"); |
|
|
|
|
let fill_string: Option<String> = match args.len() { |
|
|
|
|
1 => None, |
|
|
|
|
_ => Some(from_value(args.get(1).expect("Could not get argument").clone()).expect("Could not convert value to Option<String>")), |
|
|
|
|
_ => Some( |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("Could not convert value to Option<String>"), |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
string_pad(primitive_val, max_length, fill_string, false) |
|
|
|
@ -495,7 +507,10 @@ pub fn pad_start(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultV
|
|
|
|
|
.expect("failed to parse argument for String method"); |
|
|
|
|
let fill_string: Option<String> = match args.len() { |
|
|
|
|
1 => None, |
|
|
|
|
_ => Some(from_value(args.get(1).expect("Could not get argument").clone()).expect("Could not convert value to Option<String>")), |
|
|
|
|
_ => Some( |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("Could not convert value to Option<String>"), |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
string_pad(primitive_val, max_length, fill_string, true) |
|
|
|
@ -587,7 +602,8 @@ pub fn substring(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultV
|
|
|
|
|
let end = if args.len() < 2 { |
|
|
|
|
length |
|
|
|
|
} else { |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()).expect("failed to parse argument for String method") |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("failed to parse argument for String method") |
|
|
|
|
}; |
|
|
|
|
// Both start and end args replaced by 0 if they were negative
|
|
|
|
|
// or by the length of the String if they were greater
|
|
|
|
@ -598,7 +614,11 @@ pub fn substring(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultV
|
|
|
|
|
let to = max(final_start, final_end) as usize; |
|
|
|
|
// Extract the part of the string contained between the start index and the end index
|
|
|
|
|
// where start is guaranteed to be smaller or equals to end
|
|
|
|
|
let extracted_string: String = primitive_val.chars().skip(from).take(to.wrapping_sub(from)).collect(); |
|
|
|
|
let extracted_string: String = primitive_val |
|
|
|
|
.chars() |
|
|
|
|
.skip(from) |
|
|
|
|
.take(to.wrapping_sub(from)) |
|
|
|
|
.collect(); |
|
|
|
|
Ok(to_value(extracted_string)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -631,7 +651,8 @@ pub fn substr(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValu
|
|
|
|
|
let end = if args.len() < 2 { |
|
|
|
|
i32::max_value() |
|
|
|
|
} else { |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()).expect("failed to parse argument for String method") |
|
|
|
|
from_value(args.get(1).expect("Could not get argument").clone()) |
|
|
|
|
.expect("failed to parse argument for String method") |
|
|
|
|
}; |
|
|
|
|
// If start is negative it become the number of code units from the end of the string
|
|
|
|
|
if start < 0 { |
|
|
|
|