Created
October 11, 2017 09:18
-
-
Save santoshrajan/372fc78a31656ed71e9c3ec9aff554c2 to your computer and use it in GitHub Desktop.
String Parser in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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