MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / FormatLiteral

Method FormatLiteral

testutil/expr_printer.cc:272–302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

270 }
271
272 std::string FormatLiteral(const Constant& c) {
273 switch (c.kind_case()) {
274 case ConstantKindCase::kBool:
275 return absl::StrFormat("%s", c.bool_value() ? "true" : "false");
276 case ConstantKindCase::kBytes:
277 return cel::internal::FormatDoubleQuotedBytesLiteral(c.bytes_value());
278 case ConstantKindCase::kDouble: {
279 std::string s = absl::StrFormat("%f", c.double_value());
280 // remove trailing zeros, i.e., convert 1.600000 to just 1.6 without
281 // forcing a specific precision. There seems to be no flag to get this
282 // directly from absl::StrFormat.
283 auto idx = std::find_if_not(s.rbegin(), s.rend(),
284 [](const char c) { return c == '0'; });
285 s.erase(idx.base(), s.end());
286 if (absl::EndsWith(s, ".")) {
287 s += '0';
288 }
289 return s;
290 }
291 case ConstantKindCase::kInt:
292 return absl::StrFormat("%d", c.int_value());
293 case ConstantKindCase::kString:
294 return cel::internal::FormatDoubleQuotedStringLiteral(c.string_value());
295 case ConstantKindCase::kUint:
296 return absl::StrFormat("%uu", c.uint_value());
297 case ConstantKindCase::kNull:
298 return "null";
299 default:
300 return "<<ERROR>>";
301 }
302 }
303
304 std::string s_;
305 const ExpressionAdorner& adorner_;

Callers

nothing calls this directly

Calls 4

kind_caseMethod · 0.80
endMethod · 0.45

Tested by

no test coverage detected