Make add_char and set_char work

This commit is contained in:
Sofia 2025-07-22 20:27:29 +03:00
parent 64418635a5
commit 32059682ee
3 changed files with 20 additions and 4 deletions

View File

@ -25,6 +25,7 @@ struct String {
inner: *char,
length: u64,
max_length: u64,
must_be_freed: bool,
}
pub fn new_string() -> String {
@ -32,6 +33,7 @@ pub fn new_string() -> String {
inner: allocate(0),
length: 0,
max_length: 0,
must_be_freed: true,
}
}
@ -42,7 +44,8 @@ pub fn from_str(str: *char) -> String {
return String {
inner: cloned,
length: length,
max_length: length
max_length: length,
must_be_freed: false,
};
}
@ -51,9 +54,12 @@ pub fn add_char(string: &mut String, c: char) {
let new = allocate((*string).max_length + 4) as *char;
copy_bits((*string).inner, new, 0, (*string).max_length);
free((*string).inner as *u8);
if (*string).must_be_freed == true {
free((*string).inner as *u8);
}
(*string).max_length = (*string).max_length + 4;
(*string).inner = new;
(*string).must_be_freed = true;
}
(*string).inner[(*string).length] = c;
@ -61,6 +67,12 @@ pub fn add_char(string: &mut String, c: char) {
(*string).length = (*string).length + 1;
}
pub fn set_char(string: &mut String, c: char, position: u64) {
if position <= (*string).length {
(*string).inner[position] = c;
}
}
pub fn free_string(string: &mut String) {
free((*string).inner as *u8);
}
@ -74,7 +86,7 @@ fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 {
}
fn str_length(string: *char, position: u32) -> u32 {
if ((string[position] as u8) == 0) {
if (string[position] == '\0') {
return 0;
}
return str_length(string, position + 1) + 1;

View File

@ -1135,7 +1135,7 @@ impl mir::IfExpression {
if let Some(debug) = &scope.debug {
let before_location = self.0 .1.into_debug(scope.tokens, debug.scope).unwrap();
let before_v = debug.info.location(&debug.scope, before_location);
scope.block.set_terminator_location(before_v).unwrap();
scope.block.set_terminator_location(before_v).ok();
let then_location = self
.1

View File

@ -1,10 +1,14 @@
import std::print;
import std::from_str;
import std::add_char;
import std::set_char;
import std::free_string;
fn main() -> i32 {
let mut test = from_str("hello world");
add_char(&mut test, '!');
set_char(&mut test, 'B', 0);
print(&test);