lib/core: remove uses of Byte for Text
[nit.git] / lib / core / kernel.nit
index b2ac524..1160e72 100644 (file)
@@ -644,12 +644,12 @@ universal Byte
        # `i` bits shift fo the left
        #
        #     assert 5u8 << 1    == 10u8
-       fun <<(i: Int): Byte `{ return self << i; `}
+       fun <<(i: Int): Byte is intern `{ return self << i; `}
 
        # `i` bits shift fo the right
        #
        #     assert 5u8 >> 1    == 2u8
-       fun >>(i: Int): Byte `{ return self >> i; `}
+       fun >>(i: Int): Byte is intern `{ return self >> i; `}
 
        redef fun to_i is intern
        redef fun to_f is intern
@@ -694,6 +694,9 @@ universal Byte
                        return self
                end
        end
+
+       # Is `self` an ASCII whitespace ?
+       fun is_whitespace: Bool do return self == 0x7Fu8 or self <= 0x20u8
 end
 
 # Native integer numbers.
@@ -738,12 +741,12 @@ universal Int
        # `i` bits shift fo the left
        #
        #     assert 5 << 1    == 10
-       fun <<(i: Int): Int `{ return self << i; `}
+       fun <<(i: Int): Int is intern `{ return self << i; `}
 
        # `i` bits shift fo the right
        #
        #     assert 5 >> 1    == 2
-       fun >>(i: Int): Int `{ return self >> i; `}
+       fun >>(i: Int): Int is intern `{ return self >> i; `}
 
        redef fun to_i do return self
        redef fun to_f is intern
@@ -797,11 +800,17 @@ universal Int
                end
        end
 
-       # The character whose ASCII value is `self`.
+       # The character which code point (unicode-wise) is `self`
+       #
+       #     assert 65.code_point == 'A'
+       #     assert 10.code_point == '\n'
+       #     assert 0x220B.code_point == '∋'
+       fun code_point: Char is intern `{ return (uint32_t)self; `}
+
+       # Returns the character equivalent of `self`
        #
-       #     assert 65.ascii   == 'A'
-       #     assert 10.ascii   == '\n'
-       fun ascii: Char is intern
+       # REQUIRE: `self <= 127`
+       fun ascii: Char do return code_point
 
        # Number of digits of an integer in base `b` (plus one if negative)
        #
@@ -861,9 +870,9 @@ universal Int
        do
                assert self >= 0 and self <= 36 # TODO plan for this
                if self < 10 then
-                       return (self + '0'.ascii).ascii
+                       return (self + '0'.code_point).code_point
                else
-                       return (self + ('a'.ascii - 10)).ascii
+                       return (self - 10 + 'a'.code_point).code_point
                end
        end
 
@@ -872,15 +881,10 @@ universal Int
        #     assert (-10).abs   == 10
        #     assert 10.abs    == 10
        #     assert 0.abs     == 0
-       fun abs: Int
-       do
-           if self >= 0
-           then
-               return self
-           else
-               return -1 * self
-           end
-       end
+       fun abs: Int do return if self >= 0 then self else -self
+
+       # Is `self` an ASCII whitespace ?
+       fun is_whitespace: Bool do return self == 0x7F or self <= 0x20
 end
 
 # Native characters.
@@ -905,7 +909,7 @@ universal Char
                        printf("%c", self);
                }
        `}
-       redef fun hash do return ascii
+       redef fun hash do return code_point
        redef fun ==(o) is intern
        redef fun !=(o) is intern
 
@@ -917,9 +921,27 @@ universal Char
        redef fun successor(i) is intern
        redef fun predecessor(i) is intern
 
+       # The `i`-th char after self (in code point)
+       #
+       # ~~~
+       # assert 'A' + 5 == 'F'
+       # ~~~
+       #
+       # Alias of `successor`.
+       fun +(i: Int): Char do return successor(i)
+
+       # The `i`-th char before self (in code point)
+       #
+       # ~~~
+       # assert 'F' - 5 == 'A'
+       # ~~~
+       #
+       # Alias of `predecessor`.
+       fun -(i: Int): Char do return predecessor(i)
+
        redef fun distance(c)
        do
-               var d = self.ascii - c.ascii
+               var d = self.code_point - c.code_point
                if d >= 0 then
                        return d
                else
@@ -936,17 +958,32 @@ universal Char
                if self == '-' then
                        return -1
                else if is_digit then
-                       return self.ascii - '0'.ascii
+                       return self.code_point - '0'.code_point
                else
-                       return self.to_lower.ascii - 'a'.ascii + 10
+                       return self.to_lower.code_point - 'a'.code_point + 10
                end
        end
 
-       # the ascii value of self
+       # The ascii value of `self`
        #
        #     assert 'a'.ascii    == 97
        #     assert '\n'.ascii   == 10
-       fun ascii: Int is intern
+       #
+       # REQUIRE: `is_ascii`
+       fun ascii: Int do return code_point
+
+       # The unicode code point value of `self`
+       #
+       #     assert 'A'.code_point == 65
+       #     assert '\n'.code_point == 10
+       #     assert '∋'.code_point == 0x220B
+       fun code_point: Int is intern `{ return (long)self; `}
+
+       # Is `self` an ASCII character ?
+       #
+       #     assert 'x'.is_ascii
+       #     assert not 'ま'.is_ascii
+       fun is_ascii: Bool do return code_point <= 127
 
        # Return the lower case version of self.
        # If self is not a letter, then return self
@@ -957,7 +994,7 @@ universal Char
        fun to_lower: Char
        do
                if is_upper then
-                       return (ascii + ('a'.distance('A'))).ascii
+                       return (code_point + ('a'.distance('A'))).code_point
                else
                        return self
                end
@@ -972,7 +1009,7 @@ universal Char
        fun to_upper: Char
        do
                if is_lower then
-                       return (ascii - ('a'.distance('A'))).ascii
+                       return (code_point - ('a'.distance('A'))).code_point
                else
                        return self
                end
@@ -1029,20 +1066,51 @@ universal Char
        #
        #     assert 'A'.is_whitespace  == false
        #     assert ','.is_whitespace  == false
-       #     assert ' '.is_whitespace  == true
+       #     assert ' '.is_whitespace  == true # space
+       #     assert ' '.is_whitespace  == true # non-breaking space
        #     assert '\t'.is_whitespace == true
        fun is_whitespace: Bool
        do
-               var i = ascii
-               return i <= 0x20 or i == 0x7F
+               var i = code_point
+               return i <= 0x20 or i == 0x7F or i == 0xA0
        end
 end
 
 # Pointer classes are used to manipulate extern C structures.
 extern class Pointer
+       # C `NULL` pointer
+       new nul `{ return NULL; `}
+
        # Is the address behind this Object at NULL?
        fun address_is_null: Bool `{ return self == NULL; `}
 
        # Free the memory pointed by this pointer
        fun free `{ free(self); `}
+
+       # Use the address value
+       redef fun hash `{ return (long)(intptr_t)self; `}
+
+       # Is equal to any instance pointing to the same address
+       redef fun ==(o) do return o isa Pointer and native_equals(o)
+       private fun native_equals(o: Pointer): Bool `{ return self == o; `}
 end
+
+# Task with a `main` method to be implemented by subclasses
+#
+# This class is provided for compatibility between different parallelization systems.
+# It can be used to run a fragment of code on a different thread and
+# to register a reaction to UI events.
+interface Task
+
+       # Main method of this task
+       fun main do end
+end
+
+# Is this program currently running in a Windows OS?
+fun is_windows: Bool `{
+#ifdef _WIN32
+       return 1;
+#else
+       return 0;
+#endif
+`}