Skip to content

Commit 7e82b2d

Browse files
author
Zuhaitz Méndez Fernández de Aránguiz
committed
'std/string.md' changes
1 parent 3b910a0 commit 7e82b2d

1 file changed

Lines changed: 47 additions & 43 deletions

File tree

std/string.md

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ fn main() {
1212
1313
// Append requires a pointer to another String
1414
let part = String::from(" World");
15-
1615
s.append(&part);
1716
17+
// Iteration (UTF-8 aware)
18+
for c in s {
19+
println "{c}";
20+
}
21+
1822
// Use c_str() to print
1923
println "{s.c_str()}"; // Prints "Hello World"
2024
2125
if (s.starts_with("Hello")) {
2226
// ...
2327
}
24-
}
28+
} // s is freed automatically here
2529
```
2630

27-
## Structure
31+
## Struct Definition
2832

2933
```zc
3034
struct String {
@@ -42,20 +46,19 @@ struct String {
4246
| **from** | `String::from(s: char*) -> String` | Alias for `new`. |
4347
| **from_rune** | `String::from_rune(r: rune) -> String` | Creates a new String from a single `rune`. |
4448
| **from_runes** | `String::from_runes(runes: rune*, count: usize) -> String` | Creates a new String from an array of `runes`. |
49+
| **from_runes_vec** | `String::from_runes_vec(runes: Vec<rune>) -> String` | Creates a new String from a vector of `rune` objects. |
4550

4651
### Modification
4752

4853
| Method | Signature | Description |
4954
| :--- | :--- | :--- |
5055
| **append** | `append(self, other: String*)` | Appends another string to this one. |
51-
| **append_c** | `append_c(self, s: char*)` | Appends a C string literal. Uses value receiver. |
56+
| **append_c** | `append_c(self, s: char*)` | Appends a C string literal. |
5257
| **push_rune** | `push_rune(self, r: rune)` | Appends a single Unicode code point (`rune`) to the string. |
5358
| **insert_rune** | `insert_rune(self, idx: usize, r: rune)` | Inserts a `rune` at the specified *character index*. |
5459
| **remove_rune_at** | `remove_rune_at(self, idx: usize) -> rune` | Removes and returns the `rune` at the specified *character index*. |
55-
| **append_c_ptr** | `append_c_ptr(ptr: String*, s: char*)` | Appends a C string literal using pointer receiver for guaranteed mutation. |
56-
| **add** | `add(self, other: String*) -> String` | Concatenates this string and another into a new String. |
5760
| **reserve** | `reserve(self, cap: usize)` | Ensures the string has at least `cap` characters of capacity. |
58-
**Note:** When passing `String*` to functions that need to mutate, use `append_c_ptr` instead of `append_c` for reliable mutation.
61+
| **clear** | `clear(self)` | Clears the string. |
5962

6063
### Access & Query
6164

@@ -64,7 +67,7 @@ struct String {
6467
| **c_str** | `c_str(self) -> char*` | Returns the underlying C string pointer. |
6568
| **length** | `length(self) -> usize` | Returns the length of the string (excluding null terminator). |
6669
| **is_empty** | `is_empty(self) -> bool` | Returns true if length is 0. |
67-
| **to_string** | `to_string(self) -> char*` | Allows smooth, implicit `{var}` bracket interpolation. Maps to `c_str()`. |
70+
| **to_string** | `to_string(self) -> char*` | Maps to `c_str()`. Used for `{var}` interpolation. |
6871
| **starts_with** | `starts_with(self, prefix: char*) -> bool` | Checks if the string starts with the given prefix. |
6972
| **ends_with** | `ends_with(self, suffix: char*) -> bool` | Checks if the string ends with the given suffix. |
7073
| **contains** | `contains(self, target: char) -> bool` | Checks if the string contains the given character. |
@@ -76,61 +79,62 @@ struct String {
7679

7780
### UTF-8 Support
7881

79-
These methods handle UTF-8 character boundaries correctly, contrasting with the byte-oriented methods above.
80-
8182
| Method | Signature | Description |
8283
| :--- | :--- | :--- |
8384
| **utf8_len** | `utf8_len(self) -> usize` | Returns the number of Unicode code points (characters). |
84-
| **utf8_at** | `utf8_at(self, idx: usize) -> String` | Returns the character at the specified *character index* as a new String. |
85-
| **utf8_get** | `utf8_get(self, idx: usize) -> rune` | Returns the character at the specified *character index* as a `rune`. |
86-
| **utf8_substr** | `utf8_substr(self, start_idx: usize, num_chars: usize) -> String` | Returns a substring based on character indices and character count. |
87-
| **runes** | `runes(self) -> Vec<rune>` | Returns a vector containing all Unicode code points from the string. |
88-
| **from_runes_vec** | `String::from_runes_vec(runes: Vec<rune>) -> String` | Creates a new String from a vector of `rune` objects. |
89-
| **chars** | `chars(self) -> StringCharsIter` | Returns an iterator that yields `Option<rune>` for each character. |
90-
85+
| **utf8_at** | `utf8_at(self, idx: usize) -> String` | Returns the character at the specified index as a new String. |
86+
| **utf8_get** | `utf8_get(self, idx: usize) -> rune` | Returns the character at the specified index as a `rune`. |
87+
| **utf8_substr** | `utf8_substr(self, start_idx: usize, num_chars: usize) -> String` | Returns a substring based on character indices. |
88+
| **runes** | `runes(self) -> Vec<rune>` | Returns a vector containing all Unicode code points. |
89+
| **chars** | `chars(self) -> StringCharsIter` | Returns a manual iterator yielding `Option<rune>`. |
9190

9291
### Transformations
9392

9493
| Method | Signature | Description |
9594
| :--- | :--- | :--- |
9695
| **to_lowercase** | `to_lowercase(self) -> String` | Returns a new string converted into lowercase. |
9796
| **to_uppercase** | `to_uppercase(self) -> String` | Returns a new string converted into uppercase. |
98-
| **split** | `split(self, delim: char) -> Vec<String>` | Splits the string into a vector of substrings separated by `delim`. |
99-
| **trim** | `trim(self) -> String` | Returns a new string with leading and trailing whitespace removed. |
100-
| **replace** | `replace(self, target: char*, replacement: char*) -> String` | Returns a new string with all occurrences of `target` replaced by `replacement`. |
101-
| **pad_left** | `pad_left(self, target_len: usize, pad_char: char) -> String` | Returns a new string padded to `target_len` on the left with `pad_char`. |
102-
| **pad_right** | `pad_right(self, target_len: usize, pad_char: char) -> String` | Returns a new string padded to `target_len` on the right with `pad_char`. |
97+
| **split** | `split(self, delim: char) -> Vec<String>` | Splits the string into a vector of substrings. |
98+
| **trim** | `trim(self) -> String` | Returns a new string with leading/trailing whitespace removed. |
99+
| **replace** | `replace(self, target: char*, replacement: char*) -> String` | Returns a new string with replacements. |
100+
| **pad_left** | `pad_left(self, target_len: usize, pad_char: char) -> String` | Returns a new string padded on the left. |
101+
| **pad_right** | `pad_right(self, target_len: usize, pad_char: char) -> String` | Returns a new string padded on the right. |
103102

104103
### Comparison
105104

106105
| Method | Signature | Description |
107106
| :--- | :--- | :--- |
108-
| **eq** | `eq(self, other: String*) -> bool` | Returns true if the strings are equal content-wise. |
109-
| **neq** | `neq(self, other: String*) -> bool` | Returns true if the strings are NOT equal content-wise. |
110-
| **compare** | `compare(self, other: String*) -> int` | Returns < 0 if self < other, 0 if equal, > 0 if self > other (lexical). |
111-
| **compare_ignore_case** | `compare_ignore_case(self, other: String*) -> int` | Lexical comparison ignoring case (A == a). |
112-
| **eq_ignore_case** | `eq_ignore_case(self, other: String*) -> bool` | Returns true if strings are equal ignoring case. |
113-
114-
### Operators
107+
| **eq** | `eq(self, other: String*) -> bool` | Structural equality check. |
108+
| **neq** | `neq(self, other: String*) -> bool` | Structural inequality check. |
109+
| **compare** | `compare(self, other: String*) -> int` | Lexical comparison. |
110+
| **compare_ignore_case** | `compare_ignore_case(self, other: String*) -> int` | Case-insensitive lexical comparison. |
111+
| **eq_ignore_case** | `eq_ignore_case(self, other: String*) -> bool` | Case-insensitive equality check. |
115112

116-
Zen-C supports operator overloading. `String` implements the following:
113+
## Operators
117114

118115
| Operator | Method | Description |
119116
| :--- | :--- | :--- |
120-
| `+` | **add** | `s1 + &s2`. Concatenates `s1` and `s2`, returning a new `String`. |
121-
| `+=` | **add_assign** | `s1 += &s2`. Appends `s2` dynamically to `s1` in place. |
122-
| `==` | **eq** | `s1 == &s2`. Performs a structural string-equality check. |
123-
| `!=` | **neq** | `s1 != &s2`. Performs a structural string-inequality check. |
124-
| `<` | **lt** | `s1 < &s2`. True if `s1` is lexically less than `s2`. |
125-
| `>` | **gt** | `s1 > &s2`. True if `s1` is lexically greater than `s2`. |
126-
| `<=` | **le** | `s1 <= &s2`. True if `s1` is lexically less than or equal to `s2`. |
127-
| `>=` | **ge** | `s1 >= &s2`. True if `s1` is lexically greater than or equal to `s2`. |
128-
| `{}` | **to_string** | Automatically embeds the string contents during `printf` and `println` block formatting. |
129-
130-
### Memory Management
117+
| `+` | **add** | `s1 + &s2`. Concatenates strings into a new `String`. |
118+
| `+=` | **add_assign** | `s1 += &s2`. Appends `s2` to `s1` in place. |
119+
| `==` | **eq** | `s1 == &s2`. structural equality check. |
120+
| `!=` | **neq** | `s1 != &s2`. structural inequality check. |
121+
| `<` | **lt** | `s1 < &s2`. Lexical comparison. |
122+
| `>` | **gt** | `s1 > &s2`. Lexical comparison. |
123+
| `<=` | **le** | `s1 <= &s2`. Lexical comparison. |
124+
| `>=` | **ge** | `s1 >= &s2`. Lexical comparison. |
125+
| `{}` | **to_string** | Used for string interpolation in `printf`/`println`. |
126+
127+
## Iteration
128+
129+
| Method | Signature | Description |
130+
| :--- | :--- | :--- |
131+
| **iterator** | `iterator(self) -> StringCharsIter` | Returns an iterator yielding `rune`. Used by `for c in s`. |
132+
133+
## Memory Management
131134

132135
| Method | Signature | Description |
133136
| :--- | :--- | :--- |
134-
| **free** | `free(self)` | Frees the string memory. |
137+
| **free** | `free(self)` | Manually frees the string memory. |
135138
| **destroy** | `destroy(self)` | Alias for `free`. |
136-
| **forget** | `forget(self)` | Prevents automatic freeing (useful for transferring ownership). |
139+
| **forget** | `forget(self)` | Prevents automatic freeing (transfer ownership). |
140+
| **Trait** | `impl Drop for String` | Automatically calls `free()` when out of scope. |

0 commit comments

Comments
 (0)