My XML file encodes strings in between single-quotes. Array elements are comma-separated.
'Hello', 'World'
Normally, extracting comma-separated values is easy:
let result = myString.split{ $0 == "," }.map(String.init)
The problem is that I can't recklessly split on the comma: if the comma is enclosed in single quotes it is text, otherwise it is an array element separator:
'Hello', 'World', 'Hello, World'
Should produce:
["Hello", "World", "Hello, World"]
Note two things:
- An empty single quotes is an empty string that can't be discarded (it's an element in an array)
- I can't guarantee whitespace between the elements; a user may have tampered with a file.
'Hello', 'World' , 'Hello, World', ''
should produce:
["Hello", "World", "Hello, World", ""]
I need some way to differentiate between a comma-separated value which lies outside of the single quotes: ', ' A better approach would probably be to retain anything in between single-quotes, but I don't know how to do this.