extension.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "absl/strings/internal/str_format/extension.h"
  16. #include <errno.h>
  17. #include <algorithm>
  18. #include <string>
  19. namespace absl {
  20. ABSL_NAMESPACE_BEGIN
  21. namespace str_format_internal {
  22. std::string FlagsToString(Flags v) {
  23. std::string s;
  24. s.append(FlagsContains(v, Flags::kLeft) ? "-" : "");
  25. s.append(FlagsContains(v, Flags::kShowPos) ? "+" : "");
  26. s.append(FlagsContains(v, Flags::kSignCol) ? " " : "");
  27. s.append(FlagsContains(v, Flags::kAlt) ? "#" : "");
  28. s.append(FlagsContains(v, Flags::kZero) ? "0" : "");
  29. return s;
  30. }
  31. #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  32. #define ABSL_INTERNAL_X_VAL(id) \
  33. constexpr absl::FormatConversionChar FormatConversionCharInternal::id;
  34. ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
  35. #undef ABSL_INTERNAL_X_VAL
  36. // NOLINTNEXTLINE(readability-redundant-declaration)
  37. constexpr absl::FormatConversionChar FormatConversionCharInternal::kNone;
  38. #define ABSL_INTERNAL_CHAR_SET_CASE(c) \
  39. constexpr FormatConversionCharSet FormatConversionCharSetInternal::c;
  40. ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
  41. #undef ABSL_INTERNAL_CHAR_SET_CASE
  42. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kStar;
  43. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kIntegral;
  44. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kFloating;
  45. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kNumeric;
  46. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kPointer;
  47. #endif // ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  48. bool FormatSinkImpl::PutPaddedString(string_view value, int width,
  49. int precision, bool left) {
  50. size_t space_remaining = 0;
  51. if (width >= 0)
  52. space_remaining = static_cast<size_t>(width);
  53. size_t n = value.size();
  54. if (precision >= 0) n = std::min(n, static_cast<size_t>(precision));
  55. string_view shown(value.data(), n);
  56. space_remaining = Excess(shown.size(), space_remaining);
  57. if (!left) Append(space_remaining, ' ');
  58. Append(shown);
  59. if (left) Append(space_remaining, ' ');
  60. return true;
  61. }
  62. } // namespace str_format_internal
  63. ABSL_NAMESPACE_END
  64. } // namespace absl