From de803e90246a28369b371c00dd7768c170869b30 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 23 Jul 2025 22:01:14 +0300 Subject: [PATCH] Fix from_str in stdlib, add concat_strings --- examples/hello_world.reid | 12 +++++++++--- reid/lib/std.reid | 21 ++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/hello_world.reid b/examples/hello_world.reid index fac2c9d..4c8a10d 100644 --- a/examples/hello_world.reid +++ b/examples/hello_world.reid @@ -3,14 +3,20 @@ import std::from_str; import std::add_char; import std::set_char; import std::free_string; +import std::new_string; import std::add_num_to_str; +import std::concat_strings; fn main() -> i32 { - let mut test = from_str("hello world"); + let mut test = from_str("hello"); + let mut other = from_str(" world"); + + concat_strings(&mut test, &other); add_char(&mut test, '!'); - set_char(&mut test, 'B', 0); - add_num_to_str(&mut test, 1234); + add_char(&mut test, '\n'); + + add_num_to_str(&mut test, 175); print(&test); diff --git a/reid/lib/std.reid b/reid/lib/std.reid index c99ada2..59ec5dd 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -39,14 +39,15 @@ pub fn new_string() -> String { pub fn from_str(str: *char) -> String { let length = str_length(str); - let cloned = malloc(length as u64) as *char; - copy_bits(str, cloned, length as u64); - return String { - inner: cloned, - length: length -1, + let mut new = new_string(); + let static = String { + inner: str, + length: length - 1, max_length: length, must_be_freed: false, }; + concat_strings(&mut new, &static); + return new; } pub fn add_char(string: &mut String, c: char) { @@ -63,7 +64,7 @@ pub fn add_char(string: &mut String, c: char) { } (*string).inner[(*string).length] = c; - (((*string).inner) as *u8)[((*string).length + 1)] = 0; + (((*string).inner) as *u8)[(*string).length + 1] = 0; (*string).length = (*string).length + 1; } @@ -124,4 +125,10 @@ pub fn abs(f: f32) -> f32 { return f * (0.0 - 1.0); } return f; -} \ No newline at end of file +} + +pub fn concat_strings(destination: &mut String, source: &String) { + for i in 0 .. (str_length((*source).inner) - 1) { + add_char(destination, (*source).inner[i]); + } +}