Merge branch 'intern_native_arg'
[nit.git] / lib / standard / string.nit
index f482a88..4c575b5 100644 (file)
@@ -177,6 +177,25 @@ abstract class AbstractString
                return s.to_s
        end
 
+       # Trims trailing and preceding white spaces
+       # A whitespace is defined as any character which ascii value is less than or equal to 32
+       fun trim: String
+       do
+               if self._length == 0 then return self.to_s
+               # find position of the first non white space char (ascii < 32) from the start of the string
+               var start_pos = 0
+               while self[start_pos].ascii <= 32 do
+                       start_pos += 1
+                       if start_pos == _length then return ""
+               end
+               # find position of the first non white space char from the end of the string
+               var end_pos = length - 1
+               while self[end_pos].ascii <= 32 do
+                       end_pos -= 1
+                       if end_pos == start_pos then return self[start_pos].to_s
+               end
+               return self.substring(start_pos, end_pos - start_pos + 1)
+       end
 
        redef fun output
        do
@@ -331,6 +350,26 @@ class String
                return new String.with_native(outstr, self._length)
        end
 
+       redef fun trim: String
+       do
+               if self._length == 0 then return self
+               # find position of the first non white space char (ascii < 32) from the start of the string
+               var start_pos = self._index_from
+               while _items[start_pos].ascii <= 32 do
+                       start_pos += 1
+                       if start_pos == _index_to + 1 then return ""
+               end
+               # find position of the first non white space char from the end of the string
+               var end_pos = _index_to
+               while _items[end_pos].ascii <= 32 do
+                       end_pos -= 1
+                       if end_pos == start_pos then return _items[start_pos].to_s
+               end
+               start_pos -= index_from
+               end_pos -= index_from
+               return self.substring(start_pos, end_pos - start_pos + 1)
+       end
+
        redef fun output
        do
                var i = self._index_from
@@ -375,6 +414,19 @@ class String
                with_native(str,str.cstring_length)
        end
 
+       # Creates a new Nit String from an existing CString
+       # Pretty much equals to from_cstring but copies instead
+       # of passing a reference
+       # Avoids manual/automatic dealloc problems when dealing with native C code
+       init copy_from_native(str: NativeString)
+       do
+               var temp_length = str.cstring_length
+               var new_str = calloc_string(temp_length + 1)
+               str.copy_to(new_str, temp_length, 0, 0)
+               new_str[temp_length] = '\0'
+               with_native(new_str, temp_length)
+       end
+
        # Return a null terminated char *
        fun to_cstring: NativeString
        do
@@ -448,12 +500,7 @@ class String
                        curr_id_other += 1
                end
 
-               if my_length != its_length then
-                       if my_length < its_length then return true
-                       return false
-               end
-
-               return false
+               return my_length < its_length
        end
 
        # The concatenation of `self' with `r'
@@ -736,7 +783,7 @@ end
 redef class Float
        # Pretty print self, print needed decimals up to a max of 6.
        redef fun to_s do
-               var str = to_precision( 6 )
+               var str = to_precision( 3 )
                var len = str.length
                for i in [0..len-1] do
                        var j = len-1-i
@@ -753,7 +800,30 @@ redef class Float
        end
 
        # `self' representation with `nb' digits after the '.'.
-       fun to_precision(nb: Int): String import String::from_cstring `{
+       fun to_precision(nb: Int): String
+       do
+               if nb == 0 then return self.to_i.to_s
+               var f = self
+               for i in [0..nb[ do f = f * 10.0
+               if self > 0.0 then
+                       f = f + 0.5
+               else
+                       f = f - 0.5
+               end
+               var i = f.to_i
+               if i == 0 then return "0.0"
+               var s = i.to_s
+               var sl = s.length
+               if sl > nb then
+                       var p1 = s.substring(0, s.length-nb)
+                       var p2 = s.substring(s.length-nb, nb)
+                       return p1 + "." + p2
+               else
+                       return "0." + ("0"*(nb-sl)) + s
+               end
+       end
+
+       fun to_precision_native(nb: Int): String import String::from_cstring `{
                int size;
                char *str;
 
@@ -930,8 +1000,10 @@ redef class Sys
                _args_cache = args
        end
 
-       private fun native_argc: Int is extern "kernel_Sys_Sys_native_argc_0" # First argument of the main C function.
-       
-       private fun native_argv(i: Int): NativeString is extern "kernel_Sys_Sys_native_argv_1" # Second argument of the main C function.
+       # First argument of the main C function.
+       private fun native_argc: Int is intern
+
+       # Second argument of the main C function.
+       private fun native_argv(i: Int): NativeString is intern
 end