Skip to content

Instantly share code, notes, and snippets.

@devhawk
Created March 12, 2019 16:32
Show Gist options
  • Save devhawk/c1d4765110baf764f4defbc8a03ae9ce to your computer and use it in GitHub Desktop.
Save devhawk/c1d4765110baf764f4defbc8a03ae9ce to your computer and use it in GitHub Desktop.
void write_snake_case(writer& w, std::string_view const& name, bool uppercase)
{
auto case_func = [uppercase](char c)
{
return uppercase
? static_cast<char>(::toupper(c))
: static_cast<char>(::tolower(c));
};
w.write(case_func(name[0]));
for (std::string_view::size_type i = 1; i < name.size() - 1; i++)
{
if (name.substr(i, 4) == "UInt")
{
w.write('_');
}
if (isupper(name[i]) && islower(name[i + 1]))
{
if (name.substr(i-1, 4) != "UInt")
{
w.write('_');
}
}
w.write(case_func(name[i]));
}
w.write(case_func(name[name.size() - 1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment