7
7
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8
8
//
9
9
10
-
11
-
12
10
import CoreFoundation
13
11
14
12
open class Scanner : NSObject , NSCopying {
@@ -499,120 +497,53 @@ extension String {
499
497
}
500
498
}
501
499
502
- /// Revised API for avoiding usage of AutoreleasingUnsafeMutablePointer and better Optional usage.
503
- /// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
504
- /// - Note: Since this API is under consideration it may be either removed or revised in the near future
500
+ // 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.
505
501
extension Scanner {
506
- public func scanInt32( ) -> Int32 ? {
507
- var value : Int32 = 0
508
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Int32 > ) -> Int32 ? in
509
- if scanInt32 ( ptr) {
510
- return ptr. pointee
511
- } else {
512
- return nil
513
- }
514
- }
515
- }
502
+ // These methods are in a special bit of mess:
503
+ // - They used to exist here; but
504
+ // - They have all been replaced by methods called scan<Type>(representation:); but
505
+ // - The representation parameter has a default value, so scan<Type>() is still valid and has the same semantics as the below.
506
+ // 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.
507
+ // 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.
516
508
517
- public func scanInt( ) -> Int ? {
518
- var value : Int = 0
519
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Int > ) -> Int ? in
520
- if scanInt ( ptr) {
521
- return ptr. pointee
522
- } else {
523
- return nil
524
- }
525
- }
526
- }
509
+ // scanDecimal() is not among these methods and has not changed at all, though it has been promoted to non-experimental API.
527
510
528
- public func scanInt64( ) -> Int64 ? {
529
- var value : Int64 = 0
530
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Int64 > ) -> Int64 ? in
531
- if scanInt64 ( ptr) {
532
- return ptr. pointee
533
- } else {
534
- return nil
535
- }
536
- }
537
- }
511
+ @available ( swift, obsoleted: 5.0 , renamed: " scanInt(representation:) " )
512
+ public func scanInt( ) -> Int ? { return scanInt ( representation: . decimal) }
538
513
539
- public func scanUnsignedLongLong( ) -> UInt64 ? {
540
- var value : UInt64 = 0
541
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < UInt64 > ) -> UInt64 ? in
542
- if scanUnsignedLongLong ( ptr) {
543
- return ptr. pointee
544
- } else {
545
- return nil
546
- }
547
- }
548
- }
514
+ @available ( swift, obsoleted: 5.0 , renamed: " scanInt32(representation:) " )
515
+ public func scanInt32( ) -> Int32 ? { return scanInt32 ( representation: . decimal) }
549
516
550
- public func scanFloat( ) -> Float ? {
551
- var value : Float = 0.0
552
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Float > ) -> Float ? in
553
- if scanFloat ( ptr) {
554
- return ptr. pointee
555
- } else {
556
- return nil
557
- }
558
- }
559
- }
517
+ @available ( swift, obsoleted: 5.0 , renamed: " scanInt64(representation:) " )
518
+ public func scanInt64( ) -> Int64 ? { return scanInt64 ( representation: . decimal) }
560
519
561
- public func scanDouble( ) -> Double ? {
562
- var value : Double = 0.0
563
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Double > ) -> Double ? in
564
- if scanDouble ( ptr) {
565
- return ptr. pointee
566
- } else {
567
- return nil
568
- }
569
- }
570
- }
520
+ @available ( swift, obsoleted: 5.0 , renamed: " scanUInt64(representation:) " )
521
+ public func scanUInt64( ) -> UInt64 ? { return scanUInt64 ( representation: . decimal) }
522
+
523
+ @available ( swift, obsoleted: 5.0 , renamed: " scanFloat(representation:) " )
524
+ public func scanFloat( ) -> Float ? { return scanFloat ( representation: . decimal) }
525
+
526
+ @available ( swift, obsoleted: 5.0 , renamed: " scanDouble(representation:) " )
527
+ public func scanDouble( ) -> Double ? { return scanDouble ( representation: . decimal) }
571
528
529
+ // These existed but are now deprecated in favor of the new methods:
530
+
531
+ @available ( swift, deprecated: 5.0 , renamed: " scanUInt64(representation:) " )
572
532
public func scanHexInt32( ) -> UInt32 ? {
573
- var value : UInt32 = 0
574
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < UInt32 > ) -> UInt32 ? in
575
- if scanHexInt32 ( ptr) {
576
- return ptr. pointee
577
- } else {
578
- return nil
579
- }
580
- }
533
+ guard let value = scanUInt64 ( representation: . hexadecimal) else { return nil }
534
+ return UInt32 ( min ( value, UInt64 ( UInt32 . max) ) )
581
535
}
582
536
583
- public func scanHexInt64( ) -> UInt64 ? {
584
- var value : UInt64 = 0
585
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < UInt64 > ) -> UInt64 ? in
586
- if scanHexInt64 ( ptr) {
587
- return ptr. pointee
588
- } else {
589
- return nil
590
- }
591
- }
592
- }
537
+ @available ( swift, deprecated: 5.0 , renamed: " scanUInt64(representation:) " )
538
+ public func scanHexInt64( ) -> UInt64 ? { return scanUInt64 ( representation: . hexadecimal) }
593
539
594
- public func scanHexFloat( ) -> Float ? {
595
- var value : Float = 0.0
596
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Float > ) -> Float ? in
597
- if scanHexFloat ( ptr) {
598
- return ptr. pointee
599
- } else {
600
- return nil
601
- }
602
- }
603
- }
540
+ @available ( swift, deprecated: 5.0 , renamed: " scanFloat(representation:) " )
541
+ public func scanHexFloat( ) -> Float ? { return scanFloat ( representation: . hexadecimal) }
604
542
605
- public func scanHexDouble( ) -> Double ? {
606
- var value : Double = 0.0
607
- return withUnsafeMutablePointer ( to: & value) { ( ptr: UnsafeMutablePointer < Double > ) -> Double ? in
608
- if scanHexDouble ( ptr) {
609
- return ptr. pointee
610
- } else {
611
- return nil
612
- }
613
- }
614
- }
543
+ @available ( swift, deprecated: 5.0 , renamed: " scanDouble(representation:) " )
544
+ public func scanHexDouble( ) -> Double ? { return scanDouble ( representation: . hexadecimal) }
615
545
546
+ @available ( swift, deprecated: 5.0 , renamed: " scanString(_:) " )
616
547
@discardableResult
617
548
public func scanString( _ string: String , into ptr: UnsafeMutablePointer < String ? > ? ) -> Bool {
618
549
if let str = _scanStringSplittingGraphemes ( string) {
@@ -647,6 +578,19 @@ extension Scanner {
647
578
648
579
@available ( swift, deprecated: 5.0 , renamed: " scanCharacters(from:) " )
649
580
public func scanCharactersFromSet( _ set: CharacterSet ) -> String ? {
581
+ return _scanCharactersSplittingGraphemes ( from: set)
582
+ }
583
+
584
+ @available ( swift, deprecated: 5.0 , renamed: " scanCharacters(from:) " )
585
+ public func scanCharacters( from set: CharacterSet , into ptr: UnsafeMutablePointer < String ? > ? ) -> Bool {
586
+ if let str = _scanCharactersSplittingGraphemes ( from: set) {
587
+ ptr? . pointee = str
588
+ return true
589
+ }
590
+ return false
591
+ }
592
+
593
+ private func _scanCharactersSplittingGraphemes( from set: CharacterSet ) -> String ? {
650
594
let str = self . string. _bridgeToObjectiveC ( )
651
595
var stringLoc = scanLocation
652
596
let stringLen = str. length
@@ -667,6 +611,7 @@ extension Scanner {
667
611
return nil
668
612
}
669
613
614
+ @available ( swift, deprecated: 5.0 , renamed: " scanUpToString(_:) " )
670
615
@discardableResult
671
616
public func scanUpTo( _ string: String , into ptr: UnsafeMutablePointer < String ? > ? ) -> Bool {
672
617
if let str = _scanUpToStringSplittingGraphemes ( string) {
@@ -697,9 +642,10 @@ extension Scanner {
697
642
return nil
698
643
}
699
644
645
+ @available ( swift, deprecated: 5.0 , renamed: " scanUpToCharacters(from:) " )
700
646
@discardableResult
701
647
public func scanUpToCharacters( from set: CharacterSet , into ptr: UnsafeMutablePointer < String ? > ? ) -> Bool {
702
- if let result = scanUpToCharactersFromSet ( set) {
648
+ if let result = _scanSplittingGraphemesUpToCharacters ( from : set) {
703
649
ptr? . pointee = result
704
650
return true
705
651
}
@@ -708,6 +654,10 @@ extension Scanner {
708
654
709
655
@available ( swift, deprecated: 5.0 , renamed: " scanUpToCharacters(from:) " )
710
656
public func scanUpToCharactersFromSet( _ set: CharacterSet ) -> String ? {
657
+ return _scanSplittingGraphemesUpToCharacters ( from: set)
658
+ }
659
+
660
+ private func _scanSplittingGraphemesUpToCharacters( from set: CharacterSet ) -> String ? {
711
661
let str = self . string. _bridgeToObjectiveC ( )
712
662
var stringLoc = scanLocation
713
663
let stringLen = str. length
0 commit comments