I am working on a String parser in which the input can have various formats, and I don't know in advance which format is being used, so I need to write something that is flexible.
The first step is to check the first few characters, I can check that by using eg:
func parse(input: String) -> String {
let result: String
if (input.hasPrefix("foo") {
result = doFoo(input)
}
else if (input.hasPrefix("bar") {
result = doBar(input)
}
else if (input.hasPrefix("baz") {
result = doBaz(input)
}
else {
result = doBasic(input)
}
return result
}
and every doXXX() function has it's own parsing code, which again could have multiple options, such as different delimiters, etc.
This could easily turn into lots of if-else code, and I am wondering if with Swift there is a simpler way to do this. Maybe using switch-case statements, or something else? Could I use an enum for this?
EDIT: the code is inside a String extension.