xref: /aosp_15_r20/external/abseil-cpp/absl/strings/internal/str_format/extension.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 
16 #include "absl/strings/internal/str_format/extension.h"
17 
18 #include <errno.h>
19 #include <algorithm>
20 #include <string>
21 
22 namespace absl {
23 ABSL_NAMESPACE_BEGIN
24 namespace str_format_internal {
25 
FlagsToString(Flags v)26 std::string FlagsToString(Flags v) {
27   std::string s;
28   s.append(FlagsContains(v, Flags::kLeft) ? "-" : "");
29   s.append(FlagsContains(v, Flags::kShowPos) ? "+" : "");
30   s.append(FlagsContains(v, Flags::kSignCol) ? " " : "");
31   s.append(FlagsContains(v, Flags::kAlt) ? "#" : "");
32   s.append(FlagsContains(v, Flags::kZero) ? "0" : "");
33   return s;
34 }
35 
36 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
37 
38 #define ABSL_INTERNAL_X_VAL(id) \
39   constexpr absl::FormatConversionChar FormatConversionCharInternal::id;
40 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
41 #undef ABSL_INTERNAL_X_VAL
42 // NOLINTNEXTLINE(readability-redundant-declaration)
43 constexpr absl::FormatConversionChar FormatConversionCharInternal::kNone;
44 
45 #define ABSL_INTERNAL_CHAR_SET_CASE(c) \
46   constexpr FormatConversionCharSet FormatConversionCharSetInternal::c;
47 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
48 #undef ABSL_INTERNAL_CHAR_SET_CASE
49 
50 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kStar;
51 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kIntegral;
52 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kFloating;
53 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kNumeric;
54 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kPointer;
55 
56 #endif  // ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
57 
PutPaddedString(string_view value,int width,int precision,bool left)58 bool FormatSinkImpl::PutPaddedString(string_view value, int width,
59                                      int precision, bool left) {
60   size_t space_remaining = 0;
61   if (width >= 0)
62     space_remaining = static_cast<size_t>(width);
63   size_t n = value.size();
64   if (precision >= 0) n = std::min(n, static_cast<size_t>(precision));
65   string_view shown(value.data(), n);
66   space_remaining = Excess(shown.size(), space_remaining);
67   if (!left) Append(space_remaining, ' ');
68   Append(shown);
69   if (left) Append(space_remaining, ' ');
70   return true;
71 }
72 
73 }  // namespace str_format_internal
74 ABSL_NAMESPACE_END
75 }  // namespace absl
76