diff --git a/build.gradle b/build.gradle index ff68a1c..d3ea35b 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ repositories { } archivesBaseName = 'Simple Code Generator' -version = '1.0.3' +version = '1.0.4' apply plugin: 'maven' tasks.withType(JavaCompile) { diff --git a/src/main/java/speiger/src/builder/mappers/ArgumentMapper.java b/src/main/java/speiger/src/builder/mappers/ArgumentMapper.java index 7032a08..fb52fc5 100644 --- a/src/main/java/speiger/src/builder/mappers/ArgumentMapper.java +++ b/src/main/java/speiger/src/builder/mappers/ArgumentMapper.java @@ -1,8 +1,8 @@ package speiger.src.builder.mappers; -import java.util.regex.Matcher; import java.util.regex.Pattern; +import speiger.src.builder.misc.RegexMatcher; import speiger.src.builder.misc.RegexUtil; public class ArgumentMapper implements IMapper @@ -49,7 +49,7 @@ public class ArgumentMapper implements IMapper @Override public String apply(String t) { - Matcher matcher = pattern.matcher(t); + RegexMatcher matcher = new RegexMatcher(pattern, t); try { if(matcher.find()) @@ -57,17 +57,16 @@ public class ArgumentMapper implements IMapper StringBuffer buffer = new StringBuffer(); do { - if(matcher.end() < RegexUtil.getLastAppendPosition(matcher)) return apply(matcher.appendTail(buffer).toString()); String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1)); if(!text.isEmpty()) { - RegexUtil.skip(matcher.appendReplacement(buffer, ""), text.length()); + matcher.appendReplacement(buffer, "").skip(text.length()); buffer.append(String.format(replacement, (Object[])getString(text).split(argumentBreaker))); } } while(matcher.find()); matcher.appendTail(buffer); - return buffer.toString(); + return apply(buffer.toString()); } } catch(Exception e) diff --git a/src/main/java/speiger/src/builder/mappers/InjectMapper.java b/src/main/java/speiger/src/builder/mappers/InjectMapper.java index 7ae1d73..bd6e778 100644 --- a/src/main/java/speiger/src/builder/mappers/InjectMapper.java +++ b/src/main/java/speiger/src/builder/mappers/InjectMapper.java @@ -1,8 +1,8 @@ package speiger.src.builder.mappers; -import java.util.regex.Matcher; import java.util.regex.Pattern; +import speiger.src.builder.misc.RegexMatcher; import speiger.src.builder.misc.RegexUtil; public class InjectMapper implements IMapper @@ -47,7 +47,7 @@ public class InjectMapper implements IMapper @Override public String apply(String t) { - Matcher matcher = pattern.matcher(t); + RegexMatcher matcher = new RegexMatcher(pattern, t); try { if(matcher.find()) @@ -55,16 +55,15 @@ public class InjectMapper implements IMapper StringBuffer buffer = new StringBuffer(); do { - if(matcher.end() < RegexUtil.getLastAppendPosition(matcher)) return apply(matcher.appendTail(buffer).toString()); String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1)); if(!text.isEmpty()) { - RegexUtil.skip(matcher.appendReplacement(buffer, ""), text.length()); + matcher.appendReplacement(buffer, "").skip(text.length()); buffer.append(String.format(replacement, getString(text))); } } while (matcher.find()); matcher.appendTail(buffer); - return buffer.toString(); + return apply(buffer.toString()); } } catch(Exception e) diff --git a/src/main/java/speiger/src/builder/mappers/LineMapper.java b/src/main/java/speiger/src/builder/mappers/LineMapper.java index 58cf750..d155759 100644 --- a/src/main/java/speiger/src/builder/mappers/LineMapper.java +++ b/src/main/java/speiger/src/builder/mappers/LineMapper.java @@ -1,8 +1,8 @@ package speiger.src.builder.mappers; -import java.util.regex.Matcher; import java.util.regex.Pattern; +import speiger.src.builder.misc.RegexMatcher; import speiger.src.builder.misc.RegexUtil; public class LineMapper implements IMapper @@ -30,7 +30,7 @@ public class LineMapper implements IMapper @Override public String apply(String t) { - Matcher matcher = pattern.matcher(t); + RegexMatcher matcher = new RegexMatcher(pattern, t); if(matcher.find()) { StringBuffer buffer = new StringBuffer(); @@ -42,11 +42,11 @@ public class LineMapper implements IMapper { matcher.appendReplacement(buffer, pattern.pattern()); buffer.setLength(buffer.length() - (start - result[0])); - RegexUtil.skip(matcher, result[1] - start); + matcher.skip(result[1] - start); } } while (matcher.find()); matcher.appendTail(buffer); - return buffer.toString(); + return apply(buffer.toString()); } return t; } diff --git a/src/main/java/speiger/src/builder/misc/RegexMatcher.java b/src/main/java/speiger/src/builder/misc/RegexMatcher.java new file mode 100644 index 0000000..8d610e9 --- /dev/null +++ b/src/main/java/speiger/src/builder/misc/RegexMatcher.java @@ -0,0 +1,84 @@ +package speiger.src.builder.misc; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegexMatcher +{ + Matcher match; + StringWrapper text; + + public RegexMatcher(Pattern pattern, String text) + { + this.text = new StringWrapper(text); + match = pattern.matcher(this.text); + } + + public boolean find() + { + return match.find(); + } + + public int end() + { + return match.end()+text.getOffset(); + } + + public RegexMatcher appendReplacement(StringBuffer sb, String replacement) + { + match.appendReplacement(sb, replacement); + return this; + } + + public RegexMatcher appendTail(StringBuffer sb) + { + match.appendTail(sb); + return this; + } + + public RegexMatcher skip(int amount) + { + text.offset(amount+match.end()); + match.reset(); + return this; + } + + public static class StringWrapper implements CharSequence + { + String s; + int offset; + + public StringWrapper(String s) + { + this.s = s; + } + + @Override + public int length() + { + return s.length() - offset; + } + + @Override + public char charAt(int index) + { + return s.charAt(index+offset); + } + + @Override + public CharSequence subSequence(int start, int end) + { + return s.subSequence(start+offset, end+offset); + } + + public void offset(int offset) + { + this.offset += offset; + } + + public int getOffset() + { + return offset; + } + } +} diff --git a/src/main/java/speiger/src/builder/misc/RegexUtil.java b/src/main/java/speiger/src/builder/misc/RegexUtil.java index 99128ad..d860438 100644 --- a/src/main/java/speiger/src/builder/misc/RegexUtil.java +++ b/src/main/java/speiger/src/builder/misc/RegexUtil.java @@ -1,37 +1,7 @@ package speiger.src.builder.misc; -import java.lang.reflect.Field; -import java.util.regex.Matcher; - public class RegexUtil { - static Field LAST_POS; - - public static Matcher skip(Matcher matcher, int amount) - { - try - { - LAST_POS.setInt(matcher, LAST_POS.getInt(matcher) + amount); - } - catch(Exception e) - { - e.printStackTrace(); - } - return matcher; - } - - public static int getLastAppendPosition(Matcher matcher) - { - try - { - return LAST_POS.getInt(matcher); - } - catch(Exception e) - { - throw new RuntimeException(e); - } - } - public static String searchUntil(String text, int startIndex, char increase, char decrease) { if(text.charAt(startIndex + 1) != increase) @@ -72,19 +42,4 @@ public class RegexUtil if(start == -1) return null; return new int[]{start, offset}; } - - static - { - try - { - Field field = Matcher.class.getDeclaredField("lastAppendPosition"); - field.setAccessible(true); - LAST_POS = field; - } - catch(Exception e) - { - e.printStackTrace(); - System.exit(0); - } - } -} +} \ No newline at end of file