Skip to content

Commit

Permalink
Remove implicit conversions from String, Char16String and `CharSt…
Browse files Browse the repository at this point in the history
…ring` to data pointers. Make conversions to `StrRange` implicit to aid transition.
  • Loading branch information
Ivorforce committed Jan 8, 2025
1 parent 76c8e76 commit ddbd8c2
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions core/io/plist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,14 @@ void PListNode::store_text(String &p_stream, uint8_t p_indent) const {
p_stream += String("\t").repeat(p_indent);
p_stream += "<data>\n";
p_stream += String("\t").repeat(p_indent);
p_stream += data_string + "\n";
p_stream += String::utf8(data_string) + "\n";
p_stream += String("\t").repeat(p_indent);
p_stream += "</data>\n";
} break;
case PList::PLNodeType::PL_NODE_TYPE_DATE: {
p_stream += String("\t").repeat(p_indent);
p_stream += "<date>";
p_stream += data_string;
p_stream += String::utf8(data_string);
p_stream += "</date>\n";
} break;
case PList::PLNodeType::PL_NODE_TYPE_STRING: {
Expand Down
2 changes: 1 addition & 1 deletion core/io/resource_uid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ String ResourceUID::get_path_from_cache(Ref<FileAccess> &p_cache_file, const Str
ERR_FAIL_COND_V(rl != len, String());

if (singleton->id_to_text(id) == p_uid_string) {
return String(cs);
return String::utf8(cs);
}
}
return String();
Expand Down
16 changes: 11 additions & 5 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ class Char16String {
Char16String &operator+=(char16_t p_char);
int length() const { return size() ? size() - 1 : 0; }
const char16_t *get_data() const;
operator const char16_t *() const { return get_data(); }
explicit operator StrRange<char16_t>() const { return StrRange(get_data(), length()); }
operator StrRange<char16_t>() const { return StrRange(get_data(), length()); }

protected:
void copy_from(const char16_t *p_cstr);
Expand Down Expand Up @@ -250,8 +249,7 @@ class CharString {
CharString &operator+=(char p_char);
int length() const { return size() ? size() - 1 : 0; }
const char *get_data() const;
operator const char *() const { return get_data(); }
explicit operator StrRange<char>() const { return StrRange(get_data(), length()); }
operator StrRange<char>() const { return StrRange(get_data(), length()); }

protected:
void copy_from(const char *p_cstr);
Expand Down Expand Up @@ -523,11 +521,19 @@ class String {
CharString ascii(bool p_allow_extended = false) const;
CharString utf8() const;
Error parse_utf8(const char *p_utf8, int p_len = -1, bool p_skip_cr = false);
Error parse_utf8(const StrRange<char> p_range, bool p_skip_cr = false) {
return parse_utf8(p_range.c_str, p_range.len, p_skip_cr);
}
static String utf8(const char *p_utf8, int p_len = -1);
static String utf8(const StrRange<char> &p_range) { return utf8(p_range.c_str, p_range.len); }

Char16String utf16() const;
Error parse_utf16(const char16_t *p_utf16, int p_len = -1, bool p_default_little_endian = true);
Error parse_utf16(const StrRange<char16_t> p_range, bool p_skip_cr = false) {
return parse_utf16(p_range.c_str, p_range.len, p_skip_cr);
}
static String utf16(const char16_t *p_utf16, int p_len = -1);
static String utf16(const StrRange<char16_t> &p_range) { return utf16(p_range.c_str, p_range.len); }

static uint32_t hash(const char32_t *p_cstr, int p_len); /* hash the string */
static uint32_t hash(const char32_t *p_cstr); /* hash the string */
Expand Down Expand Up @@ -642,7 +648,7 @@ class String {
parse_utf32(p_cstr);
}

explicit operator StrRange<char32_t>() const { return StrRange(get_data(), length()); }
operator StrRange<char32_t>() const { return StrRange(get_data(), length()); }
};

bool operator==(const char *p_chr, const String &p_str);
Expand Down
8 changes: 4 additions & 4 deletions editor/export/codesign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,13 @@ _FORCE_INLINE_ void CodeSignRequirements::_parse_certificate_slot(uint32_t &r_po
_FORCE_INLINE_ void CodeSignRequirements::_parse_key(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
#define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
uint32_t key_size = _R(r_pos);
const uint32_t key_size = _R(r_pos);
ERR_FAIL_COND_MSG(r_pos + key_size > p_rq_size, "CodeSign/Requirements: Out of bounds.");
CharString key;
key.resize(key_size);
memcpy(key.ptrw(), blob.ptr() + r_pos + 4, key_size);
r_pos += 4 + key_size + PAD(key_size, 4);
r_out += "[" + String::utf8(key, key_size) + "]";
r_out += "[" + String::utf8(key) + "]";
#undef _R
}

Expand Down Expand Up @@ -526,13 +526,13 @@ _FORCE_INLINE_ void CodeSignRequirements::_parse_hash_string(uint32_t &r_pos, St
_FORCE_INLINE_ void CodeSignRequirements::_parse_value(uint32_t &r_pos, String &r_out, uint32_t p_rq_size) const {
#define _R(x) BSWAP32(*(uint32_t *)(blob.ptr() + x))
ERR_FAIL_COND_MSG(r_pos >= p_rq_size, "CodeSign/Requirements: Out of bounds.");
uint32_t key_size = _R(r_pos);
const uint32_t key_size = _R(r_pos);
ERR_FAIL_COND_MSG(r_pos + key_size > p_rq_size, "CodeSign/Requirements: Out of bounds.");
CharString key;
key.resize(key_size);
memcpy(key.ptrw(), blob.ptr() + r_pos + 4, key_size);
r_pos += 4 + key_size + PAD(key_size, 4);
r_out += "\"" + String::utf8(key, key_size) + "\"";
r_out += "\"" + String::utf8(key) + "\"";
#undef _R
}

Expand Down
6 changes: 3 additions & 3 deletions modules/camera/camera_feed_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void CameraFeedLinux::_update_buffer() {
}

void CameraFeedLinux::_query_device(const String &p_device_name) {
file_descriptor = open(p_device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
file_descriptor = open(p_device_name.ascii().get_data(), O_RDWR | O_NONBLOCK, 0);
ERR_FAIL_COND_MSG(file_descriptor == -1, vformat("Cannot open file descriptor for %s. Error: %d.", p_device_name, errno));

struct v4l2_capability capability;
Expand Down Expand Up @@ -235,7 +235,7 @@ String CameraFeedLinux::get_device_name() const {

bool CameraFeedLinux::activate_feed() {
ERR_FAIL_COND_V_MSG(selected_format == -1, false, "CameraFeed format needs to be set before activating.");
file_descriptor = open(device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
file_descriptor = open(device_name.ascii().get_data(), O_RDWR | O_NONBLOCK, 0);
if (_request_buffers() && _start_capturing()) {
buffer_decoder = _create_buffer_decoder();
_start_thread();
Expand Down Expand Up @@ -315,7 +315,7 @@ bool CameraFeedLinux::set_format(int p_index, const Dictionary &p_parameters) {

FeedFormat feed_format = formats[p_index];

file_descriptor = open(device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
file_descriptor = open(device_name.ascii().get_data(), O_RDWR | O_NONBLOCK, 0);

struct v4l2_format format;
memset(&format, 0, sizeof(format));
Expand Down
4 changes: 2 additions & 2 deletions modules/camera/camera_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ void CameraLinux::_add_device(const String &p_device_name) {
int CameraLinux::_open_device(const String &p_device_name) {
struct stat s;

if (stat(p_device_name.ascii(), &s) == -1) {
if (stat(p_device_name.ascii().get_data(), &s) == -1) {
return -1;
}

if (!S_ISCHR(s.st_mode)) {
return -1;
}

return open(p_device_name.ascii(), O_RDWR | O_NONBLOCK, 0);
return open(p_device_name.ascii().get_data(), O_RDWR | O_NONBLOCK, 0);
}

// TODO any cheaper/cleaner way to check if file descriptor is invalid?
Expand Down
2 changes: 1 addition & 1 deletion modules/openxr/openxr_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ bool OpenXRAPI::create_instance() {

Vector<const char *> extension_ptrs;
for (int i = 0; i < enabled_extensions.size(); i++) {
print_verbose(String("OpenXR: Enabling extension ") + String(enabled_extensions[i]));
print_verbose(String("OpenXR: Enabling extension ") + String::utf8(enabled_extensions[i]));
extension_ptrs.push_back(enabled_extensions[i].get_data());
}

Expand Down
4 changes: 2 additions & 2 deletions modules/regex/regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Error RegEx::compile(const String &p_pattern, bool p_show_error) {
PCRE2_UCHAR32 buf[256];
pcre2_get_error_message_32(err, buf, 256);
String message = String::num(offset) + ": " + String((const char32_t *)buf);
ERR_PRINT(message.utf8());
ERR_PRINT(message);
}
return FAILED;
}
Expand Down Expand Up @@ -348,7 +348,7 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a
PCRE2_UCHAR32 buf[256];
pcre2_get_error_message_32(res, buf, 256);
String message = "PCRE2 Error: " + String((const char32_t *)buf);
ERR_PRINT(message.utf8());
ERR_PRINT(message);

if (res == PCRE2_ERROR_NOSUBSTRING) {
flags |= PCRE2_SUBSTITUTE_UNKNOWN_UNSET;
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/display_server_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ _FORCE_INLINE_ int _convert_utf32_offset_to_utf16(const String &p_existing_text,
}

void DisplayServerIOS::clipboard_set(const String &p_text) {
[UIPasteboard generalPasteboard].string = [NSString stringWithUTF8String:p_text.utf8()];
[UIPasteboard generalPasteboard].string = [NSString stringWithUTF8String:p_text.utf8().get_data()];
}

String DisplayServerIOS::clipboard_get() const {
Expand Down
2 changes: 1 addition & 1 deletion platform/linuxbsd/tts_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void TTS_Linux::speech_init_thread_func(void *p_userdata) {
} else {
class_str = config_name.utf8();
}
tts->synth = spd_open(class_str, "Godot_Engine_Speech_API", "Godot_Engine", SPD_MODE_THREADED);
tts->synth = spd_open(class_str.get_data(), "Godot_Engine_Speech_API", "Godot_Engine", SPD_MODE_THREADED);
if (tts->synth) {
tts->synth->callback_end = &speech_event_callback;
tts->synth->callback_cancel = &speech_event_callback;
Expand Down
12 changes: 6 additions & 6 deletions platform/linuxbsd/wayland/wayland_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3676,12 +3676,12 @@ void WaylandThread::window_set_title(DisplayServer::WindowID p_window_id, const

#ifdef LIBDECOR_ENABLED
if (ws.libdecor_frame) {
libdecor_frame_set_title(ws.libdecor_frame, p_title.utf8());
libdecor_frame_set_title(ws.libdecor_frame, p_title.utf8().get_data());
}
#endif // LIBDECOR_ENABLE

if (ws.xdg_toplevel) {
xdg_toplevel_set_title(ws.xdg_toplevel, p_title.utf8());
xdg_toplevel_set_title(ws.xdg_toplevel, p_title.utf8().get_data());
}
}

Expand All @@ -3691,13 +3691,13 @@ void WaylandThread::window_set_app_id(DisplayServer::WindowID p_window_id, const

#ifdef LIBDECOR_ENABLED
if (ws.libdecor_frame) {
libdecor_frame_set_app_id(ws.libdecor_frame, p_app_id.utf8());
libdecor_frame_set_app_id(ws.libdecor_frame, p_app_id.utf8().get_data());
return;
}
#endif // LIBDECOR_ENABLED

if (ws.xdg_toplevel) {
xdg_toplevel_set_app_id(ws.xdg_toplevel, p_app_id.utf8());
xdg_toplevel_set_app_id(ws.xdg_toplevel, p_app_id.utf8().get_data());
return;
}
}
Expand Down Expand Up @@ -4185,7 +4185,7 @@ Vector<uint8_t> WaylandThread::selection_get_mime(const String &p_mime) const {
return Vector<uint8_t>();
}

return _wl_data_offer_read(wl_display, p_mime.utf8(), ss->wl_data_offer_selection);
return _wl_data_offer_read(wl_display, p_mime.utf8().get_data(), ss->wl_data_offer_selection);
}

bool WaylandThread::primary_has_mime(const String &p_mime) const {
Expand Down Expand Up @@ -4228,7 +4228,7 @@ Vector<uint8_t> WaylandThread::primary_get_mime(const String &p_mime) const {
return Vector<uint8_t>();
}

return _wp_primary_selection_offer_read(wl_display, p_mime.utf8(), ss->wp_primary_selection_offer);
return _wp_primary_selection_offer_read(wl_display, p_mime.utf8().get_data(), ss->wp_primary_selection_offer);
}

void WaylandThread::primary_set_text(const String &p_text) {
Expand Down

0 comments on commit ddbd8c2

Please sign in to comment.