mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
231
src/util.cpp
231
src/util.cpp
@@ -86,8 +86,122 @@ static bool LoadFileRaw(const char *name, bool binary, std::string *buf) {
|
||||
LoadFileFunction g_load_file_function = LoadFileRaw;
|
||||
FileExistsFunction g_file_exists_function = FileExistsRaw;
|
||||
|
||||
static std::string ToCamelCase(const std::string &input, bool first) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (!i && first)
|
||||
s += CharToUpper(input[i]);
|
||||
else if (input[i] == '_' && i + 1 < input.length())
|
||||
s += CharToUpper(input[++i]);
|
||||
else
|
||||
s += input[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static std::string ToSnakeCase(const std::string &input, bool screaming) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (i == 0) {
|
||||
s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
|
||||
} else if (input[i] == '_') {
|
||||
s += '_';
|
||||
} else if (!islower(input[i])) {
|
||||
// Prevent duplicate underscores for Upper_Snake_Case strings
|
||||
// and UPPERCASE strings.
|
||||
if (islower(input[i - 1])) { s += '_'; }
|
||||
s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
|
||||
} else {
|
||||
s += screaming ? CharToUpper(input[i]) : input[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string ToAll(const std::string &input,
|
||||
std::function<char(const char)> transform) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) { s += transform(input[i]); }
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string CamelToSnake(const std::string &input) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (i == 0) {
|
||||
s += CharToLower(input[i]);
|
||||
} else if (input[i] == '_') {
|
||||
s += '_';
|
||||
} else if (!islower(input[i])) {
|
||||
// Prevent duplicate underscores for Upper_Snake_Case strings
|
||||
// and UPPERCASE strings.
|
||||
if (islower(input[i - 1])) { s += '_'; }
|
||||
s += CharToLower(input[i]);
|
||||
} else {
|
||||
s += input[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string DasherToSnake(const std::string &input) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (input[i] == '-') {
|
||||
s += "_";
|
||||
} else {
|
||||
s += input[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string ToDasher(const std::string &input) {
|
||||
std::string s;
|
||||
char p = 0;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
char const &c = input[i];
|
||||
if (c == '_') {
|
||||
if (i > 0 && p != kPathSeparator &&
|
||||
// The following is a special case to ignore digits after a _. This is
|
||||
// because ThisExample3 would be converted to this_example_3 in the
|
||||
// CamelToSnake conversion, and then dasher would do this-example-3,
|
||||
// but it expects this-example3.
|
||||
!(i + 1 < input.length() && isdigit(input[i + 1])))
|
||||
s += "-";
|
||||
} else {
|
||||
s += c;
|
||||
}
|
||||
p = c;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
// Converts foo_bar_123baz_456 to foo_bar123_baz456
|
||||
std::string SnakeToSnake2(const std::string &s) {
|
||||
if (s.length() <= 1) return s;
|
||||
std::string result;
|
||||
result.reserve(s.size());
|
||||
for (size_t i = 0; i < s.length() - 1; i++) {
|
||||
if (s[i] == '_' && isdigit(s[i + 1])) {
|
||||
continue; // Move the `_` until after the digits.
|
||||
}
|
||||
|
||||
result.push_back(s[i]);
|
||||
|
||||
if (isdigit(s[i]) && isalpha(s[i + 1]) && islower(s[i + 1])) {
|
||||
result.push_back('_');
|
||||
}
|
||||
}
|
||||
result.push_back(s.back());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
bool LoadFile(const char *name, bool binary, std::string *buf) {
|
||||
FLATBUFFERS_ASSERT(g_load_file_function);
|
||||
return g_load_file_function(name, binary, buf);
|
||||
@@ -335,123 +449,6 @@ void SetupDefaultCRTReportMode() {
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
static std::string ToCamelCase(const std::string &input, bool first) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (!i && first)
|
||||
s += CharToUpper(input[i]);
|
||||
else if (input[i] == '_' && i + 1 < input.length())
|
||||
s += CharToUpper(input[++i]);
|
||||
else
|
||||
s += input[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static std::string ToSnakeCase(const std::string &input, bool screaming) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (i == 0) {
|
||||
s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
|
||||
} else if (input[i] == '_') {
|
||||
s += '_';
|
||||
} else if (!islower(input[i])) {
|
||||
// Prevent duplicate underscores for Upper_Snake_Case strings
|
||||
// and UPPERCASE strings.
|
||||
if (islower(input[i - 1])) { s += '_'; }
|
||||
s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
|
||||
} else {
|
||||
s += screaming ? CharToUpper(input[i]) : input[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string ToAll(const std::string &input,
|
||||
std::function<char(const char)> transform) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) { s += transform(input[i]); }
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string CamelToSnake(const std::string &input) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (i == 0) {
|
||||
s += CharToLower(input[i]);
|
||||
} else if (input[i] == '_') {
|
||||
s += '_';
|
||||
} else if (!islower(input[i])) {
|
||||
// Prevent duplicate underscores for Upper_Snake_Case strings
|
||||
// and UPPERCASE strings.
|
||||
if (islower(input[i - 1])) { s += '_'; }
|
||||
s += CharToLower(input[i]);
|
||||
} else {
|
||||
s += input[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string DasherToSnake(const std::string &input) {
|
||||
std::string s;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
if (input[i] == '-') {
|
||||
s += "_";
|
||||
} else {
|
||||
s += input[i];
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string ToDasher(const std::string &input) {
|
||||
std::string s;
|
||||
char p = 0;
|
||||
for (size_t i = 0; i < input.length(); i++) {
|
||||
char const &c = input[i];
|
||||
if (c == '_') {
|
||||
if (i > 0 && p != kPathSeparator &&
|
||||
// The following is a special case to ignore digits after a _. This is
|
||||
// because ThisExample3 would be converted to this_example_3 in the
|
||||
// CamelToSnake conversion, and then dasher would do this-example-3,
|
||||
// but it expects this-example3.
|
||||
!(i + 1 < input.length() && isdigit(input[i + 1])))
|
||||
s += "-";
|
||||
} else {
|
||||
s += c;
|
||||
}
|
||||
p = c;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
// Converts foo_bar_123baz_456 to foo_bar123_baz456
|
||||
std::string SnakeToSnake2(const std::string &s) {
|
||||
if (s.length() <= 1) return s;
|
||||
std::string result;
|
||||
result.reserve(s.size());
|
||||
for (size_t i = 0; i < s.length() - 1; i++) {
|
||||
if (s[i] == '_' && isdigit(s[i + 1])) {
|
||||
continue; // Move the `_` until after the digits.
|
||||
}
|
||||
|
||||
result.push_back(s[i]);
|
||||
|
||||
if (isdigit(s[i]) && isalpha(s[i + 1]) && islower(s[i + 1])) {
|
||||
result.push_back('_');
|
||||
}
|
||||
}
|
||||
result.push_back(s.back());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string ConvertCase(const std::string &input, Case output_case,
|
||||
Case input_case) {
|
||||
if (output_case == Case::kKeep) return input;
|
||||
|
||||
Reference in New Issue
Block a user