Skip to content

Commit a5ff35c

Browse files
author
Maxim Moiseev
committed
[stdlib] extendingOrTruncating: => truncatingIfNeeded:
1 parent d018ecc commit a5ff35c

26 files changed

+168
-169
lines changed

stdlib/private/StdlibUnicodeUnittest/Collation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,13 @@ public struct StringComparisonTest {
338338

339339
public func sortKey(forCollationElements ces: [UInt64]) -> ([UInt16], [UInt16], [UInt16]) {
340340
func L1(_ ce: UInt64) -> UInt16 {
341-
return UInt16(extendingOrTruncating: ce &>> 32)
341+
return UInt16(truncatingIfNeeded: ce &>> 32)
342342
}
343343
func L2(_ ce: UInt64) -> UInt16 {
344-
return UInt16(extendingOrTruncating: ce &>> 16)
344+
return UInt16(truncatingIfNeeded: ce &>> 16)
345345
}
346346
func L3(_ ce: UInt64) -> UInt16 {
347-
return UInt16(extendingOrTruncating: ce)
347+
return UInt16(truncatingIfNeeded: ce)
348348
}
349349

350350
var result1: [UInt16] = []

stdlib/public/core/ASCII.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension Unicode.ASCII : Unicode.Encoding {
4141
_ source: Unicode.Scalar
4242
) -> EncodedScalar? {
4343
guard source.value < (1&<<7) else { return nil }
44-
return EncodedScalar(UInt8(extendingOrTruncating: source.value))
44+
return EncodedScalar(UInt8(truncatingIfNeeded: source.value))
4545
}
4646

4747
@inline(__always)
@@ -80,7 +80,7 @@ extension Unicode.ASCII.Parser : Unicode.Parser {
8080
where I.Element == Encoding.CodeUnit {
8181
let n = input.next()
8282
if _fastPath(n != nil), let x = n {
83-
guard _fastPath(Int8(extendingOrTruncating: x) >= 0)
83+
guard _fastPath(Int8(truncatingIfNeeded: x) >= 0)
8484
else { return .error(length: 1) }
8585
return .valid(Unicode.ASCII.EncodedScalar(x))
8686
}

stdlib/public/core/ArrayBody.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal struct _ArrayBody {
3232
_storage = _SwiftArrayBodyStorage(
3333
count: count,
3434
_capacityAndFlags:
35-
(UInt(extendingOrTruncating: capacity) &<< 1) |
35+
(UInt(truncatingIfNeeded: capacity) &<< 1) |
3636
(elementTypeIsBridgedVerbatim ? 1 : 0))
3737
}
3838

stdlib/public/core/Builtin.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ func _canBeClass<T>(_: T.Type) -> Int8 {
9090
/// - Value conversion from one integer type to another. Use the destination
9191
/// type's initializer or the `numericCast(_:)` function.
9292
/// - Bitwise conversion from one integer type to another. Use the destination
93-
/// type's `init(extendingOrTruncating:)` or `init(bitPattern:)`
94-
/// initializer.
93+
/// type's `init(truncatingIfNeeded:)` or `init(bitPattern:)` initializer.
9594
/// - Conversion from a pointer to an integer value with the bit pattern of the
9695
/// pointer's address in memory, or vice versa. Use the `init(bitPattern:)`
9796
/// initializer for the destination type.

stdlib/public/core/Character.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public struct Character :
130130
shift += 16
131131
}
132132
}
133-
guard _fastPath(Int64(extendingOrTruncating: bits) >= 0) else {
133+
guard _fastPath(Int64(truncatingIfNeeded: bits) >= 0) else {
134134
break FastPath
135135
}
136136
_representation = .smallUTF16(Builtin.trunc_Int64_Int63(bits._value))
@@ -224,7 +224,7 @@ public struct Character :
224224

225225
if _fastPath(s._core.count <= 4) {
226226
let b = _UIntBuffer<UInt64, Unicode.UTF16.CodeUnit>(s._core)
227-
if _fastPath(Int64(extendingOrTruncating: b._storage) >= 0) {
227+
if _fastPath(Int64(truncatingIfNeeded: b._storage) >= 0) {
228228
_representation = .smallUTF16(
229229
Builtin.trunc_Int64_Int63(b._storage._value))
230230
return
@@ -300,7 +300,7 @@ extension Character {
300300
return _UIntBuffer<UInt64, Unicode.UTF16.CodeUnit>(
301301
_storage: bits,
302302
_bitCount: UInt8(
303-
extendingOrTruncating: 16 * Swift.max(1, (minBitWidth + 15) / 16))
303+
truncatingIfNeeded: 16 * Swift.max(1, (minBitWidth + 15) / 16))
304304
)
305305
}
306306

stdlib/public/core/CharacterUnicodeScalars.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ extension Character.UnicodeScalarView : Collection {
101101
case .valid(let s):
102102
return Index(
103103
_encodedOffset: startOfNextScalar, _scalar: s,
104-
_stride: UInt8(extendingOrTruncating: s.count))
104+
_stride: UInt8(truncatingIfNeeded: s.count))
105105
case .error:
106106
return Index(
107107
_encodedOffset: startOfNextScalar,
@@ -138,7 +138,7 @@ extension Character.UnicodeScalarView : BidirectionalCollection {
138138
case .valid(let s):
139139
return Index(
140140
_encodedOffset: i._encodedOffset - s.count, _scalar: s,
141-
_stride: UInt8(extendingOrTruncating: s.count))
141+
_stride: UInt8(truncatingIfNeeded: s.count))
142142
case .error:
143143
return Index(
144144
_encodedOffset: i._encodedOffset - 1,

stdlib/public/core/DoubleWidth.swift.gyb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
289289
by rhs: DoubleWidth
290290
) -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
291291
let (carry, product) = multipliedFullWidth(by: rhs)
292-
let result = DoubleWidth(extendingOrTruncating: product)
292+
let result = DoubleWidth(truncatingIfNeeded: product)
293293

294294
let isNegative = (self < (0 as DoubleWidth)) != (rhs < (0 as DoubleWidth))
295295
let didCarry = isNegative
@@ -447,7 +447,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
447447

448448
// Shift is exactly the width of `Base`, so low -> high.
449449
if rhs._storage.low == Base.bitWidth {
450-
lhs = DoubleWidth((High(extendingOrTruncating: lhs._storage.low), 0))
450+
lhs = DoubleWidth((High(truncatingIfNeeded: lhs._storage.low), 0))
451451
return
452452
}
453453

@@ -472,7 +472,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
472472
if rhs._storage.low == Base.bitWidth {
473473
lhs = DoubleWidth((
474474
lhs < (0 as DoubleWidth) ? ~0 : 0,
475-
Low(extendingOrTruncating: lhs._storage.high)
475+
Low(truncatingIfNeeded: lhs._storage.high)
476476
))
477477
return
478478
}
@@ -485,10 +485,10 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
485485

486486
lhs._storage.high <<= High(rhs._storage.low)
487487
if Base.bitWidth > rhs._storage.low {
488-
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low >>
488+
lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low >>
489489
(numericCast(Base.bitWidth) - rhs._storage.low))
490490
} else {
491-
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low <<
491+
lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low <<
492492
(rhs._storage.low - numericCast(Base.bitWidth)))
493493
}
494494
lhs._storage.low <<= rhs._storage.low
@@ -500,14 +500,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
500500
lhs._storage.low >>= rhs._storage.low
501501
if Base.bitWidth > rhs._storage.low {
502502
lhs._storage.low |= Low(
503-
extendingOrTruncating:
503+
truncatingIfNeeded:
504504
lhs._storage.high << (numericCast(Base.bitWidth) - rhs._storage.low))
505505
} else {
506506
lhs._storage.low |= Low(
507-
extendingOrTruncating: lhs._storage.high >>
507+
truncatingIfNeeded: lhs._storage.high >>
508508
(rhs._storage.low - numericCast(Base.bitWidth)))
509509
}
510-
lhs._storage.high >>= High(extendingOrTruncating: rhs._storage.low)
510+
lhs._storage.high >>= High(truncatingIfNeeded: rhs._storage.low)
511511
}
512512

513513
%{
@@ -578,8 +578,8 @@ binaryOperators = [
578578

579579
@_transparent
580580
public var byteSwapped: DoubleWidth {
581-
return DoubleWidth((High(extendingOrTruncating: low.byteSwapped),
582-
Low(extendingOrTruncating: high.byteSwapped)))
581+
return DoubleWidth((High(truncatingIfNeeded: low.byteSwapped),
582+
Low(truncatingIfNeeded: high.byteSwapped)))
583583
}
584584
}
585585

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,11 @@ extension ${Self} : Hashable {
754754
%if bits <= word_bits:
755755
return Int(bitPattern: UInt(bitPattern))
756756
%elif bits == 64: # Double -> 32-bit Int
757-
return Int(extendingOrTruncating: bitPattern &>> 32) ^
758-
Int(extendingOrTruncating: bitPattern)
757+
return Int(truncatingIfNeeded: bitPattern &>> 32) ^
758+
Int(truncatingIfNeeded: bitPattern)
759759
%elif word_bits == 32: # Float80 -> 32-bit Int
760-
return Int(extendingOrTruncating: significandBitPattern &>> 32) ^
761-
Int(extendingOrTruncating: significandBitPattern) ^
760+
return Int(truncatingIfNeeded: significandBitPattern &>> 32) ^
761+
Int(truncatingIfNeeded: significandBitPattern) ^
762762
Int(_representation.signAndExponent)
763763
%else: # Float80 -> 64-bit Int
764764
return Int(bitPattern: UInt(significandBitPattern)) ^

stdlib/public/core/IntegerParsing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ internal func _asciiDigit<CodeUnit : UnsignedInteger, Result : BinaryInteger>(
1818
let lower = _ascii16("a")..._ascii16("z")
1919
let upper = _ascii16("A")..._ascii16("Z")
2020

21-
let u = UInt16(extendingOrTruncating: u_)
21+
let u = UInt16(truncatingIfNeeded: u_)
2222
let d: UInt16
2323
if _fastPath(digit ~= u) { d = u &- digit.lowerBound }
2424
else if _fastPath(upper ~= u) { d = u &- upper.lowerBound &+ 10 }
2525
else if _fastPath(lower ~= u) { d = u &- lower.lowerBound &+ 10 }
2626
else { return nil }
2727
guard _fastPath(d < radix) else { return nil }
28-
return Result(extendingOrTruncating: d)
28+
return Result(truncatingIfNeeded: d)
2929
}
3030

3131
@inline(__always)

0 commit comments

Comments
 (0)