forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.swift
666 lines (582 loc) · 25.9 KB
/
Scanner.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@available(*, unavailable)
extension Scanner : @unchecked Sendable { }
open class Scanner: NSObject, NSCopying {
internal var _scanString: String
internal var _skipSet: CharacterSet?
internal var _invertedSkipSet: CharacterSet?
internal var _scanLocation: Int
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
return Scanner(string: string)
}
open var string: String {
return _scanString
}
open var scanLocation: Int {
get {
return _scanLocation
}
set {
if newValue > string.length {
fatalError("Index \(newValue) beyond bounds; string length \(string.length)")
}
_scanLocation = newValue
}
}
/*@NSCopying*/ open var charactersToBeSkipped: CharacterSet? {
get {
return _skipSet
}
set {
_skipSet = newValue
_invertedSkipSet = nil
}
}
internal var invertedSkipSet: CharacterSet? {
if let inverted = _invertedSkipSet {
return inverted
} else {
if let set = charactersToBeSkipped {
_invertedSkipSet = set.inverted
return _invertedSkipSet
}
return nil
}
}
open var caseSensitive: Bool = false
open var locale: Any?
internal static let defaultSkipSet = CharacterSet.whitespacesAndNewlines
public init(string: String) {
_scanString = string
_skipSet = Scanner.defaultSkipSet
_scanLocation = 0
}
// On overflow, the below methods will return success and clamp
@discardableResult
open func scanInt32(_ result: UnsafeMutablePointer<Int32>) -> Bool {
return _scanString.scan(_skipSet, locationToScanFrom: &_scanLocation) { (value: Int32) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanInt(_ result: UnsafeMutablePointer<Int>) -> Bool {
return _scanString.scan(_skipSet, locationToScanFrom: &_scanLocation) { (value: Int) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanInt64(_ result: UnsafeMutablePointer<Int64>) -> Bool {
return _scanString.scan(_skipSet, locationToScanFrom: &_scanLocation) { (value: Int64) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanUnsignedLongLong(_ result: UnsafeMutablePointer<UInt64>) -> Bool {
return _scanString.scan(_skipSet, locationToScanFrom: &_scanLocation) { (value: UInt64) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanFloat(_ result: UnsafeMutablePointer<Float>) -> Bool {
return _scanString.scan(_skipSet, locale: locale as? Locale, locationToScanFrom: &_scanLocation) { (value: Float) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanDouble(_ result: UnsafeMutablePointer<Double>) -> Bool {
return _scanString.scan(_skipSet, locale: locale as? Locale, locationToScanFrom: &_scanLocation) { (value: Double) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanHexInt32(_ result: UnsafeMutablePointer<UInt32>) -> Bool {
return _scanString.scanHex(_skipSet, locationToScanFrom: &_scanLocation) { (value: UInt32) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanHexInt64(_ result: UnsafeMutablePointer<UInt64>) -> Bool {
return _scanString.scanHex(_skipSet, locationToScanFrom: &_scanLocation) { (value: UInt64) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanHexFloat(_ result: UnsafeMutablePointer<Float>) -> Bool {
return _scanString.scanHex(_skipSet, locale: locale as? Locale, locationToScanFrom: &_scanLocation) { (value: Float) -> Void in
result.pointee = value
}
}
@discardableResult
open func scanHexDouble(_ result: UnsafeMutablePointer<Double>) -> Bool {
return _scanString.scanHex(_skipSet, locale: locale as? Locale, locationToScanFrom: &_scanLocation) { (value: Double) -> Void in
result.pointee = value
}
}
open var isAtEnd: Bool {
var stringLoc = scanLocation
let stringLen = string.length
if let invSet = invertedSkipSet {
let range = string._nsObject.rangeOfCharacter(from: invSet, options: [], range: NSRange(location: stringLoc, length: stringLen - stringLoc))
stringLoc = range.length > 0 ? range.location : stringLen
}
return stringLoc == stringLen
}
open class func localizedScanner(with string: String) -> Any {
let scanner = Scanner(string: string)
scanner.locale = Locale.current
return scanner
}
}
internal struct _NSStringBuffer {
var bufferLen: Int
var bufferLoc: Int
var string: NSString
var stringLen: Int
var _stringLoc: Int
var buffer = Array<unichar>(repeating: 0, count: 32)
var curChar: unichar?
static let EndCharacter = unichar(0xffff)
init(string: String, start: Int, end: Int) {
self.string = string._bridgeToObjectiveC()
_stringLoc = start
stringLen = end
if _stringLoc < stringLen {
bufferLen = min(32, stringLen - _stringLoc)
let range = NSRange(location: _stringLoc, length: bufferLen)
bufferLoc = 1
buffer.withUnsafeMutableBufferPointer({ (ptr: inout UnsafeMutableBufferPointer<unichar>) -> Void in
self.string.getCharacters(ptr.baseAddress!, range: range)
})
curChar = buffer[0]
} else {
bufferLen = 0
bufferLoc = 1
curChar = _NSStringBuffer.EndCharacter
}
}
init(string: NSString, start: Int, end: Int) {
self.string = string
_stringLoc = start
stringLen = end
if _stringLoc < stringLen {
bufferLen = min(32, stringLen - _stringLoc)
let range = NSRange(location: _stringLoc, length: bufferLen)
bufferLoc = 1
buffer.withUnsafeMutableBufferPointer({ (ptr: inout UnsafeMutableBufferPointer<unichar>) -> Void in
self.string.getCharacters(ptr.baseAddress!, range: range)
})
curChar = buffer[0]
} else {
bufferLen = 0
bufferLoc = 1
curChar = _NSStringBuffer.EndCharacter
}
}
var currentCharacter: unichar {
return curChar!
}
var isAtEnd: Bool {
return curChar == _NSStringBuffer.EndCharacter
}
mutating func fill() {
bufferLen = min(32, stringLen - _stringLoc)
let range = NSRange(location: _stringLoc, length: bufferLen)
buffer.withUnsafeMutableBufferPointer({ (ptr: inout UnsafeMutableBufferPointer<unichar>) -> Void in
string.getCharacters(ptr.baseAddress!, range: range)
})
bufferLoc = 1
curChar = buffer[0]
}
mutating func advance() {
if bufferLoc < bufferLen { /*buffer is OK*/
curChar = buffer[bufferLoc]
bufferLoc += 1
} else if (_stringLoc + bufferLen < stringLen) { /* Buffer is empty but can be filled */
_stringLoc += bufferLen
fill()
} else { /* Buffer is empty and we're at the end */
bufferLoc = bufferLen + 1
curChar = _NSStringBuffer.EndCharacter
}
}
mutating func rewind() {
if bufferLoc > 1 { /* Buffer is OK */
bufferLoc -= 1
curChar = buffer[bufferLoc - 1]
} else if _stringLoc > 0 { /* Buffer is empty but can be filled */
bufferLoc = min(32, _stringLoc)
bufferLen = bufferLoc
_stringLoc -= bufferLen
let range = NSRange(location: _stringLoc, length: bufferLen)
buffer.withUnsafeMutableBufferPointer({ (ptr: inout UnsafeMutableBufferPointer<unichar>) -> Void in
string.getCharacters(ptr.baseAddress!, range: range)
})
curChar = buffer[bufferLoc - 1]
} else {
bufferLoc = 0
curChar = _NSStringBuffer.EndCharacter
}
}
mutating func skip(_ skipSet: CharacterSet?) {
if let set = skipSet {
while set.contains(UnicodeScalar(currentCharacter)!) && !isAtEnd {
advance()
}
}
}
var location: Int {
get {
return _stringLoc + bufferLoc - 1
}
mutating set {
if newValue < _stringLoc || newValue >= _stringLoc + bufferLen {
if newValue < 16 { /* Get the first NSStringBufferSize chars */
_stringLoc = 0
} else if newValue > stringLen - 16 { /* Get the last NSStringBufferSize chars */
_stringLoc = stringLen < 32 ? 0 : stringLen - 32
} else {
_stringLoc = newValue - 16 /* Center around loc */
}
fill()
}
bufferLoc = newValue - _stringLoc
curChar = buffer[bufferLoc]
bufferLoc += 1
}
}
}
private func decimalValue(_ ch: unichar) -> Int? {
guard let s = UnicodeScalar(ch), s.isASCII else { return nil }
return Character(s).wholeNumberValue
}
private func decimalOrHexValue(_ ch: unichar) -> Int? {
guard let s = UnicodeScalar(ch), s.isASCII else { return nil }
return Character(s).hexDigitValue
}
extension String {
private func checkForNegative(inBuffer buf: inout _NSStringBuffer, skipping skipSet: CharacterSet? = nil) -> Bool {
buf.skip(skipSet)
if buf.currentCharacter == unichar(unicodeScalarLiteral: "-") || buf.currentCharacter == unichar(unicodeScalarLiteral: "+") {
let neg = buf.currentCharacter == unichar(unicodeScalarLiteral: "-")
buf.advance()
buf.skip(skipSet)
return neg
}
return false
}
// If a string starts: 0[xX]<Valid Hex Digits> return with the buffer pointing to the hex digits otherwise point to the start of buffer.
private func skipHexStart(inBuffer buf: inout _NSStringBuffer) {
let locRewindTo = buf.location
if buf.currentCharacter == unichar(unicodeScalarLiteral: "0") {
buf.advance()
if buf.currentCharacter == unichar(unicodeScalarLiteral: "x") || buf.currentCharacter == unichar(unicodeScalarLiteral: "X") {
buf.advance()
if decimalOrHexValue(buf.currentCharacter) != nil {
return
}
}
buf.location = locRewindTo
}
}
internal func scan<T: FixedWidthInteger>(_ skipSet: CharacterSet?, locationToScanFrom: inout Int, to: (T) -> Void) -> Bool {
var buf = _NSStringBuffer(string: self, start: locationToScanFrom, end: length)
var localResult: T = 0
var retval = false
var neg = checkForNegative(inBuffer: &buf, skipping: skipSet)
while let numeral = decimalValue(buf.currentCharacter) {
retval = true
if (localResult >= T.max / 10) && ((localResult > T.max / 10) || T(numeral - (neg ? 1 : 0)) >= T.max - localResult * 10) {
// apply the clamps and advance past the ending of the buffer where there are still digits
localResult = neg ? T.min : T.max
neg = false
repeat {
buf.advance()
} while decimalValue(buf.currentCharacter) != nil
break
} else {
// normal case for scanning
localResult = localResult * 10 + T(numeral)
}
buf.advance()
}
to(neg ? -1 * localResult : localResult)
locationToScanFrom = buf.location
return retval
}
internal func scanHex<T: FixedWidthInteger>(_ skipSet: CharacterSet?, locationToScanFrom: inout Int, to: (T) -> Void) -> Bool {
var buf = _NSStringBuffer(string: self, start: locationToScanFrom, end: length)
var localResult: T = 0
var retval = false
buf.skip(skipSet)
skipHexStart(inBuffer: &buf)
while let numeral = decimalOrHexValue(buf.currentCharacter) {
retval = true
if localResult > T.max >> T(4) {
localResult = T.max
} else {
localResult = (localResult << T(4)) + T(numeral)
}
buf.advance()
}
to(localResult)
locationToScanFrom = buf.location
return retval
}
internal func scan<T: BinaryFloatingPoint & LosslessStringConvertible>(_ skipSet: CharacterSet?, locale: Locale?, locationToScanFrom: inout Int, to: (T) -> Void) -> Bool {
var buf = _NSStringBuffer(string: self, start: locationToScanFrom, end: length)
let ds = (locale ?? Locale.current).decimalSeparator?.first ?? Character(".")
func nextDigit() -> Character? {
if let s = UnicodeScalar(buf.currentCharacter) {
let ch = Character(s)
if ch.isASCII && ch.isWholeNumber {
return ch
}
}
return nil
}
var hasValidCharacter = false
var stringToParse = checkForNegative(inBuffer: &buf, skipping: skipSet) ? "-" : ""
while let ch = nextDigit() {
hasValidCharacter = true
stringToParse.append(ch)
buf.advance()
}
if let us = UnicodeScalar(buf.currentCharacter), Character(us) == ds {
stringToParse += "."
buf.advance()
while let ch = nextDigit() {
hasValidCharacter = true
stringToParse.append(ch)
buf.advance()
}
}
guard hasValidCharacter else { return false }
if buf.currentCharacter == unichar(unicodeScalarLiteral: "e") || buf.currentCharacter == unichar(unicodeScalarLiteral: "E") {
hasValidCharacter = false
stringToParse += "e"
buf.advance()
if checkForNegative(inBuffer: &buf) {
stringToParse += "-"
}
while let ch = nextDigit() {
hasValidCharacter = true
stringToParse.append(ch)
buf.advance()
}
}
guard hasValidCharacter else { return false }
if let value = T(stringToParse) {
to(value)
locationToScanFrom = buf.location
return true
} else {
return false
}
}
internal func scanHex<T: BinaryFloatingPoint & LosslessStringConvertible>(_ skipSet: CharacterSet?, locale: Locale?, locationToScanFrom: inout Int, to: (T) -> Void) -> Bool {
var buf = _NSStringBuffer(string: self, start: locationToScanFrom, end: length)
let ds = (locale ?? Locale.current).decimalSeparator?.first ?? Character(".")
func nextHexDigit() -> Character? {
if let s = UnicodeScalar(buf.currentCharacter), let ascii = Character(s).asciiValue {
switch ascii {
case 0x30...0x39, 0x41...0x46, 0x61...0x66: return Character(s)
default: return nil
}
} else {
return nil
}
}
var hasValidCharacter = false
var stringToParse = checkForNegative(inBuffer: &buf, skipping: skipSet) ? "-0x" : "0x"
skipHexStart(inBuffer: &buf)
while let ch = nextHexDigit() {
hasValidCharacter = true
stringToParse.append(ch)
buf.advance()
}
if let us = UnicodeScalar(buf.currentCharacter), Character(us) == ds {
stringToParse += "."
buf.advance()
while let ch = nextHexDigit() {
hasValidCharacter = true
stringToParse.append(ch)
buf.advance()
}
}
guard hasValidCharacter else { return false }
if let value = T(stringToParse) {
to(value)
locationToScanFrom = buf.location
return true
} else {
return false
}
}
}
// This extension used to house the experimental API for Scanner. This is all deprecated in favor of API with newer semantics. Some of the experimental API have been promoted to full API with slightly different semantics; see ScannerAPI.swift.
extension Scanner {
// These methods are in a special bit of mess:
// - They used to exist here; but
// - They have all been replaced by methods called scan<Type>(representation:); but
// - The representation parameter has a default value, so scan<Type>() is still valid and has the same semantics as the below.
// This means that the new methods _aren't_ fully source compatible — most source will correctly pick up the new .scan<Type>(representation:) with the default, but things like let method = scanner.scanInt32 may or may not work any longer.
// Make sure that the signatures exist here so that in case the compiler would pick them up, we can direct people to the new ones. This should be rare.
// scanDecimal() is not among these methods and has not changed at all, though it has been promoted to non-experimental API.
@available(swift, obsoleted: 5.0, renamed: "scanInt(representation:)")
public func scanInt() -> Int? { return scanInt(representation: .decimal) }
@available(swift, obsoleted: 5.0, renamed: "scanInt32(representation:)")
public func scanInt32() -> Int32? { return scanInt32(representation: .decimal) }
@available(swift, obsoleted: 5.0, renamed: "scanInt64(representation:)")
public func scanInt64() -> Int64? { return scanInt64(representation: .decimal) }
@available(swift, obsoleted: 5.0, renamed: "scanUInt64(representation:)")
public func scanUInt64() -> UInt64? { return scanUInt64(representation: .decimal) }
@available(swift, obsoleted: 5.0, renamed: "scanFloat(representation:)")
public func scanFloat() -> Float? { return scanFloat(representation: .decimal) }
@available(swift, obsoleted: 5.0, renamed: "scanDouble(representation:)")
public func scanDouble() -> Double? { return scanDouble(representation: .decimal) }
// These existed but are now deprecated in favor of the new methods:
@available(swift, deprecated: 5.0, renamed: "scanUInt64(representation:)")
public func scanHexInt32() -> UInt32? {
guard let value = scanUInt64(representation: .hexadecimal) else { return nil }
return UInt32(min(value, UInt64(UInt32.max)))
}
@available(swift, deprecated: 5.0, renamed: "scanUInt64(representation:)")
public func scanHexInt64() -> UInt64? { return scanUInt64(representation: .hexadecimal) }
@available(swift, deprecated: 5.0, renamed: "scanFloat(representation:)")
public func scanHexFloat() -> Float? { return scanFloat(representation: .hexadecimal) }
@available(swift, deprecated: 5.0, renamed: "scanDouble(representation:)")
public func scanHexDouble() -> Double? { return scanDouble(representation: .hexadecimal) }
@available(swift, deprecated: 5.0, renamed: "scanString(_:)")
@discardableResult
public func scanString(_ string:String, into ptr: UnsafeMutablePointer<String?>?) -> Bool {
if let str = _scanStringSplittingGraphemes(string) {
ptr?.pointee = str
return true
}
return false
}
// These methods avoid calling the private API for _invertedSkipSet and manually re-construct them so that it is only usage of public API usage
// Future implementations on Darwin of these methods will likely be more optimized to take advantage of the cached values.
private func _scanStringSplittingGraphemes(_ searchString: String) -> String? {
let str = self.string._bridgeToObjectiveC()
var stringLoc = scanLocation
let stringLen = str.length
let options: NSString.CompareOptions = [caseSensitive ? [] : .caseInsensitive, .anchored]
if let invSkipSet = charactersToBeSkipped?.inverted {
let range = str.rangeOfCharacter(from: invSkipSet, options: [], range: NSRange(location: stringLoc, length: stringLen - stringLoc))
stringLoc = range.length > 0 ? range.location : stringLen
}
let range = str.range(of: searchString, options: options, range: NSRange(location: stringLoc, length: stringLen - stringLoc))
if range.length > 0 {
/* ??? Is the range below simply range? 99.9% of the time, and perhaps even 100% of the time... Hmm... */
let res = str.substring(with: NSRange(location: stringLoc, length: range.location + range.length - stringLoc))
scanLocation = range.location + range.length
return res
}
return nil
}
@available(swift, deprecated: 5.0, renamed: "scanCharacters(from:)")
public func scanCharactersFromSet(_ set: CharacterSet) -> String? {
return _scanCharactersSplittingGraphemes(from: set)
}
@available(swift, deprecated: 5.0, renamed: "scanCharacters(from:)")
public func scanCharacters(from set: CharacterSet, into ptr: UnsafeMutablePointer<String?>?) -> Bool {
if let str = _scanCharactersSplittingGraphemes(from: set) {
ptr?.pointee = str
return true
}
return false
}
private func _scanCharactersSplittingGraphemes(from set: CharacterSet) -> String? {
let str = self.string._bridgeToObjectiveC()
var stringLoc = scanLocation
let stringLen = str.length
let options: NSString.CompareOptions = caseSensitive ? [] : .caseInsensitive
if let invSkipSet = charactersToBeSkipped?.inverted {
let range = str.rangeOfCharacter(from: invSkipSet, options: [], range: NSRange(location: stringLoc, length: stringLen - stringLoc))
stringLoc = range.length > 0 ? range.location : stringLen
}
var range = str.rangeOfCharacter(from: set.inverted, options: options, range: NSRange(location: stringLoc, length: stringLen - stringLoc))
if range.length == 0 {
range.location = stringLen
}
if stringLoc != range.location {
let res = str.substring(with: NSRange(location: stringLoc, length: range.location - stringLoc))
scanLocation = range.location
return res
}
return nil
}
@available(swift, deprecated: 5.0, renamed: "scanUpToString(_:)")
@discardableResult
public func scanUpTo(_ string:String, into ptr: UnsafeMutablePointer<String?>?) -> Bool {
if let str = _scanUpToStringSplittingGraphemes(string) {
ptr?.pointee = str
return true
}
return false
}
public func _scanUpToStringSplittingGraphemes(_ string: String) -> String? {
let str = self.string._bridgeToObjectiveC()
var stringLoc = scanLocation
let stringLen = str.length
let options: NSString.CompareOptions = caseSensitive ? [] : .caseInsensitive
if let invSkipSet = charactersToBeSkipped?.inverted {
let range = str.rangeOfCharacter(from: invSkipSet, options: [], range: NSRange(location: stringLoc, length: stringLen - stringLoc))
stringLoc = range.length > 0 ? range.location : stringLen
}
var range = str.range(of: string, options: options, range: NSRange(location: stringLoc, length: stringLen - stringLoc))
if range.length == 0 {
range.location = stringLen
}
if stringLoc != range.location {
let res = str.substring(with: NSRange(location: stringLoc, length: range.location - stringLoc))
scanLocation = range.location
return res
}
return nil
}
@available(swift, deprecated: 5.0, renamed: "scanUpToCharacters(from:)")
@discardableResult
public func scanUpToCharacters(from set: CharacterSet, into ptr: UnsafeMutablePointer<String?>?) -> Bool {
if let result = _scanSplittingGraphemesUpToCharacters(from: set) {
ptr?.pointee = result
return true
}
return false
}
@available(swift, deprecated: 5.0, renamed: "scanUpToCharacters(from:)")
public func scanUpToCharactersFromSet(_ set: CharacterSet) -> String? {
return _scanSplittingGraphemesUpToCharacters(from: set)
}
private func _scanSplittingGraphemesUpToCharacters(from set: CharacterSet) -> String? {
let str = self.string._bridgeToObjectiveC()
var stringLoc = scanLocation
let stringLen = str.length
let options: NSString.CompareOptions = caseSensitive ? [] : .caseInsensitive
if let invSkipSet = charactersToBeSkipped?.inverted {
let range = str.rangeOfCharacter(from: invSkipSet, options: [], range: NSRange(location: stringLoc, length: stringLen - stringLoc))
stringLoc = range.length > 0 ? range.location : stringLen
}
var range = str.rangeOfCharacter(from: set, options: options, range: NSRange(location: stringLoc, length: stringLen - stringLoc))
if range.length == 0 {
range.location = stringLen
}
if stringLoc != range.location {
let res = str.substring(with: NSRange(location: stringLoc, length: range.location - stringLoc))
scanLocation = range.location
return res
}
return nil
}
}