Fix from_str in stdlib, add concat_strings
This commit is contained in:
		
							parent
							
								
									d96fc51b9c
								
							
						
					
					
						commit
						de803e9024
					
				| @ -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); | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
| } | ||||
| } | ||||
| 
 | ||||
| pub fn concat_strings(destination: &mut String, source: &String) { | ||||
|     for i in 0 .. (str_length((*source).inner) - 1) { | ||||
|         add_char(destination, (*source).inner[i]); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user