@@ -63,7 +63,7 @@ open class Scanner: NSObject, NSCopying {
63
63
}
64
64
65
65
open var caseSensitive : Bool = false
66
- open var locale : Locale ?
66
+ open var locale : Any ?
67
67
68
68
internal static let defaultSkipSet = CharacterSet . whitespacesAndNewlines
69
69
@@ -72,6 +72,89 @@ open class Scanner: NSObject, NSCopying {
72
72
_skipSet = Scanner . defaultSkipSet
73
73
_scanLocation = 0
74
74
}
75
+
76
+ // On overflow, the below methods will return success and clamp
77
+ @discardableResult
78
+ open func scanInt32( _ result: UnsafeMutablePointer < Int32 > ) -> Bool {
79
+ return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: Int32 ) -> Void in
80
+ result. pointee = value
81
+ }
82
+ }
83
+
84
+ @discardableResult
85
+ open func scanInt( _ result: UnsafeMutablePointer < Int > ) -> Bool {
86
+ return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: Int ) -> Void in
87
+ result. pointee = value
88
+ }
89
+ }
90
+
91
+ @discardableResult
92
+ open func scanInt64( _ result: UnsafeMutablePointer < Int64 > ) -> Bool {
93
+ return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: Int64 ) -> Void in
94
+ result. pointee = value
95
+ }
96
+ }
97
+
98
+ @discardableResult
99
+ open func scanUnsignedLongLong( _ result: UnsafeMutablePointer < UInt64 > ) -> Bool {
100
+ return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: UInt64 ) -> Void in
101
+ result. pointee = value
102
+ }
103
+ }
104
+
105
+ @discardableResult
106
+ open func scanFloat( _ result: UnsafeMutablePointer < Float > ) -> Bool {
107
+ return _scanString. scan ( _skipSet, locale: locale as? Locale , locationToScanFrom: & _scanLocation) { ( value: Float ) -> Void in
108
+ result. pointee = value
109
+ }
110
+ }
111
+
112
+ @discardableResult
113
+ open func scanDouble( _ result: UnsafeMutablePointer < Double > ) -> Bool {
114
+ return _scanString. scan ( _skipSet, locale: locale as? Locale , locationToScanFrom: & _scanLocation) { ( value: Double ) -> Void in
115
+ result. pointee = value
116
+ }
117
+ }
118
+
119
+ @discardableResult
120
+ open func scanHexInt32( _ result: UnsafeMutablePointer < UInt32 > ) -> Bool {
121
+ return _scanString. scanHex ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: UInt32 ) -> Void in
122
+ result. pointee = value
123
+ }
124
+ }
125
+
126
+ @discardableResult
127
+ open func scanHexInt64( _ result: UnsafeMutablePointer < UInt64 > ) -> Bool {
128
+ return _scanString. scanHex ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: UInt64 ) -> Void in
129
+ result. pointee = value
130
+ }
131
+ }
132
+
133
+ @discardableResult
134
+ open func scanHexFloat( _ result: UnsafeMutablePointer < Float > ) -> Bool {
135
+ return _scanString. scanHex ( _skipSet, locale: locale as? Locale , locationToScanFrom: & _scanLocation) { ( value: Float ) -> Void in
136
+ result. pointee = value
137
+ }
138
+ }
139
+
140
+ @discardableResult
141
+ open func scanHexDouble( _ result: UnsafeMutablePointer < Double > ) -> Bool {
142
+ return _scanString. scanHex ( _skipSet, locale: locale as? Locale , locationToScanFrom: & _scanLocation) { ( value: Double ) -> Void in
143
+ result. pointee = value
144
+ }
145
+ }
146
+
147
+ open var isAtEnd : Bool {
148
+ var stringLoc = scanLocation
149
+ let stringLen = string. length
150
+ if let invSet = invertedSkipSet {
151
+ let range = string. _nsObject. rangeOfCharacter ( from: invSet, options: [ ] , range: NSRange ( location: stringLoc, length: stringLen - stringLoc) )
152
+ stringLoc = range. length > 0 ? range. location : stringLen
153
+ }
154
+ return stringLoc == stringLen
155
+ }
156
+
157
+ open class func localizedScannerWithString( _ string: String ) -> AnyObject { NSUnimplemented ( ) }
75
158
}
76
159
77
160
internal struct _NSStringBuffer {
@@ -416,93 +499,6 @@ extension String {
416
499
}
417
500
}
418
501
419
- extension Scanner {
420
-
421
- // On overflow, the below methods will return success and clamp
422
- @discardableResult
423
- public func scanInt32( _ result: UnsafeMutablePointer < Int32 > ) -> Bool {
424
- return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: Int32 ) -> Void in
425
- result. pointee = value
426
- }
427
- }
428
-
429
- @discardableResult
430
- public func scanInt( _ result: UnsafeMutablePointer < Int > ) -> Bool {
431
- return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: Int ) -> Void in
432
- result. pointee = value
433
- }
434
- }
435
-
436
- @discardableResult
437
- public func scanInt64( _ result: UnsafeMutablePointer < Int64 > ) -> Bool {
438
- return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: Int64 ) -> Void in
439
- result. pointee = value
440
- }
441
- }
442
-
443
- @discardableResult
444
- public func scanUnsignedLongLong( _ result: UnsafeMutablePointer < UInt64 > ) -> Bool {
445
- return _scanString. scan ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: UInt64 ) -> Void in
446
- result. pointee = value
447
- }
448
- }
449
-
450
- @discardableResult
451
- public func scanFloat( _ result: UnsafeMutablePointer < Float > ) -> Bool {
452
- return _scanString. scan ( _skipSet, locale: locale, locationToScanFrom: & _scanLocation) { ( value: Float ) -> Void in
453
- result. pointee = value
454
- }
455
- }
456
-
457
- @discardableResult
458
- public func scanDouble( _ result: UnsafeMutablePointer < Double > ) -> Bool {
459
- return _scanString. scan ( _skipSet, locale: locale, locationToScanFrom: & _scanLocation) { ( value: Double ) -> Void in
460
- result. pointee = value
461
- }
462
- }
463
-
464
- @discardableResult
465
- public func scanHexInt32( _ result: UnsafeMutablePointer < UInt32 > ) -> Bool {
466
- return _scanString. scanHex ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: UInt32 ) -> Void in
467
- result. pointee = value
468
- }
469
- }
470
-
471
- @discardableResult
472
- public func scanHexInt64( _ result: UnsafeMutablePointer < UInt64 > ) -> Bool {
473
- return _scanString. scanHex ( _skipSet, locationToScanFrom: & _scanLocation) { ( value: UInt64 ) -> Void in
474
- result. pointee = value
475
- }
476
- }
477
-
478
- @discardableResult
479
- public func scanHexFloat( _ result: UnsafeMutablePointer < Float > ) -> Bool {
480
- return _scanString. scanHex ( _skipSet, locale: locale, locationToScanFrom: & _scanLocation) { ( value: Float ) -> Void in
481
- result. pointee = value
482
- }
483
- }
484
-
485
- @discardableResult
486
- public func scanHexDouble( _ result: UnsafeMutablePointer < Double > ) -> Bool {
487
- return _scanString. scanHex ( _skipSet, locale: locale, locationToScanFrom: & _scanLocation) { ( value: Double ) -> Void in
488
- result. pointee = value
489
- }
490
- }
491
-
492
- public var isAtEnd : Bool {
493
- var stringLoc = scanLocation
494
- let stringLen = string. length
495
- if let invSet = invertedSkipSet {
496
- let range = string. _nsObject. rangeOfCharacter ( from: invSet, options: [ ] , range: NSRange ( location: stringLoc, length: stringLen - stringLoc) )
497
- stringLoc = range. length > 0 ? range. location : stringLen
498
- }
499
- return stringLoc == stringLen
500
- }
501
-
502
- open class func localizedScannerWithString( _ string: String ) -> AnyObject { NSUnimplemented ( ) }
503
- }
504
-
505
-
506
502
/// Revised API for avoiding usage of AutoreleasingUnsafeMutablePointer and better Optional usage.
507
503
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
508
504
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
@@ -619,7 +615,7 @@ extension Scanner {
619
615
620
616
@discardableResult
621
617
public func scanString( _ string: String , into ptr: UnsafeMutablePointer < String ? > ? ) -> Bool {
622
- if let str = scanString ( string) {
618
+ if let str = _scanStringSplittingGraphemes ( string) {
623
619
ptr? . pointee = str
624
620
return true
625
621
}
@@ -628,7 +624,7 @@ extension Scanner {
628
624
629
625
// These methods avoid calling the private API for _invertedSkipSet and manually re-construct them so that it is only usage of public API usage
630
626
// Future implementations on Darwin of these methods will likely be more optimized to take advantage of the cached values.
631
- public func scanString ( _ searchString: String ) -> String ? {
627
+ private func _scanStringSplittingGraphemes ( _ searchString: String ) -> String ? {
632
628
let str = self . string. _bridgeToObjectiveC ( )
633
629
var stringLoc = scanLocation
634
630
let stringLen = str. length
@@ -649,6 +645,7 @@ extension Scanner {
649
645
return nil
650
646
}
651
647
648
+ @available ( swift, deprecated: 5.0 , renamed: " scanCharacters(from:) " )
652
649
public func scanCharactersFromSet( _ set: CharacterSet ) -> String ? {
653
650
let str = self . string. _bridgeToObjectiveC ( )
654
651
var stringLoc = scanLocation
@@ -670,7 +667,16 @@ extension Scanner {
670
667
return nil
671
668
}
672
669
673
- public func scanUpToString( _ string: String ) -> String ? {
670
+ @discardableResult
671
+ public func scanUpTo( _ string: String , into ptr: UnsafeMutablePointer < String ? > ? ) -> Bool {
672
+ if let str = _scanUpToStringSplittingGraphemes ( string) {
673
+ ptr? . pointee = str
674
+ return true
675
+ }
676
+ return false
677
+ }
678
+
679
+ public func _scanUpToStringSplittingGraphemes( _ string: String ) -> String ? {
674
680
let str = self . string. _bridgeToObjectiveC ( )
675
681
var stringLoc = scanLocation
676
682
let stringLen = str. length
@@ -700,6 +706,7 @@ extension Scanner {
700
706
return false
701
707
}
702
708
709
+ @available ( swift, deprecated: 5.0 , renamed: " scanUpToCharacters(from:) " )
703
710
public func scanUpToCharactersFromSet( _ set: CharacterSet ) -> String ? {
704
711
let str = self . string. _bridgeToObjectiveC ( )
705
712
var stringLoc = scanLocation
0 commit comments