Skip to content

[stdlib] Switch Int/Float inits + join to be on StringProtocol #11157

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

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/public/core/FloatingPointParsing.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension ${Self} : LosslessStringConvertible {
/// - Parameter text: The input string to convert to a `${Self}` instance. If
/// `text` has invalid characters or is in an invalid format, the result
/// is `nil`.
public init?(_ text: String) {
public init?<S: StringProtocol>(_ text: S) {
let u16 = text.utf16
func parseNTBS(_ chars: UnsafePointer<CChar>) -> (${Self}, Int) {
var result: ${Self} = 0
Expand All @@ -133,7 +133,7 @@ extension ${Self} : LosslessStringConvertible {
return (result, endPtr == nil ? 0 : endPtr! - chars)
}

let (result, n) = text.withCString(parseNTBS)
let (result, n) = text._ephemeralString.withCString(parseNTBS)

if n == 0 || n != u16.count
|| u16.contains(where: { $0 > 127 || _isspace_clocale($0) }) {
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/IntegerParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ extension FixedWidthInteger {
/// - radix: The radix, or base, to use for converting `text` to an integer
/// value. `radix` must be in the range `2...36`. The default is 10.
@_semantics("optimize.sil.specialize.generic.partial.never")
public init?/*<S : StringProtocol>*/(_ text: String, radix: Int = 10) {
public init?<S : StringProtocol>(_ text: S, radix: Int = 10) {
_precondition(2...36 ~= radix, "Radix not in range 2...36")
let r = Self(radix)
let s = text// ._ephemeralString
let s = text._ephemeralString
defer { _fixLifetime(s) }

let c = s._core
Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ extension String {
}
}

extension Sequence where Element == String {
extension Sequence where Element: StringProtocol {

/// Returns a new string by concatenating the elements of the sequence,
/// adding the given separator between each element.
Expand Down Expand Up @@ -914,7 +914,7 @@ extension Sequence where Element == String {
for chunk in self {
// FIXME(performance): this code assumes UTF-16 in-memory representation.
// It should be switched to low-level APIs.
r += separatorSize + chunk.utf16.count
r += separatorSize + chunk._ephemeralString.utf16.count
}
return r - separatorSize
}
Expand All @@ -925,17 +925,17 @@ extension Sequence where Element == String {

if separatorSize == 0 {
for x in self {
result.append(x)
result.append(x._ephemeralString)
}
return result
}

var iter = makeIterator()
if let first = iter.next() {
result.append(first)
result.append(first._ephemeralString)
while let next = iter.next() {
result.append(separator)
result.append(next)
result.append(next._ephemeralString)
}
}

Expand Down