Characters are denoted with simple quote.
eg. 'a'
or '\n'
.
core :: Char :: is_alphanumeric
Returns true if the char is an alpha or a numeric digitjson :: serialization_write $ Char :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONmsgpack :: serialization_write $ Char :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackandroid :: bundle $ Char :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: string_search $ Char :: search_index_in
Searchself
into s
from a certain position.
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Pattern :: defaultinit
core :: Comparable :: defaultinit
core :: Discrete :: defaultinit
core :: Object :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: Char :: is_alphanumeric
Returns true if the char is an alpha or a numeric digitcore :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).core :: Pattern :: search_all_in
Search allself
occurrences into s
.
core :: Pattern :: search_index_in
Searchself
into s
from a certain position.
serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
serialization :: DirectSerializable
Instances of this class are not delayed and instead serialized immediately
# Native characters.
# Characters are denoted with simple quote.
# eg. `'a'` or `'\n'`.
universal Char
super Discrete
redef type OTHER: Char
redef fun object_id is intern
redef fun output `{
if(self < 128){
printf("%c", self);
}else if(self < 2048){
printf("%c%c", 0xC0 | ((0x7C0 & self) >> 6), 0x80 | (0x3F & self));
}else if(self < 65536){
printf("%c%c%c", 0xE0 | ((0xF000 & self) >> 12), 0x80 | ((0xFC0 & self) >> 6) ,0x80 | (0x3F & self));
}else if(self < 2097152){
printf("%c%c%c%c", 0xF0 | ((0x1C0000 & self) >> 18), 0x80 | ((0x3F000 & self) >> 12), 0x80 | ((0xFC0 & self) >> 6), 0x80 | (0x3F & self));
}else{
// Bad char
printf("%c", self);
}
`}
redef fun hash do return code_point
redef fun ==(o) is intern
redef fun !=(o) is intern
redef fun <=(i) is intern
redef fun <(i) is intern
redef fun >=(i) is intern
redef fun >(i) is intern
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.code_point - c.code_point
if d >= 0 then
return d
else
return -d
end
end
# If `self` is a digit then return this digit else return -1.
#
# assert '5'.to_i == 5
fun to_i: Int
do
if self == '-' then
return -1
else if is_digit then
return self.code_point - '0'.code_point
else
return self.to_lower.code_point - 'a'.code_point + 10
end
end
# 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
#
# assert 'A'.to_lower == 'a'
# assert 'a'.to_lower == 'a'
# assert '$'.to_lower == '$'
fun to_lower: Char
do
if is_upper then
return (code_point + ('a'.distance('A'))).code_point
else
return self
end
end
# Return the upper case version of self.
# If self is not a letter, then return self
#
# assert 'a'.to_upper == 'A'
# assert 'A'.to_upper == 'A'
# assert '$'.to_upper == '$'
fun to_upper: Char
do
if is_lower then
return (code_point - ('a'.distance('A'))).code_point
else
return self
end
end
# Is self a digit? (from '0' to '9')
#
# assert '0'.is_digit == true
# assert '9'.is_digit == true
# assert 'a'.is_digit == false
fun is_digit : Bool
do
return self >= '0' and self <= '9'
end
# Is self a lower case letter? (from 'a' to 'z')
#
# assert 'a'.is_lower == true
# assert 'z'.is_lower == true
# assert 'A'.is_lower == false
# assert '$'.is_lower == false
fun is_lower : Bool
do
return self >= 'a' and self <= 'z'
end
# Is self a upper case letter? (from 'A' to 'Z')
#
# assert 'A'.is_upper == true
# assert 'A'.is_upper == true
# assert 'z'.is_upper == false
# assert '$'.is_upper == false
fun is_upper : Bool
do
return self >= 'A' and self <= 'Z'
end
# Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
#
# assert 'A'.is_letter == true
# assert 'A'.is_letter == true
# assert 'z'.is_letter == true
# assert '$'.is_letter == false
fun is_letter : Bool
do
return is_lower or is_upper
end
# Is self a whitespace character?
#
# These correspond to the "Other" and "Separator" groups of the Unicode.
#
# In the ASCII encoding, this is those <= to space (0x20) plus delete (0x7F).
#
# assert 'A'.is_whitespace == false
# assert ','.is_whitespace == false
# assert ' '.is_whitespace == true # space
# assert ' '.is_whitespace == true # non-breaking space
# assert '\t'.is_whitespace == true
fun is_whitespace: Bool
do
var i = code_point
return i <= 0x20 or i == 0x7F or i == 0xA0
end
end
lib/core/kernel.nit:888,1--1067,3
redef class Char
# Returns a sequence with the UTF-8 bytes of `self`
#
# ~~~
# assert 'a'.bytes == [0x61]
# assert 'ま'.bytes == [0xE3, 0x81, 0xBE]
# ~~~
fun bytes: SequenceRead[Int] do return to_s.bytes
# Is `self` an UTF-16 surrogate pair ?
fun is_surrogate: Bool do
var cp = code_point
return cp >= 0xD800 and cp <= 0xDFFF
end
# Is `self` a UTF-16 high surrogate ?
fun is_hi_surrogate: Bool do
var cp = code_point
return cp >= 0xD800 and cp <= 0xDBFF
end
# Is `self` a UTF-16 low surrogate ?
fun is_lo_surrogate: Bool do
var cp = code_point
return cp >= 0xDC00 and cp <= 0xDFFF
end
# Length of `self` in a UTF-8 String
fun u8char_len: Int do
var c = self.code_point
if c < 0x80 then return 1
if c <= 0x7FF then return 2
if c <= 0xFFFF then return 3
if c <= 0x10FFFF then return 4
# Bad character format
return 1
end
# ~~~
# assert 'x'.to_s == "x"
# ~~~
redef fun to_s do
var ln = u8char_len
var ns = new CString(ln + 1)
u8char_tos(ns, ln)
return ns.to_s_unsafe(ln, copy=false, clean=false)
end
# Returns `self` escaped to UTF-16
#
# i.e. Represents `self`.`code_point` using UTF-16 codets escaped
# with a `\u`
#
# ~~~
# assert 'A'.escape_to_utf16 == "\\u0041"
# assert 'è'.escape_to_utf16 == "\\u00e8"
# assert 'あ'.escape_to_utf16 == "\\u3042"
# assert '𐏓'.escape_to_utf16 == "\\ud800\\udfd3"
# ~~~
fun escape_to_utf16: String do
var cp = code_point
var buf: Buffer
if cp < 0xD800 or (cp >= 0xE000 and cp <= 0xFFFF) then
buf = new Buffer.with_cap(6)
buf.append("\\u0000")
var hx = cp.to_hex
var outid = 5
for i in hx.chars.reverse_iterator do
buf[outid] = i
outid -= 1
end
else
buf = new Buffer.with_cap(12)
buf.append("\\u0000\\u0000")
var lo = (((cp - 0x10000) & 0x3FF) + 0xDC00).to_hex
var hi = ((((cp - 0x10000) & 0xFFC00) >> 10) + 0xD800).to_hex
var out = 2
for i in hi do
buf[out] = i
out += 1
end
out = 8
for i in lo do
buf[out] = i
out += 1
end
end
return buf.to_s
end
private fun u8char_tos(r: CString, len: Int) `{
r[len] = '\0';
switch(len){
case 1:
r[0] = self;
break;
case 2:
r[0] = 0xC0 | ((self & 0x7C0) >> 6);
r[1] = 0x80 | (self & 0x3F);
break;
case 3:
r[0] = 0xE0 | ((self & 0xF000) >> 12);
r[1] = 0x80 | ((self & 0xFC0) >> 6);
r[2] = 0x80 | (self & 0x3F);
break;
case 4:
r[0] = 0xF0 | ((self & 0x1C0000) >> 18);
r[1] = 0x80 | ((self & 0x3F000) >> 12);
r[2] = 0x80 | ((self & 0xFC0) >> 6);
r[3] = 0x80 | (self & 0x3F);
break;
}
`}
# Returns true if the char is a numerical digit
#
# ~~~
# assert '0'.is_numeric
# assert '9'.is_numeric
# assert not 'a'.is_numeric
# assert not '?'.is_numeric
# ~~~
#
# FIXME: Works on ASCII-range only
fun is_numeric: Bool
do
return self >= '0' and self <= '9'
end
# Returns true if the char is an alpha digit
#
# ~~~
# assert 'a'.is_alpha
# assert 'Z'.is_alpha
# assert not '0'.is_alpha
# assert not '?'.is_alpha
# ~~~
#
# FIXME: Works on ASCII-range only
fun is_alpha: Bool
do
return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
end
# Is `self` an hexadecimal digit ?
#
# ~~~
# assert 'A'.is_hexdigit
# assert not 'G'.is_hexdigit
# assert 'a'.is_hexdigit
# assert not 'g'.is_hexdigit
# assert '5'.is_hexdigit
# ~~~
fun is_hexdigit: Bool do return (self >= '0' and self <= '9') or (self >= 'A' and self <= 'F') or
(self >= 'a' and self <= 'f')
# Returns true if the char is an alpha or a numeric digit
#
# ~~~
# assert 'a'.is_alphanumeric
# assert 'Z'.is_alphanumeric
# assert '0'.is_alphanumeric
# assert '9'.is_alphanumeric
# assert not '?'.is_alphanumeric
# ~~~
#
# FIXME: Works on ASCII-range only
fun is_alphanumeric: Bool
do
return self.is_numeric or self.is_alpha
end
# Returns `self` to its int value
#
# REQUIRE: `is_hexdigit`
fun from_hex: Int do
if self >= '0' and self <= '9' then return code_point - 0x30
if self >= 'A' and self <= 'F' then return code_point - 0x37
if self >= 'a' and self <= 'f' then return code_point - 0x57
# Happens if self is not a hexdigit
assert self.is_hexdigit
# To make flow analysis happy
abort
end
end
lib/core/text/abstract_text.nit:2152,1--2337,3
redef class Char
super Pattern
redef fun search_index_in(s, from)
do
var stop = s.length
while from < stop do
if s[from] == self then return from
from += 1
end
return -1
end
redef fun search_in(s, from)
do
var pos = search_index_in(s, from)
if pos < 0 then
return null
else
return new Match(s.to_s, pos, 1)
end
end
end
lib/core/text/string_search.nit:304,1--326,3
redef class Char
# Is `self` a valid Base64 character ?
fun is_base64_char: Bool do
if code_point >= 127 then return false
return code_point.is_base64_char
end
end
lib/base64/base64.nit:20,1--26,3
redef class Char
# Rotates self of `x`
#
# NOTE: works on letters only
#
# assert 'x'.rot(6) == 'd'
# assert 'T'.rot(15) == 'I'
# assert '1'.rot(10) == '1'
# assert '$'.rot(10) == '$'
# assert 'z'.rot(-2) == 'x'
fun rot(x: Int): Char do
if not is_letter then return self
x = x % 26
if x < 0 then x += 26
var up = false
var val = code_point
if is_upper then
up = true
val += 32
end
val += x
if val > 122 then val -= 26
if up then val -= 32
return val.code_point
end
end
lib/crypto/basic_ciphers.nit:18,1--43,3
redef class Char super DirectSerializable end
lib/serialization/serialization_core.nit:259,1--45
redef class Char
redef fun accept_json_serializer(v)
do
if v.plain_json then
to_s.accept_json_serializer v
else
v.stream.write "\{\"__kind\": \"char\", \"__val\": "
to_s.accept_json_serializer v
v.stream.write "\}"
end
end
end
lib/json/serialization_write.nit:276,1--287,3
redef class Char
# Is `self` a valid number start ?
private fun is_json_num_start: Bool do
if self == '-' then return true
if self.is_numeric then return true
return false
end
# Is `self` a valid JSON separator ?
private fun is_json_separator: Bool do
if self == ':' then return true
if self == ',' then return true
if self == '{' then return true
if self == '}' then return true
if self == '[' then return true
if self == ']' then return true
if self == '"' then return true
if self.is_whitespace then return true
return false
end
end
lib/json/static.nit:137,1--157,3
redef class Char
redef fun accept_msgpack_serializer(v)
do
if v.plain_msgpack then
# Write as a string
v.stream.write_msgpack_fixstr to_s
else
# Write as ext
var bytes = to_s.to_bytes
v.stream.write_msgpack_ext(v.ext_typ_char, bytes)
end
end
end
lib/msgpack/serialization_write.nit:275,1--287,3