mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
Added StringName from Godot.
This commit is contained in:
parent
88fafbe78e
commit
b613349a11
137
core/string.cpp
137
core/string.cpp
@ -1633,6 +1633,9 @@ bool String::parse_utf8(const char *p_utf8, int p_len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
String String::utf8() const {
|
||||
int l = size();
|
||||
if (!l) {
|
||||
@ -1713,6 +1716,92 @@ String String::utf8() const {
|
||||
return utf8s;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
uint32_t String::hash(const wchar_t *p_cstr, int p_len) {
|
||||
uint32_t hashv = 5381;
|
||||
for (int i = 0; i < p_len; i++) {
|
||||
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
return hashv;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
uint32_t String::hash(const wchar_t *p_cstr) {
|
||||
uint32_t hashv = 5381;
|
||||
uint32_t c;
|
||||
|
||||
while ((c = *p_cstr++)) {
|
||||
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
return hashv;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
uint32_t String::hash(const char *p_cstr) {
|
||||
uint32_t hashv = 5381;
|
||||
uint32_t c;
|
||||
|
||||
while ((c = *p_cstr++)) {
|
||||
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
return hashv;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
uint32_t String::hash(const char *p_cstr, int p_len) {
|
||||
uint32_t hashv = 5381;
|
||||
for (int i = 0; i < p_len; i++) {
|
||||
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
return hashv;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
uint32_t String::hash() const {
|
||||
/* simple djb2 hashing */
|
||||
|
||||
const char *chr = c_str();
|
||||
uint32_t hashv = 5381;
|
||||
uint32_t c;
|
||||
|
||||
while ((c = *chr++)) {
|
||||
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
return hashv;
|
||||
}
|
||||
|
||||
// Taken from the Godot Engine (MIT License)
|
||||
// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
uint64_t String::hash64() const {
|
||||
/* simple djb2 hashing */
|
||||
|
||||
const char *chr = c_str();
|
||||
uint64_t hashv = 5381;
|
||||
uint64_t c;
|
||||
|
||||
while ((c = *chr++)) {
|
||||
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
return hashv;
|
||||
}
|
||||
|
||||
char *String::c_str() {
|
||||
return _data;
|
||||
}
|
||||
@ -1862,6 +1951,54 @@ bool operator!=(const char *b, const String &a) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
bool operator==(const String &a, const wchar_t *b) {
|
||||
if (a._size == 0) {
|
||||
return b[0] == '\0';
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < a._size && b[i] != '\0') {
|
||||
if (a[i] != b[i]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
if (i != a._size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool operator!=(const String &a, const wchar_t *b) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
bool operator==(const wchar_t *b, const String &a) {
|
||||
if (a._size == 0) {
|
||||
return b[0] == '\0';
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < a._size && b[i] != '\0') {
|
||||
if (a[i] != b[i]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
if (i != a._size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool operator!=(const wchar_t *b, const String &a) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
bool operator==(const String &a, std::string &b) {
|
||||
if (a._size != b.size()) {
|
||||
return false;
|
||||
|
@ -136,6 +136,16 @@ public:
|
||||
bool parse_utf8(const char *p_utf8, int p_len = -1); //return true on error
|
||||
static String utf8(const char *p_utf8, int p_len = -1);
|
||||
|
||||
//Taken from the Godot Engine (MIT License)
|
||||
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
static uint32_t hash(const wchar_t *p_cstr, int p_len); /* hash the string */
|
||||
static uint32_t hash(const wchar_t *p_cstr); /* hash the string */
|
||||
static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
|
||||
static uint32_t hash(const char *p_cstr); /* hash the string */
|
||||
uint32_t hash() const; /* hash the string */
|
||||
uint64_t hash64() const; /* hash the string */
|
||||
|
||||
char *c_str();
|
||||
const char *c_str() const;
|
||||
|
||||
@ -164,6 +174,12 @@ public:
|
||||
friend bool operator==(const char *b, const String &a);
|
||||
friend bool operator!=(const char *b, const String &a);
|
||||
|
||||
friend bool operator==(const String &a, const wchar_t *b);
|
||||
friend bool operator!=(const String &a, const wchar_t *b);
|
||||
|
||||
friend bool operator==(const wchar_t *b, const String &a);
|
||||
friend bool operator!=(const wchar_t *b, const String &a);
|
||||
|
||||
friend bool operator==(const String &a, std::string &b);
|
||||
friend bool operator!=(const String &a, std::string &b);
|
||||
|
||||
@ -199,4 +215,27 @@ private:
|
||||
int _grow_by;
|
||||
};
|
||||
|
||||
//Taken from the Godot Engine (MIT License)
|
||||
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
|
||||
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
|
||||
template <typename L, typename R>
|
||||
bool is_str_less(const L *l_ptr, const R *r_ptr) {
|
||||
while (true) {
|
||||
if (*l_ptr == 0 && *r_ptr == 0) {
|
||||
return false;
|
||||
} else if (*l_ptr == 0) {
|
||||
return true;
|
||||
} else if (*r_ptr == 0) {
|
||||
return false;
|
||||
} else if (*l_ptr < *r_ptr) {
|
||||
return true;
|
||||
} else if (*l_ptr > *r_ptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
l_ptr++;
|
||||
r_ptr++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
405
core/string_name.cpp
Normal file
405
core/string_name.cpp
Normal file
@ -0,0 +1,405 @@
|
||||
/*************************************************************************/
|
||||
/* string_name.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "string_name.h"
|
||||
#include "core/error_macros.h"
|
||||
|
||||
StaticCString StaticCString::create(const char *p_ptr) {
|
||||
StaticCString scs;
|
||||
scs.ptr = p_ptr;
|
||||
return scs;
|
||||
}
|
||||
|
||||
StringName::_Data *StringName::_table[STRING_TABLE_LEN];
|
||||
|
||||
StringName _scs_create(const char *p_chr) {
|
||||
return (p_chr[0] ? StringName(StaticCString::create(p_chr)) : StringName());
|
||||
}
|
||||
|
||||
bool StringName::configured = false;
|
||||
Mutex StringName::lock;
|
||||
|
||||
void StringName::setup() {
|
||||
ERR_FAIL_COND(configured);
|
||||
for (int i = 0; i < STRING_TABLE_LEN; i++) {
|
||||
_table[i] = nullptr;
|
||||
}
|
||||
configured = true;
|
||||
}
|
||||
|
||||
void StringName::cleanup() {
|
||||
lock.lock();
|
||||
|
||||
int lost_strings = 0;
|
||||
for (int i = 0; i < STRING_TABLE_LEN; i++) {
|
||||
while (_table[i]) {
|
||||
_Data *d = _table[i];
|
||||
lost_strings++;
|
||||
/*
|
||||
if (OS::get_singleton()->is_stdout_verbose()) {
|
||||
if (d->cname) {
|
||||
print_line("Orphan StringName: " + String(d->cname));
|
||||
} else {
|
||||
print_line("Orphan StringName: " + String(d->name));
|
||||
}
|
||||
}*/
|
||||
|
||||
_table[i] = _table[i]->next;
|
||||
//memdelete(d);
|
||||
delete d;
|
||||
}
|
||||
}
|
||||
|
||||
//if (lost_strings) {
|
||||
// print_verbose("StringName: " + itos(lost_strings) + " unclaimed string names at exit.");
|
||||
//}
|
||||
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
void StringName::unref() {
|
||||
ERR_FAIL_COND(!configured);
|
||||
|
||||
if (_data && _data->refcount.unref()) {
|
||||
lock.lock();
|
||||
|
||||
if (_data->prev) {
|
||||
_data->prev->next = _data->next;
|
||||
} else {
|
||||
if (_table[_data->idx] != _data) {
|
||||
RLOG_ERR("BUG!");
|
||||
}
|
||||
_table[_data->idx] = _data->next;
|
||||
}
|
||||
|
||||
if (_data->next) {
|
||||
_data->next->prev = _data->prev;
|
||||
}
|
||||
//memdelete(_data);
|
||||
delete _data;
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
_data = nullptr;
|
||||
}
|
||||
|
||||
bool StringName::operator==(const String &p_name) const {
|
||||
if (!_data) {
|
||||
return (p_name.size() == 0);
|
||||
}
|
||||
|
||||
return (_data->get_name() == p_name);
|
||||
}
|
||||
|
||||
bool StringName::operator==(const char *p_name) const {
|
||||
if (!_data) {
|
||||
return (p_name[0] == 0);
|
||||
}
|
||||
|
||||
return (_data->get_name() == p_name);
|
||||
}
|
||||
|
||||
bool StringName::operator!=(const String &p_name) const {
|
||||
return !(operator==(p_name));
|
||||
}
|
||||
|
||||
bool StringName::operator!=(const StringName &p_name) const {
|
||||
// the real magic of all this mess happens here.
|
||||
// this is why path comparisons are very fast
|
||||
return _data != p_name._data;
|
||||
}
|
||||
|
||||
void StringName::operator=(const StringName &p_name) {
|
||||
if (this == &p_name) {
|
||||
return;
|
||||
}
|
||||
|
||||
unref();
|
||||
|
||||
if (p_name._data && p_name._data->refcount.ref()) {
|
||||
_data = p_name._data;
|
||||
}
|
||||
}
|
||||
|
||||
StringName::StringName(const StringName &p_name) {
|
||||
_data = nullptr;
|
||||
|
||||
ERR_FAIL_COND(!configured);
|
||||
|
||||
if (p_name._data && p_name._data->refcount.ref()) {
|
||||
_data = p_name._data;
|
||||
}
|
||||
}
|
||||
|
||||
StringName::StringName(const char *p_name) {
|
||||
_data = nullptr;
|
||||
|
||||
ERR_FAIL_COND(!configured);
|
||||
|
||||
if (!p_name || p_name[0] == 0) {
|
||||
return; //empty, ignore
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
|
||||
uint32_t hash = String::hash(p_name);
|
||||
|
||||
uint32_t idx = hash & STRING_TABLE_MASK;
|
||||
|
||||
_data = _table[idx];
|
||||
|
||||
while (_data) {
|
||||
// compare hash first
|
||||
if (_data->hash == hash && _data->get_name() == p_name) {
|
||||
break;
|
||||
}
|
||||
_data = _data->next;
|
||||
}
|
||||
|
||||
if (_data) {
|
||||
if (_data->refcount.ref()) {
|
||||
// exists
|
||||
lock.unlock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_data = new _Data();
|
||||
_data->name = p_name;
|
||||
_data->refcount.init();
|
||||
_data->hash = hash;
|
||||
_data->idx = idx;
|
||||
_data->cname = nullptr;
|
||||
_data->next = _table[idx];
|
||||
_data->prev = nullptr;
|
||||
if (_table[idx]) {
|
||||
_table[idx]->prev = _data;
|
||||
}
|
||||
_table[idx] = _data;
|
||||
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
StringName::StringName(const StaticCString &p_static_string) {
|
||||
_data = nullptr;
|
||||
|
||||
ERR_FAIL_COND(!configured);
|
||||
|
||||
ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
|
||||
|
||||
lock.lock();
|
||||
|
||||
uint32_t hash = String::hash(p_static_string.ptr);
|
||||
|
||||
uint32_t idx = hash & STRING_TABLE_MASK;
|
||||
|
||||
_data = _table[idx];
|
||||
|
||||
while (_data) {
|
||||
// compare hash first
|
||||
if (_data->hash == hash && _data->get_name() == p_static_string.ptr) {
|
||||
break;
|
||||
}
|
||||
_data = _data->next;
|
||||
}
|
||||
|
||||
if (_data) {
|
||||
if (_data->refcount.ref()) {
|
||||
// exists
|
||||
lock.unlock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_data = new _Data();
|
||||
|
||||
_data->refcount.init();
|
||||
_data->hash = hash;
|
||||
_data->idx = idx;
|
||||
_data->cname = p_static_string.ptr;
|
||||
_data->next = _table[idx];
|
||||
_data->prev = nullptr;
|
||||
if (_table[idx]) {
|
||||
_table[idx]->prev = _data;
|
||||
}
|
||||
_table[idx] = _data;
|
||||
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
StringName::StringName(const String &p_name) {
|
||||
_data = nullptr;
|
||||
|
||||
ERR_FAIL_COND(!configured);
|
||||
|
||||
if (p_name == String()) {
|
||||
return;
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
|
||||
uint32_t hash = p_name.hash();
|
||||
|
||||
uint32_t idx = hash & STRING_TABLE_MASK;
|
||||
|
||||
_data = _table[idx];
|
||||
|
||||
while (_data) {
|
||||
if (_data->hash == hash && _data->get_name() == p_name) {
|
||||
break;
|
||||
}
|
||||
_data = _data->next;
|
||||
}
|
||||
|
||||
if (_data) {
|
||||
if (_data->refcount.ref()) {
|
||||
// exists
|
||||
lock.unlock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_data = new _Data();
|
||||
_data->name = p_name;
|
||||
_data->refcount.init();
|
||||
_data->hash = hash;
|
||||
_data->idx = idx;
|
||||
_data->cname = nullptr;
|
||||
_data->next = _table[idx];
|
||||
_data->prev = nullptr;
|
||||
if (_table[idx]) {
|
||||
_table[idx]->prev = _data;
|
||||
}
|
||||
_table[idx] = _data;
|
||||
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
StringName StringName::search(const char *p_name) {
|
||||
ERR_FAIL_COND_V(!configured, StringName());
|
||||
|
||||
ERR_FAIL_COND_V(!p_name, StringName());
|
||||
if (!p_name[0]) {
|
||||
return StringName();
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
|
||||
uint32_t hash = String::hash(p_name);
|
||||
|
||||
uint32_t idx = hash & STRING_TABLE_MASK;
|
||||
|
||||
_Data *_data = _table[idx];
|
||||
|
||||
while (_data) {
|
||||
// compare hash first
|
||||
if (_data->hash == hash && _data->get_name() == p_name) {
|
||||
break;
|
||||
}
|
||||
_data = _data->next;
|
||||
}
|
||||
|
||||
if (_data && _data->refcount.ref()) {
|
||||
lock.unlock();
|
||||
|
||||
return StringName(_data);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
return StringName(); //does not exist
|
||||
}
|
||||
|
||||
StringName StringName::search(const wchar_t *p_name) {
|
||||
ERR_FAIL_COND_V(!configured, StringName());
|
||||
|
||||
ERR_FAIL_COND_V(!p_name, StringName());
|
||||
if (!p_name[0]) {
|
||||
return StringName();
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
|
||||
uint32_t hash = String::hash(p_name);
|
||||
|
||||
uint32_t idx = hash & STRING_TABLE_MASK;
|
||||
|
||||
_Data *_data = _table[idx];
|
||||
|
||||
while (_data) {
|
||||
// compare hash first
|
||||
if (_data->hash == hash && _data->get_name() == p_name) {
|
||||
break;
|
||||
}
|
||||
_data = _data->next;
|
||||
}
|
||||
|
||||
if (_data && _data->refcount.ref()) {
|
||||
lock.unlock();
|
||||
return StringName(_data);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
return StringName(); //does not exist
|
||||
}
|
||||
StringName StringName::search(const String &p_name) {
|
||||
ERR_FAIL_COND_V(p_name == "", StringName());
|
||||
|
||||
lock.lock();
|
||||
|
||||
uint32_t hash = p_name.hash();
|
||||
|
||||
uint32_t idx = hash & STRING_TABLE_MASK;
|
||||
|
||||
_Data *_data = _table[idx];
|
||||
|
||||
while (_data) {
|
||||
// compare hash first
|
||||
if (_data->hash == hash && p_name == _data->get_name()) {
|
||||
break;
|
||||
}
|
||||
_data = _data->next;
|
||||
}
|
||||
|
||||
if (_data && _data->refcount.ref()) {
|
||||
lock.unlock();
|
||||
return StringName(_data);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
return StringName(); //does not exist
|
||||
}
|
||||
|
||||
StringName::StringName() {
|
||||
_data = nullptr;
|
||||
}
|
||||
|
||||
StringName::~StringName() {
|
||||
unref();
|
||||
}
|
163
core/string_name.h
Normal file
163
core/string_name.h
Normal file
@ -0,0 +1,163 @@
|
||||
/*************************************************************************/
|
||||
/* string_name.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef STRING_NAME_H
|
||||
#define STRING_NAME_H
|
||||
|
||||
#include "core/threading/mutex.h"
|
||||
#include "core/safe_refcount.h"
|
||||
#include "core/string.h"
|
||||
|
||||
struct StaticCString {
|
||||
const char *ptr;
|
||||
static StaticCString create(const char *p_ptr);
|
||||
};
|
||||
|
||||
class StringName {
|
||||
enum {
|
||||
|
||||
STRING_TABLE_BITS = 12,
|
||||
STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
|
||||
STRING_TABLE_MASK = STRING_TABLE_LEN - 1
|
||||
};
|
||||
|
||||
struct _Data {
|
||||
SafeRefCount refcount;
|
||||
const char *cname;
|
||||
String name;
|
||||
|
||||
String get_name() const { return cname ? String(cname) : name; }
|
||||
int idx;
|
||||
uint32_t hash;
|
||||
_Data *prev;
|
||||
_Data *next;
|
||||
_Data() {
|
||||
cname = nullptr;
|
||||
next = prev = nullptr;
|
||||
idx = 0;
|
||||
hash = 0;
|
||||
}
|
||||
};
|
||||
|
||||
static _Data *_table[STRING_TABLE_LEN];
|
||||
|
||||
_Data *_data;
|
||||
|
||||
union _HashUnion {
|
||||
_Data *ptr;
|
||||
uint32_t hash;
|
||||
};
|
||||
|
||||
void unref();
|
||||
friend void register_core_types();
|
||||
friend void unregister_core_types();
|
||||
|
||||
static Mutex lock;
|
||||
static void setup();
|
||||
static void cleanup();
|
||||
static bool configured;
|
||||
|
||||
StringName(_Data *p_data) { _data = p_data; }
|
||||
|
||||
public:
|
||||
operator const void *() const { return (_data && (_data->cname || !_data->name.empty())) ? (void *)1 : nullptr; }
|
||||
|
||||
bool operator==(const String &p_name) const;
|
||||
bool operator==(const char *p_name) const;
|
||||
bool operator!=(const String &p_name) const;
|
||||
_FORCE_INLINE_ bool operator<(const StringName &p_name) const {
|
||||
return _data < p_name._data;
|
||||
}
|
||||
_FORCE_INLINE_ bool operator==(const StringName &p_name) const {
|
||||
// the real magic of all this mess happens here.
|
||||
// this is why path comparisons are very fast
|
||||
return _data == p_name._data;
|
||||
}
|
||||
_FORCE_INLINE_ uint32_t hash() const {
|
||||
if (_data) {
|
||||
return _data->hash;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
_FORCE_INLINE_ const void *data_unique_pointer() const {
|
||||
return (void *)_data;
|
||||
}
|
||||
bool operator!=(const StringName &p_name) const;
|
||||
|
||||
_FORCE_INLINE_ operator String() const {
|
||||
if (_data) {
|
||||
if (_data->cname) {
|
||||
return String(_data->cname);
|
||||
} else {
|
||||
return _data->name;
|
||||
}
|
||||
}
|
||||
|
||||
return String();
|
||||
}
|
||||
|
||||
static StringName search(const char *p_name);
|
||||
static StringName search(const wchar_t *p_name);
|
||||
static StringName search(const String &p_name);
|
||||
|
||||
struct AlphCompare {
|
||||
_FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
|
||||
const char *l_cname = l._data ? l._data->cname : "";
|
||||
const char *r_cname = r._data ? r._data->cname : "";
|
||||
|
||||
if (l_cname) {
|
||||
if (r_cname) {
|
||||
return is_str_less(l_cname, r_cname);
|
||||
} else {
|
||||
return is_str_less(l_cname, r._data->name.c_str());
|
||||
}
|
||||
} else {
|
||||
if (r_cname) {
|
||||
return is_str_less(l._data->name.c_str(), r_cname);
|
||||
} else {
|
||||
return is_str_less(l._data->name.c_str(), r._data->name.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void operator=(const StringName &p_name);
|
||||
StringName(const char *p_name);
|
||||
StringName(const StringName &p_name);
|
||||
StringName(const String &p_name);
|
||||
StringName(const StaticCString &p_static_string);
|
||||
StringName();
|
||||
~StringName();
|
||||
};
|
||||
|
||||
StringName _scs_create(const char *p_chr);
|
||||
|
||||
#endif // STRING_NAME_H
|
Loading…
Reference in New Issue
Block a user