Skip to content

Commit 9a6f3ef

Browse files
author
Dave Abrahams
committed
Revert WIP that was causing test/Prototypes/Unicode.swift to hang
1 parent 251a60a commit 9a6f3ef

File tree

1 file changed

+32
-94
lines changed

1 file changed

+32
-94
lines changed

stdlib/public/core/UnicodeStorage.swift

Lines changed: 32 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,6 @@ public func _debugLog(_ arg0: @autoclosure ()->Any, _ arg1: @autoclosure ()->Any
2222
print(arg0(), arg1())
2323
}
2424

25-
/// An index type for views onto random access collections whose elements are
26-
/// effectively variable-width.
27-
public protocol UnicodeIndexProtocol {
28-
29-
// FIXME: it's not clear that there is always enough information to construct
30-
// these from just an offset without also using the Collection into which they
31-
// are indexing (e.g. when an index caches information). If so, this
32-
// requirement would need to be replaced by a requirement on the collection.
33-
// In all such scenarios we've found *so far*, indices can have an empty cache
34-
// that will be filled on demand without loss of efficiency.
35-
init(codeUnitOffset: Int64)
36-
37-
var codeUnitOffset: Int64 { get }
38-
}
39-
40-
extension UnicodeIndexProtocol {
41-
public static func == (l: UnicodeIndexProtocol, r: UnicodeIndexProtocol) -> Bool {
42-
return l.codeUnitOffset == r.codeUnitOffset
43-
}
44-
public static func < (l: UnicodeIndexProtocol, r: UnicodeIndexProtocol) -> Bool {
45-
return l.codeUnitOffset < r.codeUnitOffset
46-
}
47-
48-
public static func == (l: Self, r: Self) -> Bool {
49-
return l.codeUnitOffset == r.codeUnitOffset
50-
}
51-
public static func < (l: Self, r: Self) -> Bool {
52-
return l.codeUnitOffset < r.codeUnitOffset
53-
}
54-
}
55-
5625
/// A collection of `CodeUnit`s to be interpreted by some `Encoding`
5726
public struct UnicodeStorage<
5827
CodeUnits : RandomAccessCollection,
@@ -91,55 +60,23 @@ extension UnicodeStorage.EncodedScalars {
9160
// and the next index. This would obviously be more complicated if
9261
// the buffer contained more than a single scalar (and it probably
9362
// should).
94-
public struct Index : UnicodeIndexProtocol, Comparable {
95-
let offset: CodeUnits.IndexDistance
63+
public struct Index : Comparable {
64+
let base: CodeUnits.Index
9665
// FIXME: We might get a much better memory footprint if we used a
9766
// UInt8 to store the distance between base and next, rather than
9867
// storing next explicitly. CodeUnits will be random-access in
9968
// practice.
100-
let nextStride: UInt8
101-
102-
public init(codeUnitOffset: Int64) {
103-
self.init(offset: numericCast(codeUnitOffset))
104-
}
105-
106-
internal init(
107-
offset: CodeUnits.IndexDistance,
108-
nextStride: UInt8 = 0,
109-
scalar: Encoding.EncodedScalar? = nil
110-
) {
111-
self.offset = offset
112-
self.nextStride = nextStride
113-
self.scalar = scalar
114-
}
115-
116-
public var codeUnitOffset: Int64 { return numericCast(offset) }
117-
118-
var nextOffset: CodeUnits.IndexDistance {
119-
return offset + numericCast(nextStride)
120-
}
121-
69+
let next: CodeUnits.Index
12270
// FIXME: there should be an invalid inhabitant we can use in
12371
// EncodedScalar so as not to waste a separate bool here.
12472
let scalar: Encoding.EncodedScalar?
125-
}
12673

127-
internal func _base(_ i: Index) -> CodeUnits.Index {
128-
return codeUnits.index(atOffset: i.offset)
129-
}
130-
131-
internal func _next(_ i: Index) -> CodeUnits.Index {
132-
return codeUnits.index(atOffset: i.nextOffset)
133-
}
134-
135-
internal func _index(
136-
base: CodeUnits.Index, next: CodeUnits.Index, scalar: Encoding.EncodedScalar?
137-
) -> Index {
138-
return Index(
139-
offset: codeUnits.offset(of: base),
140-
nextStride: numericCast(codeUnits[base..<next].count),
141-
scalar: scalar
142-
)
74+
public static func < (lhs: Index, rhs: Index) -> Bool {
75+
return lhs.base < rhs.base
76+
}
77+
public static func == (lhs: Index, rhs: Index) -> Bool {
78+
return lhs.base == rhs.base
79+
}
14380
}
14481
}
14582

@@ -148,34 +85,35 @@ extension UnicodeStorage.EncodedScalars : BidirectionalCollection {
14885
public var startIndex: Index {
14986
if _slowPath(codeUnits.isEmpty) { return endIndex }
15087
let s = codeUnits.startIndex
151-
return index(after: _index(base: s, next: s, scalar: nil))
88+
return index(after: Index(base: s, next: s, scalar: nil))
15289
}
15390

15491
public var endIndex: Index {
15592
let s = codeUnits.endIndex
156-
return _index(base: s, next: s, scalar: nil)
93+
return Index(base: s, next: s, scalar: nil)
15794
}
15895

15996
public subscript(i: Index) -> Encoding.EncodedScalar {
16097
if let r = i.scalar {
16198
return r
16299
}
163100
return index(after:
164-
_index(base: _base(i), next: _next(i), scalar: nil)
165-
).scalar!
101+
Index(base: i.base, next: i.base, scalar: nil)).scalar!
166102
}
167103

168104
public func index(after i: Index) -> Index {
169-
var remainder = codeUnits[_next(i)..<codeUnits.endIndex]
105+
var remainder = codeUnits[i.next..<codeUnits.endIndex]
170106
while true {
171107
switch Encoding.parse1Forward(remainder, knownCount: 0) {
172108
case .valid(let scalar, let nextIndex):
173-
return _index(base: _next(i), next: nextIndex, scalar: scalar)
109+
return Index(base:i.next, next: nextIndex, scalar: scalar)
174110
case .error(let nextIndex):
175111
// FIXME: don't go through UnicodeScalar once this is in the stdlib
176112
if let replacement = Encoding.encode(
177113
UTF32.EncodedScalar(UnicodeScalar(0xFFFD)!)) {
178-
return _index(base: _next(i), next: nextIndex, scalar: replacement)
114+
return Index(
115+
base:i.next, next: nextIndex,
116+
scalar: replacement)
179117
}
180118
// If we get here, the encoding couldn't represent a replacement
181119
// character, so the best we can do is to drop that scalar on the floor
@@ -188,16 +126,18 @@ extension UnicodeStorage.EncodedScalars : BidirectionalCollection {
188126
}
189127

190128
public func index(before i: Index) -> Index {
191-
var remainder = codeUnits[..<_base(i)]
129+
var remainder = codeUnits[..<i.base]
192130
while true {
193131
switch Encoding.parse1Reverse(remainder, knownCount: 0) {
194132
case .valid(let scalar, let priorIndex):
195-
return _index(base: priorIndex, next: _base(i), scalar: scalar)
133+
return Index(base: priorIndex, next: i.base, scalar: scalar)
196134
case .error(let priorIndex):
197135
// FIXME: don't go through UnicodeScalar once this is in the stdlib
198136
if let replacement = Encoding.encode(
199137
UTF32.EncodedScalar(UnicodeScalar(0xFFFD)!)) {
200-
return _index(base: priorIndex, next: _base(i), scalar: replacement)
138+
return Index(
139+
base: priorIndex, next: i.base,
140+
scalar: replacement)
201141
}
202142
// If we get here, the encoding couldn't represent a replacement
203143
// character, so the best we can do is to drop that scalar on the floor
@@ -237,6 +177,8 @@ extension UnicodeStorage {
237177
})
238178
}
239179

180+
// FIXME: this should go in the extension below but for <rdar://30320012>
181+
//typealias SubSequence = BidirectionalSlice<TranscodedView>
240182
public var startIndex : Base.Index {
241183
return base.startIndex
242184
}
@@ -282,7 +224,6 @@ extension UnicodeStorage : _UTextable {
282224
_ deep: Bool, _ status: UnsafeMutablePointer<_UErrorCode>?
283225
) -> UnsafeMutablePointer<_UText> {
284226
UnsafeMutablePointer(mutating: src)[0].validate()
285-
_sanityCheck(!deep, "deep cloning not supported")
286227
// _debugLog("_clone with dst = \(String(describing: dst))")
287228
// _debugLog("src: \(src[0])")
288229
let r = dst
@@ -294,14 +235,6 @@ extension UnicodeStorage : _UTextable {
294235
return r
295236
}
296237

297-
// A helper for translating indices out of the result of _parsedSuffix
298-
internal var _indexBase
299-
: UnicodeStorage<CodeUnits.SubSequence, Encoding>.EncodedScalars {
300-
return UnicodeStorage<
301-
CodeUnits.SubSequence, Encoding
302-
>(codeUnits[...]).scalars
303-
}
304-
305238
internal func _access(
306239
_ u: inout _UText, _ nativeTargetIndex: Int64, _ forward: Bool
307240
) -> Bool {
@@ -347,8 +280,9 @@ extension UnicodeStorage : _UTextable {
347280
buffer[u.chunkLength^] = unit
348281
u.chunkLength += 1
349282
}
350-
u.chunkNativeLimit = codeUnits.offset(of: _indexBase._next(i))^
283+
u.chunkNativeLimit = codeUnits.offset(of: i.next)^
351284
}
285+
_sanityCheck(!deep, "deep cloning not supported")
352286
}
353287
else {
354288
let chunkSource
@@ -366,7 +300,7 @@ extension UnicodeStorage : _UTextable {
366300
buffer[u.chunkLength^] = unit
367301
u.chunkLength += 1
368302
}
369-
u.chunkNativeStart = codeUnits.offset(of: _indexBase._base(i))^
303+
u.chunkNativeStart = codeUnits.offset(of: i.base)^
370304
u.chunkOffset = u.chunkLength
371305
}
372306
var b = buffer // copy due to https://bugs.swift.org/browse/SR-3782
@@ -428,7 +362,7 @@ extension UnicodeStorage : _UTextable {
428362
for i in chunkSource.indices {
429363
chunkOffset += chunkSource[i].utf16.count
430364
if chunkOffset == u[0].chunkOffset^ {
431-
return codeUnits.offset(of: _indexBase._next(i))^
365+
return codeUnits.offset(of: i.next)^
432366
}
433367
}
434368
fatalError("supposed to be unreachable")
@@ -529,6 +463,10 @@ extension UnicodeStorage {
529463
defer { __swift_stdlib_ubrk_close(bi) }
530464

531465
return storage._withUText { u in
466+
let access = u[0].pFuncs[0].access(u, storage.codeUnits.offset(of: i)^, 1)
467+
// _debugLog("access result:", access)
468+
// _debugLog("ubrk_setUText(breakIterator: \(bi), u: \(u)")
469+
// _debugLog("u: \(u.pointee)")
532470
__swift_stdlib_ubrk_setUText(bi, u, &err)
533471
_precondition(err.isSuccess, "unexpected ubrk_setUText failure")
534472
return body(bi)

0 commit comments

Comments
 (0)