extension.cc 2.9 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. #define ABSL_INTERNAL_X_VAL(id) \
  32. constexpr absl::FormatConversionChar FormatConversionCharInternal::id;
  33. ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
  34. #undef ABSL_INTERNAL_X_VAL
  35. // NOLINTNEXTLINE(readability-redundant-declaration)
  36. constexpr absl::FormatConversionChar FormatConversionCharInternal::kNone;
  37. #define ABSL_INTERNAL_CHAR_SET_CASE(c) \
  38. constexpr FormatConversionCharSet FormatConversionCharSetInternal::c;
  39. ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
  40. #undef ABSL_INTERNAL_CHAR_SET_CASE
  41. // NOLINTNEXTLINE(readability-redundant-declaration)
  42. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kStar;
  43. // NOLINTNEXTLINE(readability-redundant-declaration)
  44. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kIntegral;
  45. // NOLINTNEXTLINE(readability-redundant-declaration)
  46. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kFloating;
  47. // NOLINTNEXTLINE(readability-redundant-declaration)
  48. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kNumeric;
  49. // NOLINTNEXTLINE(readability-redundant-declaration)
  50. constexpr FormatConversionCharSet FormatConversionCharSetInternal::kPointer;
  51. bool FormatSinkImpl::PutPaddedString(string_view value, int width,
  52. int precision, bool left) {
  53. size_t space_remaining = 0;
  54. if (width >= 0) space_remaining = width;
  55. size_t n = value.size();
  56. if (precision >= 0) n = std::min(n, static_cast<size_t>(precision));
  57. string_view shown(value.data(), n);
  58. space_remaining = Excess(shown.size(), space_remaining);
  59. if (!left) Append(space_remaining, ' ');
  60. Append(shown);
  61. if (left) Append(space_remaining, ' ');
  62. return true;
  63. }
  64. } // namespace str_format_internal
  65. ABSL_NAMESPACE_END
  66. } // namespace absl