Create custom parsers to extract types from String
types.
Add github "ZkHaider/ParsingKit" "master"
to your Cartfile, and run Carthage.
Our base unit for this framework is the Parser
it has a run function which takes a Substring
due to performance benefits and parses a generic type T
.
public struct Parser<T> {
let run: (inout Substring) -> T?
}
This library has some built in Parser
s that already handle parsing of certain Character
s such as:
Float
Double
Character
CGPoint
CGRect
CGSize
{
or }
[
or ]
(
or )
You can create your own Parser
by extending it as so:
// Example string to parse:
// "{2.0}"
struct MyCustomStruct {
let someFloat: Float
}
extension Parser where T == MyCustomStruct {
public static let myCustomStructParser: Parser<T> = {
return zip(
.leftCurlyBrace,
.float,
.rightCurlyBrace
).map { _, float, _ in
return MyCustomStruct(someFloat: float)
}
}()
}
let stringToParse: String = "{2.0}"
let parser = Parser.myCustomStructParser
parser.run(stringToParse)
// Output: (match: Float(2.0), rest: nil)
link |
Stars: 1 |
Last commit: 4 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics