@Test @DisplayName("Unterminated placeholders are handled gracefully") void unterminatedTest(){ final var sut = new Template(JVM8Tools.asMap("a","b", "huhh","spooky")); Assertions.assertEquals("this is {{a...}} spooky bab{{oon", sut.resolve("this is {{a...}} {{huhh}} {{a}}a{{a}}{{oon")); }
public String resolve(String s) { final StringBuilder sb = new StringBuilder(s); int position = 0; int start; while ((start = sb.indexOf("{{", position)) >= 0) { int end = sb.indexOf("}}",start); if( end > 0 ) { final var key = sb.substring(start + 2, end); if( params.containsKey(key) && params.get(key) != null ){ final var value = params.get(key); sb.replace(start,end+2,value); position = start + value.length(); }else{ position = end + 2; } }else{ break; } } return sb.toString(); }
Template { public String resolve(String s) { final StringBuilder sb = new StringBuilder(s); int position = 0; int start; while ((start = sb.indexOf("{{", position)) >= 0) { int end = sb.indexOf("}}",start); if( end > 0 ) { final var key = sb.substring(start + 2, end); if( params.containsKey(key) && params.get(key) != null ){ final var value = params.get(key); sb.replace(start,end+2,value); position = start + value.length(); }else{ position = end + 2; } }else{ break; } } return sb.toString(); } }
Template { public String resolve(String s) { final StringBuilder sb = new StringBuilder(s); int position = 0; int start; while ((start = sb.indexOf("{{", position)) >= 0) { int end = sb.indexOf("}}",start); if( end > 0 ) { final var key = sb.substring(start + 2, end); if( params.containsKey(key) && params.get(key) != null ){ final var value = params.get(key); sb.replace(start,end+2,value); position = start + value.length(); }else{ position = end + 2; } }else{ break; } } return sb.toString(); } Template(Map<String, String> params); }
Template { public String resolve(String s) { final StringBuilder sb = new StringBuilder(s); int position = 0; int start; while ((start = sb.indexOf("{{", position)) >= 0) { int end = sb.indexOf("}}",start); if( end > 0 ) { final var key = sb.substring(start + 2, end); if( params.containsKey(key) && params.get(key) != null ){ final var value = params.get(key); sb.replace(start,end+2,value); position = start + value.length(); }else{ position = end + 2; } }else{ break; } } return sb.toString(); } Template(Map<String, String> params); String resolve(String s); }
Template { public String resolve(String s) { final StringBuilder sb = new StringBuilder(s); int position = 0; int start; while ((start = sb.indexOf("{{", position)) >= 0) { int end = sb.indexOf("}}",start); if( end > 0 ) { final var key = sb.substring(start + 2, end); if( params.containsKey(key) && params.get(key) != null ){ final var value = params.get(key); sb.replace(start,end+2,value); position = start + value.length(); }else{ position = end + 2; } }else{ break; } } return sb.toString(); } Template(Map<String, String> params); String resolve(String s);  }