Camel case and snake case: Difference between revisions
Content added Content deleted
m (Adjusting formatting.) |
(New post.) |
||
Line 464: | Line 464: | ||
c://my-docs/happy_Flag-Day/12.doc => camel: c://my-docs/happyFlag-Day/12.doc snake: c://my-docs/happy__flag-_day/12.doc |
c://my-docs/happy_Flag-Day/12.doc => camel: c://my-docs/happyFlag-Day/12.doc snake: c://my-docs/happy__flag-_day/12.doc |
||
spaces => camel: spaces snake: spaces</pre> |
spaces => camel: spaces snake: spaces</pre> |
||
=={{header|C++}}== |
|||
The output of this example reflects the author's interpretation of the task. |
|||
<syntaxhighlight lang="c++"> |
|||
#include <algorithm> |
|||
#include <iomanip> |
|||
#include <iostream> |
|||
#include <string> |
|||
#include <vector> |
|||
const char HYPHEN = '-'; |
|||
const char SPACE = ' '; |
|||
const char UNDERSCORE = '_'; |
|||
const std::string WHITESPACE = " \n\r\t\f\v"; |
|||
std::string left_trim(const std::string& text) { |
|||
size_t start = text.find_first_not_of(WHITESPACE); |
|||
return ( start == std::string::npos ) ? "" : text.substr(start); |
|||
} |
|||
std::string right_trim(const std::string& text) { |
|||
size_t end = text.find_last_not_of(WHITESPACE); |
|||
return ( end == std::string::npos ) ? "" : text.substr(0, end + 1); |
|||
} |
|||
std::string trim(const std::string& text) { |
|||
return left_trim(right_trim(text)); |
|||
} |
|||
std::string to_snake_case(std::string& camel) { |
|||
camel = trim(camel); |
|||
std::replace(camel.begin(), camel.end(), SPACE, UNDERSCORE); |
|||
std::replace(camel.begin(), camel.end(), HYPHEN, UNDERSCORE); |
|||
std::string snake = ""; |
|||
bool first = true; |
|||
for ( const char& ch : camel ) { |
|||
if ( first ) { |
|||
snake += ch; |
|||
first = false; |
|||
} else if ( ! first && ch >= 'A' && ch <= 'Z' ) { |
|||
if ( snake[snake.length() - 1] == UNDERSCORE ) { |
|||
snake += tolower(ch); |
|||
} else { |
|||
snake += UNDERSCORE; |
|||
snake += tolower(ch); |
|||
} |
|||
} else { |
|||
snake += ch; |
|||
} |
|||
} |
|||
return snake; |
|||
} |
|||
std::string to_camel_case(std::string& snake) { |
|||
snake = trim(snake); |
|||
std::replace(snake.begin(), snake.end(), SPACE, UNDERSCORE); |
|||
std::replace(snake.begin(), snake.end(), HYPHEN, UNDERSCORE); |
|||
std::string camel = ""; |
|||
bool underscore = false; |
|||
for ( const char& ch : snake ) { |
|||
if ( ch == UNDERSCORE ) { |
|||
underscore = true; |
|||
} else if ( underscore ) { |
|||
camel += toupper(ch); |
|||
underscore = false; |
|||
} else { |
|||
camel += ch; |
|||
} |
|||
} |
|||
return camel; |
|||
} |
|||
int main() { |
|||
const std::vector<std::string> variable_names = { "snakeCase", "snake_case", "variable_10_case", "variable10Case", |
|||
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces " }; |
|||
std::cout << std::setw(48) << "=== To snake_case ===" << std::endl; |
|||
for ( std::string text : variable_names ) { |
|||
std::cout << std::setw(34) << text << " --> " << to_snake_case(text) << std::endl; |
|||
} |
|||
std::cout << std::endl; |
|||
std::cout << std::setw(48) << "=== To camelCase ===" << std::endl; |
|||
for ( std::string text : variable_names ) { |
|||
std::cout << std::setw(34) << text << " --> " << to_camel_case(text) << std::endl; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
=== To snake_case === |
|||
snakeCase --> snake_case |
|||
snake_case --> snake_case |
|||
variable_10_case --> variable_10_case |
|||
variable10Case --> variable10_case |
|||
ergo rE tHis --> ergo_r_e_t_his |
|||
hurry-up-joe! --> hurry_up_joe! |
|||
c://my-docs/happy_Flag-Day/12.doc --> c://my_docs/happy_flag_day/12.doc |
|||
spaces --> spaces |
|||
=== To camelCase === |
|||
snakeCase --> snakeCase |
|||
snake_case --> snakeCase |
|||
variable_10_case --> variable10Case |
|||
variable10Case --> variable10Case |
|||
ergo rE tHis --> ergoRETHis |
|||
hurry-up-joe! --> hurryUpJoe! |
|||
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc |
|||
spaces --> spaces |
|||
</pre> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{works with|Delphi|6.0}} |
{{works with|Delphi|6.0}} |