@shd101wyy/yo 0.0.24 → 0.0.25

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/std/prelude.yo CHANGED
@@ -3607,29 +3607,92 @@ export
3607
3607
 
3608
3608
  // === Traits again ===
3609
3609
  /// === Iterator ===
3610
- Iterator :: (fn(comptime(Item) : Type) -> comptime(Trait)) {
3611
- return trait(
3612
- Item := Item,
3613
- next :
3614
- fn(self : *(Self)) -> Option(Self.Item)
3615
- );
3616
- };
3610
+ Iterator :: trait(
3611
+ Item : Type,
3612
+ next :
3613
+ fn(self : *(Self)) -> Option(Self.Item)
3614
+ );
3617
3615
  export Iterator;
3618
3616
 
3619
3617
  /// === IntoIterator ===
3620
- IntoIterator :: (fn(
3621
- comptime(Item) : Type,
3622
- comptime(IntoIter) : Type,
3623
- where(IntoIter <: Iterator(Item))) -> comptime(Trait)) {
3624
- return trait(
3625
- Item := Item,
3626
- IntoIter := IntoIter,
3627
- into_iter :
3628
- fn(self : Self) -> Self.IntoIter
3629
- );
3630
- };
3618
+ IntoIterator :: trait(
3619
+ Item : Type,
3620
+ IntoIter : Type,
3621
+ into_iter :
3622
+ fn(self : Self) -> Self.IntoIter,
3623
+ where(Self.IntoIter <: Iterator(Item := Self.Item))
3624
+ );
3631
3625
  export IntoIterator;
3632
3626
 
3627
+ /// === Array Iterator ===
3628
+ /**
3629
+ * Pointer iterator for Array(T, N) - yields *(T) pointers to each element
3630
+ * Used by Array.iter()
3631
+ */
3632
+ ArrayIterPtr :: (fn(comptime(T) : Type) -> comptime(Type))(
3633
+ struct(
3634
+ _ptr : *(T),
3635
+ _index : usize,
3636
+ _len : usize
3637
+ )
3638
+ );
3639
+
3640
+ impl(forall(T : Type), ArrayIterPtr(T), Iterator(
3641
+ Item : *(T),
3642
+ next : (fn(self : *(Self)) -> Option(*(T)))(
3643
+ cond(
3644
+ (self._index >= self._len) => .None,
3645
+ true => {
3646
+ element_ptr := (self._ptr &+ self._index);
3647
+ self._index = (self._index + usize(1));
3648
+ .Some(element_ptr)
3649
+ }
3650
+ )
3651
+ )
3652
+ ));
3653
+
3654
+ impl(forall(T : Type, N : usize), Array(T, N),
3655
+ iter : (fn(self : *(Self)) -> ArrayIterPtr(T))(
3656
+ ArrayIterPtr(T)(_ptr: &((self.*)(usize(0))), _index: usize(0), _len: N)
3657
+ )
3658
+ );
3659
+
3660
+ export ArrayIterPtr;
3661
+
3662
+ /// === Slice Iterator ===
3663
+ /**
3664
+ * Pointer iterator for Slice(T) - yields *(T) pointers to each element
3665
+ * Used by Slice.iter()
3666
+ */
3667
+ SliceIterPtr :: (fn(comptime(T) : Type) -> comptime(Type))(
3668
+ struct(
3669
+ _slice : Slice(T),
3670
+ _index : usize
3671
+ )
3672
+ );
3673
+
3674
+ impl(forall(T : Type), SliceIterPtr(T), Iterator(
3675
+ Item : *(T),
3676
+ next : (fn(self : *(Self)) -> Option(*(T)))(
3677
+ cond(
3678
+ (self._index >= self._slice.len()) => .None,
3679
+ true => {
3680
+ ptr := &((self._slice)(self._index));
3681
+ self._index = (self._index + usize(1));
3682
+ .Some(ptr)
3683
+ }
3684
+ )
3685
+ )
3686
+ ));
3687
+
3688
+ impl(forall(T : Type), Slice(T),
3689
+ iter : (fn(self : *(Self)) -> SliceIterPtr(T))(
3690
+ SliceIterPtr(T)(_slice: self.*, _index: usize(0))
3691
+ )
3692
+ );
3693
+
3694
+ export SliceIterPtr;
3695
+
3633
3696
  // === Conversion ===
3634
3697
  TryFrom :: (fn(comptime(From) : Type) -> comptime(Trait))(
3635
3698
  trait(
@@ -1356,7 +1356,94 @@ impl(String, Eq(String)(
1356
1356
  })
1357
1357
  ));
1358
1358
 
1359
+ // === Iterator support ===
1360
+
1361
+ /**
1362
+ * Rune iterator for String - yields decoded Unicode runes
1363
+ * Used by chars() and into_iter()
1364
+ */
1365
+ StringChars :: struct(
1366
+ _string : String,
1367
+ _byte_index : usize
1368
+ );
1369
+
1370
+ impl(StringChars, Iterator(
1371
+ Item : rune,
1372
+ next : (fn(self : *(Self)) -> Option(rune))(
1373
+ cond(
1374
+ (self._byte_index >= self._string._bytes.len()) => .None,
1375
+ true => {
1376
+ first_byte_opt := self._string._bytes.get(self._byte_index);
1377
+ match(first_byte_opt,
1378
+ .Some(first_byte) => {
1379
+ // Determine byte length of this UTF-8 character
1380
+ (byte_len : usize) = cond(
1381
+ (first_byte < u8(0x80)) => usize(1),
1382
+ ((first_byte >= u8(0xC0)) && (first_byte < u8(0xE0))) => usize(2),
1383
+ ((first_byte >= u8(0xE0)) && (first_byte < u8(0xF0))) => usize(3),
1384
+ ((first_byte >= u8(0xF0)) && (first_byte < u8(0xF8))) => usize(4),
1385
+ true => usize(1)
1386
+ );
1387
+ r := self._string._decode_rune_at(self._byte_index);
1388
+ self._byte_index = (self._byte_index + byte_len);
1389
+ r
1390
+ },
1391
+ .None => .None
1392
+ )
1393
+ }
1394
+ )
1395
+ )
1396
+ ));
1397
+
1398
+ /**
1399
+ * Byte iterator for String - yields raw UTF-8 bytes
1400
+ * Used by bytes()
1401
+ */
1402
+ StringBytes :: struct(
1403
+ _string : String,
1404
+ _index : usize
1405
+ );
1406
+
1407
+ impl(StringBytes, Iterator(
1408
+ Item : u8,
1409
+ next : (fn(self : *(Self)) -> Option(u8))(
1410
+ cond(
1411
+ (self._index >= self._string._bytes.len()) => .None,
1412
+ true => {
1413
+ byte_opt := self._string._bytes.get(self._index);
1414
+ self._index = (self._index + usize(1));
1415
+ byte_opt
1416
+ }
1417
+ )
1418
+ )
1419
+ ));
1420
+
1421
+ impl(String,
1422
+ /**
1423
+ * Returns a rune iterator over the string's Unicode characters
1424
+ */
1425
+ chars : (fn(self : Self) -> StringChars)(
1426
+ StringChars(_string: self, _byte_index: usize(0))
1427
+ ),
1428
+
1429
+ /**
1430
+ * Returns a byte iterator over the string's raw UTF-8 bytes
1431
+ */
1432
+ bytes : (fn(self : Self) -> StringBytes)(
1433
+ StringBytes(_string: self, _index: usize(0))
1434
+ ),
1435
+
1436
+ /**
1437
+ * Consume the string and return a rune iterator (default iteration)
1438
+ */
1439
+ into_iter : (fn(self : Self) -> StringChars)(
1440
+ StringChars(_string: self, _byte_index: usize(0))
1441
+ )
1442
+ );
1443
+
1359
1444
  export
1360
1445
  String,
1361
- StringError
1446
+ StringError,
1447
+ StringChars,
1448
+ StringBytes
1362
1449
  ;