Last active
February 27, 2022 10:58
-
-
Save angusdev/3a915a3656411fdadfdb76292122ac84 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
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Write a <code>static String format(String format, Map<String, String> values)</code> to replace | |
* named key to values. E.g. | |
* * <pre> | |
* My name is ${name}. I am ${age} years old. | |
* -> | |
* My name is alice. I am 10 years old. | |
* </pre> | |
* Also need to escape <code>$$</code> to <code>$</code>, so it can produce:<br/> | |
* <code>$${name}}</code> -> <code>${name}</code> instead of <code>$alice</code><br/> | |
* <code>$$${name}</code> -> <code>$alice</code> | |
*/ | |
public class StringFormatChallenge { | |
private static String format(String format, Map<String, String> values) { | |
format = format.replace("${foo}", "bar"); | |
format = format.replace("${a.lice}", "bob"); | |
format = format.replace("${brown}", "fox"); | |
format = format.replace("$$", "$"); | |
return format; | |
} | |
private static void doTests(String[][] data, Map<String, String> values) { | |
int maxFormatLen = 0; | |
int maxResultLen = 0; | |
int passCount = 0; | |
for (String[] d : data) { | |
maxFormatLen = Math.max(maxFormatLen, d[0].length()); | |
maxResultLen = Math.max(maxResultLen, d[1].length()); | |
} | |
for (String[] d : data) { | |
System.out.print((d[0] + " ").substring(0, maxFormatLen)); | |
System.out.print(" -> "); | |
System.out.print((d[1] + " ").substring(0, maxResultLen)); | |
System.out.print(" : "); | |
String result = StringFormatChallenge.format(d[0], values); | |
if (result.equals(d[1])) { | |
System.out.print("PASS"); | |
passCount++; | |
} | |
else { | |
System.out.print("FAIL " + result); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
System.out.println("Summary: " + passCount + "/" + data.length); | |
System.out.println(); | |
} | |
private static void doTest(String format, String expected, Map<String, String> values) { | |
String result = format(format, values); | |
System.out.println("Format: " + format); | |
System.out.println("Expected Result: " + expected); | |
if (result.equals(expected)) { | |
System.out.println("Result: PASS"); | |
} | |
else { | |
System.out.println("Result: FAIL " + result); | |
} | |
System.out.println(); | |
} | |
public static void main(String[] args) { | |
Map<String, String> values = new HashMap<>(); | |
values.put("foo", "bar"); | |
values.put("a.lice", "bob"); | |
values.put("brown", "fox"); | |
values.put("", "empty"); | |
doTests(new String[][] { { "${foo}", "bar" }, { "${a.lice}", "bob" }, { "${}", "empty" }, { "$${}", "${}" }, | |
{ "${", "${" }, { "${Foo}", "${Foo}" }, { "$foo", "$foo" }, { "$${foo}", "${foo}" }, | |
{ "$$$${foo}", "$${foo}", }, { "$${$${foo}}", "${${foo}}" }, { "$$${foo}", "$bar" }, | |
{ "$$$$${foo}", "$$bar" }, { "$${${foo}}", "${bar}" }, { "${${foo}}", "${bar}" }, | |
{ "$$$${${foo}}", "$${bar}" }, { "$$${${foo}}", "$${bar}" }, { "$${$${${foo}}}", "${${bar}}" }, | |
{ "$", "$" }, { "$$", "$" }, { "$$$", "$$" }, { "$$$$", "$$" } }, values); | |
doTest("foobar, foo:${foo}, a.lice:${a.lice}, foo:${foo}, brown:${brown}, foobar", | |
"foobar, foo:bar, a.lice:bob, foo:bar, brown:fox, foobar", values); | |
doTest("foobar, $${foo}, $$${foo}, $$$$${foo}, $${${foo}}, $${$${${foo}}} $$$$, foobar", | |
"foobar, ${foo}, $bar, $$bar, ${bar}, ${${bar}} $$, foobar", values); | |
int repeat = 10000000; | |
String format = "foobar, foo:${foo}, a.lice:${a.lice}, foo:${foo}, brown:${brown}, foobar"; | |
System.out.println("Run " + String.valueOf(repeat).replaceAll("(\\d)(?=(\\d{3})+$)", "$1,") + " times: " + format); | |
long start = System.nanoTime(); | |
for (int i = 0; i < repeat; i++) { | |
format(format, values); | |
} | |
long end = System.nanoTime(); | |
System.out.println(((end - start) / 1000000000.0) + " sec"); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment