lib/standard/string: Added methods prepend, append and insert_at to String interface...
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 9 Jun 2014 15:40:27 +0000 (11:40 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 9 Jun 2014 15:58:51 +0000 (11:58 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/ropes.nit
lib/standard/string.nit
tests/sav/test_flatrope.res [new file with mode: 0644]
tests/test_flatrope.nit [new file with mode: 0644]

index 3426b13..45c1cbd 100644 (file)
@@ -265,7 +265,7 @@ class RopeString
        end
 
        # Inserts a String `str` at position `pos`
-       fun insert_at(str: String, pos: Int): RopeString
+       redef fun insert_at(str, pos)
        do
                if str.length == 0 then return self
                if self.length == 0 then return new RopeString.from(str)
@@ -312,10 +312,10 @@ class RopeString
        end
 
        # Adds `s` at the beginning of self
-       fun prepend(s: String): String do return insert_at(s, 0)
+       redef fun prepend(s) do return insert_at(s, 0)
 
        # Adds `s` at the end of self
-       fun append(s: String): String
+       redef fun append(s)
        do
                if self.is_empty then return s
                return new RopeString.from_root(append_to_path(root,s))
@@ -400,6 +400,26 @@ class RopeString
        end
 end
 
+redef class FlatString
+
+       redef fun append(s) do return (new RopeString.from(self)) + s
+
+       redef fun prepend(s) do return (new RopeString.from(self)).prepend(s)
+
+       redef fun insert_at(s, pos)
+       do
+               if pos == 0 then return prepend(s)
+               if pos == length then return append(s)
+
+               var l = substring(0,pos)
+               var r = substring_from(pos)
+               var ret: String = new RopeString.from(l)
+               ret += s
+               return ret + r
+       end
+
+end
+
 private class RopeStringChars
        super SequenceRead[Char]
 
index 13bf585..fcc1f1b 100644 (file)
@@ -611,6 +611,12 @@ abstract class String
        redef type SELFTYPE: String
 
        redef fun to_s do return self
+
+       fun append(s: String): SELFTYPE is abstract
+
+       fun prepend(s: String): SELFTYPE is abstract
+
+       fun insert_at(s: String, pos: Int): SELFTYPE is abstract
 end
 
 private class FlatSubstringsIter
diff --git a/tests/sav/test_flatrope.res b/tests/sav/test_flatrope.res
new file mode 100644 (file)
index 0000000..5ec30ac
--- /dev/null
@@ -0,0 +1,5 @@
+quick brown fox over the lazy dog
+The quick brown fox over the lazy dog
+quick brown fox jumps over the lazy dog
+quick brown fox over the lazy dog.
+The quick brown fox jumps over the lazy dog.
diff --git a/tests/test_flatrope.nit b/tests/test_flatrope.nit
new file mode 100644 (file)
index 0000000..79b719f
--- /dev/null
@@ -0,0 +1,31 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var st = "quick brown fox over the lazy dog"
+
+print st
+
+var pr = st.prepend("The ")
+
+print pr
+
+pr = st.insert_at(" jumps", 15)
+
+print pr
+
+pr = st.append(".")
+
+print pr
+
+print st.insert_at(" jumps", 15). prepend("The ").append(".")