mirror of
				https://github.com/godotengine/godot.git
				synced 2025-10-31 21:51:22 +00:00 
			
		
		
		
	Always use String as StringName backing internally.
				
					
				
			This commit is contained in:
		
							parent
							
								
									1696ab0cb6
								
							
						
					
					
						commit
						91fe434a86
					
				
					 16 changed files with 245 additions and 383 deletions
				
			
		|  | @ -33,18 +33,8 @@ | |||
| #include "core/os/os.h" | ||||
| #include "core/string/print_string.h" | ||||
| 
 | ||||
| StaticCString StaticCString::create(const char *p_ptr) { | ||||
| 	StaticCString scs; | ||||
| 	scs.ptr = p_ptr; | ||||
| 	return scs; | ||||
| } | ||||
| 
 | ||||
| bool StringName::_Data::operator==(const String &p_name) const { | ||||
| 	if (cname) { | ||||
| 		return p_name == cname; | ||||
| 	} else { | ||||
| 		return name == p_name; | ||||
| 	} | ||||
| 	return name == p_name; | ||||
| } | ||||
| 
 | ||||
| bool StringName::_Data::operator!=(const String &p_name) const { | ||||
|  | @ -52,21 +42,13 @@ bool StringName::_Data::operator!=(const String &p_name) const { | |||
| } | ||||
| 
 | ||||
| bool StringName::_Data::operator==(const char *p_name) const { | ||||
| 	if (cname) { | ||||
| 		return strcmp(cname, p_name) == 0; | ||||
| 	} else { | ||||
| 		return name == p_name; | ||||
| 	} | ||||
| 	return name == p_name; | ||||
| } | ||||
| 
 | ||||
| bool StringName::_Data::operator!=(const char *p_name) const { | ||||
| 	return !operator==(p_name); | ||||
| } | ||||
| 
 | ||||
| StringName _scs_create(const char *p_chr, bool p_static) { | ||||
| 	return (p_chr[0] ? StringName(StaticCString::create(p_chr), p_static) : StringName()); | ||||
| } | ||||
| 
 | ||||
| void StringName::setup() { | ||||
| 	ERR_FAIL_COND(configured); | ||||
| 	for (int i = 0; i < STRING_TABLE_LEN; i++) { | ||||
|  | @ -115,9 +97,7 @@ void StringName::cleanup() { | |||
| 				lost_strings++; | ||||
| 
 | ||||
| 				if (OS::get_singleton()->is_stdout_verbose()) { | ||||
| 					String dname = String(d->cname ? d->cname : d->name); | ||||
| 
 | ||||
| 					print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", dname, d->static_count.get(), d->refcount.get())); | ||||
| 					print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", d->name, d->static_count.get(), d->refcount.get())); | ||||
| 				} | ||||
| 			} | ||||
| 
 | ||||
|  | @ -138,11 +118,7 @@ void StringName::unref() { | |||
| 		MutexLock lock(mutex); | ||||
| 
 | ||||
| 		if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) { | ||||
| 			if (_data->cname) { | ||||
| 				ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->cname)); | ||||
| 			} else { | ||||
| 				ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->name)); | ||||
| 			} | ||||
| 			ERR_PRINT("BUG: Unreferenced static string to 0: " + _data->name); | ||||
| 		} | ||||
| 		if (_data->prev) { | ||||
| 			_data->prev->next = _data->next; | ||||
|  | @ -193,12 +169,7 @@ bool StringName::operator!=(const char *p_name) const { | |||
| 
 | ||||
| char32_t StringName::operator[](int p_index) const { | ||||
| 	if (_data) { | ||||
| 		if (_data->cname) { | ||||
| 			CRASH_BAD_INDEX(p_index, static_cast<long>(strlen(_data->cname))); | ||||
| 			return _data->cname[p_index]; | ||||
| 		} else { | ||||
| 			return _data->name[p_index]; | ||||
| 		} | ||||
| 		return _data->name[p_index]; | ||||
| 	} | ||||
| 
 | ||||
| 	CRASH_BAD_INDEX(p_index, 0); | ||||
|  | @ -207,11 +178,7 @@ char32_t StringName::operator[](int p_index) const { | |||
| 
 | ||||
| int StringName::length() const { | ||||
| 	if (_data) { | ||||
| 		if (_data->cname) { | ||||
| 			return strlen(_data->cname); | ||||
| 		} else { | ||||
| 			return _data->name.length(); | ||||
| 		} | ||||
| 		return _data->name.length(); | ||||
| 	} | ||||
| 
 | ||||
| 	return 0; | ||||
|  | @ -219,11 +186,7 @@ int StringName::length() const { | |||
| 
 | ||||
| bool StringName::is_empty() const { | ||||
| 	if (_data) { | ||||
| 		if (_data->cname) { | ||||
| 			return _data->cname[0] == 0; | ||||
| 		} else { | ||||
| 			return _data->name.is_empty(); | ||||
| 		} | ||||
| 		return _data->name.is_empty(); | ||||
| 	} | ||||
| 
 | ||||
| 	return true; | ||||
|  | @ -302,7 +265,6 @@ StringName::StringName(const char *p_name, bool p_static) { | |||
| 	_data->static_count.set(p_static ? 1 : 0); | ||||
| 	_data->hash = hash; | ||||
| 	_data->idx = idx; | ||||
| 	_data->cname = nullptr; | ||||
| 	_data->next = _table[idx]; | ||||
| 	_data->prev = nullptr; | ||||
| 
 | ||||
|  | @ -319,62 +281,6 @@ StringName::StringName(const char *p_name, bool p_static) { | |||
| 	_table[idx] = _data; | ||||
| } | ||||
| 
 | ||||
| StringName::StringName(const StaticCString &p_static_string, bool p_static) { | ||||
| 	_data = nullptr; | ||||
| 
 | ||||
| 	ERR_FAIL_COND(!configured); | ||||
| 
 | ||||
| 	ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]); | ||||
| 
 | ||||
| 	const uint32_t hash = String::hash(p_static_string.ptr); | ||||
| 	const uint32_t idx = hash & STRING_TABLE_MASK; | ||||
| 
 | ||||
| 	MutexLock lock(mutex); | ||||
| 	_data = _table[idx]; | ||||
| 
 | ||||
| 	while (_data) { | ||||
| 		// compare hash first
 | ||||
| 		if (_data->hash == hash && _data->operator==(p_static_string.ptr)) { | ||||
| 			break; | ||||
| 		} | ||||
| 		_data = _data->next; | ||||
| 	} | ||||
| 
 | ||||
| 	if (_data && _data->refcount.ref()) { | ||||
| 		// exists
 | ||||
| 		if (p_static) { | ||||
| 			_data->static_count.increment(); | ||||
| 		} | ||||
| #ifdef DEBUG_ENABLED | ||||
| 		if (unlikely(debug_stringname)) { | ||||
| 			_data->debug_references++; | ||||
| 		} | ||||
| #endif | ||||
| 		return; | ||||
| 	} | ||||
| 
 | ||||
| 	_data = memnew(_Data); | ||||
| 
 | ||||
| 	_data->refcount.init(); | ||||
| 	_data->static_count.set(p_static ? 1 : 0); | ||||
| 	_data->hash = hash; | ||||
| 	_data->idx = idx; | ||||
| 	_data->cname = p_static_string.ptr; | ||||
| 	_data->next = _table[idx]; | ||||
| 	_data->prev = nullptr; | ||||
| #ifdef DEBUG_ENABLED | ||||
| 	if (unlikely(debug_stringname)) { | ||||
| 		// Keep in memory, force static.
 | ||||
| 		_data->refcount.ref(); | ||||
| 		_data->static_count.increment(); | ||||
| 	} | ||||
| #endif | ||||
| 	if (_table[idx]) { | ||||
| 		_table[idx]->prev = _data; | ||||
| 	} | ||||
| 	_table[idx] = _data; | ||||
| } | ||||
| 
 | ||||
| StringName::StringName(const String &p_name, bool p_static) { | ||||
| 	_data = nullptr; | ||||
| 
 | ||||
|  | @ -416,7 +322,6 @@ StringName::StringName(const String &p_name, bool p_static) { | |||
| 	_data->static_count.set(p_static ? 1 : 0); | ||||
| 	_data->hash = hash; | ||||
| 	_data->idx = idx; | ||||
| 	_data->cname = nullptr; | ||||
| 	_data->next = _table[idx]; | ||||
| 	_data->prev = nullptr; | ||||
| #ifdef DEBUG_ENABLED | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lukas Tenbrink
						Lukas Tenbrink