Skip to content

Commit 5ba72f2

Browse files
authored
Merge pull request #83023 from glessard/rdar155474776-extracting-62
[SE-0488, 6.2] Implementation for SE-0488
2 parents 8bf3af0 + 6414aa2 commit 5ba72f2

File tree

7 files changed

+224
-94
lines changed

7 files changed

+224
-94
lines changed

stdlib/public/core/Span/RawSpan.swift

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,20 @@ extension RawSpan {
378378
/// - Complexity: O(1)
379379
@_alwaysEmitIntoClient
380380
@lifetime(copy self)
381-
public func _extracting(_ bounds: Range<Int>) -> Self {
381+
public func extracting(_ bounds: Range<Int>) -> Self {
382382
_precondition(
383383
UInt(bitPattern: bounds.lowerBound) <= UInt(bitPattern: _count) &&
384384
UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count),
385385
"Byte offset range out of bounds"
386386
)
387-
return unsafe _extracting(unchecked: bounds)
387+
return unsafe extracting(unchecked: bounds)
388+
}
389+
390+
@available(*, deprecated, renamed: "extracting(_:)")
391+
@_alwaysEmitIntoClient
392+
@lifetime(copy self)
393+
public func _extracting(_ bounds: Range<Int>) -> Self {
394+
extracting(bounds)
388395
}
389396

390397
/// Constructs a new span over the bytes within the supplied range of
@@ -405,12 +412,20 @@ extension RawSpan {
405412
@unsafe
406413
@_alwaysEmitIntoClient
407414
@lifetime(copy self)
408-
public func _extracting(unchecked bounds: Range<Int>) -> Self {
415+
public func extracting(unchecked bounds: Range<Int>) -> Self {
409416
let newStart = unsafe _pointer?.advanced(by: bounds.lowerBound)
410417
let newSpan = unsafe RawSpan(_unchecked: newStart, byteCount: bounds.count)
411418
return unsafe _overrideLifetime(newSpan, copying: self)
412419
}
413420

421+
@unsafe
422+
@available(*, deprecated, renamed: "extracting(unchecked:)")
423+
@_alwaysEmitIntoClient
424+
@lifetime(copy self)
425+
public func _extracting(unchecked bounds: Range<Int>) -> Self {
426+
unsafe extracting(unchecked: bounds)
427+
}
428+
414429
/// Constructs a new span over the bytes within the supplied range of
415430
/// positions within this span.
416431
///
@@ -426,8 +441,15 @@ extension RawSpan {
426441
/// - Complexity: O(1)
427442
@_alwaysEmitIntoClient
428443
@lifetime(copy self)
444+
public func extracting(_ bounds: some RangeExpression<Int>) -> Self {
445+
extracting(bounds.relative(to: byteOffsets))
446+
}
447+
448+
@available(*, deprecated, renamed: "extracting(_:)")
449+
@_alwaysEmitIntoClient
450+
@lifetime(copy self)
429451
public func _extracting(_ bounds: some RangeExpression<Int>) -> Self {
430-
_extracting(bounds.relative(to: byteOffsets))
452+
extracting(bounds)
431453
}
432454

433455
/// Constructs a new span over the bytes within the supplied range of
@@ -448,13 +470,21 @@ extension RawSpan {
448470
@unsafe
449471
@_alwaysEmitIntoClient
450472
@lifetime(copy self)
451-
public func _extracting(
473+
public func extracting(
452474
unchecked bounds: ClosedRange<Int>
453475
) -> Self {
454476
let range = unsafe Range(
455477
_uncheckedBounds: (bounds.lowerBound, bounds.upperBound + 1)
456478
)
457-
return unsafe _extracting(unchecked: range)
479+
return unsafe extracting(unchecked: range)
480+
}
481+
482+
@unsafe
483+
@available(*, deprecated, renamed: "extracting(unchecked:)")
484+
@_alwaysEmitIntoClient
485+
@lifetime(copy self)
486+
public func _extracting(unchecked bounds: ClosedRange<Int>) -> Self {
487+
unsafe extracting(unchecked: bounds)
458488
}
459489

460490
/// Constructs a new span over all the bytes of this span.
@@ -468,6 +498,13 @@ extension RawSpan {
468498
/// - Complexity: O(1)
469499
@_alwaysEmitIntoClient
470500
@lifetime(copy self)
501+
public func extracting(_: UnboundedRange) -> Self {
502+
self
503+
}
504+
505+
@available(*, deprecated, renamed: "extracting(_:)")
506+
@_alwaysEmitIntoClient
507+
@lifetime(copy self)
471508
public func _extracting(_: UnboundedRange) -> Self {
472509
self
473510
}
@@ -708,13 +745,20 @@ extension RawSpan {
708745
/// - Complexity: O(1)
709746
@_alwaysEmitIntoClient
710747
@lifetime(copy self)
711-
public func _extracting(first maxLength: Int) -> Self {
748+
public func extracting(first maxLength: Int) -> Self {
712749
_precondition(maxLength >= 0, "Can't have a prefix of negative length")
713750
let newCount = min(maxLength, byteCount)
714751
let newSpan = unsafe Self(_unchecked: _pointer, byteCount: newCount)
715752
return unsafe _overrideLifetime(newSpan, copying: self)
716753
}
717754

755+
@available(*, deprecated, renamed: "extracting(first:)")
756+
@_alwaysEmitIntoClient
757+
@lifetime(copy self)
758+
public func _extracting(first maxLength: Int) -> Self {
759+
extracting(first: maxLength)
760+
}
761+
718762
/// Returns a span over all but the given number of trailing bytes.
719763
///
720764
/// If the number of elements to drop exceeds the number of elements in
@@ -731,14 +775,21 @@ extension RawSpan {
731775
/// - Complexity: O(1)
732776
@_alwaysEmitIntoClient
733777
@lifetime(copy self)
734-
public func _extracting(droppingLast k: Int) -> Self {
778+
public func extracting(droppingLast k: Int) -> Self {
735779
_precondition(k >= 0, "Can't drop a negative number of bytes")
736780
let droppedCount = min(k, byteCount)
737781
let count = byteCount &- droppedCount
738782
let newSpan = unsafe Self(_unchecked: _pointer, byteCount: count)
739783
return unsafe _overrideLifetime(newSpan, copying: self)
740784
}
741785

786+
@available(*, deprecated, renamed: "extracting(droppingLast:)")
787+
@_alwaysEmitIntoClient
788+
@lifetime(copy self)
789+
public func _extracting(droppingLast k: Int) -> Self {
790+
extracting(droppingLast: k)
791+
}
792+
742793
/// Returns a span containing the trailing bytes of the span,
743794
/// up to the given maximum length.
744795
///
@@ -756,7 +807,7 @@ extension RawSpan {
756807
/// - Complexity: O(1)
757808
@_alwaysEmitIntoClient
758809
@lifetime(copy self)
759-
public func _extracting(last maxLength: Int) -> Self {
810+
public func extracting(last maxLength: Int) -> Self {
760811
_precondition(maxLength >= 0, "Can't have a suffix of negative length")
761812
let newCount = min(maxLength, byteCount)
762813
let newStart = unsafe _pointer?.advanced(by: byteCount &- newCount)
@@ -766,6 +817,13 @@ extension RawSpan {
766817
return unsafe _overrideLifetime(newSpan, copying: self)
767818
}
768819

820+
@available(*, deprecated, renamed: "extracting(last:)")
821+
@_alwaysEmitIntoClient
822+
@lifetime(copy self)
823+
public func _extracting(last maxLength: Int) -> Self {
824+
extracting(last: maxLength)
825+
}
826+
769827
/// Returns a span over all but the given number of initial bytes.
770828
///
771829
/// If the number of elements to drop exceeds the number of bytes in
@@ -782,7 +840,7 @@ extension RawSpan {
782840
/// - Complexity: O(1)
783841
@_alwaysEmitIntoClient
784842
@lifetime(copy self)
785-
public func _extracting(droppingFirst k: Int) -> Self {
843+
public func extracting(droppingFirst k: Int) -> Self {
786844
_precondition(k >= 0, "Can't drop a negative number of bytes")
787845
let droppedCount = min(k, byteCount)
788846
let newStart = unsafe _pointer?.advanced(by: droppedCount)
@@ -792,4 +850,11 @@ extension RawSpan {
792850
// lifetime of 'self'. Make the dependence explicit.
793851
return unsafe _overrideLifetime(newSpan, copying: self)
794852
}
853+
854+
@available(*, deprecated, renamed: "extracting(droppingFirst:)")
855+
@_alwaysEmitIntoClient
856+
@lifetime(copy self)
857+
public func _extracting(droppingFirst k: Int) -> Self {
858+
extracting(droppingFirst: k)
859+
}
795860
}

stdlib/public/core/Span/Span.swift

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,20 @@ extension Span where Element: ~Copyable {
536536
/// - Complexity: O(1)
537537
@_alwaysEmitIntoClient
538538
@lifetime(copy self)
539-
public func _extracting(_ bounds: Range<Index>) -> Self {
539+
public func extracting(_ bounds: Range<Index>) -> Self {
540540
_precondition(
541541
UInt(bitPattern: bounds.lowerBound) <= UInt(bitPattern: _count) &&
542542
UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count),
543543
"Index range out of bounds"
544544
)
545-
return unsafe _extracting(unchecked: bounds)
545+
return unsafe extracting(unchecked: bounds)
546+
}
547+
548+
@available(*, deprecated, renamed: "extracting(_:)")
549+
@_alwaysEmitIntoClient
550+
@lifetime(copy self)
551+
public func _extracting(_ bounds: Range<Index>) -> Self {
552+
extracting(bounds)
546553
}
547554

548555
/// Constructs a new span over the items within the supplied range of
@@ -563,7 +570,7 @@ extension Span where Element: ~Copyable {
563570
@unsafe
564571
@_alwaysEmitIntoClient
565572
@lifetime(copy self)
566-
public func _extracting(unchecked bounds: Range<Index>) -> Self {
573+
public func extracting(unchecked bounds: Range<Index>) -> Self {
567574
let delta = bounds.lowerBound &* MemoryLayout<Element>.stride
568575
let newStart = unsafe _pointer?.advanced(by: delta)
569576
let newSpan = unsafe Span(_unchecked: newStart, count: bounds.count)
@@ -572,6 +579,14 @@ extension Span where Element: ~Copyable {
572579
return unsafe _overrideLifetime(newSpan, copying: self)
573580
}
574581

582+
@unsafe
583+
@available(*, deprecated, renamed: "extracting(unchecked:)")
584+
@_alwaysEmitIntoClient
585+
@lifetime(copy self)
586+
public func _extracting(unchecked bounds: Range<Index>) -> Self {
587+
unsafe extracting(unchecked: bounds)
588+
}
589+
575590
/// Constructs a new span over the items within the supplied range of
576591
/// positions within this span.
577592
///
@@ -587,10 +602,17 @@ extension Span where Element: ~Copyable {
587602
/// - Complexity: O(1)
588603
@_alwaysEmitIntoClient
589604
@lifetime(copy self)
590-
public func _extracting(
605+
public func extracting(
591606
_ bounds: some RangeExpression<Index>
592607
) -> Self {
593-
_extracting(bounds.relative(to: indices))
608+
extracting(bounds.relative(to: indices))
609+
}
610+
611+
@available(*, deprecated, renamed: "extracting(_:)")
612+
@_alwaysEmitIntoClient
613+
@lifetime(copy self)
614+
public func _extracting(_ bounds: some RangeExpression<Index>) -> Self {
615+
extracting(bounds)
594616
}
595617

596618
/// Constructs a new span over the items within the supplied range of
@@ -611,13 +633,21 @@ extension Span where Element: ~Copyable {
611633
@unsafe
612634
@_alwaysEmitIntoClient
613635
@lifetime(copy self)
614-
public func _extracting(
636+
public func extracting(
615637
unchecked bounds: ClosedRange<Index>
616638
) -> Self {
617639
let range = unsafe Range(
618640
_uncheckedBounds: (bounds.lowerBound, bounds.upperBound + 1)
619641
)
620-
return unsafe _extracting(unchecked: range)
642+
return unsafe extracting(unchecked: range)
643+
}
644+
645+
@unsafe
646+
@available(*, deprecated, renamed: "extracting(unchecked:)")
647+
@_alwaysEmitIntoClient
648+
@lifetime(copy self)
649+
public func _extracting(unchecked bounds: ClosedRange<Index>) -> Self {
650+
unsafe extracting(unchecked: bounds)
621651
}
622652

623653
/// Constructs a new span over all the items of this span.
@@ -631,6 +661,13 @@ extension Span where Element: ~Copyable {
631661
/// - Complexity: O(1)
632662
@_alwaysEmitIntoClient
633663
@lifetime(copy self)
664+
public func extracting(_: UnboundedRange) -> Self {
665+
self
666+
}
667+
668+
@available(*, deprecated, renamed: "extracting(_:)")
669+
@_alwaysEmitIntoClient
670+
@lifetime(copy self)
634671
public func _extracting(_: UnboundedRange) -> Self {
635672
self
636673
}
@@ -761,13 +798,20 @@ extension Span where Element: ~Copyable {
761798
/// - Complexity: O(1)
762799
@_alwaysEmitIntoClient
763800
@lifetime(copy self)
764-
public func _extracting(first maxLength: Int) -> Self {
801+
public func extracting(first maxLength: Int) -> Self {
765802
_precondition(maxLength >= 0, "Can't have a prefix of negative length")
766803
let newCount = min(maxLength, count)
767804
let newSpan = unsafe Self(_unchecked: _pointer, count: newCount)
768805
return unsafe _overrideLifetime(newSpan, copying: self)
769806
}
770807

808+
@available(*, deprecated, renamed: "extracting(first:)")
809+
@_alwaysEmitIntoClient
810+
@lifetime(copy self)
811+
public func _extracting(first maxLength: Int) -> Self {
812+
extracting(first: maxLength)
813+
}
814+
771815
/// Returns a span over all but the given number of trailing elements.
772816
///
773817
/// If the number of elements to drop exceeds the number of elements in
@@ -784,13 +828,20 @@ extension Span where Element: ~Copyable {
784828
/// - Complexity: O(1)
785829
@_alwaysEmitIntoClient
786830
@lifetime(copy self)
787-
public func _extracting(droppingLast k: Int) -> Self {
831+
public func extracting(droppingLast k: Int) -> Self {
788832
_precondition(k >= 0, "Can't drop a negative number of elements")
789833
let droppedCount = min(k, count)
790834
let newSpan = unsafe Self(_unchecked: _pointer, count: count &- droppedCount)
791835
return unsafe _overrideLifetime(newSpan, copying: self)
792836
}
793837

838+
@available(*, deprecated, renamed: "extracting(droppingLast:)")
839+
@_alwaysEmitIntoClient
840+
@lifetime(copy self)
841+
public func _extracting(droppingLast k: Int) -> Self {
842+
extracting(droppingLast: k)
843+
}
844+
794845
/// Returns a span containing the final elements of the span,
795846
/// up to the given maximum length.
796847
///
@@ -808,7 +859,7 @@ extension Span where Element: ~Copyable {
808859
/// - Complexity: O(1)
809860
@_alwaysEmitIntoClient
810861
@lifetime(copy self)
811-
public func _extracting(last maxLength: Int) -> Self {
862+
public func extracting(last maxLength: Int) -> Self {
812863
_precondition(maxLength >= 0, "Can't have a suffix of negative length")
813864
let newCount = min(maxLength, count)
814865
let offset = (count &- newCount) * MemoryLayout<Element>.stride
@@ -819,6 +870,13 @@ extension Span where Element: ~Copyable {
819870
return unsafe _overrideLifetime(newSpan, copying: self)
820871
}
821872

873+
@available(*, deprecated, renamed: "extracting(last:)")
874+
@_alwaysEmitIntoClient
875+
@lifetime(copy self)
876+
public func _extracting(last maxLength: Int) -> Self {
877+
extracting(last: maxLength)
878+
}
879+
822880
/// Returns a span over all but the given number of initial elements.
823881
///
824882
/// If the number of elements to drop exceeds the number of elements in
@@ -835,7 +893,7 @@ extension Span where Element: ~Copyable {
835893
/// - Complexity: O(1)
836894
@_alwaysEmitIntoClient
837895
@lifetime(copy self)
838-
public func _extracting(droppingFirst k: Int) -> Self {
896+
public func extracting(droppingFirst k: Int) -> Self {
839897
_precondition(k >= 0, "Can't drop a negative number of elements")
840898
let droppedCount = min(k, count)
841899
let offset = droppedCount &* MemoryLayout<Element>.stride
@@ -846,4 +904,11 @@ extension Span where Element: ~Copyable {
846904
// lifetime of 'buffer'. Make the dependence explicit.
847905
return unsafe _overrideLifetime(newSpan, copying: self)
848906
}
907+
908+
@available(*, deprecated, renamed: "extracting(droppingFirst:)")
909+
@_alwaysEmitIntoClient
910+
@lifetime(copy self)
911+
public func _extracting(droppingFirst k: Int) -> Self {
912+
extracting(droppingFirst: k)
913+
}
849914
}

stdlib/public/core/Substring.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ extension Substring.UTF8View {
760760
let base: String.UTF8View = self._base
761761
let first = base._foreignDistance(from: base.startIndex, to: startIndex)
762762
let count = base._foreignDistance(from: startIndex, to: endIndex)
763-
let span = base._underlyingSpan()._extracting(first..<(first &+ count))
763+
let span = base._underlyingSpan().extracting(first..<(first &+ count))
764764
return unsafe _overrideLifetime(span, borrowing: self)
765765
}
766766
#endif // _runtime(_ObjC)
@@ -776,7 +776,7 @@ extension Substring.UTF8View {
776776
let isFastUTF8 = _wholeGuts.isFastUTF8
777777
_precondition(isFastUTF8, "Substring must be contiguous UTF8")
778778
var span = unsafe Span(_unsafeElements: _wholeGuts._object.fastUTF8)
779-
span = span._extracting(first..<end)
779+
span = span.extracting(first..<end)
780780
return unsafe _overrideLifetime(span, borrowing: self)
781781
}
782782

0 commit comments

Comments
 (0)