CleanerPool > Cutter > 2 methods
This commit is contained in:
parent
01f9aef845
commit
4befbd7147
2 changed files with 51 additions and 5 deletions
|
@ -76,18 +76,24 @@ public class CleanerPool {
|
||||||
|
|
||||||
String execute(String value, List<String> constants, String pattern);
|
String execute(String value, List<String> constants, String pattern);
|
||||||
|
|
||||||
default String cutOpenable(String value, List<String> constants, String pattern, char open, char close){
|
default int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
System.out.println("cutOpenable "+value);
|
||||||
|
|
||||||
for(char c : value.toCharArray()){
|
for(int i = 0; i < value.length(); i++){
|
||||||
|
char c = value.charAt(i);
|
||||||
if(c == open){
|
if(c == open){
|
||||||
|
i+=cutOpenable(value.substring(i+1), constants, pattern, '<', '>');
|
||||||
|
builder.append(pattern+(constants.size()-1));
|
||||||
}else if(c == close){
|
}else if(c == close){
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
builder.append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.toString();
|
constants.add(builder.toString());
|
||||||
|
return builder.length()+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cutter;
|
||||||
|
|
||||||
|
public class CleanerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cutter(){
|
||||||
|
Cutter generic = new Cutter(){
|
||||||
|
@Override
|
||||||
|
public String execute(String value, List<String> constants, String pattern){
|
||||||
|
String result = "";
|
||||||
|
for(int i = 0; i < value.length(); i++){
|
||||||
|
char c = value.charAt(i);
|
||||||
|
if(c == '<'){
|
||||||
|
i+=cutOpenable(value.substring(i+1), constants, pattern, '<', '>');
|
||||||
|
result+=pattern+(constants.size()-1);
|
||||||
|
}else{
|
||||||
|
result+=c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
String result = generic.execute("test<Test<Lol>>", list, "$TEST");
|
||||||
|
assertEquals(list.size(), 2);
|
||||||
|
assertEquals(list.get(0), "Lol");
|
||||||
|
assertEquals(list.get(1), "Test$TEST0");
|
||||||
|
assertEquals("test$TEST1", result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue