virtualx-engine/core/string/node_path.cpp

452 lines
11 KiB
C++
Raw Normal View History

/**************************************************************************/
/* node_path.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2017-08-05 14:12:53 +02:00
#include "node_path.h"
2014-02-10 02:10:30 +01:00
#include "core/string/print_string.h"
2014-02-10 02:10:30 +01:00
void NodePath::_update_hash_cache() const {
2014-02-10 02:10:30 +01:00
uint32_t h = data->absolute ? 1 : 0;
int pc = data->path.size();
const StringName *sn = data->path.ptr();
for (int i = 0; i < pc; i++) {
2014-02-10 02:10:30 +01:00
h = h ^ sn[i].hash();
}
int spc = data->subpath.size();
const StringName *ssn = data->subpath.ptr();
for (int i = 0; i < spc; i++) {
2014-02-10 02:10:30 +01:00
h = h ^ ssn[i].hash();
}
data->hash_cache_valid = true;
data->hash_cache = h;
2014-02-10 02:10:30 +01:00
}
void NodePath::prepend_period() {
if (data->path.size() && data->path[0].operator String() != ".") {
data->path.insert(0, ".");
data->hash_cache_valid = false;
}
}
2014-02-10 02:10:30 +01:00
bool NodePath::is_absolute() const {
if (!data) {
2014-02-10 02:10:30 +01:00
return false;
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return data->absolute;
}
2014-02-10 02:10:30 +01:00
int NodePath::get_name_count() const {
if (!data) {
2014-02-10 02:10:30 +01:00
return 0;
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return data->path.size();
}
2014-02-10 02:10:30 +01:00
StringName NodePath::get_name(int p_idx) const {
ERR_FAIL_COND_V(!data, StringName());
ERR_FAIL_INDEX_V(p_idx, data->path.size(), StringName());
2014-02-10 02:10:30 +01:00
return data->path[p_idx];
}
int NodePath::get_subname_count() const {
if (!data) {
2014-02-10 02:10:30 +01:00
return 0;
}
2014-02-10 02:10:30 +01:00
return data->subpath.size();
}
2014-02-10 02:10:30 +01:00
StringName NodePath::get_subname(int p_idx) const {
ERR_FAIL_COND_V(!data, StringName());
ERR_FAIL_INDEX_V(p_idx, data->subpath.size(), StringName());
2014-02-10 02:10:30 +01:00
return data->subpath[p_idx];
}
void NodePath::unref() {
if (data && data->refcount.unref()) {
memdelete(data);
}
2020-04-02 01:20:12 +02:00
data = nullptr;
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
bool NodePath::operator==(const NodePath &p_path) const {
if (data == p_path.data) {
2014-02-10 02:10:30 +01:00
return true;
}
2016-03-09 00:00:52 +01:00
if (!data || !p_path.data) {
2014-02-10 02:10:30 +01:00
return false;
}
2016-03-09 00:00:52 +01:00
if (data->absolute != p_path.data->absolute) {
2014-02-10 02:10:30 +01:00
return false;
}
2016-03-09 00:00:52 +01:00
int path_size = data->path.size();
if (path_size != p_path.data->path.size()) {
2014-02-10 02:10:30 +01:00
return false;
}
int subpath_size = data->subpath.size();
2014-02-10 02:10:30 +01:00
if (subpath_size != p_path.data->subpath.size()) {
2014-02-10 02:10:30 +01:00
return false;
}
2014-02-10 02:10:30 +01:00
const StringName *l_path_ptr = data->path.ptr();
const StringName *r_path_ptr = p_path.data->path.ptr();
for (int i = 0; i < path_size; i++) {
if (l_path_ptr[i] != r_path_ptr[i]) {
2014-02-10 02:10:30 +01:00
return false;
}
2014-02-10 02:10:30 +01:00
}
const StringName *l_subpath_ptr = data->subpath.ptr();
const StringName *r_subpath_ptr = p_path.data->subpath.ptr();
for (int i = 0; i < subpath_size; i++) {
if (l_subpath_ptr[i] != r_subpath_ptr[i]) {
2014-02-10 02:10:30 +01:00
return false;
}
2014-02-10 02:10:30 +01:00
}
return true;
}
bool NodePath::operator!=(const NodePath &p_path) const {
2014-02-10 02:10:30 +01:00
return (!(*this == p_path));
}
2016-03-09 00:00:52 +01:00
void NodePath::operator=(const NodePath &p_path) {
if (this == &p_path) {
2014-02-10 02:10:30 +01:00
return;
}
2014-02-10 02:10:30 +01:00
unref();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (p_path.data && p_path.data->refcount.ref()) {
data = p_path.data;
2014-02-10 02:10:30 +01:00
}
}
NodePath::operator String() const {
if (!data) {
2014-02-10 02:10:30 +01:00
return String();
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
String ret;
if (data->absolute) {
ret = "/";
}
2016-03-09 00:00:52 +01:00
for (int i = 0; i < data->path.size(); i++) {
if (i > 0) {
ret += "/";
}
ret += data->path[i].operator String();
2014-02-10 02:10:30 +01:00
}
for (int i = 0; i < data->subpath.size(); i++) {
ret += ":" + data->subpath[i].operator String();
2014-02-10 02:10:30 +01:00
}
return ret;
}
Vector<StringName> NodePath::get_names() const {
if (data) {
2014-02-10 02:10:30 +01:00
return data->path;
}
2014-02-10 02:10:30 +01:00
return Vector<StringName>();
}
Vector<StringName> NodePath::get_subnames() const {
if (data) {
2014-02-10 02:10:30 +01:00
return data->subpath;
}
2014-02-10 02:10:30 +01:00
return Vector<StringName>();
}
2022-06-27 10:06:50 +02:00
StringName NodePath::get_concatenated_names() const {
ERR_FAIL_COND_V(!data, StringName());
if (!data->concatenated_path) {
int pc = data->path.size();
String concatenated;
const StringName *sn = data->path.ptr();
for (int i = 0; i < pc; i++) {
concatenated += i == 0 ? sn[i].operator String() : "/" + sn[i];
}
data->concatenated_path = concatenated;
}
return data->concatenated_path;
}
StringName NodePath::get_concatenated_subnames() const {
ERR_FAIL_COND_V(!data, StringName());
if (!data->concatenated_subpath) {
int spc = data->subpath.size();
String concatenated;
const StringName *ssn = data->subpath.ptr();
for (int i = 0; i < spc; i++) {
concatenated += i == 0 ? ssn[i].operator String() : ":" + ssn[i];
}
data->concatenated_subpath = concatenated;
}
return data->concatenated_subpath;
}
NodePath NodePath::rel_path_to(const NodePath &p_np) const {
ERR_FAIL_COND_V(!is_absolute(), NodePath());
ERR_FAIL_COND_V(!p_np.is_absolute(), NodePath());
2014-02-10 02:10:30 +01:00
Vector<StringName> src_dirs = get_names();
Vector<StringName> dst_dirs = p_np.get_names();
//find common parent
int common_parent = 0;
2014-02-10 02:10:30 +01:00
while (true) {
if (src_dirs.size() == common_parent) {
2014-02-10 02:10:30 +01:00
break;
}
if (dst_dirs.size() == common_parent) {
2014-02-10 02:10:30 +01:00
break;
}
if (src_dirs[common_parent] != dst_dirs[common_parent]) {
2014-02-10 02:10:30 +01:00
break;
}
2014-02-10 02:10:30 +01:00
common_parent++;
}
common_parent--;
Vector<StringName> relpath;
relpath.resize(src_dirs.size() + dst_dirs.size() + 1);
2014-02-10 02:10:30 +01:00
StringName *relpath_ptr = relpath.ptrw();
int path_size = 0;
StringName back_str("..");
for (int i = common_parent + 1; i < src_dirs.size(); i++) {
relpath_ptr[path_size++] = back_str;
2014-02-10 02:10:30 +01:00
}
for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
relpath_ptr[path_size++] = dst_dirs[i];
2014-02-10 02:10:30 +01:00
}
if (path_size == 0) {
relpath_ptr[path_size++] = ".";
}
2014-02-10 02:10:30 +01:00
relpath.resize(path_size);
return NodePath(relpath, p_np.get_subnames(), false);
}
NodePath NodePath::get_as_property_path() const {
if (!data || !data->path.size()) {
return *this;
} else {
Vector<StringName> new_path = data->subpath;
String initial_subname = data->path[0];
Fixes logically dead code (Coverity) Fixes reported logically dead codes by Coverity * image.cpp: Doesn't really need any modification. But to remove the bug report then we have to move the MAX call away from the for loop statement. * rasterizer_gles3.cpp: Removes unnecessary elif condition since it is checked earlier in the function * collada.cpp: If stamement never reached due to macro ERR_CONTINUE does the same. * navigation_mesh.cpp: Variables should always be null - however, also checked for the very same condition in their function call. Leaving this for review (whether the function call is necessary or not) * path_editor_plugin.cpp: If cancel is true, then it should restore the edited value to the original provided. http://docs.godotengine.org/en/3.0/classes/class_editorspatialgizmo.html#class-editorspatialgizmo-commit-handle * spatial_editor_gizmos.cpp: the very condition of i >= 3 is predetermined in the if case right before it. Thus case 1 is always '1' and case 2 is always '-1' * grid_map_editor.cpp: Same as above in spatial_editor_gizmos.cpp * voxel_light_baker.cpp: Same as above in spatial_editor_gizmos.cpp * visual_server.cpp: Same as above in spatial_editor_gizmos.cpp * visual_script_expression.cpp: char '-' is already true in the switch case mechanism. Thus it can never reach to default case. * particles.cpp: Case 'PARAM_MAX' is unreachable due to index checking right before the switch execution. * shader_language.cpp: Invalid index is handled in switch default case. `type < TYPE_FLOAT && type > TYPE_VEC4` -> `(type < TYPE_FLOAT || type > TYPE_VEC4`) Fixes the "always false problem" in TODO comment.
2018-04-21 16:35:23 +02:00
Fix warnings for comparison between signed and unsigned integers [-Wsign-compare] Also turn off -Wsign-compare warnings in the future, we do not consider them important. Fixes the following GCC 5 warnings: ``` core/node_path.cpp:279:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/oa_hash_map.h:169:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/oa_hash_map.h:314:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles2/shader_gles2.cpp:985:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles3/rasterizer_storage_gles3.cpp:1075:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/pulseaudio/audio_driver_pulseaudio.cpp:343:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/editor_plugin.cpp:525:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/editor_properties_array_dict.cpp:747:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/spatial_editor_plugin.cpp:2078:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/spatial_editor_plugin.cpp:4096:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/sprite_editor_plugin.cpp:100:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:122:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:134:77: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:339:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/etc/image_etc.cpp:222:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/gdnative/register_types.cpp:242:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/gdnative/register_types.cpp:258:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:200:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:222:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:246:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1085:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1489:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1623:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:206:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:356:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:406:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:493:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/audio_stream_player_3d.cpp:420:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/audio_stream_sample.cpp:565:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/audio_stream_sample.cpp:571:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_rb_resampler.cpp:156:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] ``` The following warnings were not fixed, as they implied casting for no gain: ``` core/io/packet_peer.cpp:228:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/io/resource_format_binary.cpp:109:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles2/rasterizer_scene_gles2.cpp:144:57: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/unix/file_access_unix.cpp:249:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:889:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:1020:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:1154:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:2255:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/bit_mask.cpp:336:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:141:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:150:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:154:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio_server.cpp:86:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio_server.cpp:89:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] ```
2018-09-26 17:38:02 +02:00
for (int i = 1; i < data->path.size(); i++) {
Fixes logically dead code (Coverity) Fixes reported logically dead codes by Coverity * image.cpp: Doesn't really need any modification. But to remove the bug report then we have to move the MAX call away from the for loop statement. * rasterizer_gles3.cpp: Removes unnecessary elif condition since it is checked earlier in the function * collada.cpp: If stamement never reached due to macro ERR_CONTINUE does the same. * navigation_mesh.cpp: Variables should always be null - however, also checked for the very same condition in their function call. Leaving this for review (whether the function call is necessary or not) * path_editor_plugin.cpp: If cancel is true, then it should restore the edited value to the original provided. http://docs.godotengine.org/en/3.0/classes/class_editorspatialgizmo.html#class-editorspatialgizmo-commit-handle * spatial_editor_gizmos.cpp: the very condition of i >= 3 is predetermined in the if case right before it. Thus case 1 is always '1' and case 2 is always '-1' * grid_map_editor.cpp: Same as above in spatial_editor_gizmos.cpp * voxel_light_baker.cpp: Same as above in spatial_editor_gizmos.cpp * visual_server.cpp: Same as above in spatial_editor_gizmos.cpp * visual_script_expression.cpp: char '-' is already true in the switch case mechanism. Thus it can never reach to default case. * particles.cpp: Case 'PARAM_MAX' is unreachable due to index checking right before the switch execution. * shader_language.cpp: Invalid index is handled in switch default case. `type < TYPE_FLOAT && type > TYPE_VEC4` -> `(type < TYPE_FLOAT || type > TYPE_VEC4`) Fixes the "always false problem" in TODO comment.
2018-04-21 16:35:23 +02:00
initial_subname += "/" + data->path[i];
}
new_path.insert(0, initial_subname);
return NodePath(Vector<StringName>(), new_path, false);
}
2014-02-10 02:10:30 +01:00
}
bool NodePath::is_empty() const {
return !data;
2014-02-10 02:10:30 +01:00
}
void NodePath::simplify() {
if (!data) {
return;
}
for (int i = 0; i < data->path.size(); i++) {
if (data->path.size() == 1) {
break;
}
if (data->path[i].operator String() == ".") {
data->path.remove_at(i);
i--;
} else if (i > 0 && data->path[i].operator String() == ".." && data->path[i - 1].operator String() != "." && data->path[i - 1].operator String() != "..") {
//remove both
data->path.remove_at(i - 1);
data->path.remove_at(i - 1);
i -= 2;
if (data->path.size() == 0) {
data->path.push_back(".");
break;
}
}
}
data->hash_cache_valid = false;
}
NodePath NodePath::simplified() const {
NodePath np = *this;
np.simplify();
return np;
}
NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
if (p_path.size() == 0) {
return;
}
2014-02-10 02:10:30 +01:00
data = memnew(Data);
data->refcount.init();
data->absolute = p_absolute;
data->path = p_path;
data->has_slashes = true;
data->hash_cache_valid = false;
}
NodePath::NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute) {
if (p_path.size() == 0 && p_subpath.size() == 0) {
return;
}
2014-02-10 02:10:30 +01:00
data = memnew(Data);
data->refcount.init();
data->absolute = p_absolute;
data->path = p_path;
data->subpath = p_subpath;
data->has_slashes = true;
data->hash_cache_valid = false;
}
NodePath::NodePath(const NodePath &p_path) {
if (p_path.data && p_path.data->refcount.ref()) {
data = p_path.data;
}
}
NodePath::NodePath(const String &p_path) {
if (p_path.length() == 0) {
2014-02-10 02:10:30 +01:00
return;
}
2014-02-10 02:10:30 +01:00
String path = p_path;
2014-02-10 02:10:30 +01:00
Vector<StringName> subpath;
2016-03-09 00:00:52 +01:00
2019-06-26 15:08:25 +02:00
bool absolute = (path[0] == '/');
bool last_is_slash = true;
bool has_slashes = false;
int slices = 0;
int subpath_pos = path.find(":");
2014-02-10 02:10:30 +01:00
if (subpath_pos != -1) {
int from = subpath_pos + 1;
2016-03-09 00:00:52 +01:00
for (int i = from; i <= path.length(); i++) {
if (path[i] == ':' || path[i] == 0) {
String str = path.substr(from, i - from);
if (str.is_empty()) {
if (path[i] == 0) {
continue; // Allow end-of-path :
}
2019-09-25 10:28:50 +02:00
ERR_FAIL_MSG("Invalid NodePath '" + p_path + "'.");
2014-02-10 02:10:30 +01:00
}
subpath.push_back(str);
2014-02-10 02:10:30 +01:00
from = i + 1;
2014-02-10 02:10:30 +01:00
}
}
path = path.substr(0, subpath_pos);
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2019-06-26 15:08:25 +02:00
for (int i = (int)absolute; i < path.length(); i++) {
if (path[i] == '/') {
last_is_slash = true;
has_slashes = true;
2014-02-10 02:10:30 +01:00
} else {
if (last_is_slash) {
2014-02-10 02:10:30 +01:00
slices++;
}
2016-03-09 00:00:52 +01:00
last_is_slash = false;
2014-02-10 02:10:30 +01:00
}
}
2016-03-09 00:00:52 +01:00
if (slices == 0 && !absolute && !subpath.size()) {
2014-02-10 02:10:30 +01:00
return;
}
2016-03-09 00:00:52 +01:00
data = memnew(Data);
2014-02-10 02:10:30 +01:00
data->refcount.init();
2019-06-26 15:08:25 +02:00
data->absolute = absolute;
data->has_slashes = has_slashes;
data->subpath = subpath;
data->hash_cache_valid = false;
2016-03-09 00:00:52 +01:00
if (slices == 0) {
2014-02-10 02:10:30 +01:00
return;
}
2016-03-09 00:00:52 +01:00
data->path.resize(slices);
last_is_slash = true;
2019-06-26 15:08:25 +02:00
int from = (int)absolute;
int slice = 0;
2016-03-09 00:00:52 +01:00
2019-06-26 15:08:25 +02:00
for (int i = (int)absolute; i < path.length() + 1; i++) {
if (path[i] == '/' || path[i] == 0) {
2014-02-10 02:10:30 +01:00
if (!last_is_slash) {
String name = path.substr(from, i - from);
ERR_FAIL_INDEX(slice, data->path.size());
data->path.write[slice++] = name;
2014-02-10 02:10:30 +01:00
}
from = i + 1;
last_is_slash = true;
2014-02-10 02:10:30 +01:00
} else {
last_is_slash = false;
2014-02-10 02:10:30 +01:00
}
}
}
NodePath::~NodePath() {
unref();
}