Camel case and snake case: Difference between revisions

Content added Content deleted
m (Small improvement to code.)
m (Small improvement to coding.)
Line 468: Line 468:
The output of this example reflects the author's interpretation of the task.
The output of this example reflects the author's interpretation of the task.
<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">

#include <algorithm>
#include <algorithm>
#include <iomanip>
#include <iomanip>
Line 491: Line 492:
std::string trim(const std::string& text) {
std::string trim(const std::string& text) {
return left_trim(right_trim(text));
return left_trim(right_trim(text));
}

void prepare_for_conversion(std::string& text) {
text = trim(text);
std::replace(text.begin(), text.end(), SPACE, UNDERSCORE);
std::replace(text.begin(), text.end(), HYPHEN, UNDERSCORE);
}
}


std::string to_snake_case(std::string& camel) {
std::string to_snake_case(std::string& camel) {
camel = trim(camel);
prepare_for_conversion(camel);
std::replace(camel.begin(), camel.end(), SPACE, UNDERSCORE);
std::replace(camel.begin(), camel.end(), HYPHEN, UNDERSCORE);
std::string snake = "";
std::string snake = "";
bool first = true;
bool first = true;
Line 518: Line 523:


std::string to_camel_case(std::string& snake) {
std::string to_camel_case(std::string& snake) {
snake = trim(snake);
prepare_for_conversion(snake);
std::replace(snake.begin(), snake.end(), SPACE, UNDERSCORE);
std::replace(snake.begin(), snake.end(), HYPHEN, UNDERSCORE);
std::string camel = "";
std::string camel = "";
bool underscore = false;
bool underscore = false;