Skip to content

Commit 6414aa2

Browse files
committed
[stdlib] rename RawSpan.extracting functions
1 parent ac3e476 commit 6414aa2

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
@@ -132,7 +132,7 @@ suite.test("unsafeLoadUnaligned(as:)")
132132
a.withUnsafeBytes {
133133
let span = RawSpan(_unsafeBytes: $0)
134134

135-
let suffix = span._extracting(droppingFirst: 2)
135+
let suffix = span.extracting(droppingFirst: 2)
136136
let u0 = suffix.unsafeLoadUnaligned(as: UInt64.self)
137137
expectEqual(u0.littleEndian & 0xff, 2)
138138
expectEqual(u0.bigEndian & 0xff, 9)
@@ -142,7 +142,7 @@ suite.test("unsafeLoadUnaligned(as:)")
142142
}
143143
}
144144

145-
suite.test("_extracting() functions")
145+
suite.test("extracting() functions")
146146
.skip(.custom(
147147
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
148148
reason: "Requires Swift 6.2's standard library"
@@ -154,10 +154,10 @@ suite.test("_extracting() functions")
154154
let b = (0..<capacity).map(Int8.init)
155155
b.withUnsafeBytes {
156156
let span = RawSpan(_unsafeBytes: $0)
157-
let sub1 = span._extracting(0..<2)
158-
let sub2 = span._extracting(..<2)
159-
let sub3 = span._extracting(...)
160-
let sub4 = span._extracting(2...)
157+
let sub1 = span.extracting(0..<2)
158+
let sub2 = span.extracting(..<2)
159+
let sub3 = span.extracting(...)
160+
let sub4 = span.extracting(2...)
161161

162162
sub1.withUnsafeBytes { p1 in
163163
sub2.withUnsafeBytes { p2 in
@@ -177,7 +177,7 @@ suite.test("_extracting() functions")
177177
}
178178
}
179179

180-
suite.test("_extracting(unchecked:) functions")
180+
suite.test("extracting(unchecked:) functions")
181181
.skip(.custom(
182182
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
183183
reason: "Requires Swift 6.2's standard library"
@@ -189,14 +189,14 @@ suite.test("_extracting(unchecked:) functions")
189189
let b = (0..<capacity).map(UInt8.init)
190190
b.withUnsafeBytes {
191191
let span = RawSpan(_unsafeBytes: $0)
192-
let prefix = span._extracting(0..<8)
193-
let beyond = prefix._extracting(unchecked: 16...23)
192+
let prefix = span.extracting(0..<8)
193+
let beyond = prefix.extracting(unchecked: 16...23)
194194
expectEqual(beyond.byteCount, 8)
195195
expectEqual(beyond.unsafeLoad(as: UInt8.self), 16)
196196
}
197197
}
198198

199-
suite.test("prefix _extracting() functions")
199+
suite.test("prefix extracting() functions")
200200
.skip(.custom(
201201
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
202202
reason: "Requires Swift 6.2's standard library"
@@ -209,35 +209,35 @@ suite.test("prefix _extracting() functions")
209209
a.withUnsafeBytes {
210210
let span = RawSpan(_unsafeBytes: $0)
211211
expectEqual(span.byteCount, capacity)
212-
expectEqual(span._extracting(first: 1).unsafeLoad(as: UInt8.self), 0)
212+
expectEqual(span.extracting(first: 1).unsafeLoad(as: UInt8.self), 0)
213213
expectEqual(
214-
span._extracting(first: capacity).unsafeLoad(
214+
span.extracting(first: capacity).unsafeLoad(
215215
fromByteOffset: capacity-1, as: UInt8.self
216216
),
217217
UInt8(capacity-1)
218218
)
219-
expectTrue(span._extracting(droppingLast: capacity).isEmpty)
219+
expectTrue(span.extracting(droppingLast: capacity).isEmpty)
220220
expectEqual(
221-
span._extracting(droppingLast: 1).unsafeLoad(
221+
span.extracting(droppingLast: 1).unsafeLoad(
222222
fromByteOffset: capacity-2, as: UInt8.self
223223
),
224224
UInt8(capacity-2)
225225
)
226-
let emptySpan = span._extracting(first: 0)
227-
let emptierSpan = emptySpan._extracting(0..<0)
226+
let emptySpan = span.extracting(first: 0)
227+
let emptierSpan = emptySpan.extracting(0..<0)
228228
expectTrue(emptySpan.isIdentical(to: emptierSpan))
229229
}
230230

231231
do {
232232
let b = UnsafeRawBufferPointer(start: nil, count: 0)
233233
let span = RawSpan(_unsafeBytes: b)
234234
expectEqual(span.byteCount, b.count)
235-
expectEqual(span._extracting(first: 1).byteCount, b.count)
236-
expectEqual(span._extracting(droppingLast: 1).byteCount, b.count)
235+
expectEqual(span.extracting(first: 1).byteCount, b.count)
236+
expectEqual(span.extracting(droppingLast: 1).byteCount, b.count)
237237
}
238238
}
239239

240-
suite.test("suffix _extracting() functions")
240+
suite.test("suffix extracting() functions")
241241
.skip(.custom(
242242
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
243243
reason: "Requires Swift 6.2's standard library"
@@ -250,19 +250,19 @@ suite.test("suffix _extracting() functions")
250250
a.withUnsafeBytes {
251251
let span = RawSpan(_unsafeBytes: $0)
252252
expectEqual(span.byteCount, capacity)
253-
expectEqual(span._extracting(last: capacity).unsafeLoad(as: UInt8.self), 0)
254-
expectEqual(span._extracting(last: capacity-1).unsafeLoad(as: UInt8.self), 1)
255-
expectEqual(span._extracting(last: 1).unsafeLoad(as: UInt8.self), UInt8(capacity-1))
256-
expectTrue(span._extracting(droppingFirst: capacity).isEmpty)
257-
expectEqual(span._extracting(droppingFirst: 1).unsafeLoad(as: UInt8.self), 1)
253+
expectEqual(span.extracting(last: capacity).unsafeLoad(as: UInt8.self), 0)
254+
expectEqual(span.extracting(last: capacity-1).unsafeLoad(as: UInt8.self), 1)
255+
expectEqual(span.extracting(last: 1).unsafeLoad(as: UInt8.self), UInt8(capacity-1))
256+
expectTrue(span.extracting(droppingFirst: capacity).isEmpty)
257+
expectEqual(span.extracting(droppingFirst: 1).unsafeLoad(as: UInt8.self), 1)
258258
}
259259

260260
do {
261261
let b = UnsafeRawBufferPointer(start: nil, count: 0)
262262
let span = RawSpan(_unsafeBytes: b)
263263
expectEqual(span.byteCount, b.count)
264-
expectEqual(span._extracting(last: 1).byteCount, b.count)
265-
expectEqual(span._extracting(droppingFirst: 1).byteCount, b.count)
264+
expectEqual(span.extracting(last: 1).byteCount, b.count)
265+
expectEqual(span.extracting(droppingFirst: 1).byteCount, b.count)
266266
}
267267
}
268268

@@ -307,9 +307,9 @@ suite.test("byteOffsets(of:)")
307307
defer { b.deallocate() }
308308

309309
let span = RawSpan(_unsafeBytes: b)
310-
let subSpan1 = span._extracting(first: 6)
311-
let subSpan2 = span._extracting(last: 6)
312-
let emptySpan = span._extracting(first: 0)
310+
let subSpan1 = span.extracting(first: 6)
311+
let subSpan2 = span.extracting(last: 6)
312+
let emptySpan = span.extracting(first: 0)
313313
let nilBuffer = UnsafeRawBufferPointer(start: nil, count: 0)
314314
let nilSpan = RawSpan(_unsafeBytes: nilBuffer)
315315

0 commit comments

Comments
 (0)