Mariusz Czarnowski 3 年之前
父节点
当前提交
01b9d1961a

+ 13 - 0
assira.core/src/main/java/net/ranides/assira/text/StrBuffer.java

@@ -868,6 +868,8 @@ public class StrBuffer implements Appendable, CharSequence, Serializable {
 
         private String close = "";
 
+        private String indent = "\t";
+
         @Override
         public String endl() {
             return endl;
@@ -912,6 +914,17 @@ public class StrBuffer implements Appendable, CharSequence, Serializable {
             return this;
         }
 
+        @Override
+        public String indent() {
+            return indent;
+        }
+
+        @Override
+        public StrBuilderOptions indent(String value) {
+            this.indent = value;
+            return this;
+        }
+
         protected BuilderState reset() {
             this.counter = 0;
             return this;

+ 40 - 2
assira.core/src/main/java/net/ranides/assira/text/StrBuilder.java

@@ -53,6 +53,8 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
     
     private int size;
 
+    private int indent;
+
     private final Supplier<Formatter> formatter = LazyReference.unique(() -> new Formatter(this));
 
     final Deque<BuilderState> options = new ArrayDeque<>();
@@ -413,7 +415,10 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
     public StrBuilder append(CharSequence value, int begin, int end) {
         if (value == null) {
             return append();
-        } 
+        }
+        if(indent > 0) {
+            append(value.subSequence(begin, end).toString(), 0, end-begin);
+        }
         reserve(length() + (end - begin));
         for(int i=begin; i<end; i++) {
             buffer[size++] = value.charAt(i);
@@ -443,6 +448,12 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
         if (value == null) {
             return append();
         }
+        if(indent > 0) {
+            String prefix = StringUtils.repeat(indent, options().indent()) + options().endl();
+            value = value.substring(begin, end).replaceAll("(\r\n|\n|\r)", prefix);
+            begin = 0;
+            end = value.length();
+        }
         NativeArrayAllocator.ensureFromTo(value.length(), begin, end);
         int n = end - begin;
         if (n > 0) {
@@ -475,6 +486,11 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
         if (value == null) {
             return append();
         }
+        if(indent > 0) {
+            char[] out = new char[end - begin];
+            value.getChars(begin, end, out, 0);
+            return append(StringUtils.wrap(out));
+        }
         NativeArrayAllocator.ensureFromTo(value.length(), begin, end);
         int n = end - begin;
         if (n > 0) {
@@ -507,6 +523,11 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
         if (value == null) {
             return append();
         }
+        if(indent > 0) {
+            char[] out = new char[end - begin];
+            value.getChars(begin, end, out, 0);
+            return append(StringUtils.wrap(out));
+        }
         NativeArrayAllocator.ensureFromTo(value.length(), begin, end);
         int n = end - begin;
         if (n > 0) {
@@ -539,6 +560,9 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
         if (value == null) {
             return append();
         }
+        if(indent > 0) {
+            return append(StringUtils.wrap(value).subSequence(begin, end));
+        }
         NativeArrayAllocator.ensureFromTo(value.length, begin, end);
         int n = end - begin;
         if (n > 0) {
@@ -1067,7 +1091,9 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
         private String open;
 
         private String close;
-        
+
+        private String indent;
+
         public BuilderState(StrBuilderOptions parent) {
             this.parent = parent;
         }
@@ -1084,6 +1110,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
             state.separator(this.separator());
             state.open(this.open());
             state.close(this.close());
+            state.indent(this.indent());
             
             return state;
         }
@@ -1142,6 +1169,17 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
             this.close = value;
             return this;
         }
+
+        @Override
+        public String indent() {
+            return indent;
+        }
+
+        @Override
+        public StrBuilderOptions indent(String value) {
+            this.indent = value;
+            return this;
+        }
          
     }
     

+ 21 - 0
assira.core/src/main/java/net/ranides/assira/text/StrBuilderOptions.java

@@ -131,6 +131,27 @@ public abstract class StrBuilderOptions implements Serializable {
         throw new UnsupportedOperationException();
     }
 
+    /**
+     * String value used for indentation of lines.
+     *
+     * Default value is: \t
+     *
+     * @return String
+     */
+    public String indent() {
+        return "\t";
+    }
+
+    /**
+     * Changes String value used for indentation of lines.
+     *
+     * @param value
+     * @return
+     */
+    public StrBuilderOptions indent(String value) {
+        throw new UnsupportedOperationException();
+    }
+
     @Override
     public String toString() {
         String b = hex(blank());