Skip to content

Commit 5d034a2

Browse files
authored
Merge pull request #9568 from apple/stdlib-coding-convention
2 parents 65b9043 + dfd9672 commit 5d034a2

File tree

4 files changed

+106
-21
lines changed

4 files changed

+106
-21
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ set(SWIFT_BENCH_MODULES
106106
single-source/StringMatch
107107
single-source/StringTests
108108
single-source/StringWalk
109+
single-source/Substring
109110
single-source/Suffix
110111
single-source/SuperChars
111112
single-source/TwoSum
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- Substring.swift --------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
// A string that doesn't fit in small string storage and doesn't fit in Latin-1
16+
let longWide = "fὢasὢodὢijὢadὢolὢsjὢalὢsdὢjlὢasὢdfὢijὢliὢsdὢjøὢslὢdiὢalὢiὢ"
17+
18+
@inline(never)
19+
public func run_SubstringFromLongString(_ N: Int) {
20+
var s = longWide
21+
s += "!" // ensure the string has a real buffer
22+
for _ in 1...N*500 {
23+
blackHole(Substring(s))
24+
}
25+
}
26+
27+
func create<T : RangeReplaceableCollection, U : Collection>(
28+
_: T.Type, from source: U
29+
) where T.Iterator.Element == U.Iterator.Element {
30+
blackHole(T(source))
31+
}
32+
33+
@inline(never)
34+
public func run_SubstringFromLongStringGeneric(_ N: Int) {
35+
var s = longWide
36+
s += "!" // ensure the string has a real buffer
37+
for _ in 1...N*500 {
38+
create(Substring.self, from: s)
39+
}
40+
}
41+
42+
@inline(never)
43+
public func run_StringFromLongWholeSubstring(_ N: Int) {
44+
var s0 = longWide
45+
s0 += "!" // ensure the string has a real buffer
46+
let s = Substring(s0)
47+
for _ in 1...N*500 {
48+
blackHole(String(s))
49+
}
50+
}
51+
52+
@inline(never)
53+
public func run_StringFromLongWholeSubstringGeneric(_ N: Int) {
54+
var s0 = longWide
55+
s0 += "!" // ensure the string has a real buffer
56+
let s = Substring(s0)
57+
for _ in 1...N*500 {
58+
create(String.self, from: s)
59+
}
60+
}
61+

benchmark/utils/main.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ import StringInterpolation
111111
import StringMatch
112112
import StringTests
113113
import StringWalk
114+
import Substring
114115
import Suffix
115116
import SuperChars
116117
import TwoSum
@@ -369,6 +370,8 @@ addTo(&precommitTests, "StringBuilder", run_StringBuilder)
369370
addTo(&precommitTests, "StringBuilderLong", run_StringBuilderLong)
370371
addTo(&precommitTests, "StringEdits", run_StringEdits)
371372
addTo(&precommitTests, "StringEqualPointerComparison", run_StringEqualPointerComparison)
373+
addTo(&precommitTests, "StringFromLongWholeSubstring", run_StringFromLongWholeSubstring)
374+
addTo(&precommitTests, "StringFromLongWholeSubstringGeneric", run_StringFromLongWholeSubstringGeneric)
372375
addTo(&precommitTests, "StringHasPrefix", run_StringHasPrefix)
373376
addTo(&precommitTests, "StringHasPrefixUnicode", run_StringHasPrefixUnicode)
374377
addTo(&precommitTests, "StringHasSuffix", run_StringHasSuffix)
@@ -378,6 +381,8 @@ addTo(&precommitTests, "StringMatch", run_StringMatch)
378381
addTo(&precommitTests, "StringUTF16Builder", run_StringUTF16Builder)
379382
addTo(&precommitTests, "StringWalk", run_StringWalk)
380383
addTo(&precommitTests, "StringWithCString", run_StringWithCString)
384+
addTo(&precommitTests, "SubstringFromLongString", run_SubstringFromLongString)
385+
addTo(&precommitTests, "SubstringFromLongStringGeneric", run_SubstringFromLongStringGeneric)
381386
addTo(&precommitTests, "SuffixAnyCollection", run_SuffixAnyCollection)
382387
addTo(&precommitTests, "SuffixAnyCollectionLazy", run_SuffixAnyCollectionLazy)
383388
addTo(&precommitTests, "SuffixAnySeqCRangeIter", run_SuffixAnySeqCRangeIter)

stdlib/public/core/Range.swift.gyb

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public protocol RangeExpression {
2525
/// its bounds. Callers should apply the same preconditions
2626
/// to the return value as they would to a range provided
2727
/// directly by the user.
28-
func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound
28+
func relative<C: _Indexable>(
29+
to collection: C
30+
) -> Range<Bound> where C.Index == Bound
2931

3032
func contains(_ element: Bound) -> Bool
3133
}
@@ -37,8 +39,8 @@ extension RangeExpression {
3739
}
3840
}
3941

40-
// FIXME(ABI)#55 (Statically Unavailable/Dynamically Available): remove this type, it creates an ABI burden
41-
// on the library.
42+
// FIXME(ABI)#55 (Statically Unavailable/Dynamically Available): remove this
43+
// type, it creates an ABI burden on the library.
4244
//
4345
// A dummy type that we can use when we /don't/ want to create an
4446
// ambiguity indexing CountableRange<T> outside a generic context.
@@ -241,8 +243,8 @@ public struct CountableRange<Bound> : RandomAccessCollection
241243
// such as x = r[0], which will trap unless 0 happens to be contained in the
242244
// range r.
243245
//
244-
// FIXME(ABI)#56 (Statically Unavailable/Dynamically Available): remove this code, it creates an ABI burden
245-
// on the library.
246+
// FIXME(ABI)#56 (Statically Unavailable/Dynamically Available): remove this
247+
// code, it creates an ABI burden on the library.
246248
extension CountableRange {
247249
/// Accesses the element at specified position.
248250
///
@@ -432,7 +434,8 @@ def get_init_warning(Self, OtherSelf):
432434
% for (Self, op) in all_range_types:
433435
% for (OtherSelf, other_op) in all_range_types:
434436
extension ${Self}
435-
% if ('Countable' in OtherSelf or 'Closed' in OtherSelf or 'Closed' in Self) and not 'Countable' in Self:
437+
% if ('Countable' in OtherSelf or 'Closed' in OtherSelf or 'Closed' in Self) \
438+
% and not 'Countable' in Self:
436439
where
437440
Bound : _Strideable, Bound.Stride : SignedInteger
438441
% end
@@ -541,14 +544,15 @@ extension ${Self} {
541544
}
542545

543546
extension ${Self}: RangeExpression {
544-
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
547+
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
548+
where C.Index == Bound {
545549
% if 'Closed' in Self:
546-
return Range(uncheckedBounds:
547-
(lower: lowerBound, upper: collection.index(after: self.upperBound))
550+
return Range(
551+
uncheckedBounds: (
552+
lower: lowerBound, upper: collection.index(after: self.upperBound)))
548553
% else:
549-
return Range(uncheckedBounds: (lower: lowerBound, upper: upperBound)
554+
return Range(uncheckedBounds: (lower: lowerBound, upper: upperBound))
550555
% end
551-
)
552556
}
553557
}
554558

@@ -656,8 +660,8 @@ extension ${Self} : Equatable {
656660
% 'Range',
657661
% 'ClosedRange',
658662
% ]:
659-
// FIXME(ABI)#57 (Conditional Conformance): replace this extension with a conditional
660-
// conformance.
663+
// FIXME(ABI)#57 (Conditional Conformance): replace this extension with a
664+
// conditional conformance.
661665
// rdar://problem/17144340
662666
/// Ranges whose `Bound` is `Strideable` with `Integer` `Stride` have all
663667
/// the capabilities of `RandomAccessCollection`s, just like
@@ -739,23 +743,30 @@ public func ..< <Bound>(
739743
@_fixed_layout
740744
public struct PartialRangeUpTo<Bound: Comparable>: RangeExpression {
741745
public init(_ upperBound: Bound) { self.upperBound = upperBound }
746+
742747
public let upperBound: Bound
748+
743749
@_transparent
744-
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
750+
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
751+
where C.Index == Bound {
745752
return collection.startIndex..<self.upperBound
746753
}
754+
747755
@_transparent
748756
public func contains(_ element: Bound) -> Bool {
749757
return element < upperBound
750758
}
751759
}
752760

753761
@_fixed_layout
754-
public struct PartialRangeThrough<Bound: Comparable>: RangeExpression {
762+
public struct PartialRangeThrough<Bound: Comparable>: RangeExpression {
755763
public init(_ upperBound: Bound) { self.upperBound = upperBound }
764+
756765
public let upperBound: Bound
766+
757767
@_transparent
758-
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
768+
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
769+
where C.Index == Bound {
759770
return collection.startIndex..<collection.index(after: self.upperBound)
760771
}
761772
@_transparent
@@ -767,11 +778,15 @@ public struct PartialRangeThrough<Bound: Comparable>: RangeExpression {
767778
@_fixed_layout
768779
public struct PartialRangeFrom<Bound: Comparable>: RangeExpression {
769780
public init(_ lowerBound: Bound) { self.lowerBound = lowerBound }
781+
770782
public let lowerBound: Bound
783+
771784
@_transparent
772-
public func relative<C: _Indexable>(to collection: C) -> Range<Bound> where C.Index == Bound {
785+
public func relative<C: _Indexable>(to collection: C) -> Range<Bound>
786+
where C.Index == Bound {
773787
return self.lowerBound..<collection.endIndex
774788
}
789+
775790
@_transparent
776791
public func contains(_ element: Bound) -> Bool {
777792
return lowerBound <= element
@@ -908,20 +923,23 @@ extension Strideable where Stride: SignedInteger {
908923
/// - Parameters:
909924
/// - minimum: The lower bound for the range.
910925
@_transparent
911-
public static postfix func ...(minimum: Self) -> CountablePartialRangeFrom<Self> {
912-
return CountablePartialRangeFrom(minimum)
926+
public static postfix func ...(minimum: Self)
927+
-> CountablePartialRangeFrom<Self> {
928+
return CountablePartialRangeFrom(minimum)
913929
}
914930
}
915931

916932
extension _Indexable {
917933
@_inlineable
918-
public subscript<R: RangeExpression>(r: R) -> SubSequence where R.Bound == Index {
934+
public subscript<R: RangeExpression>(r: R)
935+
-> SubSequence where R.Bound == Index {
919936
return self[r.relative(to: self)]
920937
}
921938
}
922939
extension _MutableIndexable {
923940
@_inlineable
924-
public subscript<R: RangeExpression>(r: R) -> SubSequence where R.Bound == Index {
941+
public subscript<R: RangeExpression>(r: R) -> SubSequence
942+
where R.Bound == Index {
925943
get {
926944
return self[r.relative(to: self)]
927945
}

0 commit comments

Comments
 (0)