X-Git-Url: http://nitlanguage.org diff --git a/lib/core/fixed_ints.nit b/lib/core/fixed_ints.nit index daad211..3fb7468 100644 --- a/lib/core/fixed_ints.nit +++ b/lib/core/fixed_ints.nit @@ -158,6 +158,9 @@ universal Int8 redef fun to_i32 is intern redef fun to_u32 is intern + # Returns `self` as a Char according to its ASCII value. + fun ascii: Char `{ return (uint32_t)self; `} + redef fun distance(i) do return (self - i).to_i redef fun <=>(other) @@ -218,13 +221,13 @@ universal Int8 # assert ~0x2Fi8 == 0xD0i8 fun ~: Int8 is intern - # C function to calculate the length of the `NativeString` to receive `self` + # C function to calculate the length of the `CString` to receive `self` private fun to_s_len: Int `{ return snprintf(NULL, 0, "%"PRIi8, self); `} - # C function to convert a nit Int to a NativeString (char*) - private fun native_to_s(nstr: NativeString, strlen: Int) `{ + # C function to convert a nit Int to a CString (char*) + private fun native_to_s(nstr: CString, strlen: Int) `{ snprintf(nstr, strlen, "%"PRIi8, self); `} @@ -234,7 +237,7 @@ universal Int8 # assert (-123i8).to_s == "-123" redef fun to_s do var nslen = to_s_len - var ns = new NativeString(nslen + 1) + var ns = new CString(nslen + 1) ns[nslen] = 0u8 native_to_s(ns, nslen + 1) return ns.to_s_with_length(nslen) @@ -271,6 +274,9 @@ universal Int16 redef fun *(i) is intern redef fun /(i) is intern + # Returns `self` as a Char according to its ASCII value. + fun ascii: Char `{ return (uint32_t)self; `} + # Modulo of `self` with `i`. # # Returns the remainder of division of `self` by `i`. @@ -361,13 +367,13 @@ universal Int16 # assert ~0x2Fi16 == 0xFFD0i16 fun ~: Int16 is intern - # C function to calculate the length of the `NativeString` to receive `self` + # C function to calculate the length of the `CString` to receive `self` private fun to_s_len: Int `{ return snprintf(NULL, 0, "%"PRIi16, self); `} - # C function to convert a nit Int to a NativeString (char*) - private fun native_to_s(nstr: NativeString, strlen: Int) `{ + # C function to convert a nit Int to a CString (char*) + private fun native_to_s(nstr: CString, strlen: Int) `{ snprintf(nstr, strlen, "%"PRIi16, self); `} @@ -377,7 +383,7 @@ universal Int16 # assert (-123i16).to_s == "-123" redef fun to_s do var nslen = to_s_len - var ns = new NativeString(nslen + 1) + var ns = new CString(nslen + 1) ns[nslen] = 0u8 native_to_s(ns, nslen + 1) return ns.to_s_with_length(nslen) @@ -425,6 +431,9 @@ universal UInt16 redef fun zero do return 0.to_u16 redef fun value_of(val) do return val.to_u16 + # Returns `self` as a Char according to its ASCII value. + fun ascii: Char `{ return (uint32_t)self; `} + # `i` bits shift to the left # # assert 5u16 << 1 == 10u16 @@ -504,13 +513,13 @@ universal UInt16 # assert ~0x2Fu16 == 0xFFD0u16 fun ~: UInt16 is intern - # C function to calculate the length of the `NativeString` to receive `self` + # C function to calculate the length of the `CString` to receive `self` private fun to_s_len: Int `{ return snprintf(NULL, 0, "%"PRIu16, self); `} - # C function to convert a nit Int to a NativeString (char*) - private fun native_to_s(nstr: NativeString, strlen: Int) `{ + # C function to convert a nit Int to a CString (char*) + private fun native_to_s(nstr: CString, strlen: Int) `{ snprintf(nstr, strlen, "%"PRIu16, self); `} @@ -520,7 +529,7 @@ universal UInt16 # assert (-123u16).to_s == "65413" redef fun to_s do var nslen = to_s_len - var ns = new NativeString(nslen + 1) + var ns = new CString(nslen + 1) ns[nslen] = 0u8 native_to_s(ns, nslen + 1) return ns.to_s_with_length(nslen) @@ -558,6 +567,9 @@ universal Int32 redef fun *(i) is intern redef fun /(i) is intern + # Returns `self` as a Char according to its ASCII value. + fun ascii: Char `{ return (uint32_t)self; `} + # Modulo of `self` with `i`. # # Returns the remainder of division of `self` by `i`. @@ -648,13 +660,13 @@ universal Int32 # assert ~0x2Fi32 == 0xFFFFFFD0i32 fun ~: Int32 is intern - # C function to calculate the length of the `NativeString` to receive `self` + # C function to calculate the length of the `CString` to receive `self` private fun to_s_len: Int `{ return snprintf(NULL, 0, "%"PRIi32, self); `} - # C function to convert a nit Int to a NativeString (char*) - private fun native_to_s(nstr: NativeString, strlen: Int) `{ + # C function to convert a nit Int to a CString (char*) + private fun native_to_s(nstr: CString, strlen: Int) `{ snprintf(nstr, strlen, "%"PRIi32, self); `} @@ -664,7 +676,7 @@ universal Int32 # assert (-123i32).to_s == "-123" redef fun to_s do var nslen = to_s_len - var ns = new NativeString(nslen + 1) + var ns = new CString(nslen + 1) ns[nslen] = 0u8 native_to_s(ns, nslen + 1) return ns.to_s_with_length(nslen) @@ -701,6 +713,9 @@ universal UInt32 redef fun *(i) is intern redef fun /(i) is intern + # Returns `self` as a Char according to its ASCII value. + fun ascii: Char `{ return (uint32_t)self; `} + # Modulo of `self` with `i`. # # Returns the remainder of division of `self` by `i`. @@ -791,13 +806,13 @@ universal UInt32 # assert ~0x2Fu32 == 0xFFFFFFD0u32 fun ~: UInt32 is intern - # C function to calculate the length of the `NativeString` to receive `self` + # C function to calculate the length of the `CString` to receive `self` private fun to_s_len: Int `{ return snprintf(NULL, 0, "%"PRIu32, self); `} - # C function to convert a nit Int to a NativeString (char*) - private fun native_to_s(nstr: NativeString, strlen: Int) `{ + # C function to convert a nit Int to a CString (char*) + private fun native_to_s(nstr: CString, strlen: Int) `{ snprintf(nstr, strlen, "%"PRIu32, self); `} @@ -807,7 +822,7 @@ universal UInt32 # assert (-123u32).to_s == "4294967173" redef fun to_s do var nslen = to_s_len - var ns = new NativeString(nslen + 1) + var ns = new CString(nslen + 1) ns[nslen] = 0u8 native_to_s(ns, nslen + 1) return ns.to_s_with_length(nslen) @@ -818,7 +833,7 @@ redef class Text # Removes the numeric head of `self` if present # - # intrude import standard::fixed_ints + # intrude import core::fixed_ints # assert "0xFFEF".strip_numhead == "FFEF" # assert "0o7364".strip_numhead == "7364" # assert "0b01001".strip_numhead == "01001" @@ -831,7 +846,7 @@ redef class Text # Gets the numeric head of `self` if present # Returns "" otherwise # - # intrude import standard::fixed_ints + # intrude import core::fixed_ints # assert "0xFEFF".get_numhead == "0x" # assert "0b01001".get_numhead == "0b" # assert "0o872".get_numhead == "0o" @@ -848,7 +863,7 @@ redef class Text # Removes the numeric extension if present # - # intrude import standard::fixed_ints + # intrude import core::fixed_ints # assert "0xFEFFu8".strip_numext == "0xFEFF" # assert "0b01001u8".strip_numext == "0b01001" # assert "0o872u8".strip_numext == "0o872" @@ -862,7 +877,7 @@ redef class Text # Gets the numeric extension (i/u 8/16/32) in `self` is present # Returns "" otherwise # - # intrude import standard::fixed_ints + # intrude import core::fixed_ints # assert "0xFEFFu8".get_numext == "u8" # assert "0b01001u8".get_numext == "u8" # assert "0o872u8".get_numext == "u8" @@ -884,11 +899,14 @@ redef class Text # assert not "0x_".is_int # assert not "0xGE".is_int # assert not "".is_int + # assert not "Not an Int".is_int + # assert not "-".is_int fun is_int: Bool do - if bytelen == 0 then return false + if byte_length == 0 then return false var s = remove_all('_') var pos = 0 - while s[pos] == '-' do + var len = s.length + while pos < len and s[pos] == '-' do pos += 1 end s = s.substring_from(pos) @@ -898,7 +916,7 @@ redef class Text if hd == "0x" or hd == "0X" then return rets.is_hex if hd == "0b" or hd == "0B" then return rets.is_bin if hd == "0o" or hd == "0O" then return rets.is_oct - return hd.is_dec + return rets.is_dec end redef fun to_i