Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit f8f19b3

Browse files
authored
Merge pull request godotengine#57562 from AnilBK/string-add-contains
String: Add contains().
2 parents 025e778 + adbe948 commit f8f19b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+142
-119
lines changed

core/debugger/remote_debugger_peer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {
226226
String debug_host = p_uri.replace("tcp://", "");
227227
uint16_t debug_port = 6007;
228228

229-
if (debug_host.find(":") != -1) {
229+
if (debug_host.contains(":")) {
230230
int sep_pos = debug_host.rfind(":");
231231
debug_port = debug_host.substr(sep_pos + 1).to_int();
232232
debug_host = debug_host.substr(0, sep_pos);

core/io/dir_access.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Error DirAccess::make_dir_recursive(String p_dir) {
159159
base = full_dir.substr(0, pos + 1);
160160
} else if (full_dir.begins_with("/")) {
161161
base = "/";
162-
} else if (full_dir.find(":/") != -1) {
162+
} else if (full_dir.contains(":/")) {
163163
base = full_dir.substr(0, full_dir.find(":/") + 2);
164164
} else {
165165
ERR_FAIL_V(ERR_INVALID_PARAMETER);

core/io/file_access.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_
538538
for (int i = 0; i < size; ++i) {
539539
String value = p_values[i];
540540

541-
if (value.find("\"") != -1 || value.find(p_delim) != -1 || value.find("\n") != -1) {
541+
if (value.contains("\"") || value.contains(p_delim) || value.contains("\n")) {
542542
value = "\"" + value.replace("\"", "\"\"") + "\"";
543543
}
544544
if (i < size - 1) {

core/io/file_access_pack.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void PackedData::add_path(const String &p_pkg_path, const String &p_path, uint64
7070
String p = p_path.replace_first("res://", "");
7171
PackedDir *cd = root;
7272

73-
if (p.find("/") != -1) { //in a subdir
73+
if (p.contains("/")) { //in a subdir
7474

7575
Vector<String> ds = p.get_base_dir().split("/");
7676

core/io/resource.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Resource : public RefCounted {
105105

106106
virtual void set_path(const String &p_path, bool p_take_over = false);
107107
String get_path() const;
108-
_FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.find("::") != -1 || path_cache.begins_with("local://"); }
108+
_FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.contains("::") || path_cache.begins_with("local://"); }
109109

110110
static String generate_scene_unique_id();
111111
void set_scene_unique_id(const String &p_id);

core/io/resource_format_binary.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) {
335335
String exttype = get_unicode_string();
336336
String path = get_unicode_string();
337337

338-
if (path.find("://") == -1 && path.is_relative_path()) {
338+
if (!path.contains("://") && path.is_relative_path()) {
339339
// path is relative to file being loaded, so convert to a resource path
340340
path = ProjectSettings::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path));
341341
}
@@ -626,7 +626,7 @@ Error ResourceLoaderBinary::load() {
626626
path = remaps[path];
627627
}
628628

629-
if (path.find("://") == -1 && path.is_relative_path()) {
629+
if (!path.contains("://") && path.is_relative_path()) {
630630
// path is relative to file being loaded, so convert to a resource path
631631
path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().plus_file(external_resources[i].path));
632632
}

core/io/translation_loader_po.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
179179
}
180180

181181
if (l.is_empty() || l.begins_with("#")) {
182-
if (l.find("fuzzy") != -1) {
182+
if (l.contains("fuzzy")) {
183183
skip_next = true;
184184
}
185185
line++;

core/object/class_db.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ void ClassDB::bind_integer_constant(const StringName &p_class, const StringName
732732

733733
String enum_name = p_enum;
734734
if (!enum_name.is_empty()) {
735-
if (enum_name.find(".") != -1) {
735+
if (enum_name.contains(".")) {
736736
enum_name = enum_name.get_slicec('.', 1);
737737
}
738738

core/string/ustring.h

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ class String {
394394
Vector<uint8_t> sha256_buffer() const;
395395

396396
_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
397+
_FORCE_INLINE_ bool contains(const char *p_str) const { return find(p_str) != -1; }
398+
_FORCE_INLINE_ bool contains(const String &p_str) const { return find(p_str) != -1; }
397399

398400
// path functions
399401
bool is_absolute_path() const;

core/variant/variant_call.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ static void _register_variant_builtin_methods() {
14211421
bind_method(String, sha1_buffer, sarray(), varray());
14221422
bind_method(String, sha256_buffer, sarray(), varray());
14231423
bind_method(String, is_empty, sarray(), varray());
1424+
bind_methodv(String, contains, static_cast<bool (String::*)(const String &) const>(&String::contains), sarray("what"), varray());
14241425

14251426
bind_method(String, is_absolute_path, sarray(), varray());
14261427
bind_method(String, is_relative_path, sarray(), varray());

core/variant/variant_parser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
14951495
case Variant::FLOAT: {
14961496
String s = rtos_fix(p_variant.operator double());
14971497
if (s != "inf" && s != "inf_neg" && s != "nan") {
1498-
if (s.find(".") == -1 && s.find("e") == -1) {
1498+
if (!s.contains(".") && !s.contains("e")) {
14991499
s += ".0";
15001500
}
15011501
}

doc/classes/String.xml

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
<description>
104104
</description>
105105
</method>
106+
<method name="contains" qualifiers="const">
107+
<return type="bool" />
108+
<argument index="0" name="what" type="String" />
109+
<description>
110+
Returns [code]true[/code] if the string contains the given string.
111+
</description>
112+
</method>
106113
<method name="count" qualifiers="const">
107114
<return type="int" />
108115
<argument index="0" name="what" type="String" />

editor/array_property_edit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
121121
}
122122

123123
} else if (pn.begins_with("indices")) {
124-
if (pn.find("_") != -1) {
124+
if (pn.contains("_")) {
125125
//type
126126
int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
127127

@@ -178,7 +178,7 @@ bool ArrayPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
178178
return true;
179179
}
180180
} else if (pn.begins_with("indices")) {
181-
if (pn.find("_") != -1) {
181+
if (pn.contains("_")) {
182182
//type
183183
int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
184184
bool valid;

editor/connections_dialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void ConnectDialog::set_dst_node(Node *p_node) {
310310

311311
StringName ConnectDialog::get_dst_method_name() const {
312312
String txt = dst_method->get_text();
313-
if (txt.find("(") != -1) {
313+
if (txt.contains("(")) {
314314
txt = txt.left(txt.find("(")).strip_edges();
315315
}
316316
return txt;

editor/debugger/editor_debugger_inspector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
155155
if (pinfo.type == Variant::OBJECT) {
156156
if (var.get_type() == Variant::STRING) {
157157
String path = var;
158-
if (path.find("::") != -1) {
158+
if (path.contains("::")) {
159159
// built-in resource
160160
String base_path = path.get_slice("::", 0);
161161
RES dependency = ResourceLoader::load(base_path);

editor/dependency_editor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void DependencyEditor::_update_list() {
171171
String path;
172172
String type;
173173

174-
if (n.find("::") != -1) {
174+
if (n.contains("::")) {
175175
path = n.get_slice("::", 0);
176176
type = n.get_slice("::", 1);
177177
} else {

editor/editor_feature_profile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ void EditorFeatureProfileManager::_erase_selected_profile() {
462462

463463
void EditorFeatureProfileManager::_create_new_profile() {
464464
String name = new_profile_name->get_text().strip_edges();
465-
if (!name.is_valid_filename() || name.find(".") != -1) {
465+
if (!name.is_valid_filename() || name.contains(".")) {
466466
EditorNode::get_singleton()->show_warning(TTR("Profile must be a valid filename and must not contain '.'"));
467467
return;
468468
}

editor/editor_help.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void EditorHelp::_class_desc_select(const String &p_select) {
8181
if (p_select.begins_with("$")) { //enum
8282
String select = p_select.substr(1, p_select.length());
8383
String class_name;
84-
if (select.find(".") != -1) {
84+
if (select.contains(".")) {
8585
class_name = select.get_slice(".", 0);
8686
select = select.get_slice(".", 1);
8787
} else {
@@ -123,7 +123,7 @@ void EditorHelp::_class_desc_select(const String &p_select) {
123123
return;
124124
}
125125

126-
if (link.find(".") != -1) {
126+
if (link.contains(".")) {
127127
emit_signal(SNAME("go_to_help"), topic + ":" + link.get_slice(".", 0) + ":" + link.get_slice(".", 1));
128128
} else {
129129
if (table->has(link)) {
@@ -185,7 +185,7 @@ void EditorHelp::_add_type(const String &p_type, const String &p_enum) {
185185
if (t.is_empty()) {
186186
t = "void";
187187
}
188-
bool can_ref = (t != "void" && t.find("*") == -1) || !p_enum.is_empty();
188+
bool can_ref = (t != "void" && !t.contains("*")) || !p_enum.is_empty();
189189

190190
if (!p_enum.is_empty()) {
191191
if (p_enum.get_slice_count(".") > 1) {
@@ -240,7 +240,7 @@ String EditorHelp::_fix_constant(const String &p_constant) const {
240240
void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview) {
241241
method_line[p_method.name] = class_desc->get_line_count() - 2; //gets overridden if description
242242

243-
const bool is_vararg = p_method.qualifiers.find("vararg") != -1;
243+
const bool is_vararg = p_method.qualifiers.contains("vararg");
244244

245245
if (p_overview) {
246246
class_desc->push_cell();
@@ -364,7 +364,7 @@ void EditorHelp::_update_method_list(const Vector<DocData::MethodDoc> p_methods,
364364

365365
for (int i = 0; i < p_methods.size(); i++) {
366366
const String &q = p_methods[i].qualifiers;
367-
if ((pass == 0 && q.find("virtual") != -1) || (pass == 1 && q.find("virtual") == -1)) {
367+
if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
368368
m.push_back(p_methods[i]);
369369
}
370370
}
@@ -429,7 +429,7 @@ void EditorHelp::_update_method_descriptions(const DocData::ClassDoc p_classdoc,
429429

430430
for (int i = 0; i < p_methods.size(); i++) {
431431
const String &q = p_methods[i].qualifiers;
432-
if ((pass == 0 && q.find("virtual") != -1) || (pass == 1 && q.find("virtual") == -1)) {
432+
if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
433433
methods_filtered.push_back(p_methods[i]);
434434
}
435435
}
@@ -820,7 +820,7 @@ void EditorHelp::_update_doc() {
820820
}
821821
}
822822
// Ignore undocumented non virtual private.
823-
if (cd.methods[i].name.begins_with("_") && cd.methods[i].description.is_empty() && cd.methods[i].qualifiers.find("virtual") == -1) {
823+
if (cd.methods[i].name.begins_with("_") && cd.methods[i].description.is_empty() && !cd.methods[i].qualifiers.contains("virtual")) {
824824
continue;
825825
}
826826
methods.push_back(cd.methods[i]);
@@ -1923,7 +1923,7 @@ void EditorHelpBit::_meta_clicked(String p_select) {
19231923

19241924
String select = p_select.substr(1, p_select.length());
19251925
String class_name;
1926-
if (select.find(".") != -1) {
1926+
if (select.contains(".")) {
19271927
class_name = select.get_slice(".", 0);
19281928
} else {
19291929
class_name = "@Global";
@@ -1936,7 +1936,7 @@ void EditorHelpBit::_meta_clicked(String p_select) {
19361936
} else if (p_select.begins_with("@")) {
19371937
String m = p_select.substr(1, p_select.length());
19381938

1939-
if (m.find(".") != -1) {
1939+
if (m.contains(".")) {
19401940
_go_to_help("class_method:" + m.get_slice(".", 0) + ":" + m.get_slice(".", 0)); //must go somewhere else
19411941
}
19421942
}

editor/editor_inspector.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void EditorProperty::_notification(int p_what) {
251251
} else {
252252
color = get_theme_color(is_read_only() ? SNAME("readonly_color") : SNAME("property_color"));
253253
}
254-
if (label.find(".") != -1) {
254+
if (label.contains(".")) {
255255
// FIXME: Move this to the project settings editor, as this is only used
256256
// for project settings feature tag overrides.
257257
color.a = 0.5;
@@ -2566,7 +2566,7 @@ void EditorInspector::update_tree() {
25662566
}
25672567

25682568
// Get the property label's string.
2569-
String property_label_string = (path.find("/") != -1) ? path.substr(path.rfind("/") + 1) : path;
2569+
String property_label_string = (path.contains("/")) ? path.substr(path.rfind("/") + 1) : path;
25702570
if (capitalize_paths) {
25712571
// Capitalize paths.
25722572
int dot = property_label_string.find(".");
@@ -2590,7 +2590,7 @@ void EditorInspector::update_tree() {
25902590

25912591
// Ignore properties that do not fit the filter.
25922592
if (use_filter && !filter.is_empty()) {
2593-
if (!filter.is_subsequence_ofn(path) && !filter.is_subsequence_ofn(property_label_string) && property_prefix.to_lower().find(filter.to_lower()) == -1) {
2593+
if (!filter.is_subsequence_ofn(path) && !filter.is_subsequence_ofn(property_label_string) && !property_prefix.to_lower().contains(filter.to_lower())) {
25942594
continue;
25952595
}
25962596
}
@@ -2664,7 +2664,7 @@ void EditorInspector::update_tree() {
26642664
array_element_prefix = p.class_name;
26652665
editor_inspector_array = memnew(EditorInspectorArray);
26662666

2667-
String array_label = (path.find("/") != -1) ? path.substr(path.rfind("/") + 1) : path;
2667+
String array_label = path.contains("/") ? path.substr(path.rfind("/") + 1) : path;
26682668
array_label = property_label_string.capitalize();
26692669
int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
26702670
editor_inspector_array->setup_with_move_element_function(object, array_label, array_element_prefix, page, c, use_folding);

editor/editor_properties.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ void EditorPropertyMember::_property_select() {
592592
} else if (hint == MEMBER_PROPERTY_OF_VARIANT_TYPE) {
593593
Variant::Type type = Variant::NIL;
594594
String tname = hint_text;
595-
if (tname.find(".") != -1) {
595+
if (tname.contains(".")) {
596596
tname = tname.get_slice(".", 0);
597597
}
598598
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
@@ -809,7 +809,7 @@ void EditorPropertyLayersGrid::_rename_operation_confirm() {
809809
if (new_name.length() == 0) {
810810
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
811811
return;
812-
} else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
812+
} else if (new_name.contains("/") || new_name.contains("\\") || new_name.contains(":")) {
813813
EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
814814
return;
815815
}
@@ -2883,7 +2883,7 @@ void EditorPropertyNodePath::update_property() {
28832883
Node *target_node = base_node->get_node(p);
28842884
ERR_FAIL_COND(!target_node);
28852885

2886-
if (String(target_node->get_name()).find("@") != -1) {
2886+
if (String(target_node->get_name()).contains("@")) {
28872887
assign->set_icon(Ref<Texture2D>());
28882888
assign->set_text(p);
28892889
return;

editor/editor_sectioned_inspector.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class SectionedInspectorFilter : public Object {
8989

9090
if (pi.name.begins_with(section + "/")) {
9191
pi.name = pi.name.replace_first(section + "/", "");
92-
if (!allow_sub && pi.name.find("/") != -1) {
92+
if (!allow_sub && pi.name.contains("/")) {
9393
continue;
9494
}
9595
p_list->push_back(pi);
@@ -227,7 +227,7 @@ void SectionedInspector::update_category_list() {
227227
continue;
228228
}
229229

230-
if (pi.name.find(":") != -1 || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name.begins_with("_global_script")) {
230+
if (pi.name.contains(":") || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name.begins_with("_global_script")) {
231231
continue;
232232
}
233233

editor/filesystem_dock.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *
629629
for (int i = 0; i < p_path->get_file_count(); i++) {
630630
String file = p_path->get_file(i);
631631

632-
if (file.to_lower().find(searched_string) != -1) {
632+
if (file.to_lower().contains(searched_string)) {
633633
FileInfo fi;
634634
fi.name = file;
635635
fi.type = p_path->get_file_type(i);
@@ -1373,8 +1373,8 @@ void FileSystemDock::_make_dir_confirm() {
13731373
if (dir_name.length() == 0) {
13741374
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
13751375
return;
1376-
} else if (dir_name.find("/") != -1 || dir_name.find("\\") != -1 || dir_name.find(":") != -1 || dir_name.find("*") != -1 ||
1377-
dir_name.find("|") != -1 || dir_name.find(">") != -1 || dir_name.ends_with(".") || dir_name.ends_with(" ")) {
1376+
} else if (dir_name.contains("/") || dir_name.contains("\\") || dir_name.contains(":") || dir_name.contains("*") ||
1377+
dir_name.contains("|") || dir_name.contains(">") || dir_name.ends_with(".") || dir_name.ends_with(" ")) {
13781378
EditorNode::get_singleton()->show_warning(TTR("Provided name contains invalid characters."));
13791379
return;
13801380
}
@@ -1478,7 +1478,7 @@ void FileSystemDock::_rename_operation_confirm() {
14781478
if (new_name.length() == 0) {
14791479
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
14801480
return;
1481-
} else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
1481+
} else if (new_name.contains("/") || new_name.contains("\\") || new_name.contains(":")) {
14821482
EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
14831483
return;
14841484
} else if (to_rename.is_file && old_name.get_extension() != new_name.get_extension()) {
@@ -1540,7 +1540,7 @@ void FileSystemDock::_duplicate_operation_confirm() {
15401540
if (new_name.length() == 0) {
15411541
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
15421542
return;
1543-
} else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
1543+
} else if (new_name.contains("/") || new_name.contains("\\") || new_name.contains(":")) {
15441544
EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
15451545
return;
15461546
}

0 commit comments

Comments
 (0)