Created
June 29, 2015 11:42
-
-
Save gavinking/e43a4b9cacdb506f3bf1 to your computer and use it in GitHub Desktop.
function to parse properties files
This file contains 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
"Parse a properties file." | |
void parsePropertiesFile(String textContent, | |
void handleEntry(String key, String text)) { | |
value lines = textContent.lines.iterator(); | |
while (!is Finished rawline = lines.next()) { | |
value builder = StringBuilder(); | |
builder.append(rawline); | |
variable value lastline = rawline; | |
while (lastline.endsWith("\\"), //line continuation | |
!is Finished nextline = lines.next()) { | |
builder.deleteTerminal(1); //remove the \ | |
variable value trimming = true; | |
value chars = nextline.iterator(); | |
while (!is Finished char = chars.next()) { | |
if (trimming) { //trim leading whitespace | |
if (char.whitespace) { | |
continue; | |
} | |
else { | |
trimming = false; | |
} | |
} | |
builder.appendCharacter(char); | |
} | |
lastline = nextline; | |
} | |
value line = builder.string; | |
if (exists first = line.first, | |
!first in "!#", //ignore comments | |
exists index //split key/value on = or : | |
= line.firstIndexWhere("=:".contains)) { | |
value [key, definition] = line.slice(index); | |
builder.clear(); | |
variable value trimming = true; | |
value chars = definition.iterator(); | |
chars.next(); //skip the = or : | |
while (!is Finished char = chars.next()) { | |
if (trimming) { //trim leading whitespace | |
if (char.whitespace) { | |
continue; | |
} | |
else { | |
trimming = false; | |
} | |
} | |
Character c; | |
if (char=='\\', //interpolate \ escape | |
!is Finished next = chars.next()) { | |
c = switch (next) | |
case ('n') '\n' | |
case ('r') '\r' | |
case ('t') '\t' | |
case ('\\') '\\' | |
//TODO: unicode escapes! | |
else next; | |
} | |
else { | |
c = char; | |
} | |
builder.appendCharacter(c); | |
} | |
handleEntry(key.trimmed, builder.string); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment