Skip to content

Instantly share code, notes, and snippets.

@santoshrajan
Created October 11, 2017 09:18
Show Gist options
  • Save santoshrajan/372fc78a31656ed71e9c3ec9aff554c2 to your computer and use it in GitHub Desktop.
Save santoshrajan/372fc78a31656ed71e9c3ec9aff554c2 to your computer and use it in GitHub Desktop.
String Parser in Swift
import Foundation
typealias ParseResult = (output: Any, rest: Substring)?
func stringParser(input: Substring) -> ParseResult {
if input[input.startIndex] != "\"" {
return nil
}
var isEscape = true
let index = input.index() {
if $0 == "\"" && !isEscape {
return true
}
isEscape = $0 == "\\" ? true : false
return false
}
if let index = index {
return (input[input.index(after: input.startIndex)...input.index(before: index)],
input[input.index(after: index)...])
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment