core :: UTF8Codec :: defaultinit
core $ UTF8Codec :: add_char_to
Adds a charc
to bytes s
core $ UTF8Codec :: add_string_to
Adds a strings
coded as the supported encoding to b
core $ UTF8Codec :: char_max_size
Maximum size of acharacter
in supported encoding
core $ UTF8Codec :: decode_char
Decodes a char fromb
to a Unicode code-point
core $ UTF8Codec :: decode_string
Decodes a stringb
to UTF-8
core $ UTF8Codec :: encode_char
Transformsc
to its representation in the format of self
core $ UTF8Codec :: encode_string
Transformss
to the format of self
core $ UTF8Codec :: is_valid_char
Is the sequence of bytes inns
at position
a valid Char ?
core $ UTF8Codec :: max_lookahead
How many lookaheads might be required to decode a single char ?core :: Codec :: add_char_to
Adds a charc
to bytes s
core :: Codec :: add_string_to
Adds a strings
coded as the supported encoding to b
core :: Codec :: char_max_size
Maximum size of acharacter
in supported encoding
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Codec :: decode_char
Decodes a char fromb
to a Unicode code-point
core :: Codec :: decode_string
Decodes a stringb
to UTF-8
core :: Codec :: defaultinit
core :: UTF8Codec :: defaultinit
core :: Object :: defaultinit
core :: Codec :: encode_char
Transformsc
to its representation in the format of self
core :: Codec :: encode_string
Transformss
to the format of self
core :: 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.
core :: Codec :: is_valid_char
Is the sequence of bytes inns
at position
a valid Char ?
core :: Codec :: max_lookahead
How many lookaheads might be required to decode a single char ?core :: Object :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).
# Codec supporting UTF-8
private class UTF8Codec
super Codec
redef fun char_max_size do return 4
redef fun codet_size do return 1
redef fun max_lookahead do return 4
redef fun encode_char(c) do
var ns = new CString(c.u8char_len)
add_char_to(c, ns)
return ns
end
redef fun add_char_to(c, stream) do
c.u8char_tos(stream, c.u8char_len)
return c.u8char_len
end
redef fun encode_string(s) do
var buf = new Bytes.with_capacity(s.byte_length)
add_string_to(s, buf)
return buf
end
redef fun add_string_to(s, b) do
s.append_to_bytes(b)
return s.byte_length
end
redef fun is_valid_char(ns, len) do
if len == 0 then return 2
if not ns[0].is_valid_utf8_start then return 2
for i in [1 .. len[ do if ns[i] & 0b1100_0000 != 0b1000_0000 then return 2
if len != ns[0].u8len then return 1
return 0
end
redef fun decode_char(b) do
var c = b.char_at(0)
var cp = c.code_point
if cp >= 0xD800 and cp <= 0xDFFF then return 0xFFFD.code_point
if cp == 0xFFFE or cp == 0xFFFF then return 0xFFFD.code_point
return c
end
redef fun decode_string(ns, len) do
assert len >= 0
var ret = ns.to_s_unsafe(len, copy=false)
var rit = ret.as(FlatString).items
if rit == ns then
var nns = new CString(len)
rit.copy_to(nns, len, 0, 0)
return nns.to_s_unsafe(ret.byte_length, ret.length, copy=false)
end
return ret
end
end
lib/core/codecs/utf8.nit:22,1--81,3