Move op_sub_string into string_methods

This commit is contained in:
Andrew Morris
2023-03-13 11:05:07 +11:00
parent 5d18949719
commit b6045b24c1
2 changed files with 30 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
use std::rc::Rc;
use crate::string_methods::get_string_method;
use crate::string_methods::op_sub_string;
use super::vs_value::Val;
use super::vs_value::ValTrait;
@@ -242,35 +242,7 @@ pub fn op_sub(left: Val, right: Val) -> Val {
Val::Null => std::panic!("Not implemented: exceptions"),
Val::Bool(_) => Val::Undefined, // TODO: toString etc
Val::Number(_) => Val::Undefined, // TODO: toString etc
Val::String(string_data) => {
let right_index = match right.to_index() {
None => {
let method = right.val_to_string();
let method_str = method.as_str();
return match method_str {
"length" => Val::Number(string_data.len() as f64),
_ => get_string_method(method_str),
};
}
Some(i) => i,
};
let string_bytes = string_data.as_bytes();
if right_index >= string_bytes.len() {
return Val::Undefined;
}
let byte = string_bytes[right_index];
// TODO: Val::Strings need to change to not use rust's string type,
// because they need to represent an actual byte array underneath. This
// occurs for invalid utf8 sequences which are getting converted to U+FFFD
// here. To be analogous to js, the information of the actual byte needs
// to be preserved, but that can't be represented in rust's string type.
return Val::String(Rc::new(String::from_utf8_lossy(&[byte]).into_owned()));
}
Val::String(string_data) => op_sub_string(&string_data, &right),
Val::Array(array_data) => {
let right_index = match right.to_index() {
None => {

View File

@@ -2,6 +2,29 @@ use std::rc::Rc;
use crate::{helpers::to_wrapping_index, native_function::NativeFunction, vs_value::Val, ValTrait};
pub fn op_sub_string(string_data: &Rc<String>, subscript: &Val) -> Val {
let right_index = match subscript.to_index() {
None => {
let method = subscript.val_to_string();
let method_str = method.as_str();
return match method_str {
"length" => Val::Number(string_data.len() as f64),
_ => get_string_method(method_str),
};
}
Some(i) => i,
};
let string_bytes = string_data.as_bytes();
if right_index >= string_bytes.len() {
return Val::Undefined;
}
string_from_byte(string_bytes[right_index])
}
pub fn get_string_method(method: &str) -> Val {
match method {
"at" => Val::Static(&AT),
@@ -83,5 +106,10 @@ fn byte_at(string: &String, index_param: Option<&Val>) -> Option<u8> {
}
fn string_from_byte(byte: u8) -> Val {
// TODO: Val::Strings need to change to not use rust's string type,
// because they need to represent an actual byte array underneath. This
// occurs for invalid utf8 sequences which are getting converted to U+FFFD
// here. To be analogous to js, the information of the actual byte needs
// to be preserved, but that can't be represented in rust's string type.
Val::String(Rc::new(String::from_utf8_lossy(&[byte]).into_owned()))
}