Skip to content

Commit 235064d

Browse files
authored
Merge pull request swiftlang#16 from lorentey/string_guts.rebase
Cherry-pick commits lost with latest rebase
2 parents 10f05c9 + 79b0514 commit 235064d

22 files changed

+418
-1515
lines changed

stdlib/public/core/Builtin.swift

-8
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,6 @@ extension ManagedBufferPointer {
579579
self._nativeBuffer = buffer
580580
}
581581
}
582-
extension _StringBuffer {
583-
@_inlineable
584-
public // FIXME: String Guts
585-
var _nativeObject: _BuiltinNativeObject {
586-
@inline(__always)
587-
get { return _storage._nativeBuffer }
588-
}
589-
}
590582

591583
/// Create a `BridgeObject` around the given `nativeObject` with the
592584
/// given spare bits.

stdlib/public/core/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ set(SWIFTLIB_ESSENTIAL
123123
StringHashable.swift # ORDER DEPENDENCY: Must precede String.swift
124124
String.swift
125125
StringBridge.swift
126-
StringBuffer.swift
127126
StringComparable.swift
128-
StringCore.swift
129127
StringGuts.swift
130128
StringObject.swift
131129
StringIndex.swift

stdlib/public/core/CString.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,14 @@ internal func _decodeCString<Encoding : _UnicodeEncoding>(
195195
-> (result: String, repairsMade: Bool)? {
196196

197197
let buffer = UnsafeBufferPointer<Encoding.CodeUnit>(
198-
start: cString, count: length)
198+
start: cString,
199+
count: length)
199200

200-
let (stringBuffer, hadError) = _StringBuffer.fromCodeUnits(
201-
buffer, encoding: encoding, repairIllFormedSequences: isRepairing)
202-
return stringBuffer.map {
203-
(result: String(_storage: $0), repairsMade: hadError)
201+
let (guts, hadError) = _StringGuts.fromCodeUnits(
202+
buffer,
203+
encoding: encoding,
204+
repairIllFormedSequences: isRepairing)
205+
return guts.map {
206+
(result: String($0), repairsMade: hadError)
204207
}
205208
}

stdlib/public/core/FixedArray.swift.gyb

+61-20
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,29 @@ internal struct _FixedArray${N}<T> {
3737
)
3838

3939
@_versioned // FIXME(sil-serialize-all)
40-
internal static var _arraySize : Int { return ${N} }
40+
var _count: Int8
41+
}
42+
43+
44+
extension _FixedArray${N} {
45+
@_inlineable // FIXME(sil-serialize-all)
46+
@_versioned // FIXME(sil-serialize-all)
47+
internal static var capacity: Int {
48+
@inline(__always) get { return ${N} }
49+
}
50+
51+
@_inlineable // FIXME(sil-serialize-all)
52+
@_versioned // FIXME(sil-serialize-all)
53+
internal var capacity: Int {
54+
@inline(__always) get { return ${N} }
55+
}
56+
57+
@_inlineable // FIXME(sil-serialize-all)
58+
@_versioned // FIXME(sil-serialize-all)
59+
internal var count : Int {
60+
@inline(__always) get { return Int(truncatingIfNeeded: _count) }
61+
@inline(__always) set { _count = Int8(newValue) }
62+
}
4163
}
4264

4365
extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
@@ -52,18 +74,17 @@ extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
5274
@_inlineable // FIXME(sil-serialize-all)
5375
@_versioned // FIXME(sil-serialize-all)
5476
internal var endIndex : Index {
55-
return _FixedArray${N}._arraySize
77+
return count
5678
}
5779

58-
@_versioned // FIXME(sil-serialize-all)
59-
internal var count : Int { return _FixedArray${N}._arraySize }
60-
6180
@_inlineable // FIXME(sil-serialize-all)
6281
@_versioned // FIXME(sil-serialize-all)
6382
internal subscript(i: Index) -> T {
6483
@_versioned
6584
@inline(__always)
6685
get {
86+
let count = self.count // for exclusive access
87+
_sanityCheck(i >= 0 && i < count)
6788
var copy = storage
6889
let res: T = withUnsafeBytes(of: &copy) {
6990
(rawPtr : UnsafeRawBufferPointer) -> T in
@@ -79,16 +100,9 @@ extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
79100
@_versioned
80101
@inline(__always)
81102
set {
82-
let count = self.count
83-
withUnsafeBytes(of: &storage) {
84-
(rawPtr : UnsafeRawBufferPointer) -> () in
85-
let rawPtr = UnsafeMutableRawBufferPointer(mutating: rawPtr)
86-
let stride = MemoryLayout<T>.stride
87-
_sanityCheck(rawPtr.count == ${N}*stride, "layout mismatch?")
88-
let bufPtr = UnsafeMutableBufferPointer(
89-
start: rawPtr.baseAddress!.assumingMemoryBound(to: T.self),
90-
count: count)
91-
bufPtr[i] = newValue
103+
_sanityCheck(i >= 0 && i < count)
104+
self.withUnsafeMutableBufferPointer { buffer in
105+
buffer[i] = newValue
92106
}
93107
}
94108
}
@@ -106,22 +120,45 @@ extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
106120
internal func index(before i: Index) -> Index {
107121
return i-1
108122
}
123+
}
109124

110-
// TODO: Any customization hooks it's profitable to override, e.g. append?
111-
125+
extension _FixedArray${N} {
126+
@_inlineable // FIXME(sil-serialize-all)
127+
@_versioned
128+
internal mutating func append(_ newElement: T) {
129+
_sanityCheck(count < capacity)
130+
self[count] = newElement
131+
_count += 1
132+
}
112133
}
113134

114135
extension _FixedArray${N} where T : ExpressibleByIntegerLiteral {
115136
@_inlineable // FIXME(sil-serialize-all)
116137
@_versioned // FIXME(sil-serialize-all)
117138
@inline(__always)
118-
internal init(allZeros: ()) {
139+
internal init(count: Int) {
140+
_sanityCheck(count >= 0 && count <= _FixedArray${N}.capacity)
119141
self.storage = (
120142
% for i in range(0, N-1):
121143
0,
122144
% end
123145
0
124146
)
147+
self._count = Int8(truncatingIfNeeded: count)
148+
}
149+
150+
@_inlineable // FIXME(sil-serialize-all)
151+
@_versioned // FIXME(sil-serialize-all)
152+
@inline(__always)
153+
internal init() {
154+
self.init(count: 0)
155+
}
156+
157+
@_inlineable // FIXME(sil-serialize-all)
158+
@_versioned // FIXME(sil-serialize-all)
159+
@inline(__always)
160+
internal init(allZeros: ()) {
161+
self.init(count: ${N})
125162
}
126163
}
127164

@@ -131,8 +168,10 @@ extension _FixedArray${N} {
131168
internal mutating func withUnsafeMutableBufferPointer<R>(
132169
_ body: (UnsafeMutableBufferPointer<Element>) throws -> R
133170
) rethrows -> R {
134-
let count = self.count
171+
let count = self.count // for exclusive access
135172
return try withUnsafeMutableBytes(of: &storage) { rawBuffer in
173+
_sanityCheck(rawBuffer.count == ${N}*MemoryLayout<T>.stride,
174+
"layout mismatch?")
136175
let buffer = UnsafeMutableBufferPointer<Element>(
137176
start: rawBuffer.baseAddress._unsafelyUnwrappedUnchecked
138177
.assumingMemoryBound(to: Element.self),
@@ -146,8 +185,10 @@ extension _FixedArray${N} {
146185
internal mutating func withUnsafeBufferPointer<R>(
147186
_ body: (UnsafeBufferPointer<Element>) throws -> R
148187
) rethrows -> R {
149-
let count = self.count
188+
let count = self.count // for exclusive access
150189
return try withUnsafeBytes(of: &storage) { rawBuffer in
190+
_sanityCheck(rawBuffer.count == ${N}*MemoryLayout<T>.stride,
191+
"layout mismatch?")
151192
let buffer = UnsafeBufferPointer<Element>(
152193
start: rawBuffer.baseAddress._unsafelyUnwrappedUnchecked
153194
.assumingMemoryBound(to: Element.self),

stdlib/public/core/GroupInfo.json

-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
"StaticString.swift",
1313
"String.swift",
1414
"StringBridge.swift",
15-
"StringBuffer.swift",
1615
"StringCharacterView.swift",
1716
"StringComparable.swift",
18-
"StringCore.swift",
1917
"StringObject.swift",
2018
"StringGuts.swift",
2119
"StringGraphemeBreaking.swift",

stdlib/public/core/String.swift

+1-29
Original file line numberDiff line numberDiff line change
@@ -784,12 +784,6 @@ public struct String {
784784
init(_ _guts: _StringGuts) {
785785
self._guts = _guts
786786
}
787-
788-
public // @testable
789-
var _core: _LegacyStringCore {
790-
get { return _guts._legacyCore }
791-
set { self._guts = _StringGuts(newValue) }
792-
}
793787
}
794788

795789
extension String {
@@ -1048,28 +1042,6 @@ extension String {
10481042
Builtin.unreachable()
10491043
}
10501044

1051-
@_inlineable // FIXME(sil-serialize-all)
1052-
public // SPI(Foundation)
1053-
init(_storage: _StringBuffer) { // FIXME: Replace with _SwiftStringStorage
1054-
if _storage.elementWidth == 1 {
1055-
let native = _SwiftStringStorage<UInt8>.create(
1056-
capacity: _storage.capacity,
1057-
count: _storage.usedCount)
1058-
native.start.initialize(
1059-
from: _storage.start.assumingMemoryBound(to: UInt8.self),
1060-
count: _storage.usedCount)
1061-
_guts = _StringGuts(native)
1062-
} else {
1063-
let native = _SwiftStringStorage<UInt16>.create(
1064-
capacity: _storage.capacity,
1065-
count: _storage.usedCount)
1066-
native.start.initialize(
1067-
from: _storage.start.assumingMemoryBound(to: UInt16.self),
1068-
count: _storage.usedCount)
1069-
_guts = _StringGuts(native)
1070-
}
1071-
}
1072-
10731045
@_inlineable // FIXME(sil-serialize-all)
10741046
public
10751047
init<CodeUnit>(_storage: _SwiftStringStorage<CodeUnit>)
@@ -1281,7 +1253,7 @@ internal func _nativeUnicodeUppercaseString(_ str: String) -> String {
12811253
dest, Int32(utf16.count), // FIXME: handle overflow case
12821254
utf16.start, Int32(utf16.count))
12831255
}
1284-
return String(_storage: buffer)
1256+
return String(_storage: storage)
12851257
}
12861258
#endif
12871259

stdlib/public/core/StringBridge.swift

-62
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,6 @@ func _stdlib_binary_CFStringGetCharactersPtr(
4848
/// Loading Foundation initializes these function variables
4949
/// with useful values
5050

51-
/// Produces a `_StringBuffer` from a given subrange of a source
52-
/// `_CocoaString`, having the given minimum capacity.
53-
@_versioned // FIXME(sil-serialize-all)
54-
@inline(never) // Hide the CF dependency
55-
internal func _cocoaStringToContiguous(
56-
source: _CocoaString, range: Range<Int>, minimumCapacity: Int
57-
) -> _StringBuffer {
58-
_sanityCheck(_swift_stdlib_CFStringGetCharactersPtr(source) == nil,
59-
"Known contiguously stored strings should already be converted to Swift")
60-
61-
let startIndex = range.lowerBound
62-
let count = range.upperBound - startIndex
63-
64-
let buffer = _StringBuffer(capacity: max(count, minimumCapacity),
65-
initialSize: count, elementWidth: 2)
66-
67-
_swift_stdlib_CFStringGetCharacters(
68-
source, _swift_shims_CFRange(location: startIndex, length: count),
69-
buffer.start.assumingMemoryBound(to: _swift_shims_UniChar.self))
70-
71-
return buffer
72-
}
73-
7451
/// Copies the entire contents of a _CocoaString into contiguous
7552
/// storage of sufficient capacity.
7653
@_versioned // FIXME(sil-serialize-all)
@@ -193,40 +170,6 @@ func _makeCocoaStringGuts(_ cocoaString: _CocoaString) -> _StringGuts {
193170
start: start)
194171
}
195172

196-
@inline(never) // Hide the CF dependency
197-
internal
198-
func makeCocoaLegacyStringCore(_cocoaString: AnyObject) -> _LegacyStringCore {
199-
if let wrapped = _cocoaString as? _NSContiguousString {
200-
return wrapped._core
201-
}
202-
203-
// "copy" it into a value to be sure nobody will modify behind
204-
// our backs. In practice, when value is already immutable, this
205-
// just does a retain.
206-
let cfImmutableValue
207-
= _stdlib_binary_CFStringCreateCopy(_cocoaString) as AnyObject
208-
209-
let length = _swift_stdlib_CFStringGetLength(cfImmutableValue)
210-
211-
// Look first for null-terminated ASCII
212-
// Note: the code in clownfish appears to guarantee
213-
// nul-termination, but I'm waiting for an answer from Chris Kane
214-
// about whether we can count on it for all time or not.
215-
let nulTerminatedASCII = _swift_stdlib_CFStringGetCStringPtr(
216-
cfImmutableValue, kCFStringEncodingASCII)
217-
218-
// start will hold the base pointer of contiguous storage, if it
219-
// is found.
220-
let (start, isUTF16) = _getCocoaStringPointer(cfImmutableValue)
221-
222-
return _LegacyStringCore(
223-
baseAddress: UnsafeMutableRawPointer(mutating: start),
224-
count: length,
225-
elementShift: isUTF16 ? 1 : 0,
226-
hasCocoaBuffer: true,
227-
owner: cfImmutableValue)
228-
}
229-
230173
extension String {
231174
public // SPI(Foundation)
232175
init(_cocoaString: AnyObject) {
@@ -405,11 +348,6 @@ public final class _NSContiguousString : _SwiftNativeNSString, _NSStringCore {
405348
}
406349
return try body(selfAsPointer, rhsAsPointer)
407350
}
408-
409-
public // @testable
410-
var _core: _LegacyStringCore {
411-
return _guts._legacyCore
412-
}
413351
}
414352

415353
extension String {

0 commit comments

Comments
 (0)