MCPcopy Create free account
hub / github.com/apache/trafficserver / evaluate

Function evaluate

plugins/prefetch/evaluate.cc:231–316  ·  view source on GitHub ↗

* @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" */

Source from the content-addressed store, hash-verified

229 * @return string containing the result, i.e. "7"
230 */
231String
232evaluate(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);

Callers 3

evaluateMethod · 0.85
expandFunction · 0.85
test_evaluate.ccFile · 0.85

Calls 9

evaluateBignumFunction · 0.85
StringClass · 0.50
emptyMethod · 0.45
assignMethod · 0.45
substrMethod · 0.45
c_strMethod · 0.45
lengthMethod · 0.45
dataMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected