Explorar o código

new: #109 StringUtils#replace

Ranides Atterwim %!s(int64=2) %!d(string=hai) anos
pai
achega
833bd5678b

+ 75 - 0
assira.core/src/main/java/net/ranides/assira/text/StringUtils.java

@@ -19,6 +19,7 @@ import java.util.NoSuchElementException;
 import java.util.function.Function;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.IntStream;
 
 import lombok.experimental.UtilityClass;
 import net.ranides.assira.collection.query.CQuery;
@@ -775,6 +776,38 @@ public class StringUtils {
         return buf.toString();
     }
 
+    public static String replace(String text, int begin, int end, String newval) {
+        if(begin == 0) {
+            if(end == text.length()) {
+                return newval;
+            }
+            return newval + text.substring(end);
+        }
+        if(end == text.length()) {
+            if(begin == 0) {
+                return newval;
+            }
+            return text.substring(0, begin) + newval;
+        }
+        return text.substring(0, begin) + newval + text.substring(end);
+    }
+
+    public static CharSequence replace(CharSequence text, int begin, int end, CharSequence newval) {
+        if(begin == 0) {
+            if(end == text.length()) {
+                return newval;
+            }
+            return concat(newval, text.subSequence(end, text.length()));
+        }
+        if(end == text.length()) {
+            if(begin == 0) {
+                return newval;
+            }
+            return concat(text.subSequence(0, begin), newval);
+        }
+        return concat(text.subSequence(0, begin), newval, text.subSequence(end, text.length()));
+    }
+
     /**
      * Concatenates all values using "separator".
      * Values are first converted to text using "toString" method.
@@ -1344,6 +1377,10 @@ public class StringUtils {
         return new NCSequence(n, chr);
     }
 
+    public static CharSequence concat(CharSequence... fragments) {
+        return new ConcatSequence(fragments);
+    }
+
     /**
      * Returns length of the string, or zero if string is null.
      *
@@ -1789,5 +1826,43 @@ public class StringUtils {
             return text.subSequence(begin,end).toString();
         }
     }
+
+    private static final class ConcatSequence implements CharSequence {
+        private final CharSequence[] fragments;
+
+        public ConcatSequence(CharSequence[] fragments) {
+            this.fragments = fragments;
+        }
+
+        @Override
+        public int length() {
+            int out = 0;
+            for(CharSequence s : fragments) {
+                out += s.length();
+            }
+            return out;
+        }
+
+        @Override
+        public char charAt(int index) {
+            for(CharSequence s : fragments) {
+                if(index < s.length()) {
+                    return s.charAt(index);
+                }
+                index -= s.length();
+            }
+            throw new StringIndexOutOfBoundsException(index);
+        }
+
+        @Override
+        public CharSequence subSequence(int start, int end) {
+            return new CSSub(this, start, end);
+        }
+
+        @Override
+        public String toString() {
+            return StringUtils.join(fragments, "");
+        }
+    }
     
 }

+ 30 - 0
assira.core/src/test/java/net/ranides/assira/text/StringUtilsTest.java

@@ -650,4 +650,34 @@ c*/
 	        assertEquals(expected.charAt(i), value.charAt(i));
         }
     }
+
+    @Test
+    public void testReplace() {
+        String input = "hello world";
+
+        assertEquals("helLO WOrld", StringUtils.replace(input, 3, 8, "LO WO"));
+        assertEquals("help the world", StringUtils.replace(input, 3, 8, "p the wo"));
+        assertEquals("helrld", StringUtils.replace(input, 3, 8, ""));
+
+        assertEquals("your world", StringUtils.replace(input, 0, 8, "your wo"));
+        assertEquals("hello sweety", StringUtils.replace(input, 6, 11, "sweety"));
+    }
+
+    @Test
+    public void testReplaceSequence() {
+        CharSequence input = "hello world";
+
+        assertEquals("helLO WOrld", StringUtils.replace(input, 3, 8, "LO WO").toString());
+        assertEquals("help the world", StringUtils.replace(input, 3, 8, "p the wo").toString());
+        assertEquals("helrld", StringUtils.replace(input, 3, 8, "").toString());
+
+        assertEquals("your world", StringUtils.replace(input, 0, 8, "your wo").toString());
+        assertEquals("hello sweety", StringUtils.replace(input, 6, 11, "sweety").toString());
+    }
+
+    @Test
+    public void testConcat() {
+        assertEquals("hello world", StringUtils.concat("hello", " ", "world").toString());
+        assertEquals("hello world", new StrBuilder().append(StringUtils.concat("hello", " ", "world")).toString());
+    }
 }