• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ToString() const26 std::string Flags::ToString() const {
27   std::string s;
28   s.append(left     ? "-" : "");
29   s.append(show_pos ? "+" : "");
30   s.append(sign_col ? " " : "");
31   s.append(alt      ? "#" : "");
32   s.append(zero     ? "0" : "");
33   return s;
34 }
35 
36 #define ABSL_INTERNAL_X_VAL(id) \
37   constexpr absl::FormatConversionChar FormatConversionCharInternal::id;
38 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
39 #undef ABSL_INTERNAL_X_VAL
40 // NOLINTNEXTLINE(readability-redundant-declaration)
41 constexpr absl::FormatConversionChar FormatConversionCharInternal::kNone;
42 
43 #define ABSL_INTERNAL_CHAR_SET_CASE(c) \
44   constexpr FormatConversionCharSet FormatConversionCharSetInternal::c;
45 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
46 #undef ABSL_INTERNAL_CHAR_SET_CASE
47 
48 // NOLINTNEXTLINE(readability-redundant-declaration)
49 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kStar;
50 // NOLINTNEXTLINE(readability-redundant-declaration)
51 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kIntegral;
52 // NOLINTNEXTLINE(readability-redundant-declaration)
53 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kFloating;
54 // NOLINTNEXTLINE(readability-redundant-declaration)
55 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kNumeric;
56 // NOLINTNEXTLINE(readability-redundant-declaration)
57 constexpr FormatConversionCharSet FormatConversionCharSetInternal::kPointer;
58 
PutPaddedString(string_view value,int width,int precision,bool left)59 bool FormatSinkImpl::PutPaddedString(string_view value, int width,
60                                      int precision, bool left) {
61   size_t space_remaining = 0;
62   if (width >= 0) space_remaining = 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