Skip to content

Commit e5864b2

Browse files
frootloopsCodaFi
authored andcommitted
Rename string reflection init
1 parent b0fdb80 commit e5864b2

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
@@ -255,7 +255,7 @@ public struct StaticString
255255

256256
extension StaticString {
257257
public var customMirror: Mirror {
258-
return Mirror(reflecting: String(self))
258+
return Mirror(reflecting: String(describing: self))
259259
}
260260
}
261261

stdlib/public/core/String.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,23 @@ extension String {
981981
return _nativeUnicodeUppercaseString(self)
982982
#endif
983983
}
984+
985+
public // @testable
986+
init<T: LosslessStringConvertible>(_ v: T) {
987+
self = v.description
988+
}
989+
}
990+
991+
extension String : CustomStringConvertible {
992+
public var description: String {
993+
return self
994+
}
995+
}
996+
997+
extension String : LosslessStringConvertible {
998+
public init?(_ description: String) {
999+
self = description
1000+
}
9841001
}
9851002

9861003
extension String {

0 commit comments

Comments
 (0)