| 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 | namespace str_format_internal { |
| 24 | namespace { |
| 25 | // clang-format off |
| 26 | #define ABSL_LENGTH_MODS_EXPAND_ \ |
| 27 | X_VAL(h) X_SEP \ |
| 28 | X_VAL(hh) X_SEP \ |
| 29 | X_VAL(l) X_SEP \ |
| 30 | X_VAL(ll) X_SEP \ |
| 31 | X_VAL(L) X_SEP \ |
| 32 | X_VAL(j) X_SEP \ |
| 33 | X_VAL(z) X_SEP \ |
| 34 | X_VAL(t) X_SEP \ |
| 35 | X_VAL(q) |
| 36 | // clang-format on |
| 37 | } // namespace |
| 38 | |
| 39 | const LengthMod::Spec LengthMod::kSpecs[] = { |
| 40 | #define X_VAL(id) { LengthMod::id, #id, strlen(#id) } |
| 41 | #define X_SEP , |
| 42 | ABSL_LENGTH_MODS_EXPAND_, {LengthMod::none, "" , 0} |
| 43 | #undef X_VAL |
| 44 | #undef X_SEP |
| 45 | }; |
| 46 | |
| 47 | const ConversionChar::Spec ConversionChar::kSpecs[] = { |
| 48 | #define X_VAL(id) { ConversionChar::id, #id[0] } |
| 49 | #define X_SEP , |
| 50 | ABSL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP), |
| 51 | {ConversionChar::none, '\0'}, |
| 52 | #undef X_VAL |
| 53 | #undef X_SEP |
| 54 | }; |
| 55 | |
| 56 | std::string Flags::ToString() const { |
| 57 | std::string s; |
| 58 | s.append(left ? "-" : "" ); |
| 59 | s.append(show_pos ? "+" : "" ); |
| 60 | s.append(sign_col ? " " : "" ); |
| 61 | s.append(alt ? "#" : "" ); |
| 62 | s.append(zero ? "0" : "" ); |
| 63 | return s; |
| 64 | } |
| 65 | |
| 66 | const size_t LengthMod::kNumValues; |
| 67 | |
| 68 | const size_t ConversionChar::kNumValues; |
| 69 | |
| 70 | bool FormatSinkImpl::PutPaddedString(string_view v, int w, int p, bool l) { |
| 71 | size_t space_remaining = 0; |
| 72 | if (w >= 0) space_remaining = w; |
| 73 | size_t n = v.size(); |
| 74 | if (p >= 0) n = std::min(n, static_cast<size_t>(p)); |
| 75 | string_view shown(v.data(), n); |
| 76 | space_remaining = Excess(shown.size(), space_remaining); |
| 77 | if (!l) Append(space_remaining, ' '); |
| 78 | Append(shown); |
| 79 | if (l) Append(space_remaining, ' '); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | } // namespace str_format_internal |
| 84 | } // namespace absl |
| 85 | |