Skip to content

Commit 06cce0e

Browse files
committed
Merge pull request #100016 from Ivorforce/camelcase-to-underscore-rolling-cache
Optimize `_camelcase_to_underscore` (and thus `String.capitalize`)
2 parents 3a948ab + 8d82933 commit 06cce0e

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

core/string/ustring.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -1109,17 +1109,21 @@ String String::_camelcase_to_underscore() const {
11091109
String new_string;
11101110
int start_index = 0;
11111111

1112-
for (int i = 1; i < size(); i++) {
1113-
bool is_prev_upper = is_unicode_upper_case(cstr[i - 1]);
1114-
bool is_prev_lower = is_unicode_lower_case(cstr[i - 1]);
1115-
bool is_prev_digit = is_digit(cstr[i - 1]);
1112+
if (length() == 0) {
1113+
return *this;
1114+
}
11161115

1117-
bool is_curr_upper = is_unicode_upper_case(cstr[i]);
1118-
bool is_curr_lower = is_unicode_lower_case(cstr[i]);
1119-
bool is_curr_digit = is_digit(cstr[i]);
1116+
bool is_prev_upper = is_unicode_upper_case(cstr[0]);
1117+
bool is_prev_lower = is_unicode_lower_case(cstr[0]);
1118+
bool is_prev_digit = is_digit(cstr[0]);
1119+
1120+
for (int i = 1; i < length(); i++) {
1121+
const bool is_curr_upper = is_unicode_upper_case(cstr[i]);
1122+
const bool is_curr_lower = is_unicode_lower_case(cstr[i]);
1123+
const bool is_curr_digit = is_digit(cstr[i]);
11201124

11211125
bool is_next_lower = false;
1122-
if (i + 1 < size()) {
1126+
if (i + 1 < length()) {
11231127
is_next_lower = is_unicode_lower_case(cstr[i + 1]);
11241128
}
11251129

@@ -1132,6 +1136,10 @@ String String::_camelcase_to_underscore() const {
11321136
new_string += substr(start_index, i - start_index) + "_";
11331137
start_index = i;
11341138
}
1139+
1140+
is_prev_upper = is_curr_upper;
1141+
is_prev_lower = is_curr_lower;
1142+
is_prev_digit = is_curr_digit;
11351143
}
11361144

11371145
new_string += substr(start_index, size() - start_index);

0 commit comments

Comments
 (0)