1
0
Fork 0

Merge pull request #100015 from Ivorforce/is-valid-filename-cache

Optimize `String::is_valid_filename()` and `String::validate_filename()`
This commit is contained in:
Thaddeus Crews 2024-12-05 14:11:57 -06:00
commit 3a948abdce
No known key found for this signature in database
GPG Key ID: 62181B86FE9E5D84
1 changed files with 4 additions and 6 deletions

View File

@ -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. // 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 { bool String::is_valid_filename() const {
String stripped = strip_edges(); String stripped = strip_edges();
@ -5298,8 +5298,7 @@ bool String::is_valid_filename() const {
return false; return false;
} }
Vector<String> chars = String(invalid_filename_characters).split(" "); for (const char *ch : invalid_filename_characters) {
for (const String &ch : chars) {
if (contains(ch)) { if (contains(ch)) {
return false; return false;
} }
@ -5308,10 +5307,9 @@ bool String::is_valid_filename() const {
} }
String String::validate_filename() const { String String::validate_filename() const {
Vector<String> chars = String(invalid_filename_characters).split(" ");
String name = strip_edges(); String name = strip_edges();
for (int i = 0; i < chars.size(); i++) { for (const char *ch : invalid_filename_characters) {
name = name.replace(chars[i], "_"); name = name.replace(ch, "_");
} }
return name; return name;
} }