extractCamelSnakeConstant("HelloWorld")
## [1] "Hello" "World"
extractCamelSnakeConstant("helloWorld")
## [1] "hello" "World"
extractCamelSnakeConstant("hello_world")
## [1] "hello" "world"
extractCamelSnakeConstant("ZERO_FLAG")
## [1] "ZERO" "FLAG"
extractCamelSnakeConstant("UPPER_DELIMITER_CASE")
## [1] "UPPER" "DELIMITER" "CASE"
#include <Rcpp.h>
#include <string>
using namespace Rcpp;
// [[Rcpp::plugins(cpp14)]]
bool isBreak(const char& ch) {
if ('A' <= ch && ch <= 'Z') {
return true;
} else if (ch == '_') {
return true;
}
return false;
}
bool isConstant(const std::string& words) {
bool ret = true;
for(auto& ch : words) {
ret = ret && isBreak(ch);
if (!ret)
return ret;
}
return ret;
}
StringVector extractSnake(const std::string& words) {
StringVector ret;
std::string holder = "";
for(auto& ch : words) {
if (ch == '_') {
if (holder.size() > 0) {
ret.push_back(holder);
holder = "";
}
if (ch != '_')
holder += ch;
} else {
holder += ch;
}
}
if (holder.size() > 0) {
ret.push_back(holder);
}
return ret;
}
StringVector extractCamelSnake(const std::string& words) {
StringVector ret;
std::string holder = "";
for(auto& ch : words) {
if (isBreak(ch)) {
if (holder.size() > 0) {
ret.push_back(holder);
holder = "";
}
if (ch != '_')
holder += ch;
} else {
holder += ch;
}
}
if (holder.size() > 0) {
ret.push_back(holder);
}
return ret;
}
// [[Rcpp::export]]
StringVector extractCamelSnakeConstant(const std::string& words) {
if (isConstant(words))
return extractSnake(words);
return extractCamelSnake(words);
}