-
-
Save savage69kr/5a775d9d5e160f75b508 to your computer and use it in GitHub Desktop.
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
class Main { | |
static function parseProperties(text:String):Map<String, String> { | |
var map:Map<String, String> = new Map(), | |
ofs:Int = 0, | |
len:Int = text.length, | |
i:Int, j:Int, | |
endl:Int; | |
while (ofs < len) { | |
// find line end offset: | |
endl = text.indexOf("\n", ofs); | |
if (endl < 0) endl = len; // last line | |
// do not process comment lines: | |
i = text.charCodeAt(ofs); | |
if (i != "#".code && i != "!".code) { | |
// find key-value delimiter: | |
i = text.indexOf("=", ofs); | |
j = text.indexOf(":", ofs); | |
if (j < i) i = j; | |
// | |
if (i >= ofs && i < endl) { | |
// key-value pair "key: value\n" | |
map.set(StringTools.trim(text.substring(ofs, i)), | |
StringTools.trim(text.substring(i + 1, endl))); | |
} else { | |
// value-less declaration "key\n" | |
map.set(StringTools.trim(text.substring(ofs, endl)), ""); | |
} | |
} | |
// move on to next line: | |
ofs = endl + 1; | |
} | |
return map; | |
} | |
static function main() { | |
var src = "value1: some text\n" | |
+ "#value2 = test\n" | |
+ "value3 = score : {num}", | |
out = parseProperties(src); | |
trace(src); | |
trace(out); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment