Created
December 17, 2016 11:51
-
-
Save Abizern/6e925a4d14c8586b7a164b0f1548e518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Scanner+Swift.swift | |
// | |
// A set of idiomatic swift extensions to Scanner | |
// | |
// Based on https://gist.github.com/natecook1000/59bb0c9117b555f5d40d | |
// Converted to Swift 3 | |
// | |
import Foundation | |
extension Scanner { | |
/// Returns a string scanned as long as characters from a given character set are encountered, or `nil` if none are found. | |
public func scanCharacters(from set: CharacterSet) -> String? { | |
var value: NSString? | |
guard scanCharacters(from: set, into: &value) else { return nil } | |
return value as? String | |
} | |
/// Returns a string scanned until a character from the given character set is encountered, or the remainder of the scanner's string. Returns `nil` if the scanner is already `atEnd`. | |
public func scanUpToCharacters(from set: CharacterSet) -> String? { | |
var value: NSString? | |
guard scanUpToCharacters(from: set, into: &value) else { return nil } | |
return value as? String | |
} | |
/// Returns the given string if scanned, or `nil` if not found. | |
public func scan(string: String) -> String? { | |
var value: NSString? | |
guard scanString(string, into: &value) else { return nil } | |
return value as? String | |
} | |
/// Returns a string, scanned until the given string is found, or the remainder of the scanner's string. Returns `nil` if the scanner is already `atEnd` | |
public func scanUpTo(string: String) -> String? { | |
var value: NSString? | |
guard scanUpTo(string, into: &value) else { return nil } | |
return value as? String | |
} | |
/// Returns a Double if scanned, or `nil` if not found. | |
public func scanDouble() -> Double? { | |
var value = Double(0) | |
guard scanDouble(&value) else { return nil } | |
return value | |
} | |
/// Returns a Float if scanned, or `nil` if not found. | |
public func scanFloat() -> Float? { | |
var value = Float(0) | |
guard scanFloat(&value) else { return nil } | |
return value | |
} | |
/// Returns an Int if scanned, or `nil` if not found. | |
public func scanInt() -> Int? { | |
var value = Int(0) | |
guard scanInt(&value) else { return nil } | |
return value | |
} | |
/// Returns an Int32 if scanned, or `nil` if not found. | |
public func scanInt32() -> Int32? { | |
var value = Int32(0) | |
guard scanInt32(&value) else { return nil } | |
return value | |
} | |
/// Returns an Int64 if scanned, or `nil` if not found. | |
public func scanInt64() -> Int64? { | |
var value = Int64(0) | |
guard scanInt64(&value) else { return nil } | |
return value | |
} | |
/// Returns a UInt64 if scanned or `nil` if not found. | |
public func scanUInt64() -> UInt64? { | |
var value = UInt64(0) | |
guard scanUnsignedLongLong(&value) else { return nil } | |
return value | |
} | |
/// Returns a Decimal if scanned, or `nil` if not found. | |
public func scanDecimal() -> Decimal? { | |
var value = Decimal(0) | |
guard scanDecimal(&value) else { return nil } | |
return value | |
} | |
/// Returns a Float if scanned in hexadecimal, or `nil` if not found. | |
public func scanHexFloat() -> Float? { | |
var value = Float(0) | |
guard scanHexFloat(&value) else { return nil } | |
return value | |
} | |
/// Returns a Double if scanned in hexadecimal, or `nil` if not found. | |
public func scanHexDouble() -> Double? { | |
var value = Double(0) | |
guard scanHexDouble(&value) else { return nil } | |
return value | |
} | |
/// Returns a UInt32 if scanned in hexadecimal, or `nil` if not found. | |
public func scanHexUInt32() -> UInt32? { | |
var value = UInt32(0) | |
guard scanHexInt32(&value) else { return nil } | |
return value | |
} | |
/// Returns a UInt64 if scanned in hexadecimal, or `nil` if not found. | |
public func scanHexUInt64() -> UInt64? { | |
var value = UInt64(0) | |
guard scanHexInt64(&value) else { return nil } | |
return value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment