mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
pass all string tests
This commit is contained in:
parent
8e6b1ff481
commit
8e1e709fc1
2 changed files with 18 additions and 98 deletions
|
@ -2,8 +2,8 @@ use std::convert::{TryFrom, TryInto};
|
|||
use std::path::Path;
|
||||
|
||||
use crate::llvm::bitcode::{
|
||||
call_bitcode_fn, call_bitcode_fn_fixing_for_convention, call_str_bitcode_fn,
|
||||
call_void_bitcode_fn,
|
||||
call_bitcode_fn, call_bitcode_fn_fixing_for_convention, call_list_bitcode_fn,
|
||||
call_str_bitcode_fn, call_void_bitcode_fn,
|
||||
};
|
||||
use crate::llvm::build_dict::{
|
||||
self, dict_contains, dict_difference, dict_empty, dict_get, dict_insert, dict_intersection,
|
||||
|
@ -18,8 +18,7 @@ use crate::llvm::build_list::{
|
|||
list_single, list_sort_with, list_sublist, list_swap, list_symbol_to_c_abi, list_to_c_abi,
|
||||
};
|
||||
use crate::llvm::build_str::{
|
||||
str_from_float, str_from_int, str_from_utf8, str_from_utf8_range, str_number_of_bytes,
|
||||
str_repeat, str_split, str_to_utf8,
|
||||
str_from_float, str_from_int, str_from_utf8, str_from_utf8_range, str_split,
|
||||
};
|
||||
use crate::llvm::compare::{generic_eq, generic_neq};
|
||||
use crate::llvm::convert::{
|
||||
|
@ -5376,7 +5375,7 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let string = load_symbol(scope, &args[0]);
|
||||
let prefix = load_symbol(scope, &args[0]);
|
||||
let prefix = load_symbol(scope, &args[1]);
|
||||
|
||||
call_bitcode_fn(env, &[string, prefix], bitcode::STR_STARTS_WITH)
|
||||
}
|
||||
|
@ -5394,7 +5393,7 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let string = load_symbol(scope, &args[0]);
|
||||
let prefix = load_symbol(scope, &args[0]);
|
||||
let prefix = load_symbol(scope, &args[1]);
|
||||
|
||||
call_bitcode_fn(env, &[string, prefix], bitcode::STR_ENDS_WITH)
|
||||
}
|
||||
|
@ -5415,9 +5414,9 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let string = super::build_str::str_symbol_to_c_abi(env, scope, args[0]);
|
||||
let string = load_symbol(scope, &args[0]);
|
||||
|
||||
call_bitcode_fn(env, &[string.into()], intrinsic)
|
||||
call_bitcode_fn(env, &[string], intrinsic)
|
||||
}
|
||||
StrFromInt => {
|
||||
// Str.fromInt : Int -> Str
|
||||
|
@ -5456,13 +5455,16 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
// Str.fromInt : Str -> List U8
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
str_to_utf8(env, scope, args[0])
|
||||
let string = load_symbol(scope, &args[0]);
|
||||
call_list_bitcode_fn(env, &[string], bitcode::STR_TO_UTF8)
|
||||
}
|
||||
StrRepeat => {
|
||||
// Str.repeat : Str, Nat -> Str
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
str_repeat(env, scope, args[0], args[1])
|
||||
let string = load_symbol(scope, &args[0]);
|
||||
let count = load_symbol(scope, &args[1]);
|
||||
call_str_bitcode_fn(env, &[string, count], bitcode::STR_REPEAT)
|
||||
}
|
||||
StrSplit => {
|
||||
// Str.split : Str, Str -> List Str
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
use crate::llvm::bitcode::{
|
||||
call_bitcode_fn, call_list_bitcode_fn, call_str_bitcode_fn, call_void_bitcode_fn,
|
||||
};
|
||||
use crate::llvm::bitcode::{call_bitcode_fn, call_str_bitcode_fn, call_void_bitcode_fn};
|
||||
use crate::llvm::build::{complex_bitcast, Env, Scope};
|
||||
use crate::llvm::build_list::{allocate_list, pass_update_mode, store_list};
|
||||
use inkwell::builder::Builder;
|
||||
|
@ -17,18 +15,6 @@ use super::build_list::list_symbol_to_c_abi;
|
|||
|
||||
pub static CHAR_LAYOUT: Layout = Layout::u8();
|
||||
|
||||
/// Str.repeat : Str, Nat -> Str
|
||||
pub fn str_repeat<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
str_symbol: Symbol,
|
||||
count_symbol: Symbol,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let str_c_abi = str_symbol_to_c_abi(env, scope, str_symbol);
|
||||
let count = load_symbol(scope, &count_symbol);
|
||||
call_str_bitcode_fn(env, &[str_c_abi.into(), count], bitcode::STR_REPEAT)
|
||||
}
|
||||
|
||||
/// Str.split : Str, Str -> List Str
|
||||
pub fn str_split<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
@ -38,15 +24,11 @@ pub fn str_split<'a, 'ctx, 'env>(
|
|||
) -> BasicValueEnum<'ctx> {
|
||||
let builder = env.builder;
|
||||
|
||||
let str_c_abi = str_symbol_to_c_abi(env, scope, str_symbol);
|
||||
let delim_c_abi = str_symbol_to_c_abi(env, scope, delimiter_symbol);
|
||||
let string = load_symbol(scope, &str_symbol);
|
||||
let delimiter = load_symbol(scope, &delimiter_symbol);
|
||||
|
||||
let segment_count = call_bitcode_fn(
|
||||
env,
|
||||
&[str_c_abi.into(), delim_c_abi.into()],
|
||||
bitcode::STR_COUNT_SEGMENTS,
|
||||
)
|
||||
.into_int_value();
|
||||
let segment_count =
|
||||
call_bitcode_fn(env, &[string, delimiter], bitcode::STR_COUNT_SEGMENTS).into_int_value();
|
||||
|
||||
// a pointer to the elements
|
||||
let ret_list_ptr = allocate_list(env, &Layout::Builtin(Builtin::Str), segment_count);
|
||||
|
@ -63,11 +45,7 @@ pub fn str_split<'a, 'ctx, 'env>(
|
|||
|
||||
call_void_bitcode_fn(
|
||||
env,
|
||||
&[
|
||||
ret_list_ptr_zig_rocstr,
|
||||
str_c_abi.into(),
|
||||
delim_c_abi.into(),
|
||||
],
|
||||
&[ret_list_ptr_zig_rocstr, string, delimiter],
|
||||
bitcode::STR_STR_SPLIT_IN_PLACE,
|
||||
);
|
||||
|
||||
|
@ -120,55 +98,6 @@ pub fn destructure<'ctx>(
|
|||
(generic_ptr, length)
|
||||
}
|
||||
|
||||
/// Str.concat : Str, Str -> Str
|
||||
pub fn str_concat<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
str1_symbol: Symbol,
|
||||
str2_symbol: Symbol,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
// swap the arguments; second argument comes before the second in the output string
|
||||
let str1 = load_symbol(scope, &str1_symbol);
|
||||
let str2 = load_symbol(scope, &str2_symbol);
|
||||
|
||||
call_str_bitcode_fn(env, &[str1, str2], bitcode::STR_CONCAT)
|
||||
}
|
||||
|
||||
/// Str.join : List Str, Str -> Str
|
||||
pub fn str_join_with<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
list_symbol: Symbol,
|
||||
str_symbol: Symbol,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
// dirty hack; pretend a `list` is a `str` that works because
|
||||
// they have the same stack layout `{ u8*, usize }`
|
||||
let list_i128 = list_symbol_to_c_abi(env, scope, list_symbol);
|
||||
let str_i128 = str_symbol_to_c_abi(env, scope, str_symbol);
|
||||
|
||||
call_str_bitcode_fn(
|
||||
env,
|
||||
&[list_i128.into(), str_i128.into()],
|
||||
bitcode::STR_JOIN_WITH,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn str_number_of_bytes<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
str_symbol: Symbol,
|
||||
) -> IntValue<'ctx> {
|
||||
let str_i128 = str_symbol_to_c_abi(env, scope, str_symbol);
|
||||
|
||||
// the builtin will always return an u64
|
||||
let length =
|
||||
call_bitcode_fn(env, &[str_i128.into()], bitcode::STR_NUMBER_OF_BYTES).into_int_value();
|
||||
|
||||
// cast to the appropriate usize of the current build
|
||||
env.builder
|
||||
.build_int_cast_sign_flag(length, env.ptr_int(), false, "len_as_usize")
|
||||
}
|
||||
|
||||
/// Str.fromInt : Int -> Str
|
||||
pub fn str_from_int<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
@ -178,17 +107,6 @@ pub fn str_from_int<'a, 'ctx, 'env>(
|
|||
call_str_bitcode_fn(env, &[value.into()], &bitcode::STR_FROM_INT[int_width])
|
||||
}
|
||||
|
||||
/// Str.toUtf8 : Str -> List U8
|
||||
pub fn str_to_utf8<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
symbol: Symbol,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let string = str_symbol_to_c_abi(env, scope, symbol);
|
||||
|
||||
call_list_bitcode_fn(env, &[string.into()], bitcode::STR_TO_UTF8)
|
||||
}
|
||||
|
||||
fn decode_from_utf8_result<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
pointer: PointerValue<'ctx>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue