final class · System.Text
UTF8Encoding
public final class UTF8Encoding
- Tipo
- final class
- Espacio de nombres
- System.Text
- Ensamblado
- System.Core
- Acceso
- public, final
- Archivo fuente
Encoding.ant
Herencia
- Clases base
-
AnyUTF8Encoding
Métodos
How many bytes text takes in UTF-8 (a str is UTF-8 already: its length).
The bytes of text (a copy: strings do not change).
The text of count bytes from start (sequences that are not UTF-8 become U+FFFD).
Miembros privados e internos (1)
Miembros heredados (5)
GetTypemétodo de AnyUndescribedmétodo de AnyToStringmétodo de AnyHashmétodo de Anydestroymétodo de Any
Código fuente
UTF8Encoding en Antril, descompilado a partir de su Larva HIR.
// System.Text.UTF8Encoding — System.Core (decompiled from Larva HIR)
import System;
into namespace System.Text
{
public final class UTF8Encoding
{
/// Whether `count` bytes from `start` are well-formed UTF-8.
static IsValid(u8[] bytes, i32 start, i32 count) : bool
{
i32 i = start;
i32 end = start + count;
loop (i < end)
{
u32 b = bytes[i];
if (b < 128)
{
i++;
continue;
}
i32 extra = b >= 245 ? 0 : b >= 240 ? 3 : b >= 224 ? 2 : b >= 194 ? 1 : 0;
if (extra == 0 || i + extra >= end)
return false;
u32 point = b & (extra == 1 ? 31 : extra == 2 ? 15 : 7);
loop (i32 k = 1..=extra)
{
u32 next = bytes[i + k];
if ((next & 192) != 128)
return false;
point = point << 6 | next & 63;
}
if (extra == 2 && (point < 2048 || point >= 55296 && point <= 57343) || extra == 3 && (point < 65536 || point > 1114111))
return false;
i += extra + 1;
}
return true;
}
public:
/// How many bytes `text` takes in UTF-8 (a str is UTF-8 already: its length).
GetByteCount(str text) : i32 => text.Length;
/// The bytes of `text` (a copy: strings do not change).
GetBytes(str text) : u8[]
{
u8[] units = text.Units();
u8[] bytes = new u8[units.Length];
loop (i32 i = 0..units.Length)
bytes[i] = units[i];
return bytes;
}
GetString(u8[] bytes) : str => GetString(bytes, 0, bytes.Length);
/// The text of `count` bytes from `start` (sequences that are not UTF-8 become U+FFFD).
GetString(u8[] bytes, i32 start, i32 count) : str
{
u8[] copy = new u8[count];
loop (i32 i = 0..count)
copy[i] = bytes[start + i];
if (IsValid(copy, 0, count))
return new str(copy);
return new str(Unicode.Encode<u8>(Unicode.Decode(copy)));
}
}
}