Skip to content

Commit

Permalink
Optimize is_valid_filename and validate_filename by caching invalid f…
Browse files Browse the repository at this point in the history
…ilename characters, instead of re-splitting each call.
  • Loading branch information
Ivorforce committed Dec 4, 2024
1 parent 0f20e67 commit 0d2e13b
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5286,7 +5286,7 @@ bool String::is_valid_html_color() const {
}

// Changes made to the set of invalid filename characters must also be reflected in the String documentation for is_valid_filename.
static const char *invalid_filename_characters = ": / \\ ? * \" | % < >";
static const char *invalid_filename_characters[] = { ":", "/", "\\", "?", "*", "\"", "|", "%", "<", ">" };

bool String::is_valid_filename() const {
String stripped = strip_edges();
Expand All @@ -5298,8 +5298,7 @@ bool String::is_valid_filename() const {
return false;
}

Vector<String> chars = String(invalid_filename_characters).split(" ");
for (const String &ch : chars) {
for (const char *ch : invalid_filename_characters) {
if (contains(ch)) {
return false;
}
Expand All @@ -5308,10 +5307,9 @@ bool String::is_valid_filename() const {
}

String String::validate_filename() const {
Vector<String> chars = String(invalid_filename_characters).split(" ");
String name = strip_edges();
for (int i = 0; i < chars.size(); i++) {
name = name.replace(chars[i], "_");
for (const char *ch : invalid_filename_characters) {
name = name.replace(ch, "_");
}
return name;
}
Expand Down

0 comments on commit 0d2e13b

Please sign in to comment.