#strict 2 // FormatN("%identifier%format specification%...", { identifier = "content", ... }); // example: FormatN("%foo%s% %bar%s%", { foo = "Hello", bar = "World" }) = "Hello World" global func FormatN(string format, map items) { var ret = ""; var inPlaceholder = 0; var placeholderType = "%"; var placeholderPart = ""; for(var i = 0; i < GetLength(format); ++i) { var c = format[i]; if(c == "%") { if(inPlaceholder == 0) { inPlaceholder = 1; } else if(inPlaceholder == 1) { if(placeholderPart == "") { ret ..= "%"; inPlaceholder = 0; } else { inPlaceholder = 2; } } else if(inPlaceholder == 2) { ret ..= Format(placeholderType, items[placeholderPart]); inPlaceholder = 0; placeholderType = "%"; placeholderPart = ""; } } else { if(inPlaceholder == 0) { ret ..= c; } else if(inPlaceholder == 1) { placeholderPart ..= c; } else if(inPlaceholder == 2) { placeholderType ..= c; } } } if(inPlaceholder == 1) { FatalError(Format("FormatN: Placeholder not finished at end of format-string: \"%%%s\"", placeholderPart)); return 0; } else if(inPlaceholder == 2) { FatalError(Format("FormatN: Placeholder not finished at end of format-string: \"%%%s%s\"", placeholderPart, placeholderType)); return 0; } return ret; }