Skip to content

Commit f03d6e5

Browse files
committed
Rename string reflection init
1 parent 3c9eba5 commit f03d6e5

15 files changed

+65
-13
lines changed

benchmark/single-source/unit-tests/ObjectiveCBridging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public func run_ObjectiveCBridgeFromNSStringForced(_ N: Int) {
7373

7474
@inline(never)
7575
func testObjectiveCBridgeToNSString() {
76-
let nativeString = String("Native")
76+
let nativeString = "Native"
7777

7878
var s: NSString?
7979
for _ in 0 ..< 10_000 {

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public func == (lhs: TypeIdentifier, rhs: TypeIdentifier) -> Bool {
127127
extension TypeIdentifier
128128
: CustomStringConvertible, CustomDebugStringConvertible {
129129
public var description: String {
130-
return String(value)
130+
return String(describing: value)
131131
}
132132
public var debugDescription: String {
133133
return "TypeIdentifier(\(description))"

stdlib/private/StdlibUnittest/StringConvertible.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ extension CustomPrintableValue : CustomDebugStringConvertible {
112112
public func expectPrinted<T>(
113113
expectedOneOf patterns: [String], _ object: T, ${TRACE}
114114
) {
115-
let actual = String(object)
115+
let actual = String(describing: object)
116116
if !patterns.contains(actual) {
117117
expectationFailure(
118118
"expected: any of \(String(reflecting: patterns))\n"

stdlib/public/core/Bool.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ extension Bool : Equatable, Hashable {
146146
}
147147
}
148148

149+
extension Bool : LosslessStringConvertible {
150+
public init?(_ description: String) {
151+
if description == "true" {
152+
self = true
153+
} else if description == "false" {
154+
self = false
155+
} else {
156+
return nil
157+
}
158+
}
159+
}
160+
149161
//===----------------------------------------------------------------------===//
150162
// Operators
151163
//===----------------------------------------------------------------------===//

stdlib/public/core/Character.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@ public struct Character :
353353
internal var _representation: Representation
354354
}
355355

356+
extension Character : CustomStringConvertible {
357+
public var description: String {
358+
return String(self)
359+
}
360+
}
361+
362+
extension Character : LosslessStringConvertible {}
363+
356364
extension Character : CustomDebugStringConvertible {
357365
/// A textual representation of the character, suitable for debugging.
358366
public var debugDescription: String {

stdlib/public/core/ImplicitlyUnwrappedOptional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension ImplicitlyUnwrappedOptional : CustomStringConvertible {
4545
public var description: String {
4646
switch self {
4747
case .some(let value):
48-
return String(value)
48+
return String(describing: value)
4949
case .none:
5050
return "nil"
5151
}

stdlib/public/core/Mirror.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ extension String {
855855
/// }
856856
///
857857
/// let p = Point(x: 21, y: 30)
858-
/// print(String(p))
858+
/// print(String(describing: p))
859859
/// // Prints "Point(x: 21, y: 30)"
860860
///
861861
/// After adding `CustomStringConvertible` conformance by implementing the
@@ -867,11 +867,11 @@ extension String {
867867
/// }
868868
/// }
869869
///
870-
/// print(String(p))
870+
/// print(String(describing: p))
871871
/// // Prints "(21, 30)"
872872
///
873873
/// - SeeAlso: `String.init<Subject>(reflecting: Subject)`
874-
public init<Subject>(_ instance: Subject) {
874+
public init<Subject>(describing instance: Subject) {
875875
self.init()
876876
_print_unlocked(instance, &self)
877877
}

stdlib/public/core/OutputStream.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public protocol CustomStringConvertible {
164164
var description: String { get }
165165
}
166166

167+
public protocol LosslessStringConvertible : CustomStringConvertible {
168+
init?(_ description: String)
169+
}
170+
167171
/// A type with a customized textual representation suitable for debugging
168172
/// purposes.
169173
///

stdlib/public/core/StaticString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public struct StaticString
259259

260260
extension StaticString {
261261
public var customMirror: Mirror {
262-
return Mirror(reflecting: String(self))
262+
return Mirror(reflecting: String(describing: self))
263263
}
264264
}
265265

stdlib/public/core/String.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,23 @@ extension String {
973973
return _nativeUnicodeUppercaseString(self)
974974
#endif
975975
}
976+
977+
public // @testable
978+
init<T: LosslessStringConvertible>(_ v: T) {
979+
self = v.description
980+
}
981+
}
982+
983+
extension String : CustomStringConvertible {
984+
public var description: String {
985+
return self
986+
}
987+
}
988+
989+
extension String : LosslessStringConvertible {
990+
public init?(_ description: String) {
991+
self = description
992+
}
976993
}
977994

978995
extension String {

stdlib/public/core/StringInterpolation.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extension String : StringInterpolationConvertible {
6565
///
6666
/// - SeeAlso: `StringInterpolationConvertible`
6767
public init<T>(stringInterpolationSegment expr: T) {
68-
self = String(expr)
68+
self = String(describing: expr)
6969
}
7070

7171
% for Type in StreamableTypes:

stdlib/public/core/StringUTF16.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ extension String {
314314
return UTF16View(_core)
315315
}
316316
set {
317-
self = String(newValue)
317+
self = String(describing: newValue)
318318
}
319319
}
320320

stdlib/public/core/StringUTF8.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ extension String {
352352
return UTF8View(self._core)
353353
}
354354
set {
355-
self = String(newValue)
355+
self = String(describing: newValue)
356356
}
357357
}
358358

stdlib/public/core/UnicodeScalar.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public struct UnicodeScalar :
249249
extension UnicodeScalar : CustomStringConvertible, CustomDebugStringConvertible {
250250
/// An escaped textual representation of the Unicode scalar.
251251
public var description: String {
252-
return "\"\(escaped(asASCII: false))\""
252+
return String(value)
253253
}
254254
/// An escaped textual representation of the Unicode scalar, suitable for
255255
/// debugging.
@@ -258,6 +258,17 @@ extension UnicodeScalar : CustomStringConvertible, CustomDebugStringConvertible
258258
}
259259
}
260260

261+
extension UnicodeScalar : LosslessStringConvertible {
262+
public init?(_ description: String) {
263+
if let v = UInt32(description) where (v < 0xD800 || v > 0xDFFF)
264+
&& v <= 0x10FFFF {
265+
self = UnicodeScalar(v)
266+
}
267+
268+
return nil
269+
}
270+
}
271+
261272
extension UnicodeScalar : Hashable {
262273
/// The Unicode scalar's hash value.
263274
///

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ extension Unsafe${Mutable}BufferPointer : CustomDebugStringConvertible {
219219
/// A textual representation of `self`, suitable for debugging.
220220
public var debugDescription: String {
221221
return "Unsafe${Mutable}BufferPointer"
222-
+ "(start: \(_position.map(String.init(_:)) ?? "nil"), count: \(count))"
222+
+ "(start: \(_position.map(String.init(describing:)) ?? "nil"), count: \(count))"
223223
}
224224
}
225225
%end

0 commit comments

Comments
 (0)