lib/string: document FlatString::with_infos
[nit.git] / lib / standard / string.nit
index 2c46179..0b4b1e9 100644 (file)
@@ -401,7 +401,7 @@ abstract class Text
        #    assert "\r\n\r\n".chomp == "\r\n"
        #    assert "\r\n\r".chomp   == "\r\n"
        #
-       # Note: unlike with most IO methods like `IStream::read_line`,
+       # Note: unlike with most IO methods like `Reader::read_line`,
        # a single `\r` is considered here to be a line terminator and will be removed.
        fun chomp: SELFTYPE
        do
@@ -439,10 +439,10 @@ abstract class Text
        # REQUIRE: `left >= 0.0 and left <= 1.0`
        # ENSURE: `self.length <= length implies result.length == length`
        # ENSURE: `self.length >= length implies result == self`
-       fun justify(length: Int, left: Float): SELFTYPE
+       fun justify(length: Int, left: Float): String
        do
                var diff = length - self.length
-               if diff <= 0 then return self
+               if diff <= 0 then return to_s
                assert left >= 0.0 and left <= 1.0
                var before = (diff.to_f * left).to_i
                return " " * before + self + " " * (diff-before)
@@ -711,7 +711,7 @@ abstract class Text
        #     assert "a&b-<>\"x\"/'".html_escape      ==  "a&amp;b-&lt;&gt;&#34;x&#34;&#47;&#39;"
        #
        # SEE: <https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content>
-       fun html_escape: SELFTYPE
+       fun html_escape: String
        do
                var buf = new FlatBuffer
 
@@ -877,7 +877,7 @@ end
 abstract class String
        super Text
 
-       redef type SELFTYPE: String
+       redef type SELFTYPE: String is fixed
 
        redef fun to_s do return self
 
@@ -1163,10 +1163,14 @@ class FlatString
        #              String Specific Methods           #
        ##################################################
 
-       private init with_infos(items: NativeString, len: Int, from: Int, to: Int)
+       # Low-level creation of a new string with given data.
+       #
+       # `items` will be used as is, without copy, to retrieve the characters of the string.
+       # Aliasing issues is the responsibility of the caller.
+       private init with_infos(items: NativeString, length: Int, from: Int, to: Int)
        do
                self.items = items
-               length = len
+               self.length = length
                index_from = from
                index_to = to
        end
@@ -1399,7 +1403,7 @@ end
 abstract class Buffer
        super Text
 
-       redef type SELFTYPE: Buffer
+       redef type SELFTYPE: Buffer is fixed
 
        # Specific implementations MUST set this to `true` in order to invalidate caches
        protected var is_dirty = true
@@ -1522,8 +1526,6 @@ class FlatBuffer
        super FlatText
        super Buffer
 
-       redef type SELFTYPE: FlatBuffer
-
        redef var chars: Sequence[Char] = new FlatBufferCharView(self)
 
        private var capacity: Int = 0
@@ -1911,15 +1913,22 @@ redef class Int
                end
        end
 
+       # C function to calculate the length of the `NativeString` to receive `self`
+       private fun int_to_s_len: Int is extern "native_int_length_str"
+
        # C function to convert an nit Int to a NativeString (char*)
-       private fun native_int_to_s: NativeString is extern "native_int_to_s"
+       private fun native_int_to_s(nstr: NativeString, strlen: Int) is extern "native_int_to_s"
 
        # return displayable int in base 10 and signed
        #
        #     assert 1.to_s            == "1"
        #     assert (-123).to_s       == "-123"
        redef fun to_s do
-               return native_int_to_s.to_s
+               var nslen = int_to_s_len
+               var ns = new NativeString(nslen + 1)
+               ns[nslen] = '\0'
+               native_int_to_s(ns, nslen + 1)
+               return ns.to_s_with_length(nslen)
        end
 
        # return displayable int in hexadecimal
@@ -2011,23 +2020,6 @@ redef class Float
 
                return p1 + "." + p2
        end
-
-       # `self` representation with `nb` digits after the '.'.
-       #
-       #     assert 12.345.to_precision_native(1) == "12.3"
-       #     assert 12.345.to_precision_native(2) == "12.35"
-       #     assert 12.345.to_precision_native(3) == "12.345"
-       #     assert 12.345.to_precision_native(4) == "12.3450"
-       fun to_precision_native(nb: Int): String import NativeString.to_s `{
-               int size;
-               char *str;
-
-               size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
-               str = malloc(size + 1);
-               sprintf(str, "%.*f", (int)nb, recv );
-
-               return NativeString_to_s( str );
-       `}
 end
 
 redef class Char