Skip to content

Commit ccd4c98

Browse files
author
Maxim Moiseev
committed
[stdlib] extendingOrTruncating: => truncatingIfNeeded:
(cherry picked from commit a5ff35c)
1 parent f6f10f7 commit ccd4c98

25 files changed

+155
-156
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: 2 additions & 2 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

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/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)

stdlib/public/core/Integers.swift.gyb

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ extension Numeric {
12421242
/// Bit Pattern Conversion
12431243
/// ----------------------
12441244
///
1245-
/// Use the `init(extendingOrTruncating:)` initializer to create a new instance
1245+
/// Use the `init(truncatingIfNeeded:)` initializer to create a new instance
12461246
/// with the same bit pattern as the passed value, extending or truncating the
12471247
/// value's representation as necessary. Note that the value may not be
12481248
/// preserved, particularly when converting between signed to unsigned integer
@@ -1253,11 +1253,11 @@ extension Numeric {
12531253
/// let q: Int16 = 850
12541254
/// // q == 0b00000011_01010010
12551255
///
1256-
/// let r = Int8(extendingOrTruncating: q) // truncate 'q' to fit in 8 bits
1256+
/// let r = Int8(truncatingIfNeeded: q) // truncate 'q' to fit in 8 bits
12571257
/// // r == 82
12581258
/// // == 0b01010010
12591259
///
1260-
/// let s = Int16(extendingOrTruncating: s) // extend 'r' to fill 16 bits
1260+
/// let s = Int16(truncatingIfNeeded: s) // extend 'r' to fill 16 bits
12611261
/// // s == 82
12621262
/// // == 0b00000000_01010010
12631263
///
@@ -1272,15 +1272,15 @@ extension Numeric {
12721272
/// // t == -100
12731273
/// // t's binary representation == 0b10011100
12741274
///
1275-
/// let u = UInt8(extendingOrTruncating: t)
1275+
/// let u = UInt8(truncatingIfNeeded: t)
12761276
/// // u == 156
12771277
/// // u's binary representation == 0b10011100
12781278
///
1279-
/// let v = Int16(extendingOrTruncating: t)
1279+
/// let v = Int16(truncatingIfNeeded: t)
12801280
/// // v == -100
12811281
/// // v's binary representation == 0b11111111_10011100
12821282
///
1283-
/// let w = UInt16(extendingOrTruncating: t)
1283+
/// let w = UInt16(truncatingIfNeeded: t)
12841284
/// // w == 65436
12851285
/// // w's binary representation == 0b11111111_10011100
12861286
///
@@ -1377,7 +1377,7 @@ public protocol BinaryInteger :
13771377
///
13781378
/// let p: Int16 = -500
13791379
/// // 'p' has a binary representation of 11111110_00001100
1380-
/// let q = Int8(extendingOrTruncating: p)
1380+
/// let q = Int8(truncatingIfNeeded: p)
13811381
/// // q == 12
13821382
/// // 'q' has a binary representation of 00001100
13831383
///
@@ -1388,21 +1388,21 @@ public protocol BinaryInteger :
13881388
///
13891389
/// let u: Int8 = 21
13901390
/// // 'u' has a binary representation of 00010101
1391-
/// let v = Int16(extendingOrTruncating: u)
1391+
/// let v = Int16(truncatingIfNeeded: u)
13921392
/// // v == 21
13931393
/// // 'v' has a binary representation of 00000000_00010101
13941394
///
13951395
/// let w: Int8 = -21
13961396
/// // 'w' has a binary representation of 11101011
1397-
/// let x = Int16(extendingOrTruncating: w)
1397+
/// let x = Int16(truncatingIfNeeded: w)
13981398
/// // x == -21
13991399
/// // 'x' has a binary representation of 11111111_11101011
1400-
/// let y = UInt16(extendingOrTruncating: w)
1400+
/// let y = UInt16(truncatingIfNeeded: w)
14011401
/// // y == 65515
14021402
/// // 'y' has a binary representation of 11111111_11101011
14031403
///
14041404
/// - Parameter source: An integer to convert to this type.
1405-
init<T : BinaryInteger>(extendingOrTruncating source: T)
1405+
init<T : BinaryInteger>(truncatingIfNeeded source: T)
14061406

14071407
/// Creates a new instance with the representable value that's closest to the
14081408
/// given integer.
@@ -1675,16 +1675,16 @@ extension BinaryInteger {
16751675
// sign bit. Therefore it is safe to convert to the unsigned type.
16761676

16771677
if lhs.bitWidth < rhs.bitWidth {
1678-
return Other(extendingOrTruncating: lhs) == rhs
1678+
return Other(truncatingIfNeeded: lhs) == rhs
16791679
}
16801680
if lhs.bitWidth > rhs.bitWidth {
1681-
return lhs == Self(extendingOrTruncating: rhs)
1681+
return lhs == Self(truncatingIfNeeded: rhs)
16821682
}
16831683

16841684
if Self.isSigned {
1685-
return Other(extendingOrTruncating: lhs) == rhs
1685+
return Other(truncatingIfNeeded: lhs) == rhs
16861686
}
1687-
return lhs == Self(extendingOrTruncating: rhs)
1687+
return lhs == Self(truncatingIfNeeded: rhs)
16881688
}
16891689

16901690
/// Returns a Boolean value indicating whether the two given values are not
@@ -1735,14 +1735,14 @@ extension BinaryInteger {
17351735
// values of the other type. Otherwise, lhs and rhs are positive, and one
17361736
// of Self, Other may be signed and the other unsigned.
17371737

1738-
let rhsAsSelf = Self(extendingOrTruncating: rhs)
1738+
let rhsAsSelf = Self(truncatingIfNeeded: rhs)
17391739
let rhsAsSelfNegative = rhsAsSelf < (0 as Self)
17401740

17411741

17421742
// Can we round-trip rhs through Other?
1743-
if Other(extendingOrTruncating: rhsAsSelf) == rhs &&
1743+
if Other(truncatingIfNeeded: rhsAsSelf) == rhs &&
17441744
// This additional check covers the `Int8.max < (128 as UInt8)` case.
1745-
// Since the types are of the same width, init(extendingOrTruncating:)
1745+
// Since the types are of the same width, init(truncatingIfNeeded:)
17461746
// will result in a simple bitcast, so that rhsAsSelf would be -128, and
17471747
// `lhs < rhsAsSelf` will return false.
17481748
// We basically guard against that bitcast by requiring rhs and rhsAsSelf
@@ -1751,7 +1751,7 @@ extension BinaryInteger {
17511751
return lhs < rhsAsSelf
17521752
}
17531753

1754-
return Other(extendingOrTruncating: lhs) < rhs
1754+
return Other(truncatingIfNeeded: lhs) < rhs
17551755
}
17561756

17571757
/// Returns a Boolean value indicating whether the value of the first
@@ -1861,7 +1861,7 @@ extension BinaryInteger {
18611861
/// var binaryString: String {
18621862
/// var result: [String] = []
18631863
/// for i in 0..<(Self.bitWidth / 8) {
1864-
/// let byte = UInt8(extendingOrTruncating: self >> (i * 8))
1864+
/// let byte = UInt8(truncatingIfNeeded: self >> (i * 8))
18651865
/// let byteString = String(byte, radix: 2)
18661866
/// let padding = String(repeating: "0",
18671867
/// count: 8 - byteString.count)
@@ -2125,7 +2125,7 @@ ${operatorComment(x.operator, False)}
21252125
public static func ${x.operator} <
21262126
Other : BinaryInteger
21272127
>(lhs: Self, rhs: Other) -> Self {
2128-
return lhs ${x.operator} Self(extendingOrTruncating: rhs)
2128+
return lhs ${x.operator} Self(truncatingIfNeeded: rhs)
21292129
}
21302130

21312131
// Heterogeneous masking shift assignment
@@ -2191,7 +2191,7 @@ ${operatorComment(x.nonMaskingOperator, True)}
21912191
let overshiftL: Self = 0
21922192
if _fastPath(rhs >= 0) {
21932193
if _fastPath(rhs < Self.bitWidth) {
2194-
return lhs ${x.operator} Self(extendingOrTruncating: rhs)
2194+
return lhs ${x.operator} Self(truncatingIfNeeded: rhs)
21952195
}
21962196
return overshift${'LR'[isRightShift]}
21972197
}
@@ -2214,7 +2214,7 @@ extension FixedWidthInteger {
22142214
else if _slowPath(source > Self.max) {
22152215
self = Self.max
22162216
}
2217-
else { self = Self(extendingOrTruncating: source) }
2217+
else { self = Self(truncatingIfNeeded: source) }
22182218
}
22192219

22202220
% for x in binaryArithmetic['Numeric'] + binaryArithmetic["BinaryInteger"][:1]:
@@ -2253,7 +2253,7 @@ ${unsafeOperationComment(x.operator)}
22532253
% end
22542254

22552255
@inline(__always)
2256-
public init<T : BinaryInteger>(extendingOrTruncating source: T) {
2256+
public init<T : BinaryInteger>(truncatingIfNeeded source: T) {
22572257
if Self.bitWidth <= ${word_bits} {
22582258
self = Self.init(_truncatingBits: source._lowWord)
22592259
}
@@ -2322,7 +2322,7 @@ extension UnsignedInteger {
23222322
/// A textual representation of this value.
23232323
public var description: String {
23242324
if self.bitWidth <= ${word_bits} {
2325-
return _uint64ToString(UInt64(extendingOrTruncating: self))
2325+
return _uint64ToString(UInt64(truncatingIfNeeded: self))
23262326
}
23272327
if self == (0 as Self) {
23282328
return "0"
@@ -2342,7 +2342,7 @@ extension UnsignedInteger {
23422342
x /= 10
23432343
buf.append(
23442344
Unicode.Scalar(
2345-
ascii0 + Int(UInt(extendingOrTruncating: r)._value))!)
2345+
ascii0 + Int(UInt(truncatingIfNeeded: r)._value))!)
23462346
}
23472347
while x != (0 as Self)
23482348
return String(buf.reversed().lazy.map { Character($0) })
@@ -2362,7 +2362,7 @@ extension UnsignedInteger where Self : FixedWidthInteger {
23622362
_precondition(source <= Self.max,
23632363
"Not enough bits to represent a signed value")
23642364
}
2365-
self.init(extendingOrTruncating: source)
2365+
self.init(truncatingIfNeeded: source)
23662366
}
23672367

23682368
@_semantics("optimize.sil.specialize.generic.partial.never")
@@ -2377,7 +2377,7 @@ extension UnsignedInteger where Self : FixedWidthInteger {
23772377
source > Self.max {
23782378
return nil
23792379
}
2380-
self.init(extendingOrTruncating: source)
2380+
self.init(truncatingIfNeeded: source)
23812381
}
23822382

23832383
/// The maximum representable integer in this type.
@@ -2415,7 +2415,7 @@ extension SignedInteger {
24152415
/// A textual representation of this value.
24162416
public var description: String {
24172417
if self.bitWidth <= ${word_bits} {
2418-
return _int64ToString(Int64(extendingOrTruncating: self))
2418+
return _int64ToString(Int64(truncatingIfNeeded: self))
24192419
}
24202420

24212421
let base = magnitude.description
@@ -2444,7 +2444,7 @@ extension SignedInteger where Self : FixedWidthInteger {
24442444
_precondition(source <= Self.max,
24452445
"Not enough bits to represent a signed value")
24462446
}
2447-
self.init(extendingOrTruncating: source)
2447+
self.init(truncatingIfNeeded: source)
24482448
}
24492449

24502450
@_semantics("optimize.sil.specialize.generic.partial.never")
@@ -2460,7 +2460,7 @@ extension SignedInteger where Self : FixedWidthInteger {
24602460
source > Self.max {
24612461
return nil
24622462
}
2463-
self.init(extendingOrTruncating: source)
2463+
self.init(truncatingIfNeeded: source)
24642464
}
24652465

24662466
/// The maximum representable integer in this type.
@@ -2954,8 +2954,8 @@ extension ${Self} : Hashable {
29542954
% elif bits == word_bits * 2:
29552955
// We have twice as many bits as we need to return.
29562956
return
2957-
Int(extendingOrTruncating: self) ^
2958-
Int(extendingOrTruncating: self &>> 32)
2957+
Int(truncatingIfNeeded: self) ^
2958+
Int(truncatingIfNeeded: self &>> 32)
29592959
% else:
29602960
_Unimplemented()
29612961
% end
@@ -2986,7 +2986,7 @@ extension ${Self} {
29862986
///
29872987
/// - Parameter source: An integer to use as the source of the new value's
29882988
/// bit pattern.
2989-
@available(swift, obsoleted: 4.0, renamed: "init(extendingOrTruncating:)")
2989+
@available(swift, obsoleted: 4.0, renamed: "init(truncatingIfNeeded:)")
29902990
@_transparent
29912991
public init(truncatingBitPattern source: ${Src}) {
29922992
let src = source._value

stdlib/public/core/SipHash.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ struct ${Self} {
248248
internal mutating func _finalizeAndReturnIntHash() -> Int {
249249
let hash: UInt64 = finalizeAndReturnHash()
250250
#if arch(i386) || arch(arm)
251-
return Int(extendingOrTruncating: hash)
251+
return Int(truncatingIfNeeded: hash)
252252
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
253253
return Int(Int64(bitPattern: hash))
254254
#endif

0 commit comments

Comments
 (0)