-
-
Save abbaswasim/0d361a00134a530020dfd7c22a9199e6 to your computer and use it in GitHub Desktop.
CamelCase to lower_with_underscore conversion for C/C++ files except symbols starting with two upper characters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%option 8bit noyywrap yylineno stack | |
%{ | |
#include <algorithm> | |
#include <iostream> | |
#include <cctype> | |
bool all_caps(const std::string &s) { | |
return std::none_of(s.begin(), s.end(), ::islower); | |
} | |
// Convert lowerCamelCase and UpperCamelCase strings to lower_with_underscore. | |
std::string convert(std::string &&camelCase) { | |
std::string str(1, camelCase[0]); | |
// First place underscores between contiguous lower and upper case letters. | |
// For example, `_LowerCamelCase` becomes `_Lower_Camel_Case`. | |
for (auto it = camelCase.begin() + 1; it != camelCase.end(); ++it) { | |
if (isupper(*it) && *(it-1) != '_' && islower(*(it-1))) { | |
str += "_"; | |
} | |
str += *it; | |
} | |
// Then convert it to lower case. | |
if (!(isupper(str[0]) && isupper(str[1]))) | |
std::transform(str.begin(), str.end(), str.begin(), ::tolower); | |
return str; | |
} | |
%} | |
%x X_STRING X_COMMENT X_INCLUDE | |
%% | |
\" ECHO; yy_push_state(X_STRING); | |
<X_STRING>\\\" ECHO; | |
<X_STRING>\\\\ ECHO; | |
<X_STRING>\" ECHO; yy_pop_state(); | |
<X_STRING>. ECHO; | |
"//".*$ ECHO; | |
"/*" ECHO; yy_push_state(X_COMMENT); | |
<X_COMMENT>"/*" ECHO; yy_push_state(X_COMMENT); | |
<X_COMMENT>"*/" ECHO; yy_pop_state(); | |
<X_COMMENT>.|\n ECHO; | |
#include ECHO; yy_push_state(X_INCLUDE); | |
<INITIAL,X_INCLUDE>[a-zA-Z_][a-zA-Z0-9_]* { | |
std::string id(yytext); | |
if (all_caps(id)) | |
std::cout << id << std::flush; | |
else | |
std::cout << convert(std::move(id)) << std::flush; | |
} | |
<X_INCLUDE>\n ECHO; yy_pop_state(); | |
.|\n ECHO; | |
%% | |
int main() { | |
yyFlexLexer tmp; | |
return tmp.yylex(); | |
} | |
// Build with | |
// rm lex.yy.cc;flex++ convert.lex; g++ -std=c++11 lex.yy.cc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment