Skip to content

Fix a few stdlib coding convention violations #9568

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 2 commits into from
May 13, 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
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ set(SWIFT_BENCH_MODULES
single-source/StringMatch
single-source/StringTests
single-source/StringWalk
single-source/Substring
single-source/Suffix
single-source/SuperChars
single-source/TwoSum
Expand Down
61 changes: 61 additions & 0 deletions benchmark/single-source/Substring.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===--- Substring.swift --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils

// A string that doesn't fit in small string storage and doesn't fit in Latin-1
let longWide = "fὢasὢodὢijὢadὢolὢsjὢalὢsdὢjlὢasὢdfὢijὢliὢsdὢjøὢslὢdiὢalὢiὢ"

@inline(never)
public func run_SubstringFromLongString(_ N: Int) {
var s = longWide
s += "!" // ensure the string has a real buffer
for _ in 1...N*500 {
blackHole(Substring(s))
}
}

func create<T : RangeReplaceableCollection, U : Collection>(
_: T.Type, from source: U
) where T.Iterator.Element == U.Iterator.Element {
blackHole(T(source))
}

@inline(never)
public func run_SubstringFromLongStringGeneric(_ N: Int) {
var s = longWide
s += "!" // ensure the string has a real buffer
for _ in 1...N*500 {
create(Substring.self, from: s)
}
}

@inline(never)
public func run_StringFromLongWholeSubstring(_ N: Int) {
var s0 = longWide
s0 += "!" // ensure the string has a real buffer
let s = Substring(s0)
for _ in 1...N*500 {
blackHole(String(s))
}
}

@inline(never)
public func run_StringFromLongWholeSubstringGeneric(_ N: Int) {
var s0 = longWide
s0 += "!" // ensure the string has a real buffer
let s = Substring(s0)
for _ in 1...N*500 {
create(String.self, from: s)
}
}

5 changes: 5 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import StringInterpolation
import StringMatch
import StringTests
import StringWalk
import Substring
import Suffix
import SuperChars
import TwoSum
Expand Down Expand Up @@ -369,6 +370,8 @@ addTo(&precommitTests, "StringBuilder", run_StringBuilder)
addTo(&precommitTests, "StringBuilderLong", run_StringBuilderLong)
addTo(&precommitTests, "StringEdits", run_StringEdits)
addTo(&precommitTests, "StringEqualPointerComparison", run_StringEqualPointerComparison)
addTo(&precommitTests, "StringFromLongWholeSubstring", run_StringFromLongWholeSubstring)
addTo(&precommitTests, "StringFromLongWholeSubstringGeneric", run_StringFromLongWholeSubstringGeneric)
addTo(&precommitTests, "StringHasPrefix", run_StringHasPrefix)
addTo(&precommitTests, "StringHasPrefixUnicode", run_StringHasPrefixUnicode)
addTo(&precommitTests, "StringHasSuffix", run_StringHasSuffix)
Expand All @@ -378,6 +381,8 @@ addTo(&precommitTests, "StringMatch", run_StringMatch)
addTo(&precommitTests, "StringUTF16Builder", run_StringUTF16Builder)
addTo(&precommitTests, "StringWalk", run_StringWalk)
addTo(&precommitTests, "StringWithCString", run_StringWithCString)
addTo(&precommitTests, "SubstringFromLongString", run_SubstringFromLongString)
addTo(&precommitTests, "SubstringFromLongStringGeneric", run_SubstringFromLongStringGeneric)
addTo(&precommitTests, "SuffixAnyCollection", run_SuffixAnyCollection)
addTo(&precommitTests, "SuffixAnyCollectionLazy", run_SuffixAnyCollectionLazy)
addTo(&precommitTests, "SuffixAnySeqCRangeIter", run_SuffixAnySeqCRangeIter)
Expand Down
60 changes: 39 additions & 21 deletions stdlib/public/core/Range.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public protocol RangeExpression {
/// its bounds. Callers should apply the same preconditions
/// to the return value as they would to a range provided
/// directly by the user.
func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound
func relative<C: _Indexable>(
to collection: C
) -> Range<Bound> where C.Index == Bound

func contains(_ element: Bound) -> Bool
}
Expand All @@ -37,8 +39,8 @@ extension RangeExpression {
}
}

// FIXME(ABI)#55 (Statically Unavailable/Dynamically Available): remove this type, it creates an ABI burden
// on the library.
// FIXME(ABI)#55 (Statically Unavailable/Dynamically Available): remove this
// type, it creates an ABI burden on the library.
//
// A dummy type that we can use when we /don't/ want to create an
// ambiguity indexing CountableRange<T> outside a generic context.
Expand Down Expand Up @@ -241,8 +243,8 @@ public struct CountableRange<Bound> : RandomAccessCollection
// such as x = r[0], which will trap unless 0 happens to be contained in the
// range r.
//
// FIXME(ABI)#56 (Statically Unavailable/Dynamically Available): remove this code, it creates an ABI burden
// on the library.
// FIXME(ABI)#56 (Statically Unavailable/Dynamically Available): remove this
// code, it creates an ABI burden on the library.
extension CountableRange {
/// Accesses the element at specified position.
///
Expand Down Expand Up @@ -432,7 +434,8 @@ def get_init_warning(Self, OtherSelf):
% for (Self, op) in all_range_types:
% for (OtherSelf, other_op) in all_range_types:
extension ${Self}
% if ('Countable' in OtherSelf or 'Closed' in OtherSelf or 'Closed' in Self) and not 'Countable' in Self:
% if ('Countable' in OtherSelf or 'Closed' in OtherSelf or 'Closed' in Self) \
% and not 'Countable' in Self:
where
Bound : _Strideable, Bound.Stride : SignedInteger
% end
Expand Down Expand Up @@ -541,14 +544,15 @@ extension ${Self} {
}

extension ${Self}: RangeExpression {
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
where C.Index == Bound {
% if 'Closed' in Self:
return Range(uncheckedBounds:
(lower: lowerBound, upper: collection.index(after: self.upperBound))
return Range(
uncheckedBounds: (
lower: lowerBound, upper: collection.index(after: self.upperBound)))
% else:
return Range(uncheckedBounds: (lower: lowerBound, upper: upperBound)
return Range(uncheckedBounds: (lower: lowerBound, upper: upperBound))
% end
)
}
}

Expand Down Expand Up @@ -656,8 +660,8 @@ extension ${Self} : Equatable {
% 'Range',
% 'ClosedRange',
% ]:
// FIXME(ABI)#57 (Conditional Conformance): replace this extension with a conditional
// conformance.
// FIXME(ABI)#57 (Conditional Conformance): replace this extension with a
// conditional conformance.
// rdar://problem/17144340
/// Ranges whose `Bound` is `Strideable` with `Integer` `Stride` have all
/// the capabilities of `RandomAccessCollection`s, just like
Expand Down Expand Up @@ -739,23 +743,30 @@ public func ..< <Bound>(
@_fixed_layout
public struct PartialRangeUpTo<Bound: Comparable>: RangeExpression {
public init(_ upperBound: Bound) { self.upperBound = upperBound }

public let upperBound: Bound

@_transparent
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
where C.Index == Bound {
return collection.startIndex..<self.upperBound
}

@_transparent
public func contains(_ element: Bound) -> Bool {
return element < upperBound
}
}

@_fixed_layout
public struct PartialRangeThrough<Bound: Comparable>: RangeExpression {
public struct PartialRangeThrough<Bound: Comparable>: RangeExpression {
public init(_ upperBound: Bound) { self.upperBound = upperBound }

public let upperBound: Bound

@_transparent
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
where C.Index == Bound {
return collection.startIndex..<collection.index(after: self.upperBound)
}
@_transparent
Expand All @@ -767,11 +778,15 @@ public struct PartialRangeThrough<Bound: Comparable>: RangeExpression {
@_fixed_layout
public struct PartialRangeFrom<Bound: Comparable>: RangeExpression {
public init(_ lowerBound: Bound) { self.lowerBound = lowerBound }

public let lowerBound: Bound

@_transparent
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
where C.Index == Bound {
return self.lowerBound..<collection.endIndex
}

@_transparent
public func contains(_ element: Bound) -> Bool {
return lowerBound <= element
Expand Down Expand Up @@ -908,20 +923,23 @@ extension Strideable where Stride: SignedInteger {
/// - Parameters:
/// - minimum: The lower bound for the range.
@_transparent
public static postfix func ...(minimum: Self) -> CountablePartialRangeFrom<Self> {
return CountablePartialRangeFrom(minimum)
public static postfix func ...(minimum: Self)
-> CountablePartialRangeFrom<Self> {
return CountablePartialRangeFrom(minimum)
}
}

extension _Indexable {
@_inlineable
public subscript<R: RangeExpression>(r: R) -> SubSequence where R.Bound == Index {
public subscript<R: RangeExpression>(r: R)
-> SubSequence where R.Bound == Index {
return self[r.relative(to: self)]
}
}
extension _MutableIndexable {
@_inlineable
public subscript<R: RangeExpression>(r: R) -> SubSequence where R.Bound == Index {
public subscript<R: RangeExpression>(r: R) -> SubSequence
where R.Bound == Index {
get {
return self[r.relative(to: self)]
}
Expand Down