| 338 | } |
| 339 | |
| 340 | absl::StatusOr<absl::string_view> FormatHex( |
| 341 | const Value& value, bool use_upper_case, |
| 342 | std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
| 343 | switch (value.kind()) { |
| 344 | case ValueKind::kString: |
| 345 | scratch = absl::BytesToHexString(value.GetString().NativeString(scratch)); |
| 346 | break; |
| 347 | case ValueKind::kBytes: |
| 348 | scratch = absl::BytesToHexString(value.GetBytes().NativeString(scratch)); |
| 349 | break; |
| 350 | case ValueKind::kInt: { |
| 351 | // Golang supports signed hex, but absl::StrFormat does not. To be |
| 352 | // compatible, we need to add a leading '-' if the value is negative. |
| 353 | auto tmp = value.GetInt().NativeValue(); |
| 354 | if (tmp < 0) { |
| 355 | // Negating min int is undefined behavior, so we need to use unsigned |
| 356 | // arithmetic. |
| 357 | using unsigned_type = std::make_unsigned<decltype(tmp)>::type; |
| 358 | scratch = absl::StrFormat("-%x", -static_cast<unsigned_type>(tmp)); |
| 359 | } else { |
| 360 | scratch = absl::StrFormat("%x", tmp); |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | case ValueKind::kUint: |
| 365 | scratch = absl::StrFormat("%x", value.GetUint().NativeValue()); |
| 366 | break; |
| 367 | default: |
| 368 | return absl::InvalidArgumentError( |
| 369 | absl::StrCat("hex clause can only be used on integers, byte buffers, " |
| 370 | "and strings, was given ", |
| 371 | value.GetTypeName())); |
| 372 | } |
| 373 | if (use_upper_case) { |
| 374 | absl::AsciiStrToUpper(&scratch); |
| 375 | } |
| 376 | return scratch; |
| 377 | } |
| 378 | |
| 379 | absl::StatusOr<absl::string_view> FormatOctal( |
| 380 | const Value& value, std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
nothing calls this directly
no test coverage detected