Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate RenderingDeviceDriver's VectorView into Span. #100338

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,16 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r
return OK;
}

void String::copy_from(const StrRange<char> &p_cstr) {
if (p_cstr.len == 0) {
void String::copy_from(const Span<const char> &p_cstr) {
if (p_cstr.empty()) {
resize(0);
return;
}

resize(p_cstr.len + 1); // include 0
resize(p_cstr.size() + 1); // include 0

const char *src = p_cstr.c_str;
const char *end = src + p_cstr.len;
const char *src = p_cstr.ptr();
const char *end = src + p_cstr.size();
char32_t *dst = ptrw();

for (; src < end; ++src, ++dst) {
Expand All @@ -323,13 +323,13 @@ void String::copy_from(const StrRange<char> &p_cstr) {
*dst = 0;
}

void String::copy_from(const StrRange<char32_t> &p_cstr) {
if (p_cstr.len == 0) {
void String::copy_from(const Span<const char32_t> &p_cstr) {
if (p_cstr.empty()) {
resize(0);
return;
}

copy_from_unchecked(p_cstr.c_str, p_cstr.len);
copy_from_unchecked(p_cstr.ptr(), p_cstr.size());
}

void String::copy_from(const char32_t &p_char) {
Expand Down Expand Up @@ -592,8 +592,8 @@ bool String::operator==(const String &p_str) const {
return true;
}

bool String::operator==(const StrRange<char32_t> &p_str_range) const {
int len = p_str_range.len;
bool String::operator==(const Span<const char32_t> &p_str_range) const {
int len = p_str_range.size();

if (length() != len) {
return false;
Expand All @@ -602,7 +602,7 @@ bool String::operator==(const StrRange<char32_t> &p_str_range) const {
return true;
}

const char32_t *c_str = p_str_range.c_str;
const char32_t *c_str = p_str_range.ptr();
const char32_t *dst = &operator[](0);

/* Compare char by char */
Expand Down Expand Up @@ -3247,7 +3247,7 @@ int String::find(const char *p_str, int p_from) const {
}

int String::find_char(char32_t p_char, int p_from) const {
return _cowdata.find(p_char, p_from);
return view().find(p_char, p_from);
}

int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
Expand Down Expand Up @@ -3484,7 +3484,7 @@ int String::rfind(const char *p_str, int p_from) const {
}

int String::rfind_char(char32_t p_char, int p_from) const {
return _cowdata.rfind(p_char, p_from);
return view().rfind(p_char, p_from);
}

int String::rfindn(const String &p_str, int p_from) const {
Expand Down
54 changes: 16 additions & 38 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,6 @@ constexpr size_t _strlen_clipped(const char32_t *p_str, int p_clip_to_len) {
return len;
}

/*************************************************************************/
/* StrRange */
/*************************************************************************/

template <typename Element>
struct StrRange {
const Element *c_str;
size_t len;

explicit StrRange(const std::nullptr_t p_cstring) :
c_str(nullptr), len(0) {}

explicit StrRange(const Element *p_cstring, const size_t p_len) :
c_str(p_cstring), len(p_len) {}

template <size_t len>
explicit StrRange(const Element (&p_cstring)[len]) :
c_str(p_cstring), len(strlen(p_cstring)) {}

static StrRange from_c_str(const Element *p_cstring) {
return StrRange(p_cstring, p_cstring ? strlen(p_cstring) : 0);
}
};

/*************************************************************************/
/* CharProxy */
/*************************************************************************/
Expand Down Expand Up @@ -177,6 +153,8 @@ class Char16String {
public:
_FORCE_INLINE_ char16_t *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const char16_t *ptr() const { return _cowdata.ptr(); }
Span<char16_t> vieww() { return Span(ptrw(), length()); }
Span<const char16_t> view() const { return Span(ptr(), length()); }
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
Error resize(int p_size) { return _cowdata.resize(p_size); }

Expand All @@ -202,7 +180,6 @@ class Char16String {
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()); }

protected:
void copy_from(const char16_t *p_cstr);
Expand All @@ -219,6 +196,8 @@ class CharString {
public:
_FORCE_INLINE_ char *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const char *ptr() const { return _cowdata.ptr(); }
Span<char> vieww() { return Span(ptrw(), length()); }
Span<const char> view() const { return Span(ptr(), length()); }
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
Error resize(int p_size) { return _cowdata.resize(p_size); }

Expand All @@ -245,7 +224,6 @@ class CharString {
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()); }

protected:
void copy_from(const char *p_cstr);
Expand All @@ -261,33 +239,33 @@ class String {
static const char32_t _replacement_char;

// Known-length copy.
void copy_from(const StrRange<char> &p_cstr);
void copy_from(const StrRange<char32_t> &p_cstr);
void copy_from(const Span<const char> &p_cstr);
void copy_from(const Span<const char32_t> &p_cstr);
void copy_from(const char32_t &p_char);
void copy_from_unchecked(const char32_t *p_char, int p_length);

// NULL-terminated c string copy - automatically parse the string to find the length.
void copy_from(const char *p_cstr) {
copy_from(StrRange<char>::from_c_str(p_cstr));
copy_from(Span(p_cstr, p_cstr ? strlen(p_cstr) : 0));
}
void copy_from(const char *p_cstr, int p_clip_to) {
copy_from(StrRange(p_cstr, p_cstr ? _strlen_clipped(p_cstr, p_clip_to) : 0));
copy_from(Span(p_cstr, p_cstr ? _strlen_clipped(p_cstr, p_clip_to) : 0));
}
void copy_from(const char32_t *p_cstr) {
copy_from(StrRange<char32_t>::from_c_str(p_cstr));
copy_from(Span(p_cstr, p_cstr ? strlen(p_cstr) : 0));
}
void copy_from(const char32_t *p_cstr, int p_clip_to) {
copy_from(StrRange(p_cstr, p_cstr ? _strlen_clipped(p_cstr, p_clip_to) : 0));
copy_from(Span(p_cstr, p_cstr ? _strlen_clipped(p_cstr, p_clip_to) : 0));
}

// wchar_t copy_from depends on the platform.
void copy_from(const StrRange<wchar_t> &p_cstr) {
void copy_from(const Span<const wchar_t> &p_cstr) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit, parse as UTF-16
parse_utf16((const char16_t *)p_cstr.c_str, p_cstr.len);
parse_utf16((const char16_t *)p_cstr.ptr(), p_cstr.size());
#else
// wchar_t is 32-bit, copy directly
copy_from((StrRange<char32_t> &)p_cstr);
copy_from((Span<const char32_t> &)p_cstr);
#endif
}
void copy_from(const wchar_t *p_cstr) {
Expand Down Expand Up @@ -321,6 +299,8 @@ class String {

_FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const char32_t *ptr() const { return _cowdata.ptr(); }
Span<char32_t> vieww() { return Span(ptrw(), length()); }
Span<const char32_t> view() const { return Span(ptr(), length()); }

void remove_at(int p_index) { _cowdata.remove_at(p_index); }

Expand Down Expand Up @@ -356,7 +336,7 @@ class String {
bool operator==(const char *p_str) const;
bool operator==(const wchar_t *p_str) const;
bool operator==(const char32_t *p_str) const;
bool operator==(const StrRange<char32_t> &p_str_range) const;
bool operator==(const Span<const char32_t> &p_str_range) const;

bool operator!=(const char *p_str) const;
bool operator!=(const wchar_t *p_str) const;
Expand Down Expand Up @@ -632,8 +612,6 @@ class String {
void operator=(const char32_t *p_cstr) {
copy_from(p_cstr);
}

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

bool operator==(const char *p_chr, const String &p_str);
Expand Down
58 changes: 7 additions & 51 deletions core/templates/cowdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "core/templates/safe_refcount.h"
#include "core/templates/span.h"

#include <string.h>
#include <type_traits>
Expand Down Expand Up @@ -235,9 +236,12 @@ class CowData {
return OK;
}

Size find(const T &p_val, Size p_from = 0) const;
Size rfind(const T &p_val, Size p_from = -1) const;
Size count(const T &p_val) const;
Span<T> vieww() {
return Span<T>(ptrw(), size());
}
Span<const T> view() const {
return Span<const T>(ptr(), size());
}

_FORCE_INLINE_ CowData() {}
_FORCE_INLINE_ ~CowData();
Expand Down Expand Up @@ -403,54 +407,6 @@ Error CowData<T>::resize(Size p_size) {
return OK;
}

template <typename T>
typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const {
Size ret = -1;

if (p_from < 0 || size() == 0) {
return ret;
}

for (Size i = p_from; i < size(); i++) {
if (get(i) == p_val) {
ret = i;
break;
}
}

return ret;
}

template <typename T>
typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const {
const Size s = size();

if (p_from < 0) {
p_from = s + p_from;
}
if (p_from < 0 || p_from >= s) {
p_from = s - 1;
}

for (Size i = p_from; i >= 0; i--) {
if (get(i) == p_val) {
return i;
}
}
return -1;
}

template <typename T>
typename CowData<T>::Size CowData<T>::count(const T &p_val) const {
Size amount = 0;
for (Size i = 0; i < size(); i++) {
if (get(i) == p_val) {
amount++;
}
}
return amount;
}

template <typename T>
void CowData<T>::_ref(const CowData *p_from) {
_ref(*p_from);
Expand Down
13 changes: 6 additions & 7 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "core/templates/sort_array.h"
#include "core/templates/span.h"
#include "core/templates/vector.h"

#include <initializer_list>
Expand All @@ -49,13 +50,11 @@ class LocalVector {
T *data = nullptr;

public:
T *ptr() {
return data;
}

const T *ptr() const {
return data;
}
T *ptrw() { return data; }
T *ptr() { return data; }
const T *ptr() const { return data; }
Span<T> vieww() { return Span(ptrw(), size()); }
Span<const T> view() const { return Span(ptr(), size()); }

_FORCE_INLINE_ void push_back(T p_elem) {
if (unlikely(count == capacity)) {
Expand Down
Loading
Loading