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
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
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
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'
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
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;
_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