| 1 | #include <RcppCommon.h> |
| 2 | using namespace Rcpp; |
| 3 | |
| 4 | class SymbolicUnits { |
| 5 | public: |
| 6 | SymbolicUnits(std::string_view x, bool strict=false) |
| 7 | : x(x), it(x.begin()), strict(strict) { tokenize(); } |
| 8 | SymbolicUnits(std::string_view x, std::string_view::iterator start, std::string_view::iterator end) |
| 9 | : x(x.substr(start - x.begin(), end - start)), it(this->x.end()) { |
| 10 | numerator.push_back(std::string(this->x)); |
| 11 | } |
| 12 | operator SEXP(); |
| 13 | |
| 14 | private: |
| 15 | std::string_view x; |
| 16 | std::string_view::iterator it; |
| 17 | std::vector<std::string> numerator, denominator; |
| 18 | bool in_denominator = false; |
| 19 | bool stay = false; |
| 20 | bool strict = false; |
| 21 | |
| 22 | /* helpers -----------------------------------------------------------------*/ |
| 23 | |
| 24 | bool is_multiplicative(const char& c) { |
| 25 | return c == '*' || c == '.' || c == ' '; |
| 26 | } |
| 27 | |
| 28 | bool is_number_char(const char& c) { |
| 29 | return std::isdigit(c) || c == '.' || c == 'e' || c == '-'; |
| 30 | } |
| 31 | |
| 32 | bool is_symbol_char(const char& c) { |
| 33 | if (!std::isdigit(c)) return !is_multiplicative(c) && c != '/' && |
| 34 | c != '(' && c != ')' && c != '^' && c != '-' && c != '+'; |
| 35 | |
| 36 | auto lookahead = it; |
| 37 | for (; lookahead != x.end() && std::isdigit(*lookahead); ++lookahead); |
| 38 | if (lookahead != x.end() && is_symbol_char(*lookahead)) |
| 39 | return true; |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | /* getters -----------------------------------------------------------------*/ |
| 44 | |
| 45 | SymbolicUnits get_paren() { |
| 46 | int level = 1; |
| 47 | auto start = ++it; |
| 48 | |
| 49 | for (; it != x.end(); ++it) { |
| 50 | const char& c = *it; |
| 51 | if (c == '(') |
| 52 | ++level; |
| 53 | else if (c == ')' && --level == 0) |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | if (level != 0) |
| 58 | stop("unmatched parenthesis"); |
| 59 | |
| 60 | return SymbolicUnits(x.substr(start - x.begin(), it++ - start), strict); |
| 61 | } |
| 62 | |
| 63 | int get_exponent() { |
| 64 | int exponent = 1; |
| 65 | if (it == x.end()) return exponent; |
| 66 | |
| 67 | bool is_negative = false; |
| 68 | bool is_exponent = false; |
| 69 | if (*it == '^') { |
| 70 | is_exponent = true; |
| 71 | ++it; |
| 72 | } |
| 73 | switch (*it) { |
| 74 | case '-': |
| 75 | is_negative = true; |
| 76 | case '+': |
| 77 | is_exponent = true; |
| 78 | ++it; |
| 79 | } |
| 80 | bool is_digit = it != x.end() && std::isdigit(*it); |
| 81 | |
| 82 | if (!is_digit && is_exponent) |
| 83 | stop("invalid exponent"); |
| 84 | else if (is_digit) { |
| 85 | exponent = *(it++) - '0'; |
| 86 | for (; it != x.end() && isdigit(*it); ++it) |
| 87 | exponent = exponent * 10 + (*it - '0'); |
| 88 | if (is_negative) exponent = -exponent; |
| 89 | } |
| 90 | |
| 91 | return exponent; |
| 92 | } |
| 93 | |
| 94 | SymbolicUnits get_number() { |
| 95 | auto start = it++; |
| 96 | for (; it != x.end() && is_number_char(*it); ++it); |
| 97 | stay = !strict; |
| 98 | return SymbolicUnits(x, start, it); |
| 99 | } |
| 100 | |
| 101 | SymbolicUnits get_symbol() { |
| 102 | auto start = (*it == '`') ? ++it : it++; |
| 103 | for (; it != x.end() && is_symbol_char(*it); ++it); |
| 104 | return SymbolicUnits(x, start, it - (*(it-1) == '`' ? 1 : 0)); |
| 105 | } |
| 106 | |
| 107 | /* main logic --------------------------------------------------------------*/ |
| 108 | |
| 109 | void combine(SymbolicUnits& o, int exponent = 1) { |
| 110 | bool invert = in_denominator != (exponent < 0); |
| 111 | auto& target_num = invert ? denominator : numerator; |
| 112 | auto& target_den = invert ? numerator : denominator; |
| 113 | |
| 114 | for (exponent = std::abs(exponent); exponent > 0; --exponent) { |
| 115 | target_num.insert(target_num.end(), o.numerator.begin(), o.numerator.end()); |
| 116 | target_den.insert(target_den.end(), o.denominator.begin(), o.denominator.end()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void tokenize() { |
| 121 | while (it != x.end()) { |
| 122 | const char& c = *it; |
| 123 | |
| 124 | // handle separators |
| 125 | if (is_multiplicative(c)) { |
| 126 | ++it; |
| 127 | continue; |
| 128 | } |
| 129 | if (c == '/') { |
| 130 | in_denominator = true; |
| 131 | ++it; |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | // handle parenthesis, digits, and symbols/names |
| 136 | auto token = (c == '(') ? get_paren() : |
| 137 | (std::isdigit(c) ? get_number() : get_symbol()); |
| 138 | combine(token, get_exponent()); |
| 139 | |
| 140 | // switch to numerator if needed |
| 141 | if (!stay) in_denominator = false; |
| 142 | stay = false; |
| 143 | } |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | #include <Rcpp.h> |
| 148 | |
| 149 | SymbolicUnits::operator SEXP() { |
| 150 | List unit = List::create( |
| 151 | _["numerator"] = numerator, |
| 152 | _["denominator"] = denominator |
| 153 | ); |
| 154 | unit.attr("class") = "symbolic_units"; |
| 155 | return unit; |
| 156 | }; |
| 157 | |
| 158 | // [[Rcpp::export]] |
| 159 | SEXP parse_unit(std::string_view x, bool strict=false) { |
| 160 | return SymbolicUnits(x, strict); |
| 161 | } |
| 162 | |
| 163 | /*** R |
| 164 | parse_unit("m") |
| 165 | parse_unit("m2") |
| 166 | parse_unit("m2 / g^3") |
| 167 | parse_unit("m2 / g-1") |
| 168 | parse_unit("ml / min / 1.73 / m^2") |
| 169 | parse_unit("ml/min/1.73m^2") |
| 170 | parse_unit("ml/min/1.73/m^2") |
| 171 | parse_unit("ml/min/(1.73m^2)") |
| 172 | parse_unit("inH2O") |
| 173 | parse_unit("inH2O2") |
| 174 | */ |