StringScanner
is a native swift implementation of a string scanner; a replacement of NSScanner
.
StringScanner
has been built by using swfit standard library only, and it does not depend on libFoundation. In that way it can be built on mac and linux, and can be build with static compilation in mind.
StringScanner
is initialized with a string, it has an internal index that get progressed as the string is scanned.
StringScanner
has two operations, peek
and scan
.
scan
returns a string and advances the internal string index.peek
returns a string but does not progress the internal string index.
The result of scan or peek is a ScannerResult
, which has the following values:
- ScannerResult.none: no string was found
- ScannerResult.end: the scanner has reached the end of the string
- ScannerResult.value(str): the string is found
Scanning for a given length
let scanner = StringScanner(string: "Hello my name is omar")
scanner.scan(length: 5)
Returns ScannerResult.("Hello")
Scan the string until a pattern is found
let scanner = StringScanner(string: "Hello my name is omar")
scanner.scan(untilPattern: "my")
Returns ScannerResult.("Hello ")
Scan the string until a string is found
let scanner = StringScanner(string: "Hello my name is omar")
scanner.scan(untilString: "my")
Returns ScannerResult.("Hello ")
Scan the string for a given string. If the search string is found, it gets rerurned and the scanner index is advanced.
let scanner = StringScanner(string: "Hello my name is omar")
scanner.scan(forString: "Hello mymy")
Returns ScannerResult.("Hello my ")
If we scan for a non exsisting string, we will get ScannerResult.none
Scan the string until a character set of strings is found. If the character set is found, it gets rerurned and the scanner index is advanced.
let scanner = StringScanner(string: "This is a test 1234")
scanner.scan(untilCharacterSet: .range("1"..<"4"))
Returns ScannerResult.("This is a test ")
If we scan for a non exsisting string, we will get ScannerResult.none
Scan the string until a character set of strings is found and return the string and the found character set. If the character set is found, it gets rerurned and the scanner index is advanced.
let scanner = StringScanner(string: "This is a test 1234")
scanner.scan(forCharacterSet: .range("1"..<"4"))
Returns ScannerResult.("This is a test 1")
If we scan for a non exsisting string, we will get ScannerResult.none
Peeking for a given length
let scanner = StringScanner(string: "Hello my name is omar")
scanner.peek(length: 5)
Returns ScannerResult.("Hello")
Peek the string until a pattern is found
let scanner = StringScanner(string: "Hello my name is omar")
scanner.peek(untilPattern: "my")
Returns ScannerResult.("Hello ")
Peek the string until a string is found
let scanner = StringScanner(string: "Hello my name is omar")
scanner.peek(untilString: "my")
Returns ScannerResult.("Hello ")
Peek the string for a given string. If the search string is found, it gets rerurned.
let scanner = StringScanner(string: "Hello my name is omar")
scanner.peek(forString: "Hello mymy")
Returns ScannerResult.("Hello my ")
If we scan for a non exsisting string, we will get ScannerResult.none
Peek the string until a character set of strings is found. If the character set is found, it gets rerurned.
let scanner = StringScanner(string: "This is a test 1234")
scanner.peek(untilCharacterSet: .range("1"..<"4"))
Returns ScannerResult.("This is a test ")
If we scan for a non exsisting string, we will get ScannerResult.none
Scan the string until a character set of strings is found and return the string and the found character set. If the character set is found, it gets rerurned.
let scanner = StringScanner(string: "This is a test 1234")
scanner.peek(forCharacterSet: .range("1"..<"4"))
Returns ScannerResult.("This is a test 1")
If we scan for a non exsisting string, we will get ScannerResult.none
CharacterSet
provide ready made character sets that represent the most used ones: such as .allLetters
, smallLetters
, alphaNumeric
.
Character set can be joined togther. For example we can search for all numbers + ABC
let scanner = StringScanner(string: "This is a test 1234")
let set = CharacterSet.numbers.join(characterSet: CharacterSet.string("ABC"))
The we can scan until we find that set
scanner.scan(untilCharacerSet: set)
This returns This is a test
scanner.reset()
resets the internal index to zero
scanner.reset()
resets the internal index to zero
You can install Swiftline using CocoaPods, carthage and Swift package manager
use_frameworks!
pod 'StringScanner'
github 'getGuaka/StringScanner'
Add swiftline as dependency in your Package.swift
import PackageDescription
let package = Package(name: "YourPackage",
dependencies: [
.Package(url: "https://github.com/getGuaka/StringScanner.git", from: "0.0.0"),
]
)
Tests can be found here.
Run them with
swift test