* @brief Evaluate a math addition or subtraction expression. * * @param v string containing an expression, i.e. "3 + 4" * @return string containing the result, i.e. "7" */
| 229 | * @return string containing the result, i.e. "7" |
| 230 | */ |
| 231 | String |
| 232 | evaluate(const StringView view, const EvalPolicy policy) |
| 233 | { |
| 234 | if (view.empty()) { |
| 235 | return String(""); |
| 236 | } |
| 237 | |
| 238 | // short circuit |
| 239 | if (policy == EvalPolicy::Bignum) { |
| 240 | return evaluateBignum(view); |
| 241 | } |
| 242 | |
| 243 | StringView v = view; |
| 244 | |
| 245 | /* Find out if width is specified (hence leading zeros are required if the width is bigger then the result width) */ |
| 246 | String stmt; |
| 247 | uint32_t len = 0; |
| 248 | StringView::size_type pos = v.find_first_of(':'); |
| 249 | if (v.npos != pos) { |
| 250 | stmt.assign(v.substr(0, pos)); |
| 251 | std::istringstream iss(stmt); |
| 252 | iss >> len; |
| 253 | v = v.substr(pos + 1); |
| 254 | } |
| 255 | PrefetchDebug("statement: '%s', formatting length: %" PRIu32, stmt.c_str(), len); |
| 256 | |
| 257 | uint64_t result = 0; |
| 258 | pos = v.find_first_of("+-"); |
| 259 | |
| 260 | if (v.npos == pos) { |
| 261 | stmt.assign(v.substr(0, pos)); |
| 262 | std::istringstream iss(stmt); |
| 263 | |
| 264 | if (policy == EvalPolicy::Overflow64) { |
| 265 | iss >> result; |
| 266 | } else { |
| 267 | uint32_t tmp32; |
| 268 | iss >> tmp32; |
| 269 | result = tmp32; |
| 270 | } |
| 271 | |
| 272 | PrefetchDebug("Single-operand expression: %s -> %" PRIu64, stmt.c_str(), result); |
| 273 | } else { |
| 274 | const String leftOperand(v.substr(0, pos)); |
| 275 | std::istringstream liss(leftOperand); |
| 276 | uint64_t a64 = 0; |
| 277 | |
| 278 | if (policy == EvalPolicy::Overflow64) { |
| 279 | liss >> a64; |
| 280 | } else { |
| 281 | uint32_t a32; |
| 282 | liss >> a32; |
| 283 | a64 = a32; |
| 284 | } |
| 285 | PrefetchDebug("Left-operand expression: %s -> %" PRIu64, leftOperand.c_str(), a64); |
| 286 | |
| 287 | const String rightOperand(v.substr(pos + 1)); |
| 288 | std::istringstream riss(rightOperand); |