mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-02 19:32:17 +00:00
add Str.getUnsafe
This commit is contained in:
parent
2a82d24847
commit
1f943a5452
8 changed files with 28 additions and 53 deletions
|
@ -221,3 +221,6 @@ toU16 : Str -> Result U16 [InvalidNumStr]*
|
||||||
toI16 : Str -> Result I16 [InvalidNumStr]*
|
toI16 : Str -> Result I16 [InvalidNumStr]*
|
||||||
toU8 : Str -> Result U8 [InvalidNumStr]*
|
toU8 : Str -> Result U8 [InvalidNumStr]*
|
||||||
toI8 : Str -> Result I8 [InvalidNumStr]*
|
toI8 : Str -> Result I8 [InvalidNumStr]*
|
||||||
|
|
||||||
|
## Gets the byte at the given index, without performing a bounds check
|
||||||
|
getUnsafe : Str, Nat -> U8
|
||||||
|
|
|
@ -330,6 +330,7 @@ pub const STR_REPEAT: &str = "roc_builtins.str.repeat";
|
||||||
pub const STR_TRIM: &str = "roc_builtins.str.trim";
|
pub const STR_TRIM: &str = "roc_builtins.str.trim";
|
||||||
pub const STR_TRIM_LEFT: &str = "roc_builtins.str.trim_left";
|
pub const STR_TRIM_LEFT: &str = "roc_builtins.str.trim_left";
|
||||||
pub const STR_TRIM_RIGHT: &str = "roc_builtins.str.trim_right";
|
pub const STR_TRIM_RIGHT: &str = "roc_builtins.str.trim_right";
|
||||||
|
pub const STR_GET_UNSAFE: &str = "roc_builtins.str.get_unsafe";
|
||||||
|
|
||||||
pub const DICT_HASH: &str = "roc_builtins.dict.hash";
|
pub const DICT_HASH: &str = "roc_builtins.dict.hash";
|
||||||
pub const DICT_HASH_STR: &str = "roc_builtins.dict.hash_str";
|
pub const DICT_HASH_STR: &str = "roc_builtins.dict.hash_str";
|
||||||
|
|
|
@ -74,6 +74,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
|
||||||
STR_CONCAT => str_concat,
|
STR_CONCAT => str_concat,
|
||||||
STR_JOIN_WITH => str_join_with,
|
STR_JOIN_WITH => str_join_with,
|
||||||
STR_TO_SCALARS => str_to_scalars,
|
STR_TO_SCALARS => str_to_scalars,
|
||||||
|
STR_GET_UNSAFE => str_get_unsafe,
|
||||||
STR_SPLIT => str_split,
|
STR_SPLIT => str_split,
|
||||||
STR_IS_EMPTY => str_is_empty,
|
STR_IS_EMPTY => str_is_empty,
|
||||||
STR_STARTS_WITH => str_starts_with,
|
STR_STARTS_WITH => str_starts_with,
|
||||||
|
@ -1669,67 +1670,24 @@ fn str_concat(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// List.getUnsafe : Str, Nat -> U8
|
||||||
|
fn str_get_unsafe(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
|
lowlevel_2(symbol, LowLevel::StrGetUnsafe, var_store)
|
||||||
|
}
|
||||||
|
|
||||||
/// Str.toScalars : Str -> List U32
|
/// Str.toScalars : Str -> List U32
|
||||||
fn str_to_scalars(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
fn str_to_scalars(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
let str_var = var_store.fresh();
|
lowlevel_1(symbol, LowLevel::StrToScalars, var_store)
|
||||||
let list_u32_var = var_store.fresh();
|
|
||||||
|
|
||||||
let body = RunLowLevel {
|
|
||||||
op: LowLevel::StrToScalars,
|
|
||||||
args: vec![(str_var, Var(Symbol::ARG_1))],
|
|
||||||
ret_var: list_u32_var,
|
|
||||||
};
|
|
||||||
|
|
||||||
defn(
|
|
||||||
symbol,
|
|
||||||
vec![(str_var, Symbol::ARG_1)],
|
|
||||||
var_store,
|
|
||||||
body,
|
|
||||||
list_u32_var,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Str.joinWith : List Str, Str -> Str
|
/// Str.joinWith : List Str, Str -> Str
|
||||||
fn str_join_with(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
fn str_join_with(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
let list_str_var = var_store.fresh();
|
lowlevel_2(symbol, LowLevel::StrJoinWith, var_store)
|
||||||
let str_var = var_store.fresh();
|
|
||||||
|
|
||||||
let body = RunLowLevel {
|
|
||||||
op: LowLevel::StrJoinWith,
|
|
||||||
args: vec![
|
|
||||||
(list_str_var, Var(Symbol::ARG_1)),
|
|
||||||
(str_var, Var(Symbol::ARG_2)),
|
|
||||||
],
|
|
||||||
ret_var: str_var,
|
|
||||||
};
|
|
||||||
|
|
||||||
defn(
|
|
||||||
symbol,
|
|
||||||
vec![(list_str_var, Symbol::ARG_1), (str_var, Symbol::ARG_2)],
|
|
||||||
var_store,
|
|
||||||
body,
|
|
||||||
str_var,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Str.isEmpty : Str -> Bool
|
/// Str.isEmpty : Str -> Bool
|
||||||
fn str_is_empty(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
fn str_is_empty(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
let str_var = var_store.fresh();
|
lowlevel_1(symbol, LowLevel::StrIsEmpty, var_store)
|
||||||
let bool_var = var_store.fresh();
|
|
||||||
|
|
||||||
let body = RunLowLevel {
|
|
||||||
op: LowLevel::StrIsEmpty,
|
|
||||||
args: vec![(str_var, Var(Symbol::ARG_1))],
|
|
||||||
ret_var: bool_var,
|
|
||||||
};
|
|
||||||
|
|
||||||
defn(
|
|
||||||
symbol,
|
|
||||||
vec![(str_var, Symbol::ARG_1)],
|
|
||||||
var_store,
|
|
||||||
body,
|
|
||||||
bool_var,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Str.startsWith : Str, Str -> Bool
|
/// Str.startsWith : Str, Str -> Bool
|
||||||
|
|
|
@ -5468,8 +5468,17 @@ fn run_low_level<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
list_prepend(env, original_wrapper, elem, elem_layout)
|
list_prepend(env, original_wrapper, elem, elem_layout)
|
||||||
}
|
}
|
||||||
|
StrGetUnsafe => {
|
||||||
|
// List.getUnsafe : List elem, Nat -> elem
|
||||||
|
debug_assert_eq!(args.len(), 2);
|
||||||
|
|
||||||
|
let wrapper_struct = load_symbol(scope, &args[0]);
|
||||||
|
let elem_index = load_symbol(scope, &args[1]);
|
||||||
|
|
||||||
|
call_bitcode_fn(env, &[wrapper_struct, elem_index], bitcode::STR_GET_UNSAFE)
|
||||||
|
}
|
||||||
ListGetUnsafe => {
|
ListGetUnsafe => {
|
||||||
// List.get : List elem, Nat -> [Ok elem, OutOfBounds]*
|
// List.getUnsafe : List elem, Nat -> elem
|
||||||
debug_assert_eq!(args.len(), 2);
|
debug_assert_eq!(args.len(), 2);
|
||||||
|
|
||||||
let (wrapper_struct, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
let (wrapper_struct, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||||
|
|
|
@ -218,6 +218,7 @@ impl<'a> LowLevelCall<'a> {
|
||||||
// Str
|
// Str
|
||||||
StrConcat => self.load_args_and_call_zig(backend, bitcode::STR_CONCAT),
|
StrConcat => self.load_args_and_call_zig(backend, bitcode::STR_CONCAT),
|
||||||
StrToScalars => self.load_args_and_call_zig(backend, bitcode::STR_TO_SCALARS),
|
StrToScalars => self.load_args_and_call_zig(backend, bitcode::STR_TO_SCALARS),
|
||||||
|
StrGetUnsafe => self.load_args_and_call_zig(backend, bitcode::STR_GET_UNSAFE),
|
||||||
StrJoinWith => self.load_args_and_call_zig(backend, bitcode::STR_JOIN_WITH),
|
StrJoinWith => self.load_args_and_call_zig(backend, bitcode::STR_JOIN_WITH),
|
||||||
StrIsEmpty => match backend.storage.get(&self.arguments[0]) {
|
StrIsEmpty => match backend.storage.get(&self.arguments[0]) {
|
||||||
StoredValue::StackMemory { location, .. } => {
|
StoredValue::StackMemory { location, .. } => {
|
||||||
|
|
|
@ -24,6 +24,7 @@ pub enum LowLevel {
|
||||||
StrTrimRight,
|
StrTrimRight,
|
||||||
StrToNum,
|
StrToNum,
|
||||||
StrToScalars,
|
StrToScalars,
|
||||||
|
StrGetUnsafe,
|
||||||
ListLen,
|
ListLen,
|
||||||
ListWithCapacity,
|
ListWithCapacity,
|
||||||
ListGetUnsafe,
|
ListGetUnsafe,
|
||||||
|
@ -167,6 +168,7 @@ impl LowLevelWrapperType {
|
||||||
|
|
||||||
match symbol {
|
match symbol {
|
||||||
Symbol::STR_CONCAT => CanBeReplacedBy(StrConcat),
|
Symbol::STR_CONCAT => CanBeReplacedBy(StrConcat),
|
||||||
|
Symbol::STR_GET_UNSAFE => CanBeReplacedBy(StrGetUnsafe),
|
||||||
Symbol::STR_TO_SCALARS => CanBeReplacedBy(StrToScalars),
|
Symbol::STR_TO_SCALARS => CanBeReplacedBy(StrToScalars),
|
||||||
Symbol::STR_JOIN_WITH => CanBeReplacedBy(StrJoinWith),
|
Symbol::STR_JOIN_WITH => CanBeReplacedBy(StrJoinWith),
|
||||||
Symbol::STR_IS_EMPTY => CanBeReplacedBy(StrIsEmpty),
|
Symbol::STR_IS_EMPTY => CanBeReplacedBy(StrIsEmpty),
|
||||||
|
|
|
@ -1190,6 +1190,7 @@ define_builtins! {
|
||||||
32 STR_TO_U8: "toU8"
|
32 STR_TO_U8: "toU8"
|
||||||
33 STR_TO_I8: "toI8"
|
33 STR_TO_I8: "toI8"
|
||||||
34 STR_TO_SCALARS: "toScalars"
|
34 STR_TO_SCALARS: "toScalars"
|
||||||
|
35 STR_GET_UNSAFE: "getUnsafe"
|
||||||
}
|
}
|
||||||
5 LIST: "List" => {
|
5 LIST: "List" => {
|
||||||
0 LIST_LIST: "List" imported // the List.List type alias
|
0 LIST_LIST: "List" imported // the List.List type alias
|
||||||
|
|
|
@ -892,7 +892,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
|
||||||
}
|
}
|
||||||
ListWithCapacity => arena.alloc_slice_copy(&[irrelevant]),
|
ListWithCapacity => arena.alloc_slice_copy(&[irrelevant]),
|
||||||
ListReplaceUnsafe => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
ListReplaceUnsafe => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
||||||
ListGetUnsafe => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
StrGetUnsafe | ListGetUnsafe => arena.alloc_slice_copy(&[borrowed, irrelevant]),
|
||||||
ListConcat => arena.alloc_slice_copy(&[owned, owned]),
|
ListConcat => arena.alloc_slice_copy(&[owned, owned]),
|
||||||
StrConcat => arena.alloc_slice_copy(&[owned, borrowed]),
|
StrConcat => arena.alloc_slice_copy(&[owned, borrowed]),
|
||||||
StrTrim => arena.alloc_slice_copy(&[owned]),
|
StrTrim => arena.alloc_slice_copy(&[owned]),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue