Swiftpack.co - ZkHaider/ParsingKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ZkHaider.
ZkHaider/ParsingKit 2.1.0
Parse generic types from strings, create your own custom parsers.
⭐️ 1
🕓 4 years ago
.package(url: "https://github.com/ZkHaider/ParsingKit.git", from: "2.1.0")

ParsingKit

Create custom parsers to extract types from String types.

Installation

Carthage

Add github "ZkHaider/ParsingKit" "master" to your Cartfile, and run Carthage.

Requirements

  • Deployment target iOS 10.0+
  • Swift 4+
  • Xcode 10+

Parser Type

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 Parsers that already handle parsing of certain Characters such as:

  • Float
  • Double
  • Character
  • CGPoint
  • CGRect
  • CGSize
  • { or }
  • [ or ]
  • ( or )
  • One or more spaces
  • Zero or more spaces

Create your own

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)

GitHub

link
Stars: 1
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics