@shd101wyy/yo 0.1.33 → 0.1.34
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-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +51 -21
- package/.github/skills/yo-syntax/SKILL.md +1 -1
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +56 -67
- package/out/cjs/index.cjs +664 -649
- package/out/cjs/yo-cli.cjs +736 -721
- package/out/cjs/yo-lsp.cjs +682 -667
- package/out/esm/index.mjs +498 -483
- package/out/types/src/codegen/exprs/comptime-value.d.ts +2 -1
- package/out/types/src/codegen/types/generation.d.ts +1 -1
- package/out/types/src/codegen/utils/index.d.ts +2 -4
- package/out/types/src/evaluator/exprs/_expr.d.ts +1 -0
- package/out/types/src/evaluator/types/flowability.d.ts +21 -0
- package/out/types/src/expr.d.ts +4 -13
- package/out/types/src/types/creators.d.ts +2 -3
- package/out/types/src/types/definitions.d.ts +2 -3
- package/out/types/src/types/guards.d.ts +2 -2
- package/out/types/src/types/tags.d.ts +2 -2
- package/out/types/src/value-tag.d.ts +0 -1
- package/out/types/src/value.d.ts +2 -11
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +5 -3
- package/std/build.yo +46 -46
- package/std/collections/array_list.yo +73 -66
- package/std/collections/hash_map.yo +2 -81
- package/std/collections/list_view.yo +77 -0
- package/std/crypto/random.yo +5 -5
- package/std/encoding/base64.yo +12 -13
- package/std/encoding/hex.yo +6 -6
- package/std/encoding/json.yo +6 -5
- package/std/encoding/utf16.yo +9 -9
- package/std/env.yo +8 -8
- package/std/fmt/to_string.yo +12 -12
- package/std/http/client.yo +1 -1
- package/std/imm/list.yo +4 -3
- package/std/imm/map.yo +3 -2
- package/std/imm/set.yo +4 -3
- package/std/imm/sorted_map.yo +3 -2
- package/std/imm/sorted_set.yo +4 -3
- package/std/imm/string.yo +12 -5
- package/std/imm/vec.yo +8 -3
- package/std/prelude.yo +174 -401
- package/std/string/string.yo +198 -71
- package/std/url/index.yo +26 -26
- package/out/types/src/evaluator/types/slice.d.ts +0 -8
package/std/encoding/base64.yo
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
//! { base64_encode, base64_decode } :: import "std/encoding/base64";
|
|
7
7
|
//!
|
|
8
8
|
//! s := base64_encode(data);
|
|
9
|
-
//! b := base64_decode(s.
|
|
9
|
+
//! b := base64_decode(s.to_string()).unwrap();
|
|
10
10
|
//! ```
|
|
11
11
|
open(import("../string"));
|
|
12
12
|
{ ArrayList } :: import("../collections/array_list");
|
|
@@ -76,26 +76,26 @@ _decode_char :: (fn(c : u8, alpha : str, exn : Exception) -> u8)({
|
|
|
76
76
|
});
|
|
77
77
|
exn.throw(dyn(EncodingError.InvalidChar(c)))
|
|
78
78
|
});
|
|
79
|
-
_decode_with :: (fn(s :
|
|
79
|
+
_decode_with :: (fn(s : String, alpha : str, exn : Exception) -> ArrayList(u8))({
|
|
80
80
|
// Strip trailing padding
|
|
81
|
-
len := s.
|
|
82
|
-
while((len > usize(0)) && (s.
|
|
81
|
+
len := s.bytes_len();
|
|
82
|
+
while((len > usize(0)) && (s.byte_at(len - usize(1)) == _PAD), len = (len - usize(1)), ());
|
|
83
83
|
out := ArrayList(u8).with_capacity((len * usize(3)) / usize(4));
|
|
84
84
|
i := usize(0);
|
|
85
85
|
while(i < len, i = (i + usize(4)), {
|
|
86
|
-
c0 := _decode_char(s.
|
|
86
|
+
c0 := _decode_char(s.byte_at(i), alpha, exn);
|
|
87
87
|
c1 := cond(
|
|
88
|
-
((i + usize(1)) < len) => _decode_char(s.
|
|
88
|
+
((i + usize(1)) < len) => _decode_char(s.byte_at(i + usize(1)), alpha, exn),
|
|
89
89
|
true => u8(0)
|
|
90
90
|
);
|
|
91
91
|
out.push((c0 << u8(2)) | (c1 >> u8(4)));
|
|
92
92
|
cond(
|
|
93
93
|
((i + usize(2)) < len) => {
|
|
94
|
-
c2 := _decode_char(s.
|
|
94
|
+
c2 := _decode_char(s.byte_at(i + usize(2)), alpha, exn);
|
|
95
95
|
out.push(((c1 & u8(15)) << u8(4)) | (c2 >> u8(2)));
|
|
96
96
|
cond(
|
|
97
97
|
((i + usize(3)) < len) => {
|
|
98
|
-
c3 := _decode_char(s.
|
|
98
|
+
c3 := _decode_char(s.byte_at(i + usize(3)), alpha, exn);
|
|
99
99
|
out.push(((c2 & u8(3)) << u8(6)) | c3);
|
|
100
100
|
},
|
|
101
101
|
true => ()
|
|
@@ -107,11 +107,11 @@ _decode_with :: (fn(s : str, alpha : str, exn : Exception) -> ArrayList(u8))({
|
|
|
107
107
|
out
|
|
108
108
|
});
|
|
109
109
|
/// Decode a standard base64 string to bytes. Throws via `Exception` on invalid input.
|
|
110
|
-
base64_decode :: (fn(s :
|
|
110
|
+
base64_decode :: (fn(s : String, exn : Exception) -> ArrayList(u8))(
|
|
111
111
|
_decode_with(s, _STD_ALPHA, exn)
|
|
112
112
|
);
|
|
113
113
|
/// Decode a URL-safe base64 string to bytes. Throws via `Exception` on invalid input.
|
|
114
|
-
base64_decode_url :: (fn(s :
|
|
114
|
+
base64_decode_url :: (fn(s : String, exn : Exception) -> ArrayList(u8))(
|
|
115
115
|
_decode_with(s, _URL_ALPHA, exn)
|
|
116
116
|
);
|
|
117
117
|
export(base64_decode, base64_decode_url);
|
|
@@ -138,11 +138,10 @@ _base64_index :: (fn(c : u8) -> i32)({
|
|
|
138
138
|
});
|
|
139
139
|
/// Decode a base64 `String`, ignoring whitespace. Returns `Result(String, String)`.
|
|
140
140
|
base64_decode_string :: (fn(input : String) -> Result(String, String))({
|
|
141
|
-
raw := input.as_str();
|
|
142
141
|
cleaned := ArrayList(u8).new();
|
|
143
142
|
i := usize(0);
|
|
144
|
-
while(i <
|
|
145
|
-
ch :=
|
|
143
|
+
while(i < input.bytes_len(), i = (i + usize(1)), {
|
|
144
|
+
ch := input.byte_at(i);
|
|
146
145
|
cond(
|
|
147
146
|
(((ch == u8(32)) || (ch == u8(9))) || ((ch == u8(10)) || (ch == u8(13)))) => (),
|
|
148
147
|
true => {
|
package/std/encoding/hex.yo
CHANGED
|
@@ -76,18 +76,18 @@ _hex_nibble :: (fn(c : u8, exn : Exception) -> u8)(
|
|
|
76
76
|
)
|
|
77
77
|
);
|
|
78
78
|
/// Decode a hexadecimal string to bytes. Throws via `Exception` on invalid input.
|
|
79
|
-
hex_decode :: (fn(s :
|
|
79
|
+
hex_decode :: (fn(s : String, exn : Exception) -> ArrayList(u8))({
|
|
80
80
|
cond(
|
|
81
|
-
((s.
|
|
81
|
+
((s.bytes_len() % usize(2)) != usize(0)) => {
|
|
82
82
|
exn.throw(dyn(EncodingError.OddLength));
|
|
83
83
|
},
|
|
84
84
|
true => ()
|
|
85
85
|
);
|
|
86
|
-
out := ArrayList(u8).with_capacity(s.
|
|
86
|
+
out := ArrayList(u8).with_capacity(s.bytes_len() / usize(2));
|
|
87
87
|
i := usize(0);
|
|
88
|
-
while(i < s.
|
|
89
|
-
hi := _hex_nibble(s.
|
|
90
|
-
lo := _hex_nibble(s.
|
|
88
|
+
while(i < s.bytes_len(), i = (i + usize(2)), {
|
|
89
|
+
hi := _hex_nibble(s.byte_at(i), exn);
|
|
90
|
+
lo := _hex_nibble(s.byte_at(i + usize(1)), exn);
|
|
91
91
|
out.push((hi << u8(4)) | lo);
|
|
92
92
|
});
|
|
93
93
|
out
|
package/std/encoding/json.yo
CHANGED
|
@@ -173,9 +173,9 @@ impl(
|
|
|
173
173
|
if(keys(i) == key, {
|
|
174
174
|
// SAFETY: `i` is bounded by `keys.len()` and the
|
|
175
175
|
// Object variant invariant says `values.len() ==
|
|
176
|
-
// keys.len()`, so
|
|
177
|
-
//
|
|
178
|
-
return(unsafe(&(values
|
|
176
|
+
// keys.len()`, so `&(values(i))` (Index trait) yields a
|
|
177
|
+
// valid pointer into the Rc-managed values list.
|
|
178
|
+
return(unsafe(&(values(i))));
|
|
179
179
|
});
|
|
180
180
|
});
|
|
181
181
|
panic("JsonValue: key not found in Object")
|
|
@@ -198,8 +198,9 @@ impl(
|
|
|
198
198
|
assert(idx < items.len(), "JsonValue: Array index out of bounds");
|
|
199
199
|
// SAFETY: assert above bounds `idx`; `items` is the
|
|
200
200
|
// Rc-managed Array list whose storage outlives this call
|
|
201
|
-
// (ref(self) keeps `self` and therefore `items` alive)
|
|
202
|
-
|
|
201
|
+
// (ref(self) keeps `self` and therefore `items` alive);
|
|
202
|
+
// `&(items(idx))` (Index trait) yields the element pointer.
|
|
203
|
+
return(unsafe(&(items(idx))));
|
|
203
204
|
},
|
|
204
205
|
_ => panic("JsonValue: index by usize requires .Array variant")
|
|
205
206
|
)
|
package/std/encoding/utf16.yo
CHANGED
|
@@ -20,34 +20,34 @@ open(import("../string"));
|
|
|
20
20
|
/// Convert a UTF-8 string to an `ArrayList(u16)` of UTF-16 code units.
|
|
21
21
|
///
|
|
22
22
|
/// Handles BMP characters directly and encodes supplementary characters as surrogate pairs.
|
|
23
|
-
utf8_to_utf16 :: (fn(s :
|
|
23
|
+
utf8_to_utf16 :: (fn(s : String) -> ArrayList(u16))({
|
|
24
24
|
out := ArrayList(u16).new();
|
|
25
25
|
i := usize(0);
|
|
26
|
-
while(i < s.
|
|
27
|
-
b0 := u32(s.
|
|
26
|
+
while(i < s.bytes_len(), {
|
|
27
|
+
b0 := u32(s.byte_at(i));
|
|
28
28
|
cond(
|
|
29
29
|
(b0 < u32(0x80)) => {
|
|
30
30
|
out.push(u16(b0));
|
|
31
31
|
i = (i + usize(1));
|
|
32
32
|
},
|
|
33
33
|
((b0 & u32(0xE0)) == u32(0xC0)) => {
|
|
34
|
-
b1 := u32(s.
|
|
34
|
+
b1 := u32(s.byte_at(i + usize(1)));
|
|
35
35
|
cp := (((b0 & u32(0x1F)) << u32(6)) | (b1 & u32(0x3F)));
|
|
36
36
|
out.push(u16(cp));
|
|
37
37
|
i = (i + usize(2));
|
|
38
38
|
},
|
|
39
39
|
((b0 & u32(0xF0)) == u32(0xE0)) => {
|
|
40
|
-
b1 := u32(s.
|
|
41
|
-
b2 := u32(s.
|
|
40
|
+
b1 := u32(s.byte_at(i + usize(1)));
|
|
41
|
+
b2 := u32(s.byte_at(i + usize(2)));
|
|
42
42
|
cp := ((((b0 & u32(0x0F)) << u32(12)) | ((b1 & u32(0x3F)) << u32(6))) | (b2 & u32(0x3F)));
|
|
43
43
|
out.push(u16(cp));
|
|
44
44
|
i = (i + usize(3));
|
|
45
45
|
},
|
|
46
46
|
true => {
|
|
47
47
|
// 4-byte sequence → surrogate pair
|
|
48
|
-
b1 := u32(s.
|
|
49
|
-
b2 := u32(s.
|
|
50
|
-
b3 := u32(s.
|
|
48
|
+
b1 := u32(s.byte_at(i + usize(1)));
|
|
49
|
+
b2 := u32(s.byte_at(i + usize(2)));
|
|
50
|
+
b3 := u32(s.byte_at(i + usize(3)));
|
|
51
51
|
cp := (((((b0 & u32(0x07)) << u32(18)) | ((b1 & u32(0x3F)) << u32(12))) | ((b2 & u32(0x3F)) << u32(6))) | (b3 & u32(0x3F)));
|
|
52
52
|
cp2 := (cp - u32(0x10000));
|
|
53
53
|
hi := u16((cp2 >> u32(10)) + u32(0xD800));
|
package/std/env.yo
CHANGED
|
@@ -14,14 +14,14 @@ open(import("./path"));
|
|
|
14
14
|
extern(
|
|
15
15
|
"Yo",
|
|
16
16
|
__yo_argc : i32,
|
|
17
|
-
__yo_argv : *(*(u8))
|
|
18
|
-
__yo_args : [*(u8)]
|
|
17
|
+
__yo_argv : *(*(u8))
|
|
19
18
|
);
|
|
20
19
|
// === Raw command-line arguments ===
|
|
21
|
-
/// Get raw command-line arguments as a
|
|
22
|
-
/// The first element is the
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
/// Get raw command-line arguments as a RawSlice of C strings (privileged:
|
|
21
|
+
/// callers must be pragma'd to name RawSlice). The first element is the
|
|
22
|
+
/// program name.
|
|
23
|
+
raw_args :: (fn() -> RawSlice(*(u8)))({
|
|
24
|
+
return(RawSlice(*(u8))(ptr : __yo_argv, len : usize(__yo_argc)));
|
|
25
25
|
});
|
|
26
26
|
export(raw_args);
|
|
27
27
|
/// Get the number of command-line arguments (including program name).
|
|
@@ -38,12 +38,12 @@ export(argv);
|
|
|
38
38
|
/// Get command-line arguments as an `ArrayList(String)`.
|
|
39
39
|
/// The first element is the program name.
|
|
40
40
|
args :: (fn() -> ArrayList(String))({
|
|
41
|
-
raw := raw_args();
|
|
42
41
|
i := usize(0);
|
|
43
42
|
len := usize(argc());
|
|
44
43
|
result := ArrayList(String).with_capacity(len);
|
|
45
44
|
while(i < len, {
|
|
46
|
-
|
|
45
|
+
// SAFETY: i < argc; argv is the C runtime's argument vector.
|
|
46
|
+
arg_ptr := unsafe((__yo_argv &+ i).*);
|
|
47
47
|
arg_str := String.from_cstr(arg_ptr).unwrap();
|
|
48
48
|
result.push(arg_str);
|
|
49
49
|
i = (i + 1);
|
package/std/fmt/to_string.yo
CHANGED
|
@@ -252,22 +252,22 @@ impl(
|
|
|
252
252
|
// TODO: Array to_string implementation?
|
|
253
253
|
// --- derive_rule for ToString ---
|
|
254
254
|
// Local helpers (not exported from prelude, so defined locally)
|
|
255
|
-
__ts_s2 :: (fn(comptime(a) :
|
|
256
|
-
__ts_s3 :: (fn(comptime(a) :
|
|
255
|
+
__ts_s2 :: (fn(comptime(a) : comptime_str, comptime(b) : comptime_str) -> comptime(comptime_str))(a + b);
|
|
256
|
+
__ts_s3 :: (fn(comptime(a) : comptime_str, comptime(b) : comptime_str, comptime(c) : comptime_str) -> comptime(comptime_str))(
|
|
257
257
|
(a + b) + c
|
|
258
258
|
);
|
|
259
|
-
__ts_s4 :: (fn(comptime(a) :
|
|
259
|
+
__ts_s4 :: (fn(comptime(a) : comptime_str, comptime(b) : comptime_str, comptime(c) : comptime_str, comptime(d) : comptime_str) -> comptime(comptime_str))(
|
|
260
260
|
((a + b) + c) + d
|
|
261
261
|
);
|
|
262
|
-
__ts_s5 :: (fn(comptime(a) :
|
|
262
|
+
__ts_s5 :: (fn(comptime(a) : comptime_str, comptime(b) : comptime_str, comptime(c) : comptime_str, comptime(d) : comptime_str, comptime(e) : comptime_str) -> comptime(comptime_str))(
|
|
263
263
|
(((a + b) + c) + d) + e
|
|
264
264
|
);
|
|
265
265
|
__ts_fold_range :: (
|
|
266
266
|
fn(
|
|
267
267
|
comptime(n) : usize,
|
|
268
|
-
comptime(init) :
|
|
269
|
-
comptime(f) : (fn(comptime(acc) :
|
|
270
|
-
) -> comptime(
|
|
268
|
+
comptime(init) : comptime_str,
|
|
269
|
+
comptime(f) : (fn(comptime(acc) : comptime_str, comptime(i) : usize) -> comptime(comptime_str))
|
|
270
|
+
) -> comptime(comptime_str)
|
|
271
271
|
)(
|
|
272
272
|
cond(
|
|
273
273
|
(n == usize(0)) => init,
|
|
@@ -276,7 +276,7 @@ __ts_fold_range :: (
|
|
|
276
276
|
)
|
|
277
277
|
);
|
|
278
278
|
// Helper: join string parts with + operator, left-to-right parenthesized
|
|
279
|
-
__derive_join_plus :: (fn(comptime(parts) :
|
|
279
|
+
__derive_join_plus :: (fn(comptime(parts) : comptime_str, comptime(new_part) : comptime_str) -> comptime(comptime_str))(
|
|
280
280
|
cond(
|
|
281
281
|
(parts == "") => new_part,
|
|
282
282
|
true => __ts_s5("(", parts, " + ", new_part, ")")
|
|
@@ -304,7 +304,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
|
|
|
304
304
|
body :: __ts_fold_range(
|
|
305
305
|
fc,
|
|
306
306
|
__ts_s3("String.from(\"", type_name, "(\")"),
|
|
307
|
-
(fn(comptime(a) :
|
|
307
|
+
(fn(comptime(a) : comptime_str, comptime(fi) : usize) -> comptime(comptime_str))({
|
|
308
308
|
fname :: fields.get(fi).name;
|
|
309
309
|
with_sep :: cond(
|
|
310
310
|
(fi == 0) => a,
|
|
@@ -330,7 +330,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
|
|
|
330
330
|
ts_branches :: __ts_fold_range(
|
|
331
331
|
vc,
|
|
332
332
|
"",
|
|
333
|
-
(fn(comptime(acc) :
|
|
333
|
+
(fn(comptime(acc) : comptime_str, comptime(vi) : usize) -> comptime(comptime_str))({
|
|
334
334
|
v :: variants.get(vi);
|
|
335
335
|
branch :: cond(
|
|
336
336
|
(v.fields.len() == 0) =>
|
|
@@ -339,7 +339,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
|
|
|
339
339
|
bindings :: __ts_fold_range(
|
|
340
340
|
v.fields.len(),
|
|
341
341
|
"",
|
|
342
|
-
(fn(comptime(a) :
|
|
342
|
+
(fn(comptime(a) : comptime_str, comptime(fi) : usize) -> comptime(comptime_str))({
|
|
343
343
|
fname :: v.fields.get(fi).name;
|
|
344
344
|
cond(
|
|
345
345
|
(a == "") => __ts_s2("__v_", fname),
|
|
@@ -350,7 +350,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
|
|
|
350
350
|
body :: __ts_fold_range(
|
|
351
351
|
v.fields.len(),
|
|
352
352
|
__ts_s5("String.from(\"", type_name, ".", v.name, "(\")"),
|
|
353
|
-
(fn(comptime(a) :
|
|
353
|
+
(fn(comptime(a) : comptime_str, comptime(fi) : usize) -> comptime(comptime_str))({
|
|
354
354
|
fname :: v.fields.get(fi).name;
|
|
355
355
|
with_sep :: cond(
|
|
356
356
|
(fi == 0) => a,
|
package/std/http/client.yo
CHANGED
package/std/imm/list.yo
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
//! ```
|
|
18
18
|
pragma(Pragma.AllowUnsafe);
|
|
19
19
|
{ GlobalAllocator } :: import("../allocator.yo");
|
|
20
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
20
21
|
{ malloc, free } :: GlobalAllocator;
|
|
21
22
|
{ memcpy } :: import("../libc/string.yo");
|
|
22
23
|
/// Internal cons cell — atomic object for thread-safe structural sharing.
|
|
@@ -197,12 +198,12 @@ impl(
|
|
|
197
198
|
false
|
|
198
199
|
}),
|
|
199
200
|
/// Build a list from a slice. O(n).
|
|
200
|
-
|
|
201
|
+
from_list : (fn(l : ArrayList(T)) -> Self)({
|
|
201
202
|
(result : Self) = Self.new();
|
|
202
|
-
(i : usize) =
|
|
203
|
+
(i : usize) = l.len();
|
|
203
204
|
while(i > usize(0), {
|
|
204
205
|
i = (i - usize(1));
|
|
205
|
-
result = result.prepend(
|
|
206
|
+
result = result.prepend(match(l.get(i),.Some(__e) => __e,.None => panic("from_list: index out of bounds")));
|
|
206
207
|
});
|
|
207
208
|
result
|
|
208
209
|
})
|
package/std/imm/map.yo
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
//! ```
|
|
20
20
|
pragma(Pragma.AllowUnsafe);
|
|
21
21
|
{ GlobalAllocator } :: import("../allocator.yo");
|
|
22
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
22
23
|
{ malloc, free } :: GlobalAllocator;
|
|
23
24
|
/// Number of bits per HAMT level.
|
|
24
25
|
BITS :: usize(5);
|
|
@@ -948,12 +949,12 @@ impl(
|
|
|
948
949
|
result
|
|
949
950
|
}),
|
|
950
951
|
/// Create a map from a slice of key-value pairs.
|
|
951
|
-
from_entries : (fn(pairs :
|
|
952
|
+
from_entries : (fn(pairs : ArrayList(Pair(K, V))) -> Self)({
|
|
952
953
|
(result : Self) = Self.new();
|
|
953
954
|
i := usize(0);
|
|
954
955
|
plen := pairs.len();
|
|
955
956
|
while(i < plen, i = (i + usize(1)), {
|
|
956
|
-
p := (pairs.
|
|
957
|
+
p := match(pairs.get(i),.Some(__e) => __e,.None => panic("from_entries: index out of bounds"));
|
|
957
958
|
result = result.insert(p.key, p.value);
|
|
958
959
|
});
|
|
959
960
|
result
|
package/std/imm/set.yo
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
//! ```
|
|
20
20
|
pragma(Pragma.AllowUnsafe);
|
|
21
21
|
{ Map } :: import("./map.yo");
|
|
22
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
22
23
|
{ List } :: import("./list.yo");
|
|
23
24
|
/// Persistent immutable hash set backed by a HAMT.
|
|
24
25
|
///
|
|
@@ -152,12 +153,12 @@ impl(
|
|
|
152
153
|
self._inner.keys()
|
|
153
154
|
),
|
|
154
155
|
/// Create a set from a slice of elements.
|
|
155
|
-
|
|
156
|
+
from_list : (fn(l : ArrayList(T)) -> Self)({
|
|
156
157
|
(result : Self) = Self.new();
|
|
157
158
|
i := usize(0);
|
|
158
|
-
slen :=
|
|
159
|
+
slen := l.len();
|
|
159
160
|
while(i < slen, i = (i + usize(1)), {
|
|
160
|
-
result = result.insert((
|
|
161
|
+
result = result.insert(match(l.get(i),.Some(__e) => __e,.None => panic("from_list: index out of bounds")));
|
|
161
162
|
});
|
|
162
163
|
result
|
|
163
164
|
})
|
package/std/imm/sorted_map.yo
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
//! ```
|
|
20
20
|
pragma(Pragma.AllowUnsafe);
|
|
21
21
|
{ List } :: import("./list.yo");
|
|
22
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
22
23
|
{ Pair } :: import("./map.yo");
|
|
23
24
|
/// Color of a red-black tree node.
|
|
24
25
|
Color :: enum(Red, Black);
|
|
@@ -614,12 +615,12 @@ impl(
|
|
|
614
615
|
_collect_values(K, V, self._root, List(V).new())
|
|
615
616
|
),
|
|
616
617
|
/// Create a sorted map from a slice of key-value pairs.
|
|
617
|
-
from_entries : (fn(pairs :
|
|
618
|
+
from_entries : (fn(pairs : ArrayList(Pair(K, V))) -> Self)({
|
|
618
619
|
(result : Self) = Self.new();
|
|
619
620
|
i := usize(0);
|
|
620
621
|
plen := pairs.len();
|
|
621
622
|
while(i < plen, i = (i + usize(1)), {
|
|
622
|
-
p := (pairs.
|
|
623
|
+
p := match(pairs.get(i),.Some(__e) => __e,.None => panic("from_entries: index out of bounds"));
|
|
623
624
|
result = result.insert(p.key, p.value);
|
|
624
625
|
});
|
|
625
626
|
result
|
package/std/imm/sorted_set.yo
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
//! ```
|
|
20
20
|
pragma(Pragma.AllowUnsafe);
|
|
21
21
|
{ SortedMap } :: import("./sorted_map.yo");
|
|
22
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
22
23
|
{ List } :: import("./list.yo");
|
|
23
24
|
/// Persistent immutable sorted set backed by a left-leaning red-black tree.
|
|
24
25
|
SortedSet :: (fn(comptime(T) : Type, where(T <: (Eq(T), Ord(T), Send))) -> comptime(Type))(
|
|
@@ -174,12 +175,12 @@ impl(
|
|
|
174
175
|
result
|
|
175
176
|
}),
|
|
176
177
|
/// Create a sorted set from a slice of elements.
|
|
177
|
-
|
|
178
|
+
from_list : (fn(l : ArrayList(T)) -> Self)({
|
|
178
179
|
(result : Self) = Self.new();
|
|
179
180
|
i := usize(0);
|
|
180
|
-
slen :=
|
|
181
|
+
slen := l.len();
|
|
181
182
|
while(i < slen, i = (i + usize(1)), {
|
|
182
|
-
result = result.insert((
|
|
183
|
+
result = result.insert(match(l.get(i),.Some(__e) => __e,.None => panic("from_list: index out of bounds")));
|
|
183
184
|
});
|
|
184
185
|
result
|
|
185
186
|
})
|
package/std/imm/string.yo
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//! All "modification" operations return a new string; the original is unchanged.
|
|
5
5
|
pragma(Pragma.AllowUnsafe);
|
|
6
6
|
{ GlobalAllocator } :: import("../allocator.yo");
|
|
7
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
7
8
|
{ malloc, free } :: GlobalAllocator;
|
|
8
9
|
{ memcpy, memcmp } :: import("../libc/string.yo");
|
|
9
10
|
{ rune } :: import("../string/rune.yo");
|
|
@@ -68,8 +69,8 @@ impl(
|
|
|
68
69
|
return(Self(_ptr :.None, _len : usize(0), _capacity : usize(0)));
|
|
69
70
|
});
|
|
70
71
|
ptr := _alloc_bytes(slen);
|
|
71
|
-
|
|
72
|
-
unsafe(memcpy(*(void)(ptr), *(void)(
|
|
72
|
+
rb := s.raw_bytes();
|
|
73
|
+
unsafe(memcpy(*(void)(ptr), *(void)(rb.ptr), slen));
|
|
73
74
|
return(Self(_ptr :.Some(ptr), _len : slen, _capacity : slen));
|
|
74
75
|
}),
|
|
75
76
|
/// Byte length of the string.
|
|
@@ -722,9 +723,15 @@ impl(
|
|
|
722
723
|
impl(
|
|
723
724
|
String,
|
|
724
725
|
ToString(
|
|
725
|
-
to_string : (fn(ref(self) : Self) -> std_string.String)(
|
|
726
|
-
|
|
727
|
-
|
|
726
|
+
to_string : (fn(ref(self) : Self) -> std_string.String)({
|
|
727
|
+
bytes := ArrayList(u8).new();
|
|
728
|
+
match(
|
|
729
|
+
self._ptr,
|
|
730
|
+
.None => (),
|
|
731
|
+
.Some(p) => bytes.extend_from_ptr(p, self._len)
|
|
732
|
+
);
|
|
733
|
+
std_string.String.from_bytes(bytes)
|
|
734
|
+
})
|
|
728
735
|
)
|
|
729
736
|
);
|
|
730
737
|
export(String);
|
package/std/imm/vec.yo
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
//! ```
|
|
19
19
|
pragma(Pragma.AllowUnsafe);
|
|
20
20
|
{ GlobalAllocator } :: import("../allocator.yo");
|
|
21
|
+
{ ArrayList } :: import("../collections/array_list.yo");
|
|
21
22
|
{ malloc, free } :: GlobalAllocator;
|
|
22
23
|
/// Persistent immutable vector.
|
|
23
24
|
///
|
|
@@ -412,15 +413,19 @@ impl(
|
|
|
412
413
|
Vec(V)(_ptr : new_ptr, _len : min_len, _cap : min_len)
|
|
413
414
|
}),
|
|
414
415
|
/// Create a vector from a slice, copying all elements.
|
|
415
|
-
|
|
416
|
-
slen :=
|
|
416
|
+
from_list : (fn(l : ArrayList(T)) -> Self)({
|
|
417
|
+
slen := l.len();
|
|
417
418
|
cap := cond(
|
|
418
419
|
(slen > usize(0)) => slen,
|
|
419
420
|
true => usize(4)
|
|
420
421
|
);
|
|
421
422
|
new_ptr := Self._raw_alloc(cap);
|
|
422
423
|
if(slen > usize(0), {
|
|
423
|
-
|
|
424
|
+
match(
|
|
425
|
+
l.ptr(),
|
|
426
|
+
.Some(src) => Self._copy_elems(new_ptr, src, usize(0), usize(0), slen),
|
|
427
|
+
.None => ()
|
|
428
|
+
);
|
|
424
429
|
});
|
|
425
430
|
Self(_ptr : new_ptr, _len : slen, _cap : cap)
|
|
426
431
|
})
|