| @@ -16,7 +16,7 @@ use tilrt_prims::{ |
| 16 | 16 | use crate::abi::{Block, Ctx, TAG_FLOAT, TAG_REF, TAG_REGION, UNIT, Value, int, is_ptr}; |
| 17 | 17 | use crate::heap::{drop_value, tl_alloc}; |
| 18 | 18 | use crate::int::saturating; |
| 19 | | −use crate::values::{string, string_bytes, text, tl_string_from}; |
| 19 | +use crate::values::{cell, list, string, string_bytes, text, tl_string_from}; |
| 20 | 20 | |
| 21 | 21 | const WORD: usize = size_of::<Value>(); |
| 22 | 22 | |
| @@ -267,18 +267,7 @@ pub unsafe extern "C" fn tl_string_trim(ctx: *mut Ctx, a: Value) -> Value { |
| 267 | 267 | pub unsafe extern "C" fn tl_string_split(ctx: *mut Ctx, a: Value, sep: Value) -> Value { |
| 268 | 268 | // SAFETY: caller guarantees live strings. |
| 269 | 269 | let parts = unsafe { split(text(a), text(sep)) }; |
| 270 | | − let mut list = int(0); // Nil |
| 271 | | − for p in parts.iter().rev() { |
| 272 | | − let cell = tl_alloc(ctx, 1, 2, 0); |
| 273 | | − // SAFETY: a fresh Cons cell. |
| 274 | | − unsafe { |
| 275 | | − let f = Block::fields_ptr(cell); |
| 276 | | − f.write(string(ctx, p)); |
| 277 | | − f.add(1).write(list); |
| 278 | | − } |
| 279 | | − list = cell as Value; |
| 280 | | − } |
| 281 | | − list |
| 270 | + list(ctx, parts.iter().map(|p| string(ctx, p))) |
| 282 | 271 | } |
| 283 | 272 | |
| 284 | 273 | /// `(True, n)` if `a` is a decimal Int (optional sign), else `(False, 0)`. |
| @@ -292,14 +281,7 @@ pub unsafe extern "C" fn tl_string_parse_int(ctx: *mut Ctx, a: Value) -> Value { |
| 292 | 281 | let n = parsed |
| 293 | 282 | .as_ref() |
| 294 | 283 | .map_or(int(0), |n| crate::int::from_big(ctx, n)); |
| 295 | | − let pair = tl_alloc(ctx, 0, 2, 0); |
| 296 | | − // SAFETY: a fresh tuple. |
| 297 | | − unsafe { |
| 298 | | − let f = Block::fields_ptr(pair); |
| 299 | | − f.write(boolean(parsed.is_some())); |
| 300 | | − f.add(1).write(n); |
| 301 | | − } |
| 302 | | − pair as Value |
| 284 | + cell(ctx, 0, &[boolean(parsed.is_some()), n]) |
| 303 | 285 | } |
| 304 | 286 | |
| 305 | 287 | /// The number of characters (Unicode scalar values). |
| @@ -624,14 +606,8 @@ pub unsafe extern "C" fn tl_random_token(ctx: *mut Ctx, n: Value) -> Value { |
| 624 | 606 | pub unsafe extern "C" fn tl_os_env(ctx: *mut Ctx, name: Value) -> Value { |
| 625 | 607 | // SAFETY: caller guarantees a live string. |
| 626 | 608 | let found = std::env::var(unsafe { text(name) }).ok(); |
| 627 | | − let pair = tl_alloc(ctx, 0, 2, 0); |
| 628 | | − // SAFETY: a fresh tuple. |
| 629 | | − unsafe { |
| 630 | | − let f = Block::fields_ptr(pair); |
| 631 | | − f.write(boolean(found.is_some())); |
| 632 | | − f.add(1).write(string(ctx, found.as_deref().unwrap_or(""))); |
| 633 | | − } |
| 634 | | − pair as Value |
| 609 | + let value = string(ctx, found.as_deref().unwrap_or("")); |
| 610 | + cell(ctx, 0, &[boolean(found.is_some()), value]) |
| 635 | 611 | } |
| 636 | 612 | |
| 637 | 613 | /// # Safety |