Skip to content

Commit b06c636

Browse files
authored
Merge pull request #18456 from milseman/internalable
[string] Internalize many faux-testable declarations
2 parents d9fbde5 + 336ae86 commit b06c636

16 files changed

+196
-126
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@ set(SWIFTLIB_ESSENTIAL
134134
StringObject.swift
135135
StringProtocol.swift
136136
StringIndex.swift
137+
StringIndexConversions.swift
137138
StringInterpolation.swift
138139
StringLegacy.swift
140+
StringNormalization.swift
139141
StringRangeReplaceableCollection.swift
140142
StringStorage.swift
141143
StringSwitch.swift
142-
StringIndexConversions.swift
143-
StringNormalization.swift
144+
StringTesting.swift
144145
StringUnicodeScalarView.swift
145146
StringUTF16View.swift
146147
StringUTF8View.swift

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"StringRangeReplaceableCollection.swift",
3333
"StringStorage.swift",
3434
"StringSwitch.swift",
35+
"StringTesting.swift",
3536
"StringUTF16View.swift",
3637
"StringUTF8View.swift",
3738
"StringUnicodeScalarView.swift",

stdlib/public/core/SmallString.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func unsupportedOn32bit() -> Never { _conditionallyUnreachable() }
3131

3232
#else
3333
@_fixed_layout
34-
public // @testable
35-
struct _SmallUTF8String {
34+
@usableFromInline
35+
internal struct _SmallUTF8String {
3636
@usableFromInline
3737
typealias _RawBitPattern = (low: UInt, high: UInt)
3838

@@ -61,12 +61,11 @@ struct _SmallUTF8String {
6161
//
6262
extension _SmallUTF8String {
6363
@inlinable
64-
public // @testable
65-
static var capacity: Int { return 15 }
64+
static internal var capacity: Int { return 15 }
6665

6766
#if _runtime(_ObjC)
68-
public // @testable
69-
init?(_cocoaString cocoa: _CocoaString) {
67+
@usableFromInline
68+
internal init?(_cocoaString cocoa: _CocoaString) {
7069
#if arch(i386) || arch(arm)
7170
return nil // Never form small strings on 32-bit
7271
#else
@@ -89,8 +88,7 @@ extension _SmallUTF8String {
8988
#endif // _runtime(_ObjC)
9089

9190
@inlinable
92-
public // @testable
93-
init?<C: RandomAccessCollection>(_ codeUnits: C) where C.Element == UInt16 {
91+
internal init?<C: RandomAccessCollection>(_ codeUnits: C) where C.Element == UInt16 {
9492
#if arch(i386) || arch(arm)
9593
return nil // Never form small strings on 32-bit
9694
#else

stdlib/public/core/String.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ extension String {
540540
/// [equivalence]: http://www.unicode.org/glossary/#canonical_equivalent
541541
@_fixed_layout
542542
public struct String {
543-
public var _guts: _StringGuts
543+
public // SPI(Foundation)
544+
var _guts: _StringGuts
544545

545546
/// Creates an empty string.
546547
///
@@ -555,8 +556,7 @@ public struct String {
555556
}
556557

557558
@inlinable // FIXME(sil-serialize-all)
558-
public // @testable
559-
init(_ _guts: _StringGuts) {
559+
internal init(_ _guts: _StringGuts) {
560560
self._guts = _guts
561561
}
562562
}

stdlib/public/core/StringBridge.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ internal func _bridgeASCIICocoaString(
136136
return length == numCharWritten ? count : nil
137137
}
138138

139-
public // @testable
140-
func _bridgeToCocoa(_ small: _SmallUTF8String) -> _CocoaString {
139+
@usableFromInline
140+
internal func _bridgeToCocoa(_ small: _SmallUTF8String) -> _CocoaString {
141141
return small.withUTF8CodeUnits { bufPtr in
142142
return _swift_stdlib_CFStringCreateWithBytes(
143143
nil, bufPtr.baseAddress._unsafelyUnwrappedUnchecked,

stdlib/public/core/StringGuts.swift

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ extension _StringGuts {
100100

101101
@inlinable
102102
@inline(__always)
103-
public // @testable
104-
mutating func _isUniqueNative() -> Bool {
103+
internal mutating func _isUniqueNative() -> Bool {
105104
guard _isNative else { return false }
106105
// Note that the isUnique test must be in a separate statement;
107106
// `isNative && _isUnique` always evaluates to false in debug builds,
@@ -121,8 +120,7 @@ extension _StringGuts {
121120

122121
extension _StringGuts {
123122
@inlinable
124-
public // @testable
125-
var isASCII: Bool {
123+
internal var isASCII: Bool {
126124
@inline(__always) get { return _object.isContiguousASCII }
127125
}
128126

@@ -135,40 +133,32 @@ extension _StringGuts {
135133
}
136134

137135
@inlinable
138-
public // @testable
139-
var _isNative: Bool {
136+
internal var _isNative: Bool {
140137
return _object.isNative
141138
}
142139

143-
#if _runtime(_ObjC)
144140
@inlinable
145-
public // @testable
146-
var _isCocoa: Bool {
141+
internal var _isCocoa: Bool {
147142
return _object.isCocoa
148143
}
149-
#endif
150144

151145
@inlinable
152-
public // @testable
153-
var _isUnmanaged: Bool {
146+
internal var _isUnmanaged: Bool {
154147
return _object.isUnmanaged
155148
}
156149

157150
@inlinable
158-
public // @testable
159-
var _isSmall: Bool {
151+
internal var _isSmall: Bool {
160152
return _object.isSmall
161153
}
162154

163155
@inlinable
164-
public // @testable
165-
var _owner: AnyObject? {
156+
internal var _owner: AnyObject? {
166157
return _object.owner
167158
}
168159

169160
@inlinable
170-
public // @testable
171-
var isSingleByte: Bool {
161+
internal var isSingleByte: Bool {
172162
// FIXME: Currently used to sometimes mean contiguous ASCII
173163
return _object.isSingleByte
174164
}
@@ -180,8 +170,7 @@ extension _StringGuts {
180170
}
181171

182172
@inlinable
183-
public // @testable
184-
var byteWidth: Int {
173+
internal var byteWidth: Int {
185174
return _object.byteWidth
186175
}
187176

@@ -215,8 +204,7 @@ extension _StringGuts {
215204
extension _StringGuts {
216205
@inlinable
217206
@inline(__always)
218-
public // @testable
219-
init() {
207+
internal init() {
220208
self.init(object: _StringObject(), otherBits: 0)
221209
_invariantCheck()
222210
}
@@ -474,8 +462,8 @@ extension _StringGuts {
474462
/// Return the object identifier for the reference counted heap object
475463
/// referred to by this string (if any). This is useful for testing allocation
476464
/// behavior.
477-
public // @testable
478-
var _objectIdentifier: ObjectIdentifier? {
465+
@usableFromInline
466+
internal var _objectIdentifier: ObjectIdentifier? {
479467
if _object.isNative {
480468
return ObjectIdentifier(_object.nativeRawStorage)
481469
}
@@ -793,8 +781,7 @@ extension _StringGuts {
793781
}
794782

795783
@inlinable
796-
public // @testable
797-
var count: Int {
784+
internal var count: Int {
798785
if _slowPath(!_hasStoredCount) {
799786
return _nonStoredCount
800787
}
@@ -823,8 +810,7 @@ extension _StringGuts {
823810
}
824811

825812
@inlinable
826-
public // @testable
827-
var capacity: Int {
813+
internal var capacity: Int {
828814
if _fastPath(_object.isNative) {
829815
return _object.nativeRawStorage.capacity
830816
}
@@ -839,8 +825,7 @@ extension _StringGuts {
839825

840826
/// Get the UTF-16 code unit stored at the specified position in this string.
841827
@inlinable // FIXME(sil-serialize-all)
842-
public // @testable
843-
subscript(position: Int) -> UTF16.CodeUnit {
828+
internal subscript(position: Int) -> UTF16.CodeUnit {
844829
if _slowPath(_isOpaque) {
845830
return _opaquePosition(position)
846831
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
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+
// Declarations to enable ease-of-testing
14+
15+
public // @testable
16+
struct _StringRepresentation {
17+
public var _isASCII: Bool
18+
public var _count: Int
19+
public var _capacity: Int
20+
21+
public enum _Form {
22+
case _small
23+
case _cocoa(object: AnyObject)
24+
case _native(object: AnyObject)
25+
case _immortal(address: UInt)
26+
}
27+
public var _form: _Form
28+
29+
public var _objectIdentifier: ObjectIdentifier? {
30+
switch _form {
31+
case ._cocoa(let object): return ObjectIdentifier(object)
32+
case ._native(let object): return ObjectIdentifier(object)
33+
default: return nil
34+
}
35+
}
36+
}
37+
38+
extension String {
39+
public // @testable
40+
func _classify() -> _StringRepresentation {
41+
var result = _StringRepresentation(
42+
_isASCII: _guts._isASCIIOrSmallASCII,
43+
_count: _guts.count,
44+
_capacity: _guts.capacity,
45+
_form: ._small
46+
)
47+
if _guts._isSmall {
48+
return result
49+
}
50+
if _guts._isNative {
51+
result._form = ._native(object: _guts._owner!)
52+
return result
53+
}
54+
if _guts._isCocoa {
55+
result._form = ._cocoa(object: _guts._owner!)
56+
return result
57+
}
58+
if _guts._isUnmanaged {
59+
result._form = ._immortal(
60+
address: UInt(bitPattern: _guts._unmanagedRawStart))
61+
return result
62+
}
63+
fatalError()
64+
}
65+
}
66+

test/stdlib/BridgedObjectDebuggerSupport.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ func debugVal<T>(_ x: inout T) -> String {
3030

3131
// Check if @x uses the small-string or Cocoa representations.
3232
func hasSmallStringOrCocoaVariant(_ x: String) -> Bool {
33-
return x._guts._isCocoa || x._guts._isSmall
33+
switch x._classify()._form {
34+
case ._small: return true
35+
case ._cocoa: return true
36+
default: return false
37+
}
3438
}
3539

3640
StringForPrintObjectTests.test("Basic") {

test/stdlib/Character.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ func checkUnicodeScalars(_ s: String) {
256256
}
257257

258258
func checkRepresentation(_ s: String) {
259+
let utf16 = Array(s.utf16)
259260
let expectSmall
260-
= s.utf16.count < 4 || s.utf16.count == 4 && s._guts[3] < 0x8000
261+
= utf16.count < 4 || utf16.count == 4 && utf16[3] < 0x8000
261262
let isSmall = isSmallRepresentation(s)
262263

263264
let expectedSize = expectSmall ? "small" : "large"

0 commit comments

Comments
 (0)