| 443 | } |
| 444 | |
| 445 | absl::StatusOr<std::pair<int64_t, absl::string_view>> ParseAndFormatClause( |
| 446 | absl::string_view format, const Value& value, int max_precision, |
| 447 | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
| 448 | google::protobuf::MessageFactory* absl_nonnull message_factory, |
| 449 | google::protobuf::Arena* absl_nonnull arena, |
| 450 | std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
| 451 | CEL_ASSIGN_OR_RETURN(auto precision_pair, |
| 452 | ParsePrecision(format, max_precision)); |
| 453 | auto [read, precision] = precision_pair; |
| 454 | switch (format[read]) { |
| 455 | case 's': { |
| 456 | CEL_ASSIGN_OR_RETURN(auto result, |
| 457 | FormatString(value, descriptor_pool, message_factory, |
| 458 | arena, scratch)); |
| 459 | return std::pair{read, result}; |
| 460 | } |
| 461 | case 'd': { |
| 462 | CEL_ASSIGN_OR_RETURN(auto result, FormatDecimal(value, scratch)); |
| 463 | return std::pair{read, result}; |
| 464 | } |
| 465 | case 'f': { |
| 466 | CEL_ASSIGN_OR_RETURN(auto result, FormatFixed(value, precision, scratch)); |
| 467 | return std::pair{read, result}; |
| 468 | } |
| 469 | case 'e': { |
| 470 | CEL_ASSIGN_OR_RETURN(auto result, |
| 471 | FormatScientific(value, precision, scratch)); |
| 472 | return std::pair{read, result}; |
| 473 | } |
| 474 | case 'b': { |
| 475 | CEL_ASSIGN_OR_RETURN(auto result, FormatBinary(value, scratch)); |
| 476 | return std::pair{read, result}; |
| 477 | } |
| 478 | case 'x': |
| 479 | case 'X': { |
| 480 | CEL_ASSIGN_OR_RETURN( |
| 481 | auto result, |
| 482 | FormatHex(value, |
| 483 | /*use_upper_case=*/format[read] == 'X', scratch)); |
| 484 | return std::pair{read, result}; |
| 485 | } |
| 486 | case 'o': { |
| 487 | CEL_ASSIGN_OR_RETURN(auto result, FormatOctal(value, scratch)); |
| 488 | return std::pair{read, result}; |
| 489 | } |
| 490 | default: |
| 491 | return absl::InvalidArgumentError(absl::StrFormat( |
| 492 | "unrecognized formatting clause \"%c\"", format[read])); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | absl::StatusOr<Value> Format( |
| 497 | const StringValue& format_value, const ListValue& args, int max_precision, |