File tree Expand file tree Collapse file tree 3 files changed +35
-0
lines changed
Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -145,6 +145,9 @@ bool IsCString(std::string_view text) noexcept;
145145// / convert CamelCase to snake_case(underscore)
146146std::string CamelCaseToSnake (std::string_view camel);
147147
148+ // / convert snake_case or SCREAMING_SNAKE_CASE to CamelCase
149+ std::string SnakeCaseToCamel (std::string_view snake);
150+
148151} // namespace utils::text
149152
150153USERVER_NAMESPACE_END
Original file line number Diff line number Diff line change @@ -343,6 +343,24 @@ std::string CamelCaseToSnake(std::string_view camel) {
343343 return snake;
344344}
345345
346+ std::string SnakeCaseToCamel (std::string_view snake) {
347+ std::string camel;
348+
349+ bool next_upper = true ;
350+ for (const char c : snake) {
351+ if (next_upper) {
352+ camel += static_cast <char >(std::toupper (c));
353+ next_upper = false ;
354+ } else if (c == ' _' ) {
355+ next_upper = true ;
356+ } else {
357+ camel += static_cast <char >(std::tolower (c));
358+ }
359+ }
360+
361+ return camel;
362+ }
363+
346364} // namespace utils::text
347365
348366USERVER_NAMESPACE_END
Original file line number Diff line number Diff line change @@ -284,6 +284,20 @@ TEST(TextUtils, CamelCaseToSnake) {
284284 EXPECT_STREQ (" _ab_f/_bcd_e-_kq_ " , CamelCaseToSnake (" AbF/BcdE-Kq_ " ).c_str ());
285285}
286286
287+ TEST (TextUtils, SnakeCaseToCamel) {
288+ using utils::text::SnakeCaseToCamel;
289+
290+ EXPECT_EQ (" " , SnakeCaseToCamel (" " ));
291+ EXPECT_EQ (" A" , SnakeCaseToCamel (" a" ));
292+ EXPECT_EQ (" _" , SnakeCaseToCamel (" _" ));
293+ EXPECT_EQ (" -" , SnakeCaseToCamel (" -" ));
294+ EXPECT_EQ (" ABCD" , SnakeCaseToCamel (" a_b_c_d" ));
295+ EXPECT_EQ (" FooBar" , SnakeCaseToCamel (" foo_bar" ));
296+ EXPECT_EQ (" FooBar" , SnakeCaseToCamel (" FOO_BAR" ));
297+ EXPECT_EQ (" _A" , SnakeCaseToCamel (" __A" ));
298+ EXPECT_EQ (" AbF/BcdE-Kq " , SnakeCaseToCamel (" _AB_F/_BCD_E-_KQ_ " ));
299+ }
300+
287301TEST (CheckTextTest, Ascii) {
288302 using utils::text::IsPrintable;
289303
You can’t perform that action at this time.
0 commit comments