Skip to content

Commit 1e3d26f

Browse files
committed
[stdlib] rename RawSpan.extracting functions
1 parent dd83356 commit 1e3d26f

File tree

2 files changed

+104
-39
lines changed

2 files changed

+104
-39
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
}

test/stdlib/Span/RawSpanTests.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ suite.test("unsafeLoadUnaligned(as:)")
129129
a.withUnsafeBytes {
130130
let span = RawSpan(_unsafeBytes: $0)
131131

132-
let suffix = span._extracting(droppingFirst: 2)
132+
let suffix = span.extracting(droppingFirst: 2)
133133
let u0 = suffix.unsafeLoadUnaligned(as: UInt64.self)
134134
expectEqual(u0.littleEndian & 0xff, 2)
135135
expectEqual(u0.bigEndian & 0xff, 9)
@@ -139,7 +139,7 @@ suite.test("unsafeLoadUnaligned(as:)")
139139
}
140140
}
141141

142-
suite.test("_extracting() functions")
142+
suite.test("extracting() functions")
143143
.skip(.custom(
144144
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
145145
reason: "Requires Swift 6.2's standard library"
@@ -151,10 +151,10 @@ suite.test("_extracting() functions")
151151
let b = (0..<capacity).map(Int8.init)
152152
b.withUnsafeBytes {
153153
let span = RawSpan(_unsafeBytes: $0)
154-
let sub1 = span._extracting(0..<2)
155-
let sub2 = span._extracting(..<2)
156-
let sub3 = span._extracting(...)
157-
let sub4 = span._extracting(2...)
154+
let sub1 = span.extracting(0..<2)
155+
let sub2 = span.extracting(..<2)
156+
let sub3 = span.extracting(...)
157+
let sub4 = span.extracting(2...)
158158

159159
sub1.withUnsafeBytes { p1 in
160160
sub2.withUnsafeBytes { p2 in
@@ -174,7 +174,7 @@ suite.test("_extracting() functions")
174174
}
175175
}
176176

177-
suite.test("_extracting(unchecked:) functions")
177+
suite.test("extracting(unchecked:) functions")
178178
.skip(.custom(
179179
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
180180
reason: "Requires Swift 6.2's standard library"
@@ -186,14 +186,14 @@ suite.test("_extracting(unchecked:) functions")
186186
let b = (0..<capacity).map(UInt8.init)
187187
b.withUnsafeBytes {
188188
let span = RawSpan(_unsafeBytes: $0)
189-
let prefix = span._extracting(0..<8)
190-
let beyond = prefix._extracting(unchecked: 16...23)
189+
let prefix = span.extracting(0..<8)
190+
let beyond = prefix.extracting(unchecked: 16...23)
191191
expectEqual(beyond.byteCount, 8)
192192
expectEqual(beyond.unsafeLoad(as: UInt8.self), 16)
193193
}
194194
}
195195

196-
suite.test("prefix _extracting() functions")
196+
suite.test("prefix extracting() functions")
197197
.skip(.custom(
198198
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
199199
reason: "Requires Swift 6.2's standard library"
@@ -206,35 +206,35 @@ suite.test("prefix _extracting() functions")
206206
a.withUnsafeBytes {
207207
let span = RawSpan(_unsafeBytes: $0)
208208
expectEqual(span.byteCount, capacity)
209-
expectEqual(span._extracting(first: 1).unsafeLoad(as: UInt8.self), 0)
209+
expectEqual(span.extracting(first: 1).unsafeLoad(as: UInt8.self), 0)
210210
expectEqual(
211-
span._extracting(first: capacity).unsafeLoad(
211+
span.extracting(first: capacity).unsafeLoad(
212212
fromByteOffset: capacity-1, as: UInt8.self
213213
),
214214
UInt8(capacity-1)
215215
)
216-
expectTrue(span._extracting(droppingLast: capacity).isEmpty)
216+
expectTrue(span.extracting(droppingLast: capacity).isEmpty)
217217
expectEqual(
218-
span._extracting(droppingLast: 1).unsafeLoad(
218+
span.extracting(droppingLast: 1).unsafeLoad(
219219
fromByteOffset: capacity-2, as: UInt8.self
220220
),
221221
UInt8(capacity-2)
222222
)
223-
let emptySpan = span._extracting(first: 0)
224-
let emptierSpan = emptySpan._extracting(0..<0)
223+
let emptySpan = span.extracting(first: 0)
224+
let emptierSpan = emptySpan.extracting(0..<0)
225225
expectTrue(emptySpan.isIdentical(to: emptierSpan))
226226
}
227227

228228
do {
229229
let b = UnsafeRawBufferPointer(start: nil, count: 0)
230230
let span = RawSpan(_unsafeBytes: b)
231231
expectEqual(span.byteCount, b.count)
232-
expectEqual(span._extracting(first: 1).byteCount, b.count)
233-
expectEqual(span._extracting(droppingLast: 1).byteCount, b.count)
232+
expectEqual(span.extracting(first: 1).byteCount, b.count)
233+
expectEqual(span.extracting(droppingLast: 1).byteCount, b.count)
234234
}
235235
}
236236

237-
suite.test("suffix _extracting() functions")
237+
suite.test("suffix extracting() functions")
238238
.skip(.custom(
239239
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
240240
reason: "Requires Swift 6.2's standard library"
@@ -247,19 +247,19 @@ suite.test("suffix _extracting() functions")
247247
a.withUnsafeBytes {
248248
let span = RawSpan(_unsafeBytes: $0)
249249
expectEqual(span.byteCount, capacity)
250-
expectEqual(span._extracting(last: capacity).unsafeLoad(as: UInt8.self), 0)
251-
expectEqual(span._extracting(last: capacity-1).unsafeLoad(as: UInt8.self), 1)
252-
expectEqual(span._extracting(last: 1).unsafeLoad(as: UInt8.self), UInt8(capacity-1))
253-
expectTrue(span._extracting(droppingFirst: capacity).isEmpty)
254-
expectEqual(span._extracting(droppingFirst: 1).unsafeLoad(as: UInt8.self), 1)
250+
expectEqual(span.extracting(last: capacity).unsafeLoad(as: UInt8.self), 0)
251+
expectEqual(span.extracting(last: capacity-1).unsafeLoad(as: UInt8.self), 1)
252+
expectEqual(span.extracting(last: 1).unsafeLoad(as: UInt8.self), UInt8(capacity-1))
253+
expectTrue(span.extracting(droppingFirst: capacity).isEmpty)
254+
expectEqual(span.extracting(droppingFirst: 1).unsafeLoad(as: UInt8.self), 1)
255255
}
256256

257257
do {
258258
let b = UnsafeRawBufferPointer(start: nil, count: 0)
259259
let span = RawSpan(_unsafeBytes: b)
260260
expectEqual(span.byteCount, b.count)
261-
expectEqual(span._extracting(last: 1).byteCount, b.count)
262-
expectEqual(span._extracting(droppingFirst: 1).byteCount, b.count)
261+
expectEqual(span.extracting(last: 1).byteCount, b.count)
262+
expectEqual(span.extracting(droppingFirst: 1).byteCount, b.count)
263263
}
264264
}
265265

@@ -304,9 +304,9 @@ suite.test("byteOffsets(of:)")
304304
defer { b.deallocate() }
305305

306306
let span = RawSpan(_unsafeBytes: b)
307-
let subSpan1 = span._extracting(first: 6)
308-
let subSpan2 = span._extracting(last: 6)
309-
let emptySpan = span._extracting(first: 0)
307+
let subSpan1 = span.extracting(first: 6)
308+
let subSpan2 = span.extracting(last: 6)
309+
let emptySpan = span.extracting(first: 0)
310310
let nilBuffer = UnsafeRawBufferPointer(start: nil, count: 0)
311311
let nilSpan = RawSpan(_unsafeBytes: nilBuffer)
312312

0 commit comments

Comments
 (0)