From 7b47153072eca95c0becf1dc735637d228b4e427 Mon Sep 17 00:00:00 2001 From: Julian Murgia - StraToN Date: Wed, 17 Feb 2016 19:26:22 +0100 Subject: Fixed String::camelcase_to_underscore() so it works in all cases. Fixes PR #1650 --- core/ustring.cpp | 31 ++++++++++++++++++++----------- core/ustring.h | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) (limited to 'core') diff --git a/core/ustring.cpp b/core/ustring.cpp index ee750c39e5..1017fc0ca3 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -507,26 +507,35 @@ String String::capitalize() const { return cap; } -String String::camelcase_to_underscore() const { +String String::camelcase_to_underscore(bool lowercase) const { const CharType * cstr = c_str(); - String newString; + String new_string; 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++ ) { - bool isCapital = cstr[i] >= A && cstr[i] <= Z; + for ( size_t i = 1; i < this->size(); i++ ) { + 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 ) { - newString += "_" + this->substr(startIndex, i-startIndex); - startIndex = i; + if (i+2 < this->size()) { + are_next_2_lower = cstr[i+1] >= a && cstr[i+1] <= z && cstr[i+2] >= a && cstr[i+2] <= z; } - } - newString += "_" + this->substr(startIndex, this->size()-startIndex); + 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; + } + } - return newString; + new_string += this->substr(start_index, this->size() - start_index); + return lowercase ? new_string.to_lower() : new_string; } + + int String::get_slice_count(String p_splitter) const{ if (empty()) diff --git a/core/ustring.h b/core/ustring.h index 9276afa0f7..e65103ff99 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -149,7 +149,7 @@ public: 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); String capitalize() const; - String camelcase_to_underscore() const; + String camelcase_to_underscore(bool lowercase=true) const; int get_slice_count(String p_splitter) const; String get_slice(String p_splitter,int p_slice) const; -- cgit v1.2.3