@shd101wyy/yo 0.1.26 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/.github/skills/yo-async-effects/SKILL.md +4 -4
  2. package/.github/skills/yo-async-effects/async-effects-recipes.md +34 -34
  3. package/.github/skills/yo-core-patterns/SKILL.md +1 -1
  4. package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +26 -26
  5. package/.github/skills/yo-project-workflow/SKILL.md +6 -3
  6. package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
  7. package/.github/skills/yo-syntax/SKILL.md +7 -6
  8. package/.github/skills/yo-syntax/syntax-cheatsheet.md +73 -60
  9. package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
  10. package/README.md +10 -8
  11. package/out/cjs/index.cjs +456 -438
  12. package/out/cjs/yo-cli.cjs +576 -543
  13. package/out/cjs/yo-lsp.cjs +559 -532
  14. package/out/esm/index.mjs +281 -263
  15. package/out/types/src/formatter.d.ts +11 -0
  16. package/out/types/src/lsp/formatting.d.ts +2 -0
  17. package/out/types/src/tests/formatter.test.d.ts +1 -0
  18. package/out/types/tsconfig.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/std/alg/hash.yo +13 -21
  21. package/std/allocator.yo +25 -40
  22. package/std/async.yo +3 -7
  23. package/std/build.yo +105 -151
  24. package/std/cli/arg_parser.yo +184 -169
  25. package/std/collections/array_list.yo +350 -314
  26. package/std/collections/btree_map.yo +142 -131
  27. package/std/collections/deque.yo +132 -128
  28. package/std/collections/hash_map.yo +542 -566
  29. package/std/collections/hash_set.yo +623 -687
  30. package/std/collections/linked_list.yo +275 -293
  31. package/std/collections/ordered_map.yo +113 -85
  32. package/std/collections/priority_queue.yo +73 -73
  33. package/std/crypto/md5.yo +191 -95
  34. package/std/crypto/random.yo +56 -64
  35. package/std/crypto/sha256.yo +151 -107
  36. package/std/encoding/base64.yo +87 -81
  37. package/std/encoding/hex.yo +43 -50
  38. package/std/encoding/html.yo +56 -81
  39. package/std/encoding/html_char_utils.yo +7 -13
  40. package/std/encoding/html_entities.yo +2248 -2253
  41. package/std/encoding/json.yo +316 -224
  42. package/std/encoding/punycode.yo +86 -116
  43. package/std/encoding/toml.yo +67 -66
  44. package/std/encoding/utf16.yo +37 -44
  45. package/std/env.yo +62 -91
  46. package/std/error.yo +7 -15
  47. package/std/fmt/display.yo +5 -9
  48. package/std/fmt/index.yo +8 -14
  49. package/std/fmt/to_string.yo +330 -315
  50. package/std/fmt/writer.yo +58 -87
  51. package/std/fs/dir.yo +83 -102
  52. package/std/fs/file.yo +147 -180
  53. package/std/fs/metadata.yo +45 -78
  54. package/std/fs/temp.yo +55 -65
  55. package/std/fs/types.yo +27 -40
  56. package/std/fs/walker.yo +53 -68
  57. package/std/gc.yo +5 -8
  58. package/std/glob.yo +30 -43
  59. package/std/http/client.yo +107 -120
  60. package/std/http/http.yo +106 -96
  61. package/std/http/index.yo +4 -6
  62. package/std/imm/list.yo +88 -93
  63. package/std/imm/map.yo +528 -464
  64. package/std/imm/set.yo +52 -57
  65. package/std/imm/sorted_map.yo +340 -286
  66. package/std/imm/sorted_set.yo +57 -63
  67. package/std/imm/string.yo +404 -345
  68. package/std/imm/vec.yo +173 -181
  69. package/std/io/reader.yo +3 -6
  70. package/std/io/writer.yo +4 -8
  71. package/std/libc/assert.yo +5 -9
  72. package/std/libc/ctype.yo +32 -22
  73. package/std/libc/dirent.yo +26 -25
  74. package/std/libc/errno.yo +164 -90
  75. package/std/libc/fcntl.yo +52 -45
  76. package/std/libc/float.yo +66 -44
  77. package/std/libc/limits.yo +42 -33
  78. package/std/libc/math.yo +53 -82
  79. package/std/libc/signal.yo +72 -47
  80. package/std/libc/stdatomic.yo +217 -188
  81. package/std/libc/stdint.yo +5 -29
  82. package/std/libc/stdio.yo +5 -29
  83. package/std/libc/stdlib.yo +32 -39
  84. package/std/libc/string.yo +5 -23
  85. package/std/libc/sys/stat.yo +58 -56
  86. package/std/libc/time.yo +5 -19
  87. package/std/libc/unistd.yo +5 -20
  88. package/std/libc/wctype.yo +6 -9
  89. package/std/libc/windows.yo +26 -30
  90. package/std/log.yo +41 -55
  91. package/std/net/addr.yo +102 -97
  92. package/std/net/dns.yo +27 -28
  93. package/std/net/errors.yo +50 -49
  94. package/std/net/tcp.yo +113 -124
  95. package/std/net/udp.yo +55 -66
  96. package/std/os/env.yo +35 -33
  97. package/std/os/signal.yo +15 -25
  98. package/std/path.yo +276 -311
  99. package/std/prelude.yo +6304 -4315
  100. package/std/process/command.yo +87 -103
  101. package/std/process/index.yo +12 -31
  102. package/std/regex/compiler.yo +196 -95
  103. package/std/regex/flags.yo +58 -39
  104. package/std/regex/index.yo +157 -173
  105. package/std/regex/match.yo +20 -31
  106. package/std/regex/node.yo +134 -152
  107. package/std/regex/parser.yo +283 -259
  108. package/std/regex/unicode.yo +172 -202
  109. package/std/regex/vm.yo +155 -171
  110. package/std/string/index.yo +5 -7
  111. package/std/string/rune.yo +45 -55
  112. package/std/string/string.yo +937 -964
  113. package/std/string/string_builder.yo +94 -104
  114. package/std/string/unicode.yo +46 -64
  115. package/std/sync/channel.yo +72 -73
  116. package/std/sync/cond.yo +31 -36
  117. package/std/sync/mutex.yo +30 -32
  118. package/std/sync/once.yo +13 -16
  119. package/std/sync/rwlock.yo +26 -31
  120. package/std/sync/waitgroup.yo +20 -25
  121. package/std/sys/advise.yo +16 -24
  122. package/std/sys/bufio/buf_reader.yo +77 -93
  123. package/std/sys/bufio/buf_writer.yo +52 -65
  124. package/std/sys/clock.yo +4 -9
  125. package/std/sys/constants.yo +77 -61
  126. package/std/sys/copy.yo +4 -10
  127. package/std/sys/dir.yo +26 -43
  128. package/std/sys/dns.yo +41 -61
  129. package/std/sys/errors.yo +95 -103
  130. package/std/sys/events.yo +45 -57
  131. package/std/sys/externs.yo +319 -267
  132. package/std/sys/fallocate.yo +7 -11
  133. package/std/sys/fcntl.yo +14 -22
  134. package/std/sys/file.yo +26 -40
  135. package/std/sys/future.yo +5 -8
  136. package/std/sys/iov.yo +12 -25
  137. package/std/sys/lock.yo +12 -13
  138. package/std/sys/mmap.yo +38 -43
  139. package/std/sys/path.yo +3 -8
  140. package/std/sys/perm.yo +7 -21
  141. package/std/sys/pipe.yo +5 -12
  142. package/std/sys/process.yo +23 -29
  143. package/std/sys/seek.yo +10 -12
  144. package/std/sys/signal.yo +7 -13
  145. package/std/sys/signals.yo +52 -35
  146. package/std/sys/socket.yo +63 -58
  147. package/std/sys/socketpair.yo +3 -6
  148. package/std/sys/sockinfo.yo +11 -20
  149. package/std/sys/statfs.yo +11 -34
  150. package/std/sys/statx.yo +25 -52
  151. package/std/sys/sysinfo.yo +15 -20
  152. package/std/sys/tcp.yo +62 -92
  153. package/std/sys/temp.yo +5 -9
  154. package/std/sys/time.yo +5 -15
  155. package/std/sys/timer.yo +6 -11
  156. package/std/sys/tty.yo +10 -18
  157. package/std/sys/udp.yo +22 -39
  158. package/std/sys/umask.yo +3 -6
  159. package/std/sys/unix.yo +33 -52
  160. package/std/testing/bench.yo +49 -52
  161. package/std/thread.yo +10 -15
  162. package/std/time/datetime.yo +105 -89
  163. package/std/time/duration.yo +43 -56
  164. package/std/time/instant.yo +13 -18
  165. package/std/time/sleep.yo +5 -9
  166. package/std/url/index.yo +184 -209
  167. package/std/worker.yo +6 -10
@@ -1,268 +1,279 @@
1
1
  //! ToString trait and implementations for all primitive types.
2
-
3
- { String, rune } :: import "../string";
4
- { snprintf } :: import "../libc/stdio";
5
-
2
+ { String, rune } :: import("../string");
3
+ { snprintf } :: import("../libc/stdio");
6
4
  /// Trait for converting values to their `String` representation.
7
5
  /// Types implementing this trait can be used with `println`, `print`, and template strings.
8
6
  ToString :: trait(
9
- to_string : (fn(self: *(Self)) -> String)
7
+ to_string : (fn(self : *(Self)) -> String)
10
8
  );
11
- export ToString;
12
-
13
-
9
+ export(ToString);
14
10
  // === Helper function for integer conversion ===
15
-
16
11
  // Buffer size for integer to string conversion
17
12
  // i64 min is -9223372036854775808 (20 chars + null = 21)
18
13
  // u64 max is 18446744073709551615 (20 chars + null = 21)
19
14
  _INT_BUFFER_SIZE :: usize(24);
20
-
21
-
22
15
  // === Signed Integer Implementations ===
23
-
24
- impl(i8, ToString(
25
- to_string : (fn(self: *(Self)) -> String)({
26
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
27
- buffer_ptr := (&(buffer(0)));
28
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhd", self.*);
29
- String.from_cstr(buffer_ptr).unwrap()
30
- })
31
- ));
32
-
33
- impl(i16, ToString(
34
- to_string : (fn(self: *(Self)) -> String)({
35
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
36
- buffer_ptr := (&(buffer(0)));
37
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hd", self.*);
38
- String.from_cstr(buffer_ptr).unwrap()
39
- })
40
- ));
41
-
42
- impl(i32, ToString(
43
- to_string : (fn(self: *(Self)) -> String)({
44
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
45
- buffer_ptr := (&(buffer(0)));
46
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%d", self.*);
47
- String.from_cstr(buffer_ptr).unwrap()
48
- })
49
- ));
50
-
51
- impl(i64, ToString(
52
- to_string : (fn(self: *(Self)) -> String)({
53
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
54
- buffer_ptr := (&(buffer(0)));
55
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%lld", self.*);
56
- String.from_cstr(buffer_ptr).unwrap()
57
- })
58
- ));
59
-
60
- impl(isize, ToString(
61
- to_string : (fn(self: *(Self)) -> String)({
62
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
63
- buffer_ptr := (&(buffer(0)));
64
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zd", self.*);
65
- String.from_cstr(buffer_ptr).unwrap()
66
- })
67
- ));
68
-
69
-
16
+ impl(
17
+ i8,
18
+ ToString(
19
+ to_string : (fn(self : *(Self)) -> String)({
20
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
21
+ buffer_ptr := (&(buffer(0)));
22
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhd", self.*);
23
+ String.from_cstr(buffer_ptr).unwrap()
24
+ })
25
+ )
26
+ );
27
+ impl(
28
+ i16,
29
+ ToString(
30
+ to_string : (fn(self : *(Self)) -> String)({
31
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
32
+ buffer_ptr := (&(buffer(0)));
33
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hd", self.*);
34
+ String.from_cstr(buffer_ptr).unwrap()
35
+ })
36
+ )
37
+ );
38
+ impl(
39
+ i32,
40
+ ToString(
41
+ to_string : (fn(self : *(Self)) -> String)({
42
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
43
+ buffer_ptr := (&(buffer(0)));
44
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%d", self.*);
45
+ String.from_cstr(buffer_ptr).unwrap()
46
+ })
47
+ )
48
+ );
49
+ impl(
50
+ i64,
51
+ ToString(
52
+ to_string : (fn(self : *(Self)) -> String)({
53
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
54
+ buffer_ptr := (&(buffer(0)));
55
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%lld", self.*);
56
+ String.from_cstr(buffer_ptr).unwrap()
57
+ })
58
+ )
59
+ );
60
+ impl(
61
+ isize,
62
+ ToString(
63
+ to_string : (fn(self : *(Self)) -> String)({
64
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
65
+ buffer_ptr := (&(buffer(0)));
66
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zd", self.*);
67
+ String.from_cstr(buffer_ptr).unwrap()
68
+ })
69
+ )
70
+ );
70
71
  // === Unsigned Integer Implementations ===
71
-
72
- impl(u8, ToString(
73
- to_string : (fn(self: *(Self)) -> String)({
74
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
75
- buffer_ptr := (&(buffer(0)));
76
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhu", self.*);
77
- String.from_cstr(buffer_ptr).unwrap()
78
- })
79
- ));
80
-
81
- impl(u16, ToString(
82
- to_string : (fn(self: *(Self)) -> String)({
83
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
84
- buffer_ptr := (&(buffer(0)));
85
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hu", self.*);
86
- String.from_cstr(buffer_ptr).unwrap()
87
- })
88
- ));
89
-
90
- impl(u32, ToString(
91
- to_string : (fn(self: *(Self)) -> String)({
92
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
93
- buffer_ptr := (&(buffer(0)));
94
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%u", self.*);
95
- String.from_cstr(buffer_ptr).unwrap()
96
- })
97
- ));
98
-
99
- impl(u64, ToString(
100
- to_string : (fn(self: *(Self)) -> String)({
101
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
102
- buffer_ptr := (&(buffer(0)));
103
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%llu", self.*);
104
- String.from_cstr(buffer_ptr).unwrap()
105
- })
106
- ));
107
-
108
- impl(usize, ToString(
109
- to_string : (fn(self: *(Self)) -> String)({
110
- buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
111
- buffer_ptr := (&(buffer(0)));
112
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zu", self.*);
113
- String.from_cstr(buffer_ptr).unwrap()
114
- })
115
- ));
116
-
117
-
72
+ impl(
73
+ u8,
74
+ ToString(
75
+ to_string : (fn(self : *(Self)) -> String)({
76
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
77
+ buffer_ptr := (&(buffer(0)));
78
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhu", self.*);
79
+ String.from_cstr(buffer_ptr).unwrap()
80
+ })
81
+ )
82
+ );
83
+ impl(
84
+ u16,
85
+ ToString(
86
+ to_string : (fn(self : *(Self)) -> String)({
87
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
88
+ buffer_ptr := (&(buffer(0)));
89
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hu", self.*);
90
+ String.from_cstr(buffer_ptr).unwrap()
91
+ })
92
+ )
93
+ );
94
+ impl(
95
+ u32,
96
+ ToString(
97
+ to_string : (fn(self : *(Self)) -> String)({
98
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
99
+ buffer_ptr := (&(buffer(0)));
100
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%u", self.*);
101
+ String.from_cstr(buffer_ptr).unwrap()
102
+ })
103
+ )
104
+ );
105
+ impl(
106
+ u64,
107
+ ToString(
108
+ to_string : (fn(self : *(Self)) -> String)({
109
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
110
+ buffer_ptr := (&(buffer(0)));
111
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%llu", self.*);
112
+ String.from_cstr(buffer_ptr).unwrap()
113
+ })
114
+ )
115
+ );
116
+ impl(
117
+ usize,
118
+ ToString(
119
+ to_string : (fn(self : *(Self)) -> String)({
120
+ buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
121
+ buffer_ptr := (&(buffer(0)));
122
+ snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zu", self.*);
123
+ String.from_cstr(buffer_ptr).unwrap()
124
+ })
125
+ )
126
+ );
118
127
  // === Floating Point Implementations ===
119
-
120
128
  // Buffer size for float to string conversion
121
129
  // Scientific notation can be long, use generous buffer
122
130
  _FLOAT_BUFFER_SIZE :: usize(64);
123
-
124
- impl(f32, ToString(
125
- to_string : (fn(self: *(Self)) -> String)({
126
- buffer := Array(u8, _FLOAT_BUFFER_SIZE).fill(0);
127
- buffer_ptr := (&(buffer(0)));
128
- // Use %g for general format (removes trailing zeros)
129
- snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", f64(self.*));
130
- String.from_cstr(buffer_ptr).unwrap()
131
- })
132
- ));
133
-
134
- impl(f64, ToString(
135
- to_string : (fn(self: *(Self)) -> String)({
136
- buffer := Array(u8, _FLOAT_BUFFER_SIZE).fill(0);
137
- buffer_ptr := (&(buffer(0)));
138
- // Use %g for general format (removes trailing zeros)
139
- snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", self.*);
140
- String.from_cstr(buffer_ptr).unwrap()
141
- })
142
- ));
143
-
144
-
131
+ impl(
132
+ f32,
133
+ ToString(
134
+ to_string : (fn(self : *(Self)) -> String)({
135
+ buffer := Array(u8, _FLOAT_BUFFER_SIZE).fill(0);
136
+ buffer_ptr := (&(buffer(0)));
137
+ // Use %g for general format (removes trailing zeros)
138
+ snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", f64(self.*));
139
+ String.from_cstr(buffer_ptr).unwrap()
140
+ })
141
+ )
142
+ );
143
+ impl(
144
+ f64,
145
+ ToString(
146
+ to_string : (fn(self : *(Self)) -> String)({
147
+ buffer := Array(u8, _FLOAT_BUFFER_SIZE).fill(0);
148
+ buffer_ptr := (&(buffer(0)));
149
+ // Use %g for general format (removes trailing zeros)
150
+ snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", self.*);
151
+ String.from_cstr(buffer_ptr).unwrap()
152
+ })
153
+ )
154
+ );
145
155
  // === Boolean Implementation ===
146
-
147
- impl(bool, ToString(
148
- to_string : (fn(self: *(Self)) -> String)(
149
- cond(
150
- self.* => String.from("true"),
151
- true => String.from("false")
156
+ impl(
157
+ bool,
158
+ ToString(
159
+ to_string : (fn(self : *(Self)) -> String)(
160
+ cond(
161
+ self.* => String.from("true"),
162
+ true => String.from("false")
163
+ )
152
164
  )
153
165
  )
154
- ));
155
-
156
-
166
+ );
157
167
  // === Char Implementation ===
158
-
159
- impl(char, ToString(
160
- to_string : (fn(self: *(Self)) -> String)({
161
- // char is a single byte, create a 2-byte buffer (char + null)
162
- buffer := Array(u8, usize(2)).fill(0);
163
- buffer(0) = u8(self.*);
164
- buffer_ptr := (&(buffer(0)));
165
- String.from_cstr(buffer_ptr).unwrap()
166
- })
167
- ));
168
-
169
-
168
+ impl(
169
+ char,
170
+ ToString(
171
+ to_string : (fn(self : *(Self)) -> String)({
172
+ // char is a single byte, create a 2-byte buffer (char + null)
173
+ buffer := Array(u8, usize(2)).fill(0);
174
+ buffer(0) = u8(self.*);
175
+ buffer_ptr := (&(buffer(0)));
176
+ String.from_cstr(buffer_ptr).unwrap()
177
+ })
178
+ )
179
+ );
170
180
  // === String Implementation (identity) ===
171
-
172
- impl(String, ToString(
173
- to_string : (fn(self: *(Self)) -> String)(
174
- // String already is a string, just return a clone
175
- self.*
181
+ impl(
182
+ String,
183
+ ToString(
184
+ to_string : (fn(self : *(Self)) -> String)(
185
+ // String already is a string, just return a clone
186
+ self.*
187
+ )
176
188
  )
177
- ));
178
-
189
+ );
179
190
  // === str Implementation ===
180
- impl(str, ToString(
181
- to_string : (fn(self: *(Self)) -> String)({
182
- // Convert byte array to String assuming UTF-8
183
- return String.from(self.*);
184
- })
185
- ));
186
-
191
+ impl(
192
+ str,
193
+ ToString(
194
+ to_string : (fn(self : *(Self)) -> String)({
195
+ // Convert byte array to String assuming UTF-8
196
+ return(String.from(self.*));
197
+ })
198
+ )
199
+ );
187
200
  // === rune Implementation ===
188
201
  // Convert Unicode code point to UTF-8 string
189
-
190
- impl(rune, ToString(
191
- to_string : (fn(self: *(Self)) -> String)({
192
- // Get the code point value
193
- code := self.*.char;
194
-
195
- // UTF-8 encoding:
196
- // 0x00-0x7F: 1 byte
197
- // 0x80-0x7FF: 2 bytes
198
- // 0x800-0xFFFF: 3 bytes (excluding surrogates 0xD800-0xDFFF)
199
- // 0x10000-0x10FFFF: 4 bytes
200
- // Use 5-byte buffer to always have space for null terminator
201
-
202
- buffer := cond(
203
- (code <= 0x7F) => {
204
- // 1-byte encoding: 0xxxxxxx
205
- arr := Array(u8, usize(5)).fill(0);
206
- arr(0) = u8(code);
207
- arr
208
- },
209
- (code <= 0x7FF) => {
210
- // 2-byte encoding: 110xxxxx 10xxxxxx
211
- arr := Array(u8, usize(5)).fill(0);
212
- arr(0) = u8(u32(0xC0) | ((code >> 6) & 0x1F));
213
- arr(1) = u8(u32(0x80) | (code & 0x3F));
214
- arr
215
- },
216
- (code <= 0xFFFF) => {
217
- // 3-byte encoding: 1110xxxx 10xxxxxx 10xxxxxx
218
- arr := Array(u8, usize(5)).fill(0);
219
- arr(0) = u8(u32(0xE0) | ((code >> 12) & 0x0F));
220
- arr(1) = u8(u32(0x80) | ((code >> 6) & 0x3F));
221
- arr(2) = u8(u32(0x80) | (code & 0x3F));
222
- arr
223
- },
224
- true => {
225
- // 4-byte encoding: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
226
- arr := Array(u8, usize(5)).fill(0);
227
- arr(0) = u8(u32(0xF0) | ((code >> 18) & 0x07));
228
- arr(1) = u8(u32(0x80) | ((code >> 12) & 0x3F));
229
- arr(2) = u8(u32(0x80) | ((code >> 6) & 0x3F));
230
- arr(3) = u8(u32(0x80) | (code & 0x3F));
231
- arr
232
- }
233
- );
234
-
235
- buffer_ptr := (&(buffer(0)));
236
- String.from_cstr(buffer_ptr).unwrap()
237
- })
238
- ));
239
-
202
+ impl(
203
+ rune,
204
+ ToString(
205
+ to_string : (fn(self : *(Self)) -> String)({
206
+ // Get the code point value
207
+ code := self.*.char;
208
+ // UTF-8 encoding:
209
+ // 0x00-0x7F: 1 byte
210
+ // 0x80-0x7FF: 2 bytes
211
+ // 0x800-0xFFFF: 3 bytes (excluding surrogates 0xD800-0xDFFF)
212
+ // 0x10000-0x10FFFF: 4 bytes
213
+ // Use 5-byte buffer to always have space for null terminator
214
+ buffer := cond(
215
+ (code <= 0x7F) => {
216
+ // 1-byte encoding: 0xxxxxxx
217
+ arr := Array(u8, usize(5)).fill(0);
218
+ arr(0) = u8(code);
219
+ arr
220
+ },
221
+ (code <= 0x7FF) => {
222
+ // 2-byte encoding: 110xxxxx 10xxxxxx
223
+ arr := Array(u8, usize(5)).fill(0);
224
+ arr(0) = u8(u32(0xC0) | ((code >> 6) & 0x1F));
225
+ arr(1) = u8(u32(0x80) | (code & 0x3F));
226
+ arr
227
+ },
228
+ (code <= 0xFFFF) => {
229
+ // 3-byte encoding: 1110xxxx 10xxxxxx 10xxxxxx
230
+ arr := Array(u8, usize(5)).fill(0);
231
+ arr(0) = u8(u32(0xE0) | ((code >> 12) & 0x0F));
232
+ arr(1) = u8(u32(0x80) | ((code >> 6) & 0x3F));
233
+ arr(2) = u8(u32(0x80) | (code & 0x3F));
234
+ arr
235
+ },
236
+ true => {
237
+ // 4-byte encoding: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
238
+ arr := Array(u8, usize(5)).fill(0);
239
+ arr(0) = u8(u32(0xF0) | ((code >> 18) & 0x07));
240
+ arr(1) = u8(u32(0x80) | ((code >> 12) & 0x3F));
241
+ arr(2) = u8(u32(0x80) | ((code >> 6) & 0x3F));
242
+ arr(3) = u8(u32(0x80) | (code & 0x3F));
243
+ arr
244
+ }
245
+ );
246
+ buffer_ptr := (&(buffer(0)));
247
+ String.from_cstr(buffer_ptr).unwrap()
248
+ })
249
+ )
250
+ );
240
251
  // TODO: Array to_string implementation?
241
-
242
252
  // --- derive_rule for ToString ---
243
253
  // Local helpers (not exported from prelude, so defined locally)
244
- __ts_s2 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string) -> comptime(comptime_string))((a + b));
254
+ __ts_s2 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string) -> comptime(comptime_string))(a + b);
245
255
  __ts_s3 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string, comptime(c) : comptime_string) -> comptime(comptime_string))(
246
- ((a + b) + c)
256
+ (a + b) + c
247
257
  );
248
258
  __ts_s4 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string, comptime(c) : comptime_string, comptime(d) : comptime_string) -> comptime(comptime_string))(
249
- (((a + b) + c) + d)
259
+ ((a + b) + c) + d
250
260
  );
251
261
  __ts_s5 :: (fn(comptime(a) : comptime_string, comptime(b) : comptime_string, comptime(c) : comptime_string, comptime(d) : comptime_string, comptime(e) : comptime_string) -> comptime(comptime_string))(
252
- ((((a + b) + c) + d) + e)
262
+ (((a + b) + c) + d) + e
253
263
  );
254
- __ts_fold_range :: (fn(
255
- comptime(n) : usize,
256
- comptime(init) : comptime_string,
257
- comptime(f) : (fn(comptime(acc) : comptime_string, comptime(i) : usize) -> comptime(comptime_string))
258
- ) -> comptime(comptime_string))(
264
+ __ts_fold_range :: (
265
+ fn(
266
+ comptime(n) : usize,
267
+ comptime(init) : comptime_string,
268
+ comptime(f) : (fn(comptime(acc) : comptime_string, comptime(i) : usize) -> comptime(comptime_string))
269
+ ) -> comptime(comptime_string)
270
+ )(
259
271
  cond(
260
272
  (n == usize(0)) => init,
261
273
  (n == usize(1)) => f(init, usize(0)),
262
- true => f(recur((n - usize(1)), init, f), (n - usize(1)))
274
+ true => f(recur(n - usize(1), init, f), n - usize(1))
263
275
  )
264
276
  );
265
-
266
277
  // Helper: join string parts with + operator, left-to-right parenthesized
267
278
  __derive_join_plus :: (fn(comptime(parts) : comptime_string, comptime(new_part) : comptime_string) -> comptime(comptime_string))(
268
279
  cond(
@@ -270,108 +281,112 @@ __derive_join_plus :: (fn(comptime(parts) : comptime_string, comptime(new_part)
270
281
  true => __ts_s5("(", parts, " + ", new_part, ")")
271
282
  )
272
283
  );
273
-
274
- __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime(trait_params) : ComptimeList(Expr)) -> comptime(Expr))(
275
- {
276
- type_name :: Type.to_comptime_string(T);
277
- info :: Type.get_info(T);
278
- cond(
279
- info.is_struct() => {
280
- fields :: Type.get_struct_fields(T);
281
- fc :: fields.len();
282
- cond(
283
- (fc == 0) => {
284
- label :: __ts_s3("String.from(\"", type_name, "()\")");
285
- ctx.make_impl(quote(
284
+ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comptime(trait_params) : ComptimeList(Expr)) -> comptime(Expr))({
285
+ type_name :: Type.to_comptime_string(T);
286
+ info :: Type.get_info(T);
287
+ cond(
288
+ info.is_struct() => {
289
+ fields :: Type.get_struct_fields(T);
290
+ fc :: fields.len();
291
+ cond(
292
+ (fc == 0) => {
293
+ label :: __ts_s3("String.from(\"", type_name, "()\")");
294
+ ctx.make_impl(
295
+ quote(
286
296
  ToString(
287
297
  to_string : ((self) -> #(label.to_expr()))
288
298
  )
289
- ))
290
- },
291
- true => {
292
- body :: __ts_fold_range(fc, __ts_s3("String.from(\"", type_name, "(\")" ),
293
- (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
294
- {
295
- fname :: fields.get(fi).name;
296
- with_sep :: cond(
297
- (fi == 0) => a,
298
- true => __derive_join_plus(a, "String.from(\", \")")
299
- );
300
- __derive_join_plus(with_sep, __ts_s3("(&(self.*.", fname, ")).to_string()"))
301
- }
302
- )
303
- );
304
- full_body :: __derive_join_plus(body, "String.from(\")\")");
305
- ctx.make_impl(quote(
299
+ )
300
+ )
301
+ },
302
+ true => {
303
+ body :: __ts_fold_range(
304
+ fc,
305
+ __ts_s3("String.from(\"", type_name, "(\")"),
306
+ (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
307
+ fname :: fields.get(fi).name;
308
+ with_sep :: cond(
309
+ (fi == 0) => a,
310
+ true => __derive_join_plus(a, "String.from(\", \")")
311
+ );
312
+ __derive_join_plus(with_sep, __ts_s3("(&(self.*.", fname, ")).to_string()"))
313
+ })
314
+ );
315
+ full_body :: __derive_join_plus(body, "String.from(\")\")");
316
+ ctx.make_impl(
317
+ quote(
306
318
  ToString(
307
319
  to_string : ((self) -> #(full_body.to_expr()))
308
320
  )
309
- ))
310
- }
311
- )
312
- },
313
- info.is_enum() => {
314
- variants :: Type.get_enum_variants(T);
315
- vc :: variants.len();
316
- ts_branches :: __ts_fold_range(vc, "",
317
- (fn(comptime(acc) : comptime_string, comptime(vi) : usize) -> comptime(comptime_string))(
318
- {
319
- v :: variants.get(vi);
320
- branch :: cond(
321
- (v.fields.len() == 0) =>
322
- __ts_s5(".", v.name, " => String.from(\"", type_name, __ts_s3(".", v.name, "\")")),
323
- true => {
324
- bindings :: __ts_fold_range(v.fields.len(), "",
325
- (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
326
- {
327
- fname :: v.fields.get(fi).name;
328
- cond(
329
- (a == "") => __ts_s2("__v_", fname),
330
- true => __ts_s3(a, ", __v_", fname)
331
- )
332
- }
333
- )
334
- );
335
- body :: __ts_fold_range(v.fields.len(),
336
- __ts_s5("String.from(\"", type_name, ".", v.name, "(\")" ),
337
- (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))(
338
- {
339
- fname :: v.fields.get(fi).name;
340
- with_sep :: cond(
341
- (fi == 0) => a,
342
- true => __derive_join_plus(a, "String.from(\", \")")
343
- );
344
- __derive_join_plus(with_sep, __ts_s3("(&(__v_", fname, ")).to_string()"))
345
- }
346
- )
321
+ )
322
+ )
323
+ }
324
+ )
325
+ },
326
+ info.is_enum() => {
327
+ variants :: Type.get_enum_variants(T);
328
+ vc :: variants.len();
329
+ ts_branches :: __ts_fold_range(
330
+ vc,
331
+ "",
332
+ (fn(comptime(acc) : comptime_string, comptime(vi) : usize) -> comptime(comptime_string))({
333
+ v :: variants.get(vi);
334
+ branch :: cond(
335
+ (v.fields.len() == 0) =>
336
+ __ts_s5(".", v.name, " => String.from(\"", type_name, __ts_s3(".", v.name, "\")")),
337
+ true => {
338
+ bindings :: __ts_fold_range(
339
+ v.fields.len(),
340
+ "",
341
+ (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
342
+ fname :: v.fields.get(fi).name;
343
+ cond(
344
+ (a == "") => __ts_s2("__v_", fname),
345
+ true => __ts_s3(a, ", __v_", fname)
346
+ )
347
+ })
348
+ );
349
+ body :: __ts_fold_range(
350
+ v.fields.len(),
351
+ __ts_s5("String.from(\"", type_name, ".", v.name, "(\")"),
352
+ (fn(comptime(a) : comptime_string, comptime(fi) : usize) -> comptime(comptime_string))({
353
+ fname :: v.fields.get(fi).name;
354
+ with_sep :: cond(
355
+ (fi == 0) => a,
356
+ true => __derive_join_plus(a, "String.from(\", \")")
347
357
  );
348
- full_body :: __derive_join_plus(body, "String.from(\")\")");
349
- __ts_s4(".", v.name, __ts_s3("(", bindings, ") => "), full_body)
350
- }
358
+ __derive_join_plus(with_sep, __ts_s3("(&(__v_", fname, ")).to_string()"))
359
+ })
351
360
  );
352
- cond(
353
- (acc == "") => branch,
354
- true => __ts_s3(acc, ",\n ", branch)
355
- )
361
+ full_body :: __derive_join_plus(body, "String.from(\")\")");
362
+ __ts_s4(".", v.name, __ts_s3("(", bindings, ") => "), full_body)
356
363
  }
364
+ );
365
+ cond(
366
+ (acc == "") => branch,
367
+ true => __ts_s3(acc, ",\n ", branch)
357
368
  )
358
- );
359
- match_body :: __ts_s3("match(self.*,\n ", ts_branches, "\n )");
360
- ctx.make_impl(quote(
369
+ })
370
+ );
371
+ match_body :: __ts_s3("match(self.*,\n ", ts_branches, "\n )");
372
+ ctx.make_impl(
373
+ quote(
361
374
  ToString(
362
375
  to_string : ((self) -> #(match_body.to_expr()))
363
376
  )
364
- ))
365
- },
366
- true => {
367
- label :: __ts_s3("String.from(\"", type_name, "\")");
368
- ctx.make_impl(quote(
377
+ )
378
+ )
379
+ },
380
+ true => {
381
+ label :: __ts_s3("String.from(\"", type_name, "\")");
382
+ ctx.make_impl(
383
+ quote(
369
384
  ToString(
370
385
  to_string : ((self) -> #(label.to_expr()))
371
386
  )
372
- ))
373
- }
374
- )
375
- }
376
- );
377
- derive_rule(ToString, __derive_tostring);
387
+ )
388
+ )
389
+ }
390
+ )
391
+ });
392
+ derive_rule(ToString, __derive_tostring);