PackedByteArray¶
打包的字节数组。
描述¶
专门设计用于保存字节的数组。紧密打包数据,因此它为大数组大小节省内存。
PackedByteArray还提供了对各种类型的字节进行编码/解码的方法。值的编码方式是实现细节,在与外部应用程序交互时不应依赖。
注意:打包数组总是通过引用传递的。要获得可以独立于原始数组进行修改的数组的副本,请使用duplicate()。内置属性和方法的情况是而不是。这些返回的打包数组是副本,更改它将而不是影响原始值。要更新内置属性,您需要修改返回的数组,然后再次将其分配给属性。
构造函数¶
方法¶
void |
append_array(array: PackedByteArray) |
void |
clear() |
decode_double(byte_offset: int) const |
|
decode_float(byte_offset: int) const |
|
decode_half(byte_offset: int) const |
|
decode_s16(byte_offset: int) const |
|
decode_s32(byte_offset: int) const |
|
decode_s64(byte_offset: int) const |
|
decode_u16(byte_offset: int) const |
|
decode_u32(byte_offset: int) const |
|
decode_u64(byte_offset: int) const |
|
decode_var(byte_offset: int, allow_objects: bool = false) const |
|
decode_var_size(byte_offset: int, allow_objects: bool = false) const |
|
decompress(buffer_size: int, compression_mode: int = 0) const |
|
decompress_dynamic(max_output_size: int, compression_mode: int = 0) const |
|
void |
encode_double(byte_offset: int, value: float) |
void |
encode_float(byte_offset: int, value: float) |
void |
encode_half(byte_offset: int, value: float) |
void |
|
void |
encode_s16(byte_offset: int, value: int) |
void |
encode_s32(byte_offset: int, value: int) |
void |
encode_s64(byte_offset: int, value: int) |
void |
|
void |
encode_u16(byte_offset: int, value: int) |
void |
encode_u32(byte_offset: int, value: int) |
void |
encode_u64(byte_offset: int, value: int) |
encode_var(byte_offset: int, value: Variant, allow_objects: bool = false) |
|
void |
|
get_string_from_ascii() const |
|
get_string_from_utf8() const |
|
get_string_from_utf16() const |
|
get_string_from_utf32() const |
|
get_string_from_wchar() const |
|
has_encoded_var(byte_offset: int, allow_objects: bool = false) const |
|
hex_encode() const |
|
is_empty() const |
|
void |
|
void |
reverse() |
void |
|
size() const |
|
void |
sort() |
to_float32_array() const |
|
to_float64_array() const |
|
to_int32_array() const |
|
to_int64_array() const |
运算符¶
operator !=(right: PackedByteArray) |
|
operator +(right: PackedByteArray) |
|
operator ==(right: PackedByteArray) |
|
operator [](index: int) |
构造函数说明¶
PackedByteArray PackedByteArray() 🔗
构造一个空的PackedByteArray。
PackedByteArray PackedByteArray(from: PackedByteArray)
构造一个PackedByteArray作为给定PackedByteArray的副本。
PackedByteArray PackedByteArray(from: Array)
构造一个新的PackedByteArray。或者,您可以传入将被转换的通用Array。
方法说明¶
在数组的末尾附加一个元素(push_back()的别名)。
void append_array(array: PackedByteArray) 🔗
在此数组的末尾附加一个PackedByteArray。
int bsearch(value: int, before: bool = true) 🔗
使用二进制搜索查找现有值的索引(或保持排序顺序的插入索引,如果该值尚未出现在数组中)。可选地,可以传递before说明符。如果false,则返回的索引位于数组中该值的所有现有条目之后。
注意:在未排序的数组上调用bsearch()会导致意外行为。
void clear() 🔗
清除数组。这相当于使用大小为0的resize()。
PackedByteArray compress(compression_mode: int = 0) const 🔗
返回一个压缩数据的新PackedByteArray。使用CompressionMode的常量之一设置压缩模式。
返回元素在数组中的次数。
float decode_double(byte_offset: int) const 🔗
从byte_offset开始的字节解码64位浮点数。如果字节数不足,则失败。如果无法解码有效数字,则返回0.0。
float decode_float(byte_offset: int) const 🔗
从byte_offset开始的字节解码32位浮点数。如果字节数不足,则失败。如果无法解码有效数字,则返回0.0。
float decode_half(byte_offset: int) const 🔗
从byte_offset开始的字节解码16位浮点数。如果字节数不足,则失败。如果无法解码有效数字,则返回0.0。
int decode_s8(byte_offset: int) const 🔗
从byte_offset开始的字节解码一个8位有符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_s16(byte_offset: int) const 🔗
从byte_offset开始的字节解码16位有符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_s32(byte_offset: int) const 🔗
从byte_offset开始的字节解码一个32位有符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_s64(byte_offset: int) const 🔗
从byte_offset开始的字节解码64位有符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_u8(byte_offset: int) const 🔗
从byte_offset开始的字节解码一个8位无符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_u16(byte_offset: int) const 🔗
从byte_offset开始的字节解码16位无符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_u32(byte_offset: int) const 🔗
从byte_offset开始的字节解码一个32位无符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
int decode_u64(byte_offset: int) const 🔗
从byte_offset开始的字节解码64位无符号整数。如果字节数不足,则失败。如果无法解码有效数字,则返回0。
Variant decode_var(byte_offset: int, allow_objects: bool = false) const 🔗
从byte_offset开始的字节解码Variant。如果无法解码有效的变体或值是Object派生的并且allow_objects是false,则返回null。
int decode_var_size(byte_offset: int, allow_objects: bool = false) const 🔗
从byte_offset开始的字节解码Variant的大小。需要从偏移量开始的至少4个字节的数据,否则失败。
PackedByteArray decompress(buffer_size: int, compression_mode: int = 0) const 🔗
返回一个新的PackedByteArray,其中数据已解压缩。将buffer_size设置为未压缩数据的大小。使用CompressionMode的常量之一设置压缩模式。
注意:不能保证解压缩适用于未被i3D压缩的数据,例如,如果使用压缩模式压缩的数据缺少校验和或标头。
PackedByteArray decompress_dynamic(max_output_size: int, compression_mode: int = 0) const 🔗
返回一个新的PackedByteArray,其中数据已解压缩。使用CompressionMode的常量之一设置压缩模式。此方法仅接受brotli、gzip和压缩模式。
此方法可能比decompress()慢,因为它可能必须在解压缩时多次重新分配其输出缓冲区,而decompress()从一开始就知道它的输出缓冲区大小。
GZIP的最大压缩比为1032:1,这意味着小压缩负载很有可能解压缩为潜在的非常大的输出。为了防止这种情况,您可以通过max_output_size提供此函数允许以字节为单位分配的最大大小。传递-1将允许无界输出。如果传递任何正值,并且解压缩超过该字节数,则将返回错误。
注意:不能保证解压缩适用于未被i3D压缩的数据,例如,如果使用压缩模式压缩的数据缺少校验和或标头。
PackedByteArray duplicate() 🔗
创建数组的副本,并返回它。
void encode_double(byte_offset: int, value: float) 🔗
将64位浮点数编码为byte_offset字节索引处的字节。数组必须至少有8个字节的分配空间,从偏移量开始。
void encode_float(byte_offset: int, value: float) 🔗
将32位浮点数编码为byte_offset字节索引处的字节。数组必须至少有4个字节的空间,从偏移量开始。
void encode_half(byte_offset: int, value: float) 🔗
将16位浮点数编码为byte_offset字节索引处的字节。数组必须至少有2个字节的空间,从偏移量开始。
void encode_s8(byte_offset: int, value: int) 🔗
在byte_offset字节的索引处编码一个8位有符号整数(有符号字节)。数组必须至少有1个字节的空间,从偏移量开始。
void encode_s16(byte_offset: int, value: int) 🔗
将16位有符号整数编码为byte_offset字节索引处的字节。数组必须至少有2个字节的空间,从偏移量开始。
void encode_s32(byte_offset: int, value: int) 🔗
将32位有符号整数编码为byte_offset字节索引处的字节。数组必须至少有4个字节的空间,从偏移量开始。
void encode_s64(byte_offset: int, value: int) 🔗
将64位有符号整数编码为byte_offset字节索引处的字节。数组必须至少有8个字节的空间,从偏移量开始。
void encode_u8(byte_offset: int, value: int) 🔗
在byte_offset字节的索引处编码一个8位无符号整数(字节)。数组必须至少有1个字节的空间,从偏移量开始。
void encode_u16(byte_offset: int, value: int) 🔗
将16位无符号整数编码为byte_offset字节索引处的字节。数组必须至少有2个字节的空间,从偏移量开始。
void encode_u32(byte_offset: int, value: int) 🔗
将32位无符号整数编码为byte_offset字节索引处的字节。数组必须至少有4个字节的空间,从偏移量开始。
void encode_u64(byte_offset: int, value: int) 🔗
将64位无符号整数编码为byte_offset字节索引处的字节。数组必须至少有8个字节的空间,从偏移量开始。
int encode_var(byte_offset: int, value: Variant, allow_objects: bool = false) 🔗
在byte_offset字节的索引处编码Variant。必须分配足够的空间,具体取决于编码变体的大小。如果allow_objects为false,则不允许Object派生值,而是将其序列化为仅ID。
将给定值分配给数组中的所有元素。这通常可以与resize()一起使用,以创建具有给定大小和初始化元素的数组。
int find(value: int, from: int = 0) const 🔗
在数组中搜索一个值,如果未找到,则返回其索引或-1。可选地,可以传递初始搜索索引。
返回数组中给定index处的字节。这与使用[]运算符(array[index])相同。
String get_string_from_ascii() const 🔗
将ASCII/Latin-1编码数组转换为String。如果内容仅为ASCII/Latin-1,则可以快速替代get_string_from_utf8()。与UTF-8函数不同,此函数将每个字节映射到数组中的一个字符。多字节序列将无法正确解释。对于解析用户输入,请始终使用get_string_from_utf8()。这与String.to_ascii_buffer()相反。
String get_string_from_utf8() const 🔗
将UTF-8编码数组转换为String。比get_string_from_ascii()慢,但支持UTF-8编码数据。如果您不确定数据的来源,请使用此函数。对于用户输入,此函数应始终是首选项。如果源数组不是有效的UTF-8字符串,则返回空字符串。这与String.to_utf8_buffer()相反。
String get_string_from_utf16() const 🔗
将UTF-16编码数组转换为String。如果缺少BOM,则假定系统顺序。如果源数组不是有效的UTF-16字符串,则返回空字符串。这与String.to_utf16_buffer()相反。
String get_string_from_utf32() const 🔗
将UTF-32编码数组转换为String。假定系统顺序。如果源数组不是有效的UTF-32字符串,则返回空字符串。这与String.to_utf32_buffer()相反。
String get_string_from_wchar() const 🔗
将宽字符(wchar_t,Windows上的UTF-16,其他平台上的UTF-32)编码数组转换为String。如果源数组不是有效的宽字符串,则返回空字符串。这与String.to_wchar_buffer()相反。
如果数组包含value,则返回true。
bool has_encoded_var(byte_offset: int, allow_objects: bool = false) const 🔗
如果可以在byte_offset处解码有效的Variant值,则返回true。当值是Object-派生的并且allow_objects是false时返回false。
以String形式返回此数组的十六进制表示。
var array = PackedByteArray([11, 46, 255])
print(array.hex_encode()) # Prints "0b2eff"
byte[] array = [11, 46, 255];
S3.Print(array.HexEncode()); // Prints "0b2eff"
int insert(at_index: int, value: int) 🔗
在数组中的给定位置插入一个新元素。该位置必须有效,或者在数组的末尾(idx==size())。
如果数组为空,则返回true。
在数组末尾附加一个元素。
按索引从数组中删除元素。
设置数组的大小。如果数组增长,则在数组末尾保留元素。如果数组缩小,则将数组截断为新大小。调用resize()一次并分配新值比逐个添加新元素更快。
void reverse() 🔗
反转数组中元素的顺序。
int rfind(value: int, from: int = -1) const 🔗
以相反的顺序搜索数组。或者,可以传递开始搜索索引。如果为负,则认为开始索引相对于数组的末尾。
void set(index: int, value: int) 🔗
更改给定索引处的字节。
返回数组中元素的数量。
PackedByteArray slice(begin: int, end: int = 2147483647) const 🔗
返回PackedByteArray的切片,从begin(包括)到end(不包括),作为新的PackedByteArray。
begin和end的绝对值将被钳制到数组大小,因此end的默认值使其默认切片到数组的大小(即arr.片段(1)是arr.片段(1, arr.size())的简写)。
如果begin或end为负数,它们将相对于数组的末尾(即arr.片段(0,-2)是arr.片段(0, arr.size()-2)的简写)。
void sort() 🔗
按升序对数组的元素进行排序。
PackedFloat32Array to_float32_array() const 🔗
返回转换为PackedFloat32Array的数据的副本,其中每个4字节的块已转换为32位浮点数(C++flo)。
输入数组的大小必须是4的倍数(32位浮点数的大小)。新数组的大小将是byte_array. size()/4。
如果原始数据无法转换为32位浮点数,则生成的数据未定义。
PackedFloat64Array to_float64_array() const 🔗
返回转换为PackedFloat64Array的数据的副本,其中每个8字节的块已转换为64位浮点数(C++Double, i3Dfloat)。
输入数组的大小必须是8的倍数(大小为64位双精度)。新数组的大小将是byte_array. size()/8。
如果原始数据无法转换为64位浮点数,则生成的数据未定义。
PackedInt32Array to_int32_array() const 🔗
返回转换为PackedInt32Array的数据的副本,其中每个4个字节的块已转换为有符号的32位整数(C++int32_t)。
输入数组的大小必须是4的倍数(32位整数的大小)。新数组的大小将是byte_array. size()/4。
如果原始数据无法转换为有符号的32位整数,则生成的数据未定义。
PackedInt64Array to_int64_array() const 🔗
返回转换为PackedInt64Array的数据的副本,其中每个8字节的块已转换为有符号的64位整数(C++int64_t, i3Dint)。
输入数组的大小必须是8的倍数(64位整数的大小)。新数组的大小将是byte_array. size()/8。
如果原始数据无法转换为有符号64位整数,则生成的数据未定义。
运算符说明¶
bool operator !=(right: PackedByteArray) 🔗
如果数组的内容不同,则返回true。
PackedByteArray operator +(right: PackedByteArray) 🔗
返回一个新的PackedByteArray,在此数组末尾添加了right的内容。为了获得更好的性能,请考虑改用append_array()。
bool operator ==(right: PackedByteArray) 🔗
如果两个数组的内容相同,即它们在相应的索引处具有相等的字节,则返回true。
返回indexindex处的字节。负索引可用于访问从末尾开始的元素。在数组边界之外使用索引将导致错误。
请注意,字节作为64位int返回。