Fixed String::camelcase_to_underscore() so it works in all cases. Fixes PR #1650
This commit is contained in:
parent
39f69cbfc3
commit
7b47153072
2 changed files with 21 additions and 12 deletions
|
@ -507,26 +507,35 @@ String String::capitalize() const {
|
||||||
return cap;
|
return cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::camelcase_to_underscore() const {
|
String String::camelcase_to_underscore(bool lowercase) const {
|
||||||
const CharType * cstr = c_str();
|
const CharType * cstr = c_str();
|
||||||
String newString;
|
String new_string;
|
||||||
const char A = 'A', Z = 'Z';
|
const char A = 'A', Z = 'Z';
|
||||||
int startIndex = 0;
|
const char a = 'a', z = 'z';
|
||||||
|
int start_index = 0;
|
||||||
|
|
||||||
for ( int i = 1; i < this->size()-1; i++ ) {
|
for ( size_t i = 1; i < this->size(); i++ ) {
|
||||||
bool isCapital = cstr[i] >= A && cstr[i] <= Z;
|
bool is_upper = cstr[i] >= A && cstr[i] <= Z;
|
||||||
|
bool are_next_2_lower = false;
|
||||||
|
bool was_precedent_upper = cstr[i-1] >= A && cstr[i-1] <= Z;
|
||||||
|
|
||||||
if ( isCapital ) {
|
if (i+2 < this->size()) {
|
||||||
newString += "_" + this->substr(startIndex, i-startIndex);
|
are_next_2_lower = cstr[i+1] >= a && cstr[i+1] <= z && cstr[i+2] >= a && cstr[i+2] <= z;
|
||||||
startIndex = i;
|
}
|
||||||
|
|
||||||
|
bool should_split = ((is_upper && !was_precedent_upper) || (was_precedent_upper && is_upper && are_next_2_lower));
|
||||||
|
if (should_split) {
|
||||||
|
new_string += this->substr(start_index, i - start_index) + "_";
|
||||||
|
start_index = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newString += "_" + this->substr(startIndex, this->size()-startIndex);
|
new_string += this->substr(start_index, this->size() - start_index);
|
||||||
|
return lowercase ? new_string.to_lower() : new_string;
|
||||||
return newString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int String::get_slice_count(String p_splitter) const{
|
int String::get_slice_count(String p_splitter) const{
|
||||||
|
|
||||||
if (empty())
|
if (empty())
|
||||||
|
|
|
@ -149,7 +149,7 @@ public:
|
||||||
static double to_double(const CharType* p_str, const CharType **r_end=NULL);
|
static double to_double(const CharType* p_str, const CharType **r_end=NULL);
|
||||||
static int64_t to_int(const CharType* p_str,int p_len=-1);
|
static int64_t to_int(const CharType* p_str,int p_len=-1);
|
||||||
String capitalize() const;
|
String capitalize() const;
|
||||||
String camelcase_to_underscore() const;
|
String camelcase_to_underscore(bool lowercase=true) const;
|
||||||
|
|
||||||
int get_slice_count(String p_splitter) const;
|
int get_slice_count(String p_splitter) const;
|
||||||
String get_slice(String p_splitter,int p_slice) const;
|
String get_slice(String p_splitter,int p_slice) const;
|
||||||
|
|
Loading…
Reference in a new issue