| @@ -18,7 +18,7 @@ use crate::abi::{Block, Ctx, TAG_FLOAT, UNIT, Value, int}; |
| 18 | 18 | use crate::console; |
| 19 | 19 | use crate::heap::tl_alloc; |
| 20 | 20 | use crate::int::saturating; |
| 21 | | −use crate::values::{cell, list, string, string_bytes, text, tl_string_from}; |
| 21 | +use crate::values::{cell, list, string, string_bytes, text}; |
| 22 | 22 | |
| 23 | 23 | const WORD: usize = size_of::<Value>(); |
| 24 | 24 | |
| @@ -208,11 +208,11 @@ pub unsafe extern "C" fn tl_string_compare(_ctx: *mut Ctx, a: Value, b: Value) - |
| 208 | 208 | pub unsafe extern "C" fn tl_string_concat(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 209 | 209 | // SAFETY: caller guarantees live strings. |
| 210 | 210 | let (x, y) = unsafe { (string_bytes(a), string_bytes(b)) }; |
| 211 | | − let mut joined = Vec::with_capacity(x.len() + y.len()); |
| 212 | | − joined.extend_from_slice(x); |
| 213 | | − joined.extend_from_slice(y); |
| 214 | | − // SAFETY: `joined` is valid for its length. |
| 215 | | − unsafe { tl_string_from(ctx, joined.as_ptr().cast(), joined.len()) } |
| 211 | + let parts = || [x, y].into_iter(); |
| 212 | + let (s, out) = crate::values::string_uninit(ctx, crate::values::joined_len(parts(), 0)); |
| 213 | + // SAFETY: `out` has room for both, by the length just computed. |
| 214 | + unsafe { crate::values::write_joined(out, parts(), &[]) }; |
| 215 | + s |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | macro_rules! string_test { |
| @@ -243,7 +243,15 @@ pub unsafe extern "C" fn tl_string_byte_length(_ctx: *mut Ctx, a: Value) -> Valu |
| 243 | 243 | #[unsafe(no_mangle)] |
| 244 | 244 | pub unsafe extern "C" fn tl_string_to_lower(ctx: *mut Ctx, a: Value) -> Value { |
| 245 | 245 | // SAFETY: caller guarantees a live string. |
| 246 | | − string(ctx, &unsafe { text(a) }.to_ascii_lowercase()) |
| 246 | + let x = unsafe { string_bytes(a) }; |
| 247 | + let (s, out) = crate::values::string_uninit(ctx, x.len()); |
| 248 | + // SAFETY: `out` has room for `x`, which ASCII lowercasing keeps the |
| 249 | + // same length (and valid UTF-8). |
| 250 | + unsafe { |
| 251 | + std::ptr::copy_nonoverlapping(x.as_ptr(), out, x.len()); |
| 252 | + std::slice::from_raw_parts_mut(out, x.len()).make_ascii_lowercase(); |
| 253 | + } |
| 254 | + s |
| 247 | 255 | } |
| 248 | 256 | |
| 249 | 257 | /// # Safety |
| @@ -314,18 +322,20 @@ pub unsafe extern "C" fn tl_hash_string(_ctx: *mut Ctx, a: Value) -> Value { |
| 314 | 322 | /// `parts` must be a live list of strings; `sep` a live string. |
| 315 | 323 | #[unsafe(no_mangle)] |
| 316 | 324 | pub unsafe extern "C" fn tl_string_join(ctx: *mut Ctx, parts: Value, sep: Value) -> Value { |
| 317 | | − // SAFETY: caller guarantees a list of strings and a string. |
| 318 | | − let (parts, sep): (Vec<&[u8]>, _) = unsafe { |
| 325 | + // Two passes over the list: the length, then the bytes, straight into |
| 326 | + // the string (issue #129). |
| 327 | + // SAFETY: caller guarantees a list of strings and a string, live while |
| 328 | + // the new string is built. |
| 329 | + let (items, sep) = unsafe { |
| 319 | 330 | ( |
| 320 | | − crate::values::list_items(parts) |
| 321 | | − .map(|p| string_bytes(p)) |
| 322 | | − .collect(), |
| 331 | + || crate::values::list_items(parts).map(|p| string_bytes(p)), |
| 323 | 332 | string_bytes(sep), |
| 324 | 333 | ) |
| 325 | 334 | }; |
| 326 | | − let joined = parts.join(sep); |
| 327 | | − // SAFETY: `joined` is valid for its length. |
| 328 | | − unsafe { tl_string_from(ctx, joined.as_ptr().cast(), joined.len()) } |
| 335 | + let (s, out) = crate::values::string_uninit(ctx, crate::values::joined_len(items(), sep.len())); |
| 336 | + // SAFETY: `out` has room for the joined length of the same parts. |
| 337 | + unsafe { crate::values::write_joined(out, items(), sep) }; |
| 338 | + s |
| 329 | 339 | } |
| 330 | 340 | |
| 331 | 341 | /// # Safety |
| @@ -446,6 +456,52 @@ mod tests { |
| 446 | 456 | UNIT |
| 447 | 457 | } |
| 448 | 458 | |
| 459 | + /// Strings joined and concatenated in place (issue #129): lengths, NUL |
| 460 | + /// terminators and contents, and nothing leaked. |
| 461 | + unsafe extern "C" fn builds_strings_in_place(ctx: *mut Ctx) -> Value { |
| 462 | + // SAFETY: every value is owned by this test. |
| 463 | + unsafe { |
| 464 | + let check = |s: Value, want: &str| { |
| 465 | + assert_eq!(text(s), want); |
| 466 | + assert_eq!(*string_bytes(s).as_ptr().add(want.len()), 0, "NUL"); |
| 467 | + drop_value(ctx, s); |
| 468 | + }; |
| 469 | + let big = "é".repeat(500_000); |
| 470 | + for (parts, sep, want) in [ |
| 471 | + (vec![], ", ", String::new()), |
| 472 | + (vec![""], ", ", String::new()), |
| 473 | + (vec!["a"], ", ", "a".into()), |
| 474 | + (vec!["a", "b", "c"], ", ", "a, b, c".into()), |
| 475 | + (vec!["a", "", "c"], "", "ac".into()), |
| 476 | + (vec!["日本", "語"], "·", "日本·語".into()), |
| 477 | + (vec![big.as_str(), "!"], "", format!("{big}!")), |
| 478 | + ] { |
| 479 | + let list = list(ctx, parts.iter().map(|p| string(ctx, p))); |
| 480 | + let sep = string(ctx, sep); |
| 481 | + check(tl_string_join(ctx, list, sep), &want); |
| 482 | + drop_value(ctx, list); |
| 483 | + drop_value(ctx, sep); |
| 484 | + } |
| 485 | + let (x, y) = (string(ctx, "ab"), string(ctx, "çd")); |
| 486 | + check(tl_string_concat(ctx, x, y), "abçd"); |
| 487 | + check(tl_string_to_lower(ctx, y), "çd"); |
| 488 | + let shout = string(ctx, "HeLLo, Wörld"); |
| 489 | + check(tl_string_to_lower(ctx, shout), "hello, wörld"); |
| 490 | + for v in [x, y, shout] { |
| 491 | + drop_value(ctx, v); |
| 492 | + } |
| 493 | + } |
| 494 | + assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 495 | + UNIT |
| 496 | + } |
| 497 | + |
| 498 | + #[test] |
| 499 | + fn strings_are_built_in_place() { |
| 500 | + // SAFETY: the test follows the generated-code convention. |
| 501 | + let code = unsafe { crate::sched::run_test_actor(builds_strings_in_place) }; |
| 502 | + assert_eq!(code, 0); |
| 503 | + } |
| 504 | + |
| 449 | 505 | #[test] |
| 450 | 506 | fn externs_build_prelude_values() { |
| 451 | 507 | // SAFETY: the test follows the generated-code convention. |