@@ -22,37 +22,6 @@ public func _debugLog(_ arg0: @autoclosure ()->Any, _ arg1: @autoclosure ()->Any
22
22
print ( arg0 ( ) , arg1 ( ) )
23
23
}
24
24
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
-
56
25
/// A collection of `CodeUnit`s to be interpreted by some `Encoding`
57
26
public struct UnicodeStorage <
58
27
CodeUnits : RandomAccessCollection ,
@@ -91,55 +60,23 @@ extension UnicodeStorage.EncodedScalars {
91
60
// and the next index. This would obviously be more complicated if
92
61
// the buffer contained more than a single scalar (and it probably
93
62
// should).
94
- public struct Index : UnicodeIndexProtocol , Comparable {
95
- let offset : CodeUnits . IndexDistance
63
+ public struct Index : Comparable {
64
+ let base : CodeUnits . Index
96
65
// FIXME: We might get a much better memory footprint if we used a
97
66
// UInt8 to store the distance between base and next, rather than
98
67
// storing next explicitly. CodeUnits will be random-access in
99
68
// 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
122
70
// FIXME: there should be an invalid inhabitant we can use in
123
71
// EncodedScalar so as not to waste a separate bool here.
124
72
let scalar : Encoding . EncodedScalar ?
125
- }
126
73
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
+ }
143
80
}
144
81
}
145
82
@@ -148,34 +85,35 @@ extension UnicodeStorage.EncodedScalars : BidirectionalCollection {
148
85
public var startIndex : Index {
149
86
if _slowPath ( codeUnits. isEmpty) { return endIndex }
150
87
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 ) )
152
89
}
153
90
154
91
public var endIndex : Index {
155
92
let s = codeUnits. endIndex
156
- return _index ( base: s, next: s, scalar: nil )
93
+ return Index ( base: s, next: s, scalar: nil )
157
94
}
158
95
159
96
public subscript( i: Index ) -> Encoding . EncodedScalar {
160
97
if let r = i. scalar {
161
98
return r
162
99
}
163
100
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!
166
102
}
167
103
168
104
public func index( after i: Index ) -> Index {
169
- var remainder = codeUnits [ _next ( i ) ..< codeUnits. endIndex]
105
+ var remainder = codeUnits [ i . next ..< codeUnits. endIndex]
170
106
while true {
171
107
switch Encoding . parse1Forward ( remainder, knownCount: 0 ) {
172
108
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)
174
110
case . error( let nextIndex) :
175
111
// FIXME: don't go through UnicodeScalar once this is in the stdlib
176
112
if let replacement = Encoding . encode (
177
113
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)
179
117
}
180
118
// If we get here, the encoding couldn't represent a replacement
181
119
// character, so the best we can do is to drop that scalar on the floor
@@ -188,16 +126,18 @@ extension UnicodeStorage.EncodedScalars : BidirectionalCollection {
188
126
}
189
127
190
128
public func index( before i: Index ) -> Index {
191
- var remainder = codeUnits [ ..< _base ( i ) ]
129
+ var remainder = codeUnits [ ..< i . base ]
192
130
while true {
193
131
switch Encoding . parse1Reverse ( remainder, knownCount: 0 ) {
194
132
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)
196
134
case . error( let priorIndex) :
197
135
// FIXME: don't go through UnicodeScalar once this is in the stdlib
198
136
if let replacement = Encoding . encode (
199
137
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)
201
141
}
202
142
// If we get here, the encoding couldn't represent a replacement
203
143
// character, so the best we can do is to drop that scalar on the floor
@@ -237,6 +177,8 @@ extension UnicodeStorage {
237
177
} )
238
178
}
239
179
180
+ // FIXME: this should go in the extension below but for <rdar://30320012>
181
+ //typealias SubSequence = BidirectionalSlice<TranscodedView>
240
182
public var startIndex : Base . Index {
241
183
return base. startIndex
242
184
}
@@ -282,7 +224,6 @@ extension UnicodeStorage : _UTextable {
282
224
_ deep: Bool , _ status: UnsafeMutablePointer < _UErrorCode > ?
283
225
) -> UnsafeMutablePointer < _UText > {
284
226
UnsafeMutablePointer ( mutating: src) [ 0 ] . validate ( )
285
- _sanityCheck ( !deep, " deep cloning not supported " )
286
227
// _debugLog("_clone with dst = \(String(describing: dst))")
287
228
// _debugLog("src: \(src[0])")
288
229
let r = dst
@@ -294,14 +235,6 @@ extension UnicodeStorage : _UTextable {
294
235
return r
295
236
}
296
237
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
-
305
238
internal func _access(
306
239
_ u: inout _UText , _ nativeTargetIndex: Int64 , _ forward: Bool
307
240
) -> Bool {
@@ -347,8 +280,9 @@ extension UnicodeStorage : _UTextable {
347
280
buffer [ u. chunkLength^] = unit
348
281
u. chunkLength += 1
349
282
}
350
- u. chunkNativeLimit = codeUnits. offset ( of: _indexBase . _next ( i ) ) ^
283
+ u. chunkNativeLimit = codeUnits. offset ( of: i . next ) ^
351
284
}
285
+ _sanityCheck ( !deep, " deep cloning not supported " )
352
286
}
353
287
else {
354
288
let chunkSource
@@ -366,7 +300,7 @@ extension UnicodeStorage : _UTextable {
366
300
buffer [ u. chunkLength^] = unit
367
301
u. chunkLength += 1
368
302
}
369
- u. chunkNativeStart = codeUnits. offset ( of: _indexBase . _base ( i ) ) ^
303
+ u. chunkNativeStart = codeUnits. offset ( of: i . base ) ^
370
304
u. chunkOffset = u. chunkLength
371
305
}
372
306
var b = buffer // copy due to https://bugs.swift.org/browse/SR-3782
@@ -428,7 +362,7 @@ extension UnicodeStorage : _UTextable {
428
362
for i in chunkSource. indices {
429
363
chunkOffset += chunkSource [ i] . utf16. count
430
364
if chunkOffset == u [ 0 ] . chunkOffset^ {
431
- return codeUnits. offset ( of: _indexBase . _next ( i ) ) ^
365
+ return codeUnits. offset ( of: i . next ) ^
432
366
}
433
367
}
434
368
fatalError ( " supposed to be unreachable " )
@@ -529,6 +463,10 @@ extension UnicodeStorage {
529
463
defer { __swift_stdlib_ubrk_close ( bi) }
530
464
531
465
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)")
532
470
__swift_stdlib_ubrk_setUText ( bi, u, & err)
533
471
_precondition ( err. isSuccess, " unexpected ubrk_setUText failure " )
534
472
return body ( bi)
0 commit comments