StringScanner

public class StringScanner : CustomStringConvertible

A pure Swift implementation of a String scanner, that can leverage some of the strengths of swift (over using NSScanner). In general consumers of OysterKit do not need to use this class directly as it is abstracted away by a concrete implementation of Lexical Analyzer.

At this point (Swift 4.0) the Swift String subsystem is still enjoying a reasonable amount of churn so details of the implementation will change to fully leverage any improved performance affordances. However at this point the implementation is based around the UnicodeScalarView of a String as it enables direct cosumption of other useful classes such as CharacterSet.

This may change in the future as Swift strings evolve.

  • The string being scanned

    Declaration

    Swift

    public let string: String
  • The current scanning index in scalars

    Declaration

    Swift

    public var scanLocation: String.UnicodeScalarView.Index
  • true if the scanner is at the end of the String

    Declaration

    Swift

    public var isAtEnd: Bool { get }
  • Create a new instance of the class.

    Declaration

    Swift

    public init(_ string: String)

    Parameters

    string

    The String to be scanned

  • A useful description of the state of the scanner

    Declaration

    Swift

    public var description: String { get }
  • Advances the scan-head by one position. If it is at the end of the String it will not move (that is the scan-head will not advance past the last character in the String.

    Declaration

    Swift

    public func scanNext() -> UnicodeScalar?

    Return Value

    The UnicodeScalar currently at the new scan-head position, or nil if the end of the String has been reached.

  • Scans for the supplied regular expression. If the expression is matched the scanner location will be at the end of the match

    Declaration

    Swift

    public func scan(regularExpression regex: NSRegularExpression) -> Bool

    Parameters

    regularExpression

    The NSRegularExpression to be used

    Return Value

    true if the regular expression was matched, false otherwise

  • Scans all characters from the supplied set, continuing until the scanner hits a character not in the set.

    Declaration

    Swift

    public func scan(charactersFrom set: CharacterSet) -> Bool

    Parameters

    charactersFrom

    The CharacterSet to scan for

    Return Value

    true if at least one is found, false otherwise

  • Scans a single character from the supplied set

    Declaration

    Swift

    public func scan(characterFrom set: CharacterSet) -> Bool

    Parameters

    charactersFrom

    The CharacterSet to scan for

    Return Value

    true if one is found, false otherwise

  • Advances the scan-head until a character from the supplied CharacterSet is found, or the end of the String is reached.

    Declaration

    Swift

    public func scanUpTo(characterFrom set: CharacterSet) -> Bool

    Parameters

    charactersFrom

    The CharacterSet that should stop scanning

    Return Value

    true if a character from the supplied set is found before the end of the string.

  • Advances the scan-head until a character from the supplied String is found, or the end of the String is reached.

    Declaration

    Swift

    public func scanUpTo(string: String) -> Bool

    Parameters

    string

    The String that should stop scanning

    Return Value

    true if string is found before the end of the source.

  • Advances the scan-head to the end of the supplied string providing it matches.

    Declaration

    Swift

    public func scan(string: String) -> Bool

    Parameters

    string

    The String that should be matched

    Return Value

    true if string is sfound strating from the current scan-head position and before the end of the source.