diff options
Diffstat (limited to 'core/variant_call.cpp')
-rw-r--r-- | core/variant_call.cpp | 119 |
1 files changed, 101 insertions, 18 deletions
diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 91af127d32..0ebb2f04a1 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -239,6 +239,7 @@ struct _VariantCall { VCALL_LOCALMEM1R(String, casecmp_to); VCALL_LOCALMEM1R(String, nocasecmp_to); + VCALL_LOCALMEM1R(String, naturalnocasecmp_to); VCALL_LOCALMEM0R(String, length); VCALL_LOCALMEM3R(String, count); VCALL_LOCALMEM3R(String, countn); @@ -311,6 +312,8 @@ struct _VariantCall { VCALL_LOCALMEM0R(String, to_int); VCALL_LOCALMEM0R(String, to_float); VCALL_LOCALMEM0R(String, hex_to_int); + VCALL_LOCALMEM2R(String, lpad); + VCALL_LOCALMEM2R(String, rpad); VCALL_LOCALMEM1R(String, pad_decimals); VCALL_LOCALMEM1R(String, pad_zeros); VCALL_LOCALMEM1R(String, trim_prefix); @@ -350,6 +353,39 @@ struct _VariantCall { r_ret = retval; } + static void _call_String_to_utf16(Variant &r_ret, Variant &p_self, const Variant **p_args) { + String *s = reinterpret_cast<String *>(p_self._data._mem); + if (s->empty()) { + r_ret = PackedByteArray(); + return; + } + Char16String charstr = s->utf16(); + + PackedByteArray retval; + size_t len = charstr.length() * 2; + retval.resize(len); + uint8_t *w = retval.ptrw(); + copymem(w, (const void *)charstr.ptr(), len); + + r_ret = retval; + } + + static void _call_String_to_utf32(Variant &r_ret, Variant &p_self, const Variant **p_args) { + String *s = reinterpret_cast<String *>(p_self._data._mem); + if (s->empty()) { + r_ret = PackedByteArray(); + return; + } + + PackedByteArray retval; + size_t len = s->length() * 4; + retval.resize(len); + uint8_t *w = retval.ptrw(); + copymem(w, (const void *)s->ptr(), len); + + r_ret = retval; + } + VCALL_LOCALMEM1R(Vector2, distance_to); VCALL_LOCALMEM1R(Vector2, distance_squared_to); VCALL_LOCALMEM0R(Vector2, length); @@ -594,14 +630,14 @@ struct _VariantCall { VCALL_LOCALMEM0R(Array, min); static void _call_PackedByteArray_get_string_from_ascii(Variant &r_ret, Variant &p_self, const Variant **p_args) { - PackedByteArray *ba = reinterpret_cast<PackedByteArray *>(p_self._data._mem); + Variant::PackedArrayRef<uint8_t> *ba = reinterpret_cast<Variant::PackedArrayRef<uint8_t> *>(p_self._data.packed_array); String s; - if (ba->size() > 0) { - const uint8_t *r = ba->ptr(); + if (ba->array.size() > 0) { + const uint8_t *r = ba->array.ptr(); CharString cs; - cs.resize(ba->size() + 1); - copymem(cs.ptrw(), r, ba->size()); - cs[ba->size()] = 0; + cs.resize(ba->array.size() + 1); + copymem(cs.ptrw(), r, ba->array.size()); + cs[ba->array.size()] = 0; s = cs.get_data(); } @@ -609,32 +645,54 @@ struct _VariantCall { } static void _call_PackedByteArray_get_string_from_utf8(Variant &r_ret, Variant &p_self, const Variant **p_args) { + Variant::PackedArrayRef<uint8_t> *ba = reinterpret_cast<Variant::PackedArrayRef<uint8_t> *>(p_self._data.packed_array); + + String s; + if (ba->array.size() > 0) { + const uint8_t *r = ba->array.ptr(); + s.parse_utf8((const char *)r, ba->array.size()); + } + r_ret = s; + } + + static void _call_PackedByteArray_get_string_from_utf16(Variant &r_ret, Variant &p_self, const Variant **p_args) { PackedByteArray *ba = reinterpret_cast<PackedByteArray *>(p_self._data._mem); String s; if (ba->size() > 0) { const uint8_t *r = ba->ptr(); - s.parse_utf8((const char *)r, ba->size()); + s.parse_utf16((const char16_t *)r, ba->size() / 2); } r_ret = s; } - static void _call_PackedByteArray_compress(Variant &r_ret, Variant &p_self, const Variant **p_args) { + static void _call_PackedByteArray_get_string_from_utf32(Variant &r_ret, Variant &p_self, const Variant **p_args) { PackedByteArray *ba = reinterpret_cast<PackedByteArray *>(p_self._data._mem); - PackedByteArray compressed; + String s; if (ba->size() > 0) { - Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]); + const uint8_t *r = ba->ptr(); + s = String((const char32_t *)r, ba->size() / 4); + } + r_ret = s; + } + + static void _call_PackedByteArray_compress(Variant &r_ret, Variant &p_self, const Variant **p_args) { + Variant::PackedArrayRef<uint8_t> *ba = reinterpret_cast<Variant::PackedArrayRef<uint8_t> *>(p_self._data.packed_array); + PackedByteArray compressed; - compressed.resize(Compression::get_max_compressed_buffer_size(ba->size(), mode)); - int result = Compression::compress(compressed.ptrw(), ba->ptr(), ba->size(), mode); + if (ba->array.size() > 0) { + Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]); + compressed.resize(Compression::get_max_compressed_buffer_size(ba->array.size(), mode)); + int result = Compression::compress(compressed.ptrw(), ba->array.ptr(), ba->array.size(), mode); result = result >= 0 ? result : 0; compressed.resize(result); } + r_ret = compressed; } static void _call_PackedByteArray_decompress(Variant &r_ret, Variant &p_self, const Variant **p_args) { - PackedByteArray *ba = reinterpret_cast<PackedByteArray *>(p_self._data._mem); + Variant::PackedArrayRef<uint8_t> *ba = reinterpret_cast<Variant::PackedArrayRef<uint8_t> *>(p_self._data.packed_array); PackedByteArray decompressed; Compression::Mode mode = (Compression::Mode)(int)(*p_args[1]); @@ -646,7 +704,7 @@ struct _VariantCall { } decompressed.resize(buffer_size); - int result = Compression::decompress(decompressed.ptrw(), buffer_size, ba->ptr(), ba->size(), mode); + int result = Compression::decompress(decompressed.ptrw(), buffer_size, ba->array.ptr(), ba->array.size(), mode); result = result >= 0 ? result : 0; decompressed.resize(result); @@ -654,14 +712,31 @@ struct _VariantCall { r_ret = decompressed; } + static void _call_PackedByteArray_decompress_dynamic(Variant &r_ret, Variant &p_self, const Variant **p_args) { + Variant::PackedArrayRef<uint8_t> *ba = reinterpret_cast<Variant::PackedArrayRef<uint8_t> *>(p_self._data.packed_array); + PackedByteArray decompressed; + int max_output_size = (int)(*p_args[0]); + Compression::Mode mode = (Compression::Mode)(int)(*p_args[1]); + + int result = Compression::decompress_dynamic(&decompressed, max_output_size, ba->array.ptr(), ba->array.size(), mode); + + if (result == OK) { + r_ret = decompressed; + } else { + decompressed.clear(); + r_ret = decompressed; + ERR_FAIL_MSG("Decompression failed."); + } + } + static void _call_PackedByteArray_hex_encode(Variant &r_ret, Variant &p_self, const Variant **p_args) { - PackedByteArray *ba = reinterpret_cast<PackedByteArray *>(p_self._data._mem); - if (ba->size() == 0) { + Variant::PackedArrayRef<uint8_t> *ba = reinterpret_cast<Variant::PackedArrayRef<uint8_t> *>(p_self._data.packed_array); + if (ba->array.size() == 0) { r_ret = String(); return; } - const uint8_t *r = ba->ptr(); - String s = String::hex_encode_buffer(&r[0], ba->size()); + const uint8_t *r = ba->array.ptr(); + String s = String::hex_encode_buffer(&r[0], ba->array.size()); r_ret = s; } @@ -1789,6 +1864,7 @@ void register_variant_methods() { /* STRING */ ADDFUNC1R(STRING, INT, String, casecmp_to, STRING, "to", varray()); ADDFUNC1R(STRING, INT, String, nocasecmp_to, STRING, "to", varray()); + ADDFUNC1R(STRING, INT, String, naturalnocasecmp_to, STRING, "to", varray()); ADDFUNC0R(STRING, INT, String, length, varray()); ADDFUNC2R(STRING, STRING, String, substr, INT, "from", INT, "len", varray(-1)); @@ -1867,6 +1943,8 @@ void register_variant_methods() { ADDFUNC0R(STRING, INT, String, to_int, varray()); ADDFUNC0R(STRING, FLOAT, String, to_float, varray()); ADDFUNC0R(STRING, INT, String, hex_to_int, varray()); + ADDFUNC2R(STRING, STRING, String, lpad, INT, "min_length", STRING, "character", varray(" ")); + ADDFUNC2R(STRING, STRING, String, rpad, INT, "min_length", STRING, "character", varray(" ")); ADDFUNC1R(STRING, STRING, String, pad_decimals, INT, "digits", varray()); ADDFUNC1R(STRING, STRING, String, pad_zeros, INT, "digits", varray()); ADDFUNC1R(STRING, STRING, String, trim_prefix, STRING, "prefix", varray()); @@ -1874,6 +1952,8 @@ void register_variant_methods() { ADDFUNC0R(STRING, PACKED_BYTE_ARRAY, String, to_ascii, varray()); ADDFUNC0R(STRING, PACKED_BYTE_ARRAY, String, to_utf8, varray()); + ADDFUNC0R(STRING, PACKED_BYTE_ARRAY, String, to_utf16, varray()); + ADDFUNC0R(STRING, PACKED_BYTE_ARRAY, String, to_utf32, varray()); ADDFUNC0R(VECTOR2, FLOAT, Vector2, angle, varray()); ADDFUNC1R(VECTOR2, FLOAT, Vector2, angle_to, VECTOR2, "to", varray()); @@ -2109,9 +2189,12 @@ void register_variant_methods() { ADDFUNC0R(PACKED_BYTE_ARRAY, STRING, PackedByteArray, get_string_from_ascii, varray()); ADDFUNC0R(PACKED_BYTE_ARRAY, STRING, PackedByteArray, get_string_from_utf8, varray()); + ADDFUNC0R(PACKED_BYTE_ARRAY, STRING, PackedByteArray, get_string_from_utf16, varray()); + ADDFUNC0R(PACKED_BYTE_ARRAY, STRING, PackedByteArray, get_string_from_utf32, varray()); ADDFUNC0R(PACKED_BYTE_ARRAY, STRING, PackedByteArray, hex_encode, varray()); ADDFUNC1R(PACKED_BYTE_ARRAY, PACKED_BYTE_ARRAY, PackedByteArray, compress, INT, "compression_mode", varray(0)); ADDFUNC2R(PACKED_BYTE_ARRAY, PACKED_BYTE_ARRAY, PackedByteArray, decompress, INT, "buffer_size", INT, "compression_mode", varray(0)); + ADDFUNC2R(PACKED_BYTE_ARRAY, PACKED_BYTE_ARRAY, PackedByteArray, decompress_dynamic, INT, "max_output_size", INT, "compression_mode", varray(0)); ADDFUNC0R(PACKED_INT32_ARRAY, INT, PackedInt32Array, size, varray()); ADDFUNC0R(PACKED_INT32_ARRAY, BOOL, PackedInt32Array, empty, varray()); |