diff options
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 37 | ||||
-rw-r--r-- | core/string/ustring.h | 3 |
2 files changed, 40 insertions, 0 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 1b3b070592..773445edb6 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1644,6 +1644,35 @@ String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) { return ret; } +Vector<uint8_t> String::hex_decode() const { + ERR_FAIL_COND_V_MSG(length() % 2 != 0, Vector<uint8_t>(), "Hexadecimal string of uneven length."); + +#define HEX_TO_BYTE(m_output, m_index) \ + uint8_t m_output; \ + c = operator[](m_index); \ + if (is_digit(c)) { \ + m_output = c - '0'; \ + } else if (c >= 'a' && c <= 'f') { \ + m_output = c - 'a' + 10; \ + } else if (c >= 'A' && c <= 'F') { \ + m_output = c - 'A' + 10; \ + } else { \ + ERR_FAIL_V_MSG(Vector<uint8_t>(), "Invalid hexadecimal character \"" + chr(c) + "\" at index " + m_index + "."); \ + } + + Vector<uint8_t> out; + int len = length() / 2; + out.resize(len); + for (int i = 0; i < len; i++) { + char32_t c; + HEX_TO_BYTE(first, i * 2); + HEX_TO_BYTE(second, i * 2 + 1); + out.write[i] = first * 16 + second; + } + return out; +#undef HEX_TO_BYTE +} + void String::print_unicode_error(const String &p_message, bool p_critical) const { if (p_critical) { print_error(vformat("Unicode parsing error, some characters were replaced with spaces: %s", p_message)); @@ -5034,6 +5063,14 @@ Vector<uint8_t> String::to_utf32_buffer() const { return retval; } +Vector<uint8_t> String::to_wchar_buffer() const { +#ifdef WINDOWS_ENABLED + return to_utf16_buffer(); +#else + return to_utf32_buffer(); +#endif +} + #ifdef TOOLS_ENABLED /** * "Tools TRanslate". Performs string replacement for internationalization diff --git a/core/string/ustring.h b/core/string/ustring.h index 1582504c57..90034b1b07 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -321,6 +321,8 @@ public: static String chr(char32_t p_char); static String md5(const uint8_t *p_md5); static String hex_encode_buffer(const uint8_t *p_buffer, int p_len); + Vector<uint8_t> hex_decode() const; + bool is_numeric() const; double to_float() const; @@ -455,6 +457,7 @@ public: Vector<uint8_t> to_utf8_buffer() const; Vector<uint8_t> to_utf16_buffer() const; Vector<uint8_t> to_utf32_buffer() const; + Vector<uint8_t> to_wchar_buffer() const; String(const char *p_str); String(const wchar_t *p_str); |