-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[stdlib][SE-0089] Finish off Lossless String Conversion #3761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ internal func _isspace_clocale(_ u: UTF16.CodeUnit) -> Bool { | |
| % end | ||
|
|
||
| //===--- Parsing ----------------------------------------------------------===// | ||
| extension ${Self} { | ||
| extension ${Self} : LosslessStringConvertible { | ||
|
||
| /// Construct from an ASCII representation. | ||
| /// | ||
| /// Returns the result of calling the POSIX function | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -981,6 +981,24 @@ extension String { | |
| return _nativeUnicodeUppercaseString(self) | ||
| #endif | ||
| } | ||
|
|
||
| /// Creates an instance from the description of a given | ||
| /// `LosslessStringConvertible` instance. | ||
| public init<T : LosslessStringConvertible>(_ value: T) { | ||
| self = value.description | ||
| } | ||
| } | ||
|
|
||
| extension String : CustomStringConvertible { | ||
| public var description: String { | ||
| return self | ||
| } | ||
| } | ||
|
|
||
| extension String : LosslessStringConvertible { | ||
|
||
| public init?(_ description: String) { | ||
| self = description | ||
| } | ||
| } | ||
|
|
||
| extension String { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,17 +271,30 @@ public struct UnicodeScalar : | |
| } | ||
|
|
||
| extension UnicodeScalar : CustomStringConvertible, CustomDebugStringConvertible { | ||
| /// An escaped textual representation of the Unicode scalar. | ||
| /// A textual representation of the Unicode scalar. | ||
| public var description: String { | ||
| return "\"\(escaped(asASCII: false))\"" | ||
| return String._fromWellFormedCodeUnitSequence( | ||
| UTF32.self, | ||
| input: repeatElement(self.value, count: 1)) | ||
| } | ||
|
|
||
| /// An escaped textual representation of the Unicode scalar, suitable for | ||
| /// debugging. | ||
| public var debugDescription: String { | ||
| return "\"\(escaped(asASCII: true))\"" | ||
| } | ||
| } | ||
|
|
||
| extension UnicodeScalar : LosslessStringConvertible { | ||
|
||
| public init?(_ description: String) { | ||
| let scalars = description.unicodeScalars | ||
| guard let v = scalars.first, scalars.count == 1 else { | ||
| return nil | ||
| } | ||
| self = v | ||
| } | ||
| } | ||
|
|
||
| extension UnicodeScalar : Hashable { | ||
| /// The Unicode scalar's hash value. | ||
| /// | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure we have tests for this. It would be best if you could add a utility to
StdlibUnittestthat could test anyLosslessStringConvertibleandEquatabletype, by accepting a list of instances.