@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.
- package/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +34 -34
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +26 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +73 -60
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +456 -438
- package/out/cjs/yo-cli.cjs +576 -543
- package/out/cjs/yo-lsp.cjs +559 -532
- package/out/esm/index.mjs +281 -263
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +7 -15
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6304 -4315
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- package/std/worker.yo +6 -10
package/std/fmt/to_string.yo
CHANGED
|
@@ -1,268 +1,279 @@
|
|
|
1
1
|
//! ToString trait and implementations for all primitive types.
|
|
2
|
-
|
|
3
|
-
{
|
|
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
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
));
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
));
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
));
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
));
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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))(
|
|
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
|
-
(
|
|
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
|
-
((
|
|
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
|
-
(((
|
|
262
|
+
(((a + b) + c) + d) + e
|
|
253
263
|
);
|
|
254
|
-
__ts_fold_range :: (
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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(
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
info
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
fc
|
|
282
|
-
|
|
283
|
-
(
|
|
284
|
-
|
|
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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
)
|
|
303
|
-
)
|
|
304
|
-
|
|
305
|
-
|
|
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
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
)
|
|
334
|
-
)
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
358
|
+
__derive_join_plus(with_sep, __ts_s3("(&(__v_", fname, ")).to_string()"))
|
|
359
|
+
})
|
|
351
360
|
);
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
360
|
-
|
|
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
|
-
|
|
367
|
-
|
|
368
|
-
|
|
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);
|