extension.cc (1759B)
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 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 bool FormatSinkImpl::PutPaddedString(string_view value, int width, 37 int precision, bool left) { 38 size_t space_remaining = 0; 39 if (width >= 0) 40 space_remaining = static_cast<size_t>(width); 41 size_t n = value.size(); 42 if (precision >= 0) n = std::min(n, static_cast<size_t>(precision)); 43 string_view shown(value.data(), n); 44 space_remaining = Excess(shown.size(), space_remaining); 45 if (!left) Append(space_remaining, ' '); 46 Append(shown); 47 if (left) Append(space_remaining, ' '); 48 return true; 49 } 50 51 } // namespace str_format_internal 52 ABSL_NAMESPACE_END 53 } // namespace absl