SwiftScanner is a pure native Swift implementation of a string scanner; with no dependecies, full unicode support (who does not love emoji?), lots of useful featurs and swift in mind, StringScanner is a good alternative to built-in Apple’s NSScanner.
★★ Star our github repository to help us! ★★
Related Projects
I’m also working on several other projects you may like.
Take a look below:
SwiftScanner is initialized with a string and mantain an internal index used to navigate backward and forward through the string using two main concepts:
scan to return string which also increment the internal index
peek to return a string without incrementing the internal index
Results of these operations returns collected String or Indexes.
If operation fail due to an error (ie. eof, notFound, invalidInt…) and exception is thrown, in pure Swift style.
Peek functions are the same as concept of scan() but unless it it does not update internal scanner’s position index.
These functions usually return only starting index of matched pattern.
SwiftScanner
SwiftScanneris a pure native Swift implementation of a string scanner; with no dependecies, full unicode support (who does not love emoji?), lots of useful featurs and swift in mind, StringScanner is a good alternative to built-in Apple’sNSScanner.★★ Star our github repository to help us! ★★
Related Projects
I’m also working on several other projects you may like. Take a look below:
Main Features
SwiftScanner is initialized with a string and mantain an internal index used to navigate backward and forward through the string using two main concepts:
scanto return string which also increment the internal indexpeekto return a string without incrementing the internal indexResults of these operations returns collected String or Indexes. If operation fail due to an error (ie.
eof,notFound,invalidInt…) and exception is thrown, in pure Swift style.API Documentation
Other
#### `func scanChar() throws -> UnicodeScalar` `scanChar` allows you to scan the next character after the current's scanner `position` and return it as `UnicodeScalar`. If operation succeded internal scanner's `position` is advanced by 1 character (as unicode). If operation fails an exception is thrown.scanfunctionsExample:
#### `func scanInt() throws -> Int` Scan the next integer value after the current scanner's `position`; consume scalars from {0...9} until a non numeric value is encountered. Return the integer representation in base 10. Throw `.invalidInt` if scalar at current position is not in allowed range (may also return `.eof`). If operation succeded internal scanner's `position` is advanced by the number of character which represent an integer. If operation fails an exception is thrown.Example:
#### `func scanFloat() throws -> Float` Scan for a float value (in format ##.##) and convert it to a valid Floast. If scan succeded scanner's `position` is updated at the end of the represented string, otherwise an exception (`.invalidFloat`, `.eof`) is thrown and index is not touched.Example:
#### `func scanHexInt(digits: BitDigits) throws -> Int` Scan an HEX digit expressed in these formats:0x[VALUE](example:0x0000000000564534)0X[VALUE](example:0x0929)#[VALUE](example:#1602)If scan succeded scanner’s
positionis updated at the end of the represented string, otherwise an exception ((.notFound, ).invalidHex,.eof) is thrown and index is not touched.Example:
#### `public func scan(upTo char: UnicodeScalar) throws -> String?` Scan until given character is found starting from current scanner `position` till the end of the source string. Scanner's `position` is updated only if character is found and set just before it. Throw an exception if `.eof` is reached or `.notFound` if char was not found (in this case scanner's position is not updated)Example:
#### `func scan(upTo charSet: CharacterSet) throws -> String?` Scan until given character's is found. Index is reported before the start of the sequence, scanner's `position` is updated only if sequence is found. Throw an exception if `.eof` is reached or `.notFound` if sequence was not found.Example:
#### `func scan(untilIn charSet: CharacterSet) throws -> String?` Scan, starting from scanner's `position` until the next character of the scanner is contained into given character set. Scanner's `position` is updated automatically at the end of the sequence if validated, otherwise it will not touched.Example:
#### `func scan(upTo string: String) throws -> String?` Scan, starting from scanner's `position` until specified string is encountered. Scanner's `position` is updated automatically at the end of the sequence if validated, otherwise it will not touched.Example:
#### `func scan(untilTrue test: ((UnicodeScalar) -> (Bool))) -> String` Scan and consume at the scalar starting from current `position`, testing it with function test. If test returns `true`, the `position` increased. If `false`, the function returns.Example:
#### `func scan(length: Int=1) -> String` Read next length characters and accumulate it If operation is succeded scanner's `position` are updated according to consumed scalars. If fails an exception is thrown and `position` is not updated.Example:
peekfunctionsPeek functions are the same as concept of
#### `func peek(upTo char: UnicodeScalar) -> String.UnicodeScalarView.Index` Peek until chracter is found starting from current scanner's `position`. Scanner's `position` is never updated. Throw an exception if `.eof` is reached or `.notFound` if char was not found.scan()but unless it it does not update internal scanner’spositionindex. These functions usually return onlystarting indexof matched pattern.Example:
#### `func peek(upTo charSet: CharacterSet) -> String.UnicodeScalarView.Index` Peek until one the characters specified by set is encountered Index is reported before the start of the sequence, but scanner's `position` is never updated. Throw an exception if .eof is reached or .notFound if sequence was not foundExample:
#### `func peek(untilIn charSet: CharacterSet) -> String.UnicodeScalarView.Index` Peek until the next character of the scanner is contained into given. Scanner's `position` is never updated.Example:
#### `func peek(upTo string: String) -> String.UnicodeScalarView.Index` Iterate until specified string is encountered without updating indexes. Scanner's `position` is never updated but it's reported the index just before found occourence.Example:
#### `func peek(untilTrue test: ((UnicodeScalar) -> (Bool))) -> String.UnicodeScalarView.Index` Peeks at the scalar at the current position, testing it with function test. It only peeks so current scanner's `position` is not increased at the end of the operationExample:
Other Functions
#### `func match(_ char: UnicodeScalar) -> Bool` Return false if the scalar at the current position don't match given scalar. Advance scanner's `position` to the end of the match if match. #### `func match(_ match: String) -> Bool` Return false if scalars starting at the current position don't match scalars in given string. Advance scanner's `position` to the end of the match string if match. #### `func reset()` Move scanner's internal `position` to the start of the string. #### `func peekAtEnd()` Move to the index's end index. #### `func skip(length: Int = 1) throws` Attempt to advance scanner's by length If operation is not possible (reached the end of the string) it throws and current scanner's `position` of the index did not change If operation succeded scanner's `position` is updated. #### `func back(length: Int = 1) throws` Attempt to advance the position back by length If operation fails scanner's `position` is not touched If operation succeded scaner's `position` is modified according to new value ## Installation You can install Swiftline using CocoaPods, carthage and Swift package managerCocoaPods
Carthage
Swift Package Manager
Add swiftline as dependency in your
## Tests Tests can be found [here](https://github.com/malcommac/SwiftScanner/tree/master/Tests).Package.swiftRun them with
## RequirementsCurrent version is compatible with:
## Credits & License SwiftScanner is owned and maintained by [Daniele Margutti](http://www.danielemargutti.com/).Swift 4.x >= 1.0.4
Swift 3.x: up to 1.0.3
iOS 8 or later
macOS 10.10 or later
watchOS 2.0 or later
tvOS 9.0 or later
…and virtually any platform which is compatible with Swift 3 and implements the Swift Foundation Library
As open source creation any help is welcome!
The code of this library is licensed under MIT License; you can use it in commercial products without any limitation.
The only requirement is to add a line in your Credits/About section with the text below: