mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-13 23:26:24 +00:00
Add vendored dependencies & cleanup script
This commit is contained in:
488
third_party/abseil-cpp/absl/strings/internal/str_format/arg.cc
vendored
Normal file
488
third_party/abseil-cpp/absl/strings/internal/str_format/arg.cc
vendored
Normal file
@@ -0,0 +1,488 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//
|
||||
// POSIX spec:
|
||||
// http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html
|
||||
//
|
||||
#include "absl/strings/internal/str_format/arg.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/strings/internal/str_format/float_conversion.h"
|
||||
#include "absl/strings/numbers.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
namespace {
|
||||
|
||||
// Reduce *capacity by s.size(), clipped to a 0 minimum.
|
||||
void ReducePadding(string_view s, size_t *capacity) {
|
||||
*capacity = Excess(s.size(), *capacity);
|
||||
}
|
||||
|
||||
// Reduce *capacity by n, clipped to a 0 minimum.
|
||||
void ReducePadding(size_t n, size_t *capacity) {
|
||||
*capacity = Excess(n, *capacity);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct MakeUnsigned : std::make_unsigned<T> {};
|
||||
template <>
|
||||
struct MakeUnsigned<absl::int128> {
|
||||
using type = absl::uint128;
|
||||
};
|
||||
template <>
|
||||
struct MakeUnsigned<absl::uint128> {
|
||||
using type = absl::uint128;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct IsSigned : std::is_signed<T> {};
|
||||
template <>
|
||||
struct IsSigned<absl::int128> : std::true_type {};
|
||||
template <>
|
||||
struct IsSigned<absl::uint128> : std::false_type {};
|
||||
|
||||
// Integral digit printer.
|
||||
// Call one of the PrintAs* routines after construction once.
|
||||
// Use with_neg_and_zero/without_neg_or_zero/is_negative to access the results.
|
||||
class IntDigits {
|
||||
public:
|
||||
// Print the unsigned integer as octal.
|
||||
// Supports unsigned integral types and uint128.
|
||||
template <typename T>
|
||||
void PrintAsOct(T v) {
|
||||
static_assert(!IsSigned<T>::value, "");
|
||||
char *p = storage_ + sizeof(storage_);
|
||||
do {
|
||||
*--p = static_cast<char>('0' + (static_cast<size_t>(v) & 7));
|
||||
v >>= 3;
|
||||
} while (v);
|
||||
start_ = p;
|
||||
size_ = storage_ + sizeof(storage_) - p;
|
||||
}
|
||||
|
||||
// Print the signed or unsigned integer as decimal.
|
||||
// Supports all integral types.
|
||||
template <typename T>
|
||||
void PrintAsDec(T v) {
|
||||
static_assert(std::is_integral<T>::value, "");
|
||||
start_ = storage_;
|
||||
size_ = numbers_internal::FastIntToBuffer(v, storage_) - storage_;
|
||||
}
|
||||
|
||||
void PrintAsDec(int128 v) {
|
||||
auto u = static_cast<uint128>(v);
|
||||
bool add_neg = false;
|
||||
if (v < 0) {
|
||||
add_neg = true;
|
||||
u = uint128{} - u;
|
||||
}
|
||||
PrintAsDec(u, add_neg);
|
||||
}
|
||||
|
||||
void PrintAsDec(uint128 v, bool add_neg = false) {
|
||||
// This function can be sped up if needed. We can call FastIntToBuffer
|
||||
// twice, or fix FastIntToBuffer to support uint128.
|
||||
char *p = storage_ + sizeof(storage_);
|
||||
do {
|
||||
p -= 2;
|
||||
numbers_internal::PutTwoDigits(static_cast<size_t>(v % 100), p);
|
||||
v /= 100;
|
||||
} while (v);
|
||||
if (p[0] == '0') {
|
||||
// We printed one too many hexits.
|
||||
++p;
|
||||
}
|
||||
if (add_neg) {
|
||||
*--p = '-';
|
||||
}
|
||||
size_ = storage_ + sizeof(storage_) - p;
|
||||
start_ = p;
|
||||
}
|
||||
|
||||
// Print the unsigned integer as hex using lowercase.
|
||||
// Supports unsigned integral types and uint128.
|
||||
template <typename T>
|
||||
void PrintAsHexLower(T v) {
|
||||
static_assert(!IsSigned<T>::value, "");
|
||||
char *p = storage_ + sizeof(storage_);
|
||||
|
||||
do {
|
||||
p -= 2;
|
||||
constexpr const char* table = numbers_internal::kHexTable;
|
||||
std::memcpy(p, table + 2 * (static_cast<size_t>(v) & 0xFF), 2);
|
||||
if (sizeof(T) == 1) break;
|
||||
v >>= 8;
|
||||
} while (v);
|
||||
if (p[0] == '0') {
|
||||
// We printed one too many digits.
|
||||
++p;
|
||||
}
|
||||
start_ = p;
|
||||
size_ = storage_ + sizeof(storage_) - p;
|
||||
}
|
||||
|
||||
// Print the unsigned integer as hex using uppercase.
|
||||
// Supports unsigned integral types and uint128.
|
||||
template <typename T>
|
||||
void PrintAsHexUpper(T v) {
|
||||
static_assert(!IsSigned<T>::value, "");
|
||||
char *p = storage_ + sizeof(storage_);
|
||||
|
||||
// kHexTable is only lowercase, so do it manually for uppercase.
|
||||
do {
|
||||
*--p = "0123456789ABCDEF"[static_cast<size_t>(v) & 15];
|
||||
v >>= 4;
|
||||
} while (v);
|
||||
start_ = p;
|
||||
size_ = storage_ + sizeof(storage_) - p;
|
||||
}
|
||||
|
||||
// The printed value including the '-' sign if available.
|
||||
// For inputs of value `0`, this will return "0"
|
||||
string_view with_neg_and_zero() const { return {start_, size_}; }
|
||||
|
||||
// The printed value not including the '-' sign.
|
||||
// For inputs of value `0`, this will return "".
|
||||
string_view without_neg_or_zero() const {
|
||||
static_assert('-' < '0', "The check below verifies both.");
|
||||
size_t advance = start_[0] <= '0' ? 1 : 0;
|
||||
return {start_ + advance, size_ - advance};
|
||||
}
|
||||
|
||||
bool is_negative() const { return start_[0] == '-'; }
|
||||
|
||||
private:
|
||||
const char *start_;
|
||||
size_t size_;
|
||||
// Max size: 128 bit value as octal -> 43 digits, plus sign char
|
||||
char storage_[128 / 3 + 1 + 1];
|
||||
};
|
||||
|
||||
// Note: 'o' conversions do not have a base indicator, it's just that
|
||||
// the '#' flag is specified to modify the precision for 'o' conversions.
|
||||
string_view BaseIndicator(const IntDigits &as_digits,
|
||||
const FormatConversionSpecImpl conv) {
|
||||
// always show 0x for %p.
|
||||
bool alt = conv.has_alt_flag() ||
|
||||
conv.conversion_char() == FormatConversionCharInternal::p;
|
||||
bool hex = (conv.conversion_char() == FormatConversionCharInternal::x ||
|
||||
conv.conversion_char() == FormatConversionCharInternal::X ||
|
||||
conv.conversion_char() == FormatConversionCharInternal::p);
|
||||
// From the POSIX description of '#' flag:
|
||||
// "For x or X conversion specifiers, a non-zero result shall have
|
||||
// 0x (or 0X) prefixed to it."
|
||||
if (alt && hex && !as_digits.without_neg_or_zero().empty()) {
|
||||
return conv.conversion_char() == FormatConversionCharInternal::X ? "0X"
|
||||
: "0x";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
string_view SignColumn(bool neg, const FormatConversionSpecImpl conv) {
|
||||
if (conv.conversion_char() == FormatConversionCharInternal::d ||
|
||||
conv.conversion_char() == FormatConversionCharInternal::i) {
|
||||
if (neg) return "-";
|
||||
if (conv.has_show_pos_flag()) return "+";
|
||||
if (conv.has_sign_col_flag()) return " ";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ConvertCharImpl(unsigned char v, const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
size_t fill = 0;
|
||||
if (conv.width() >= 0) fill = conv.width();
|
||||
ReducePadding(1, &fill);
|
||||
if (!conv.has_left_flag()) sink->Append(fill, ' ');
|
||||
sink->Append(1, v);
|
||||
if (conv.has_left_flag()) sink->Append(fill, ' ');
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConvertIntImplInnerSlow(const IntDigits &as_digits,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
// Print as a sequence of Substrings:
|
||||
// [left_spaces][sign][base_indicator][zeroes][formatted][right_spaces]
|
||||
size_t fill = 0;
|
||||
if (conv.width() >= 0) fill = conv.width();
|
||||
|
||||
string_view formatted = as_digits.without_neg_or_zero();
|
||||
ReducePadding(formatted, &fill);
|
||||
|
||||
string_view sign = SignColumn(as_digits.is_negative(), conv);
|
||||
ReducePadding(sign, &fill);
|
||||
|
||||
string_view base_indicator = BaseIndicator(as_digits, conv);
|
||||
ReducePadding(base_indicator, &fill);
|
||||
|
||||
int precision = conv.precision();
|
||||
bool precision_specified = precision >= 0;
|
||||
if (!precision_specified)
|
||||
precision = 1;
|
||||
|
||||
if (conv.has_alt_flag() &&
|
||||
conv.conversion_char() == FormatConversionCharInternal::o) {
|
||||
// From POSIX description of the '#' (alt) flag:
|
||||
// "For o conversion, it increases the precision (if necessary) to
|
||||
// force the first digit of the result to be zero."
|
||||
if (formatted.empty() || *formatted.begin() != '0') {
|
||||
int needed = static_cast<int>(formatted.size()) + 1;
|
||||
precision = std::max(precision, needed);
|
||||
}
|
||||
}
|
||||
|
||||
size_t num_zeroes = Excess(formatted.size(), precision);
|
||||
ReducePadding(num_zeroes, &fill);
|
||||
|
||||
size_t num_left_spaces = !conv.has_left_flag() ? fill : 0;
|
||||
size_t num_right_spaces = conv.has_left_flag() ? fill : 0;
|
||||
|
||||
// From POSIX description of the '0' (zero) flag:
|
||||
// "For d, i, o, u, x, and X conversion specifiers, if a precision
|
||||
// is specified, the '0' flag is ignored."
|
||||
if (!precision_specified && conv.has_zero_flag()) {
|
||||
num_zeroes += num_left_spaces;
|
||||
num_left_spaces = 0;
|
||||
}
|
||||
|
||||
sink->Append(num_left_spaces, ' ');
|
||||
sink->Append(sign);
|
||||
sink->Append(base_indicator);
|
||||
sink->Append(num_zeroes, '0');
|
||||
sink->Append(formatted);
|
||||
sink->Append(num_right_spaces, ' ');
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool ConvertIntArg(T v, const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
using U = typename MakeUnsigned<T>::type;
|
||||
IntDigits as_digits;
|
||||
|
||||
// This odd casting is due to a bug in -Wswitch behavior in gcc49 which causes
|
||||
// it to complain about a switch/case type mismatch, even though both are
|
||||
// FormatConverionChar. Likely this is because at this point
|
||||
// FormatConversionChar is declared, but not defined.
|
||||
switch (static_cast<uint8_t>(conv.conversion_char())) {
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::c):
|
||||
return ConvertCharImpl(static_cast<unsigned char>(v), conv, sink);
|
||||
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::o):
|
||||
as_digits.PrintAsOct(static_cast<U>(v));
|
||||
break;
|
||||
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::x):
|
||||
as_digits.PrintAsHexLower(static_cast<U>(v));
|
||||
break;
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::X):
|
||||
as_digits.PrintAsHexUpper(static_cast<U>(v));
|
||||
break;
|
||||
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::u):
|
||||
as_digits.PrintAsDec(static_cast<U>(v));
|
||||
break;
|
||||
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::d):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::i):
|
||||
as_digits.PrintAsDec(v);
|
||||
break;
|
||||
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::a):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::e):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::f):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::g):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::A):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::E):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::F):
|
||||
case static_cast<uint8_t>(FormatConversionCharInternal::G):
|
||||
return ConvertFloatImpl(static_cast<double>(v), conv, sink);
|
||||
|
||||
default:
|
||||
ABSL_INTERNAL_ASSUME(false);
|
||||
}
|
||||
|
||||
if (conv.is_basic()) {
|
||||
sink->Append(as_digits.with_neg_and_zero());
|
||||
return true;
|
||||
}
|
||||
return ConvertIntImplInnerSlow(as_digits, conv, sink);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool ConvertFloatArg(T v, const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return FormatConversionCharIsFloat(conv.conversion_char()) &&
|
||||
ConvertFloatImpl(v, conv, sink);
|
||||
}
|
||||
|
||||
inline bool ConvertStringArg(string_view v, const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
if (conv.is_basic()) {
|
||||
sink->Append(v);
|
||||
return true;
|
||||
}
|
||||
return sink->PutPaddedString(v, conv.width(), conv.precision(),
|
||||
conv.has_left_flag());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ==================== Strings ====================
|
||||
StringConvertResult FormatConvertImpl(const std::string &v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertStringArg(v, conv, sink)};
|
||||
}
|
||||
|
||||
StringConvertResult FormatConvertImpl(string_view v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertStringArg(v, conv, sink)};
|
||||
}
|
||||
|
||||
ArgConvertResult<FormatConversionCharSetUnion(
|
||||
FormatConversionCharSetInternal::s, FormatConversionCharSetInternal::p)>
|
||||
FormatConvertImpl(const char *v, const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
if (conv.conversion_char() == FormatConversionCharInternal::p)
|
||||
return {FormatConvertImpl(VoidPtr(v), conv, sink).value};
|
||||
size_t len;
|
||||
if (v == nullptr) {
|
||||
len = 0;
|
||||
} else if (conv.precision() < 0) {
|
||||
len = std::strlen(v);
|
||||
} else {
|
||||
// If precision is set, we look for the NUL-terminator on the valid range.
|
||||
len = std::find(v, v + conv.precision(), '\0') - v;
|
||||
}
|
||||
return {ConvertStringArg(string_view(v, len), conv, sink)};
|
||||
}
|
||||
|
||||
// ==================== Raw pointers ====================
|
||||
ArgConvertResult<FormatConversionCharSetInternal::p> FormatConvertImpl(
|
||||
VoidPtr v, const FormatConversionSpecImpl conv, FormatSinkImpl *sink) {
|
||||
if (!v.value) {
|
||||
sink->Append("(nil)");
|
||||
return {true};
|
||||
}
|
||||
IntDigits as_digits;
|
||||
as_digits.PrintAsHexLower(v.value);
|
||||
return {ConvertIntImplInnerSlow(as_digits, conv, sink)};
|
||||
}
|
||||
|
||||
// ==================== Floats ====================
|
||||
FloatingConvertResult FormatConvertImpl(float v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertFloatArg(v, conv, sink)};
|
||||
}
|
||||
FloatingConvertResult FormatConvertImpl(double v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertFloatArg(v, conv, sink)};
|
||||
}
|
||||
FloatingConvertResult FormatConvertImpl(long double v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertFloatArg(v, conv, sink)};
|
||||
}
|
||||
|
||||
// ==================== Chars ====================
|
||||
IntegralConvertResult FormatConvertImpl(char v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(signed char v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(unsigned char v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
|
||||
// ==================== Ints ====================
|
||||
IntegralConvertResult FormatConvertImpl(short v, // NOLINT
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(unsigned short v, // NOLINT
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(int v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(unsigned v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(long v, // NOLINT
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(unsigned long v, // NOLINT
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(long long v, // NOLINT
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(absl::int128 v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
IntegralConvertResult FormatConvertImpl(absl::uint128 v,
|
||||
const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl *sink) {
|
||||
return {ConvertIntArg(v, conv, sink)};
|
||||
}
|
||||
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_();
|
||||
|
||||
|
||||
|
||||
} // namespace str_format_internal
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
526
third_party/abseil-cpp/absl/strings/internal/str_format/arg.h
vendored
Normal file
526
third_party/abseil-cpp/absl/strings/internal/str_format/arg.h
vendored
Normal file
@@ -0,0 +1,526 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_
|
||||
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/numeric/int128.h"
|
||||
#include "absl/strings/internal/str_format/extension.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
class Cord;
|
||||
class FormatCountCapture;
|
||||
class FormatSink;
|
||||
|
||||
template <absl::FormatConversionCharSet C>
|
||||
struct FormatConvertResult;
|
||||
class FormatConversionSpec;
|
||||
|
||||
namespace str_format_internal {
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct HasUserDefinedConvert : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct HasUserDefinedConvert<T, void_t<decltype(AbslFormatConvert(
|
||||
std::declval<const T&>(),
|
||||
std::declval<const FormatConversionSpec&>(),
|
||||
std::declval<FormatSink*>()))>>
|
||||
: std::true_type {};
|
||||
|
||||
void AbslFormatConvert(); // Stops the lexical name lookup
|
||||
template <typename T>
|
||||
auto FormatConvertImpl(const T& v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink)
|
||||
-> decltype(AbslFormatConvert(v,
|
||||
std::declval<const FormatConversionSpec&>(),
|
||||
std::declval<FormatSink*>())) {
|
||||
using FormatConversionSpecT =
|
||||
absl::enable_if_t<sizeof(const T& (*)()) != 0, FormatConversionSpec>;
|
||||
using FormatSinkT =
|
||||
absl::enable_if_t<sizeof(const T& (*)()) != 0, FormatSink>;
|
||||
auto fcs = conv.Wrap<FormatConversionSpecT>();
|
||||
auto fs = sink->Wrap<FormatSinkT>();
|
||||
return AbslFormatConvert(v, fcs, &fs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class StreamedWrapper;
|
||||
|
||||
// If 'v' can be converted (in the printf sense) according to 'conv',
|
||||
// then convert it, appending to `sink` and return `true`.
|
||||
// Otherwise fail and return `false`.
|
||||
|
||||
// AbslFormatConvert(v, conv, sink) is intended to be found by ADL on 'v'
|
||||
// as an extension mechanism. These FormatConvertImpl functions are the default
|
||||
// implementations.
|
||||
// The ADL search is augmented via the 'Sink*' parameter, which also
|
||||
// serves as a disambiguator to reject possible unintended 'AbslFormatConvert'
|
||||
// functions in the namespaces associated with 'v'.
|
||||
|
||||
// Raw pointers.
|
||||
struct VoidPtr {
|
||||
VoidPtr() = default;
|
||||
template <typename T,
|
||||
decltype(reinterpret_cast<uintptr_t>(std::declval<T*>())) = 0>
|
||||
VoidPtr(T* ptr) // NOLINT
|
||||
: value(ptr ? reinterpret_cast<uintptr_t>(ptr) : 0) {}
|
||||
uintptr_t value;
|
||||
};
|
||||
|
||||
template <FormatConversionCharSet C>
|
||||
struct ArgConvertResult {
|
||||
bool value;
|
||||
};
|
||||
|
||||
template <FormatConversionCharSet C>
|
||||
constexpr FormatConversionCharSet ExtractCharSet(FormatConvertResult<C>) {
|
||||
return C;
|
||||
}
|
||||
|
||||
template <FormatConversionCharSet C>
|
||||
constexpr FormatConversionCharSet ExtractCharSet(ArgConvertResult<C>) {
|
||||
return C;
|
||||
}
|
||||
|
||||
using StringConvertResult =
|
||||
ArgConvertResult<FormatConversionCharSetInternal::s>;
|
||||
ArgConvertResult<FormatConversionCharSetInternal::p> FormatConvertImpl(
|
||||
VoidPtr v, FormatConversionSpecImpl conv, FormatSinkImpl* sink);
|
||||
|
||||
// Strings.
|
||||
StringConvertResult FormatConvertImpl(const std::string& v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
StringConvertResult FormatConvertImpl(string_view v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
#if defined(ABSL_HAVE_STD_STRING_VIEW) && !defined(ABSL_USES_STD_STRING_VIEW)
|
||||
inline StringConvertResult FormatConvertImpl(std::string_view v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink) {
|
||||
return FormatConvertImpl(absl::string_view(v.data(), v.size()), conv, sink);
|
||||
}
|
||||
#endif // ABSL_HAVE_STD_STRING_VIEW && !ABSL_USES_STD_STRING_VIEW
|
||||
|
||||
ArgConvertResult<FormatConversionCharSetUnion(
|
||||
FormatConversionCharSetInternal::s, FormatConversionCharSetInternal::p)>
|
||||
FormatConvertImpl(const char* v, const FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
|
||||
template <class AbslCord, typename std::enable_if<std::is_same<
|
||||
AbslCord, absl::Cord>::value>::type* = nullptr>
|
||||
StringConvertResult FormatConvertImpl(const AbslCord& value,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink) {
|
||||
bool is_left = conv.has_left_flag();
|
||||
size_t space_remaining = 0;
|
||||
|
||||
int width = conv.width();
|
||||
if (width >= 0) space_remaining = width;
|
||||
|
||||
size_t to_write = value.size();
|
||||
|
||||
int precision = conv.precision();
|
||||
if (precision >= 0)
|
||||
to_write = (std::min)(to_write, static_cast<size_t>(precision));
|
||||
|
||||
space_remaining = Excess(to_write, space_remaining);
|
||||
|
||||
if (space_remaining > 0 && !is_left) sink->Append(space_remaining, ' ');
|
||||
|
||||
for (string_view piece : value.Chunks()) {
|
||||
if (piece.size() > to_write) {
|
||||
piece.remove_suffix(piece.size() - to_write);
|
||||
to_write = 0;
|
||||
} else {
|
||||
to_write -= piece.size();
|
||||
}
|
||||
sink->Append(piece);
|
||||
if (to_write == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (space_remaining > 0 && is_left) sink->Append(space_remaining, ' ');
|
||||
return {true};
|
||||
}
|
||||
|
||||
using IntegralConvertResult = ArgConvertResult<FormatConversionCharSetUnion(
|
||||
FormatConversionCharSetInternal::c,
|
||||
FormatConversionCharSetInternal::kNumeric,
|
||||
FormatConversionCharSetInternal::kStar)>;
|
||||
using FloatingConvertResult =
|
||||
ArgConvertResult<FormatConversionCharSetInternal::kFloating>;
|
||||
|
||||
// Floats.
|
||||
FloatingConvertResult FormatConvertImpl(float v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
FloatingConvertResult FormatConvertImpl(double v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
FloatingConvertResult FormatConvertImpl(long double v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
|
||||
// Chars.
|
||||
IntegralConvertResult FormatConvertImpl(char v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(signed char v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(unsigned char v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
|
||||
// Ints.
|
||||
IntegralConvertResult FormatConvertImpl(short v, // NOLINT
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(unsigned short v, // NOLINT
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(int v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(unsigned v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(long v, // NOLINT
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(unsigned long v, // NOLINT
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(long long v, // NOLINT
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(int128 v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
IntegralConvertResult FormatConvertImpl(uint128 v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink);
|
||||
template <typename T, enable_if_t<std::is_same<T, bool>::value, int> = 0>
|
||||
IntegralConvertResult FormatConvertImpl(T v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink) {
|
||||
return FormatConvertImpl(static_cast<int>(v), conv, sink);
|
||||
}
|
||||
|
||||
// We provide this function to help the checker, but it is never defined.
|
||||
// FormatArgImpl will use the underlying Convert functions instead.
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_enum<T>::value &&
|
||||
!HasUserDefinedConvert<T>::value,
|
||||
IntegralConvertResult>::type
|
||||
FormatConvertImpl(T v, FormatConversionSpecImpl conv, FormatSinkImpl* sink);
|
||||
|
||||
template <typename T>
|
||||
StringConvertResult FormatConvertImpl(const StreamedWrapper<T>& v,
|
||||
FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* out) {
|
||||
std::ostringstream oss;
|
||||
oss << v.v_;
|
||||
if (!oss) return {false};
|
||||
return str_format_internal::FormatConvertImpl(oss.str(), conv, out);
|
||||
}
|
||||
|
||||
// Use templates and dependent types to delay evaluation of the function
|
||||
// until after FormatCountCapture is fully defined.
|
||||
struct FormatCountCaptureHelper {
|
||||
template <class T = int>
|
||||
static ArgConvertResult<FormatConversionCharSetInternal::n> ConvertHelper(
|
||||
const FormatCountCapture& v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink) {
|
||||
const absl::enable_if_t<sizeof(T) != 0, FormatCountCapture>& v2 = v;
|
||||
|
||||
if (conv.conversion_char() !=
|
||||
str_format_internal::FormatConversionCharInternal::n) {
|
||||
return {false};
|
||||
}
|
||||
*v2.p_ = static_cast<int>(sink->size());
|
||||
return {true};
|
||||
}
|
||||
};
|
||||
|
||||
template <class T = int>
|
||||
ArgConvertResult<FormatConversionCharSetInternal::n> FormatConvertImpl(
|
||||
const FormatCountCapture& v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* sink) {
|
||||
return FormatCountCaptureHelper::ConvertHelper(v, conv, sink);
|
||||
}
|
||||
|
||||
// Helper friend struct to hide implementation details from the public API of
|
||||
// FormatArgImpl.
|
||||
struct FormatArgImplFriend {
|
||||
template <typename Arg>
|
||||
static bool ToInt(Arg arg, int* out) {
|
||||
// A value initialized FormatConversionSpecImpl has a `none` conv, which
|
||||
// tells the dispatcher to run the `int` conversion.
|
||||
return arg.dispatcher_(arg.data_, {}, out);
|
||||
}
|
||||
|
||||
template <typename Arg>
|
||||
static bool Convert(Arg arg, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* out) {
|
||||
return arg.dispatcher_(arg.data_, conv, out);
|
||||
}
|
||||
|
||||
template <typename Arg>
|
||||
static typename Arg::Dispatcher GetVTablePtrForTest(Arg arg) {
|
||||
return arg.dispatcher_;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Arg>
|
||||
constexpr FormatConversionCharSet ArgumentToConv() {
|
||||
return absl::str_format_internal::ExtractCharSet(
|
||||
decltype(str_format_internal::FormatConvertImpl(
|
||||
std::declval<const Arg&>(),
|
||||
std::declval<const FormatConversionSpecImpl&>(),
|
||||
std::declval<FormatSinkImpl*>())){});
|
||||
}
|
||||
|
||||
// A type-erased handle to a format argument.
|
||||
class FormatArgImpl {
|
||||
private:
|
||||
enum { kInlinedSpace = 8 };
|
||||
|
||||
using VoidPtr = str_format_internal::VoidPtr;
|
||||
|
||||
union Data {
|
||||
const void* ptr;
|
||||
const volatile void* volatile_ptr;
|
||||
char buf[kInlinedSpace];
|
||||
};
|
||||
|
||||
using Dispatcher = bool (*)(Data, FormatConversionSpecImpl, void* out);
|
||||
|
||||
template <typename T>
|
||||
struct store_by_value
|
||||
: std::integral_constant<bool, (sizeof(T) <= kInlinedSpace) &&
|
||||
(std::is_integral<T>::value ||
|
||||
std::is_floating_point<T>::value ||
|
||||
std::is_pointer<T>::value ||
|
||||
std::is_same<VoidPtr, T>::value)> {};
|
||||
|
||||
enum StoragePolicy { ByPointer, ByVolatilePointer, ByValue };
|
||||
template <typename T>
|
||||
struct storage_policy
|
||||
: std::integral_constant<StoragePolicy,
|
||||
(std::is_volatile<T>::value
|
||||
? ByVolatilePointer
|
||||
: (store_by_value<T>::value ? ByValue
|
||||
: ByPointer))> {
|
||||
};
|
||||
|
||||
// To reduce the number of vtables we will decay values before hand.
|
||||
// Anything with a user-defined Convert will get its own vtable.
|
||||
// For everything else:
|
||||
// - Decay char* and char arrays into `const char*`
|
||||
// - Decay any other pointer to `const void*`
|
||||
// - Decay all enums to their underlying type.
|
||||
// - Decay function pointers to void*.
|
||||
template <typename T, typename = void>
|
||||
struct DecayType {
|
||||
static constexpr bool kHasUserDefined =
|
||||
str_format_internal::HasUserDefinedConvert<T>::value;
|
||||
using type = typename std::conditional<
|
||||
!kHasUserDefined && std::is_convertible<T, const char*>::value,
|
||||
const char*,
|
||||
typename std::conditional<!kHasUserDefined &&
|
||||
std::is_convertible<T, VoidPtr>::value,
|
||||
VoidPtr, const T&>::type>::type;
|
||||
};
|
||||
template <typename T>
|
||||
struct DecayType<T,
|
||||
typename std::enable_if<
|
||||
!str_format_internal::HasUserDefinedConvert<T>::value &&
|
||||
std::is_enum<T>::value>::type> {
|
||||
using type = typename std::underlying_type<T>::type;
|
||||
};
|
||||
|
||||
public:
|
||||
template <typename T>
|
||||
explicit FormatArgImpl(const T& value) {
|
||||
using D = typename DecayType<T>::type;
|
||||
static_assert(
|
||||
std::is_same<D, const T&>::value || storage_policy<D>::value == ByValue,
|
||||
"Decayed types must be stored by value");
|
||||
Init(static_cast<D>(value));
|
||||
}
|
||||
|
||||
private:
|
||||
friend struct str_format_internal::FormatArgImplFriend;
|
||||
template <typename T, StoragePolicy = storage_policy<T>::value>
|
||||
struct Manager;
|
||||
|
||||
template <typename T>
|
||||
struct Manager<T, ByPointer> {
|
||||
static Data SetValue(const T& value) {
|
||||
Data data;
|
||||
data.ptr = std::addressof(value);
|
||||
return data;
|
||||
}
|
||||
|
||||
static const T& Value(Data arg) { return *static_cast<const T*>(arg.ptr); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Manager<T, ByVolatilePointer> {
|
||||
static Data SetValue(const T& value) {
|
||||
Data data;
|
||||
data.volatile_ptr = &value;
|
||||
return data;
|
||||
}
|
||||
|
||||
static const T& Value(Data arg) {
|
||||
return *static_cast<const T*>(arg.volatile_ptr);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Manager<T, ByValue> {
|
||||
static Data SetValue(const T& value) {
|
||||
Data data;
|
||||
memcpy(data.buf, &value, sizeof(value));
|
||||
return data;
|
||||
}
|
||||
|
||||
static T Value(Data arg) {
|
||||
T value;
|
||||
memcpy(&value, arg.buf, sizeof(T));
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void Init(const T& value) {
|
||||
data_ = Manager<T>::SetValue(value);
|
||||
dispatcher_ = &Dispatch<T>;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static int ToIntVal(const T& val) {
|
||||
using CommonType = typename std::conditional<std::is_signed<T>::value,
|
||||
int64_t, uint64_t>::type;
|
||||
if (static_cast<CommonType>(val) >
|
||||
static_cast<CommonType>((std::numeric_limits<int>::max)())) {
|
||||
return (std::numeric_limits<int>::max)();
|
||||
} else if (std::is_signed<T>::value &&
|
||||
static_cast<CommonType>(val) <
|
||||
static_cast<CommonType>((std::numeric_limits<int>::min)())) {
|
||||
return (std::numeric_limits<int>::min)();
|
||||
}
|
||||
return static_cast<int>(val);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool ToInt(Data arg, int* out, std::true_type /* is_integral */,
|
||||
std::false_type) {
|
||||
*out = ToIntVal(Manager<T>::Value(arg));
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool ToInt(Data arg, int* out, std::false_type,
|
||||
std::true_type /* is_enum */) {
|
||||
*out = ToIntVal(static_cast<typename std::underlying_type<T>::type>(
|
||||
Manager<T>::Value(arg)));
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool ToInt(Data, int*, std::false_type, std::false_type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool Dispatch(Data arg, FormatConversionSpecImpl spec, void* out) {
|
||||
// A `none` conv indicates that we want the `int` conversion.
|
||||
if (ABSL_PREDICT_FALSE(spec.conversion_char() ==
|
||||
FormatConversionCharInternal::kNone)) {
|
||||
return ToInt<T>(arg, static_cast<int*>(out), std::is_integral<T>(),
|
||||
std::is_enum<T>());
|
||||
}
|
||||
if (ABSL_PREDICT_FALSE(!Contains(ArgumentToConv<T>(),
|
||||
spec.conversion_char()))) {
|
||||
return false;
|
||||
}
|
||||
return str_format_internal::FormatConvertImpl(
|
||||
Manager<T>::Value(arg), spec,
|
||||
static_cast<FormatSinkImpl*>(out))
|
||||
.value;
|
||||
}
|
||||
|
||||
Data data_;
|
||||
Dispatcher dispatcher_;
|
||||
};
|
||||
|
||||
#define ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(T, E) \
|
||||
E template bool FormatArgImpl::Dispatch<T>(Data, FormatConversionSpecImpl, \
|
||||
void*)
|
||||
|
||||
#define ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(...) \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(str_format_internal::VoidPtr, \
|
||||
__VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(bool, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(char, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(signed char, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned char, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(short, __VA_ARGS__); /* NOLINT */ \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned short, /* NOLINT */ \
|
||||
__VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(int, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned int, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long, __VA_ARGS__); /* NOLINT */ \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long, /* NOLINT */ \
|
||||
__VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long long, /* NOLINT */ \
|
||||
__VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long long, /* NOLINT */ \
|
||||
__VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(int128, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(uint128, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(float, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long double, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(const char*, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(std::string, __VA_ARGS__); \
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(string_view, __VA_ARGS__)
|
||||
|
||||
ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern);
|
||||
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_ARG_H_
|
||||
130
third_party/abseil-cpp/absl/strings/internal/str_format/arg_test.cc
vendored
Normal file
130
third_party/abseil-cpp/absl/strings/internal/str_format/arg_test.cc
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/arg.h"
|
||||
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
namespace {
|
||||
|
||||
class FormatArgImplTest : public ::testing::Test {
|
||||
public:
|
||||
enum Color { kRed, kGreen, kBlue };
|
||||
|
||||
static const char *hi() { return "hi"; }
|
||||
|
||||
struct X {};
|
||||
|
||||
X x_;
|
||||
};
|
||||
|
||||
inline FormatConvertResult<FormatConversionCharSet{}> AbslFormatConvert(
|
||||
const FormatArgImplTest::X &, const FormatConversionSpec &, FormatSink *) {
|
||||
return {false};
|
||||
}
|
||||
|
||||
TEST_F(FormatArgImplTest, ToInt) {
|
||||
int out = 0;
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(1), &out));
|
||||
EXPECT_EQ(1, out);
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(-1), &out));
|
||||
EXPECT_EQ(-1, out);
|
||||
EXPECT_TRUE(
|
||||
FormatArgImplFriend::ToInt(FormatArgImpl(static_cast<char>(64)), &out));
|
||||
EXPECT_EQ(64, out);
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(
|
||||
FormatArgImpl(static_cast<unsigned long long>(123456)), &out)); // NOLINT
|
||||
EXPECT_EQ(123456, out);
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(
|
||||
FormatArgImpl(static_cast<unsigned long long>( // NOLINT
|
||||
std::numeric_limits<int>::max()) +
|
||||
1),
|
||||
&out));
|
||||
EXPECT_EQ(std::numeric_limits<int>::max(), out);
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(
|
||||
FormatArgImpl(static_cast<long long>( // NOLINT
|
||||
std::numeric_limits<int>::min()) -
|
||||
10),
|
||||
&out));
|
||||
EXPECT_EQ(std::numeric_limits<int>::min(), out);
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(false), &out));
|
||||
EXPECT_EQ(0, out);
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(true), &out));
|
||||
EXPECT_EQ(1, out);
|
||||
EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(2.2), &out));
|
||||
EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(3.2f), &out));
|
||||
EXPECT_FALSE(FormatArgImplFriend::ToInt(
|
||||
FormatArgImpl(static_cast<int *>(nullptr)), &out));
|
||||
EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(hi()), &out));
|
||||
EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl("hi"), &out));
|
||||
EXPECT_FALSE(FormatArgImplFriend::ToInt(FormatArgImpl(x_), &out));
|
||||
EXPECT_TRUE(FormatArgImplFriend::ToInt(FormatArgImpl(kBlue), &out));
|
||||
EXPECT_EQ(2, out);
|
||||
}
|
||||
|
||||
extern const char kMyArray[];
|
||||
|
||||
TEST_F(FormatArgImplTest, CharArraysDecayToCharPtr) {
|
||||
const char* a = "";
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
|
||||
FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("")));
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
|
||||
FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("A")));
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
|
||||
FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl("ABC")));
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(a)),
|
||||
FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(kMyArray)));
|
||||
}
|
||||
|
||||
TEST_F(FormatArgImplTest, OtherPtrDecayToVoidPtr) {
|
||||
auto expected = FormatArgImplFriend::GetVTablePtrForTest(
|
||||
FormatArgImpl(static_cast<void *>(nullptr)));
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(
|
||||
FormatArgImpl(static_cast<int *>(nullptr))),
|
||||
expected);
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(
|
||||
FormatArgImpl(static_cast<volatile int *>(nullptr))),
|
||||
expected);
|
||||
|
||||
auto p = static_cast<void (*)()>([] {});
|
||||
EXPECT_EQ(FormatArgImplFriend::GetVTablePtrForTest(FormatArgImpl(p)),
|
||||
expected);
|
||||
}
|
||||
|
||||
TEST_F(FormatArgImplTest, WorksWithCharArraysOfUnknownSize) {
|
||||
std::string s;
|
||||
FormatSinkImpl sink(&s);
|
||||
FormatConversionSpecImpl conv;
|
||||
FormatConversionSpecImplFriend::SetConversionChar(
|
||||
FormatConversionCharInternal::s, &conv);
|
||||
FormatConversionSpecImplFriend::SetFlags(Flags(), &conv);
|
||||
FormatConversionSpecImplFriend::SetWidth(-1, &conv);
|
||||
FormatConversionSpecImplFriend::SetPrecision(-1, &conv);
|
||||
EXPECT_TRUE(
|
||||
FormatArgImplFriend::Convert(FormatArgImpl(kMyArray), conv, &sink));
|
||||
sink.Flush();
|
||||
EXPECT_EQ("ABCDE", s);
|
||||
}
|
||||
const char kMyArray[] = "ABCDE";
|
||||
|
||||
} // namespace
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
258
third_party/abseil-cpp/absl/strings/internal/str_format/bind.cc
vendored
Normal file
258
third_party/abseil-cpp/absl/strings/internal/str_format/bind.cc
vendored
Normal file
@@ -0,0 +1,258 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/bind.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
namespace {
|
||||
|
||||
inline bool BindFromPosition(int position, int* value,
|
||||
absl::Span<const FormatArgImpl> pack) {
|
||||
assert(position > 0);
|
||||
if (static_cast<size_t>(position) > pack.size()) {
|
||||
return false;
|
||||
}
|
||||
// -1 because positions are 1-based
|
||||
return FormatArgImplFriend::ToInt(pack[position - 1], value);
|
||||
}
|
||||
|
||||
class ArgContext {
|
||||
public:
|
||||
explicit ArgContext(absl::Span<const FormatArgImpl> pack) : pack_(pack) {}
|
||||
|
||||
// Fill 'bound' with the results of applying the context's argument pack
|
||||
// to the specified 'unbound'. We synthesize a BoundConversion by
|
||||
// lining up a UnboundConversion with a user argument. We also
|
||||
// resolve any '*' specifiers for width and precision, so after
|
||||
// this call, 'bound' has all the information it needs to be formatted.
|
||||
// Returns false on failure.
|
||||
bool Bind(const UnboundConversion* unbound, BoundConversion* bound);
|
||||
|
||||
private:
|
||||
absl::Span<const FormatArgImpl> pack_;
|
||||
};
|
||||
|
||||
inline bool ArgContext::Bind(const UnboundConversion* unbound,
|
||||
BoundConversion* bound) {
|
||||
const FormatArgImpl* arg = nullptr;
|
||||
int arg_position = unbound->arg_position;
|
||||
if (static_cast<size_t>(arg_position - 1) >= pack_.size()) return false;
|
||||
arg = &pack_[arg_position - 1]; // 1-based
|
||||
|
||||
if (unbound->flags != Flags::kBasic) {
|
||||
int width = unbound->width.value();
|
||||
bool force_left = false;
|
||||
if (unbound->width.is_from_arg()) {
|
||||
if (!BindFromPosition(unbound->width.get_from_arg(), &width, pack_))
|
||||
return false;
|
||||
if (width < 0) {
|
||||
// "A negative field width is taken as a '-' flag followed by a
|
||||
// positive field width."
|
||||
force_left = true;
|
||||
// Make sure we don't overflow the width when negating it.
|
||||
width = -std::max(width, -std::numeric_limits<int>::max());
|
||||
}
|
||||
}
|
||||
|
||||
int precision = unbound->precision.value();
|
||||
if (unbound->precision.is_from_arg()) {
|
||||
if (!BindFromPosition(unbound->precision.get_from_arg(), &precision,
|
||||
pack_))
|
||||
return false;
|
||||
}
|
||||
|
||||
FormatConversionSpecImplFriend::SetWidth(width, bound);
|
||||
FormatConversionSpecImplFriend::SetPrecision(precision, bound);
|
||||
|
||||
if (force_left) {
|
||||
FormatConversionSpecImplFriend::SetFlags(unbound->flags | Flags::kLeft,
|
||||
bound);
|
||||
} else {
|
||||
FormatConversionSpecImplFriend::SetFlags(unbound->flags, bound);
|
||||
}
|
||||
} else {
|
||||
FormatConversionSpecImplFriend::SetFlags(unbound->flags, bound);
|
||||
FormatConversionSpecImplFriend::SetWidth(-1, bound);
|
||||
FormatConversionSpecImplFriend::SetPrecision(-1, bound);
|
||||
}
|
||||
FormatConversionSpecImplFriend::SetConversionChar(unbound->conv, bound);
|
||||
bound->set_arg(arg);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Converter>
|
||||
class ConverterConsumer {
|
||||
public:
|
||||
ConverterConsumer(Converter converter, absl::Span<const FormatArgImpl> pack)
|
||||
: converter_(converter), arg_context_(pack) {}
|
||||
|
||||
bool Append(string_view s) {
|
||||
converter_.Append(s);
|
||||
return true;
|
||||
}
|
||||
bool ConvertOne(const UnboundConversion& conv, string_view conv_string) {
|
||||
BoundConversion bound;
|
||||
if (!arg_context_.Bind(&conv, &bound)) return false;
|
||||
return converter_.ConvertOne(bound, conv_string);
|
||||
}
|
||||
|
||||
private:
|
||||
Converter converter_;
|
||||
ArgContext arg_context_;
|
||||
};
|
||||
|
||||
template <typename Converter>
|
||||
bool ConvertAll(const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args, Converter converter) {
|
||||
if (format.has_parsed_conversion()) {
|
||||
return format.parsed_conversion()->ProcessFormat(
|
||||
ConverterConsumer<Converter>(converter, args));
|
||||
} else {
|
||||
return ParseFormatString(format.str(),
|
||||
ConverterConsumer<Converter>(converter, args));
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultConverter {
|
||||
public:
|
||||
explicit DefaultConverter(FormatSinkImpl* sink) : sink_(sink) {}
|
||||
|
||||
void Append(string_view s) const { sink_->Append(s); }
|
||||
|
||||
bool ConvertOne(const BoundConversion& bound, string_view /*conv*/) const {
|
||||
return FormatArgImplFriend::Convert(*bound.arg(), bound, sink_);
|
||||
}
|
||||
|
||||
private:
|
||||
FormatSinkImpl* sink_;
|
||||
};
|
||||
|
||||
class SummarizingConverter {
|
||||
public:
|
||||
explicit SummarizingConverter(FormatSinkImpl* sink) : sink_(sink) {}
|
||||
|
||||
void Append(string_view s) const { sink_->Append(s); }
|
||||
|
||||
bool ConvertOne(const BoundConversion& bound, string_view /*conv*/) const {
|
||||
UntypedFormatSpecImpl spec("%d");
|
||||
|
||||
std::ostringstream ss;
|
||||
ss << "{" << Streamable(spec, {*bound.arg()}) << ":"
|
||||
<< FormatConversionSpecImplFriend::FlagsToString(bound);
|
||||
if (bound.width() >= 0) ss << bound.width();
|
||||
if (bound.precision() >= 0) ss << "." << bound.precision();
|
||||
ss << bound.conversion_char() << "}";
|
||||
Append(ss.str());
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
FormatSinkImpl* sink_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
bool BindWithPack(const UnboundConversion* props,
|
||||
absl::Span<const FormatArgImpl> pack,
|
||||
BoundConversion* bound) {
|
||||
return ArgContext(pack).Bind(props, bound);
|
||||
}
|
||||
|
||||
std::string Summarize(const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args) {
|
||||
typedef SummarizingConverter Converter;
|
||||
std::string out;
|
||||
{
|
||||
// inner block to destroy sink before returning out. It ensures a last
|
||||
// flush.
|
||||
FormatSinkImpl sink(&out);
|
||||
if (!ConvertAll(format, args, Converter(&sink))) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool FormatUntyped(FormatRawSinkImpl raw_sink,
|
||||
const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args) {
|
||||
FormatSinkImpl sink(raw_sink);
|
||||
using Converter = DefaultConverter;
|
||||
return ConvertAll(format, args, Converter(&sink));
|
||||
}
|
||||
|
||||
std::ostream& Streamable::Print(std::ostream& os) const {
|
||||
if (!FormatUntyped(&os, format_, args_)) os.setstate(std::ios::failbit);
|
||||
return os;
|
||||
}
|
||||
|
||||
std::string& AppendPack(std::string* out, const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args) {
|
||||
size_t orig = out->size();
|
||||
if (ABSL_PREDICT_FALSE(!FormatUntyped(out, format, args))) {
|
||||
out->erase(orig);
|
||||
}
|
||||
return *out;
|
||||
}
|
||||
|
||||
std::string FormatPack(const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args) {
|
||||
std::string out;
|
||||
if (ABSL_PREDICT_FALSE(!FormatUntyped(&out, format, args))) {
|
||||
out.clear();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int FprintF(std::FILE* output, const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args) {
|
||||
FILERawSink sink(output);
|
||||
if (!FormatUntyped(&sink, format, args)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (sink.error()) {
|
||||
errno = sink.error();
|
||||
return -1;
|
||||
}
|
||||
if (sink.count() > static_cast<size_t>(std::numeric_limits<int>::max())) {
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
return static_cast<int>(sink.count());
|
||||
}
|
||||
|
||||
int SnprintF(char* output, size_t size, const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args) {
|
||||
BufferRawSink sink(output, size ? size - 1 : 0);
|
||||
if (!FormatUntyped(&sink, format, args)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
size_t total = sink.total_written();
|
||||
if (size) output[std::min(total, size - 1)] = 0;
|
||||
return static_cast<int>(total);
|
||||
}
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
217
third_party/abseil-cpp/absl/strings/internal/str_format/bind.h
vendored
Normal file
217
third_party/abseil-cpp/absl/strings/internal/str_format/bind.h
vendored
Normal file
@@ -0,0 +1,217 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
|
||||
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/strings/internal/str_format/arg.h"
|
||||
#include "absl/strings/internal/str_format/checker.h"
|
||||
#include "absl/strings/internal/str_format/parser.h"
|
||||
#include "absl/types/span.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
class UntypedFormatSpec;
|
||||
|
||||
namespace str_format_internal {
|
||||
|
||||
class BoundConversion : public FormatConversionSpecImpl {
|
||||
public:
|
||||
const FormatArgImpl* arg() const { return arg_; }
|
||||
void set_arg(const FormatArgImpl* a) { arg_ = a; }
|
||||
|
||||
private:
|
||||
const FormatArgImpl* arg_;
|
||||
};
|
||||
|
||||
// This is the type-erased class that the implementation uses.
|
||||
class UntypedFormatSpecImpl {
|
||||
public:
|
||||
UntypedFormatSpecImpl() = delete;
|
||||
|
||||
explicit UntypedFormatSpecImpl(string_view s)
|
||||
: data_(s.data()), size_(s.size()) {}
|
||||
explicit UntypedFormatSpecImpl(
|
||||
const str_format_internal::ParsedFormatBase* pc)
|
||||
: data_(pc), size_(~size_t{}) {}
|
||||
|
||||
bool has_parsed_conversion() const { return size_ == ~size_t{}; }
|
||||
|
||||
string_view str() const {
|
||||
assert(!has_parsed_conversion());
|
||||
return string_view(static_cast<const char*>(data_), size_);
|
||||
}
|
||||
const str_format_internal::ParsedFormatBase* parsed_conversion() const {
|
||||
assert(has_parsed_conversion());
|
||||
return static_cast<const str_format_internal::ParsedFormatBase*>(data_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static const UntypedFormatSpecImpl& Extract(const T& s) {
|
||||
return s.spec_;
|
||||
}
|
||||
|
||||
private:
|
||||
const void* data_;
|
||||
size_t size_;
|
||||
};
|
||||
|
||||
template <typename T, FormatConversionCharSet...>
|
||||
struct MakeDependent {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
// Implicitly convertible from `const char*`, `string_view`, and the
|
||||
// `ExtendedParsedFormat` type. This abstraction allows all format functions to
|
||||
// operate on any without providing too many overloads.
|
||||
template <FormatConversionCharSet... Args>
|
||||
class FormatSpecTemplate
|
||||
: public MakeDependent<UntypedFormatSpec, Args...>::type {
|
||||
using Base = typename MakeDependent<UntypedFormatSpec, Args...>::type;
|
||||
|
||||
public:
|
||||
#ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
// Honeypot overload for when the string is not constexpr.
|
||||
// We use the 'unavailable' attribute to give a better compiler error than
|
||||
// just 'method is deleted'.
|
||||
FormatSpecTemplate(...) // NOLINT
|
||||
__attribute__((unavailable("Format string is not constexpr.")));
|
||||
|
||||
// Honeypot overload for when the format is constexpr and invalid.
|
||||
// We use the 'unavailable' attribute to give a better compiler error than
|
||||
// just 'method is deleted'.
|
||||
// To avoid checking the format twice, we just check that the format is
|
||||
// constexpr. If is it valid, then the overload below will kick in.
|
||||
// We add the template here to make this overload have lower priority.
|
||||
template <typename = void>
|
||||
FormatSpecTemplate(const char* s) // NOLINT
|
||||
__attribute__((
|
||||
enable_if(str_format_internal::EnsureConstexpr(s), "constexpr trap"),
|
||||
unavailable(
|
||||
"Format specified does not match the arguments passed.")));
|
||||
|
||||
template <typename T = void>
|
||||
FormatSpecTemplate(string_view s) // NOLINT
|
||||
__attribute__((enable_if(str_format_internal::EnsureConstexpr(s),
|
||||
"constexpr trap"))) {
|
||||
static_assert(sizeof(T*) == 0,
|
||||
"Format specified does not match the arguments passed.");
|
||||
}
|
||||
|
||||
// Good format overload.
|
||||
FormatSpecTemplate(const char* s) // NOLINT
|
||||
__attribute__((enable_if(ValidFormatImpl<Args...>(s), "bad format trap")))
|
||||
: Base(s) {}
|
||||
|
||||
FormatSpecTemplate(string_view s) // NOLINT
|
||||
__attribute__((enable_if(ValidFormatImpl<Args...>(s), "bad format trap")))
|
||||
: Base(s) {}
|
||||
|
||||
#else // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
FormatSpecTemplate(const char* s) : Base(s) {} // NOLINT
|
||||
FormatSpecTemplate(string_view s) : Base(s) {} // NOLINT
|
||||
|
||||
#endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
template <
|
||||
FormatConversionCharSet... C,
|
||||
typename = typename std::enable_if<sizeof...(C) == sizeof...(Args)>::type,
|
||||
typename = typename std::enable_if<AllOf(Contains(Args,
|
||||
C)...)>::type>
|
||||
FormatSpecTemplate(const ExtendedParsedFormat<C...>& pc) // NOLINT
|
||||
: Base(&pc) {}
|
||||
};
|
||||
|
||||
class Streamable {
|
||||
public:
|
||||
Streamable(const UntypedFormatSpecImpl& format,
|
||||
absl::Span<const FormatArgImpl> args)
|
||||
: format_(format) {
|
||||
if (args.size() <= ABSL_ARRAYSIZE(few_args_)) {
|
||||
for (size_t i = 0; i < args.size(); ++i) {
|
||||
few_args_[i] = args[i];
|
||||
}
|
||||
args_ = absl::MakeSpan(few_args_, args.size());
|
||||
} else {
|
||||
many_args_.assign(args.begin(), args.end());
|
||||
args_ = many_args_;
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& Print(std::ostream& os) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Streamable& l) {
|
||||
return l.Print(os);
|
||||
}
|
||||
|
||||
private:
|
||||
const UntypedFormatSpecImpl& format_;
|
||||
absl::Span<const FormatArgImpl> args_;
|
||||
// if args_.size() is 4 or less:
|
||||
FormatArgImpl few_args_[4] = {FormatArgImpl(0), FormatArgImpl(0),
|
||||
FormatArgImpl(0), FormatArgImpl(0)};
|
||||
// if args_.size() is more than 4:
|
||||
std::vector<FormatArgImpl> many_args_;
|
||||
};
|
||||
|
||||
// for testing
|
||||
std::string Summarize(UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args);
|
||||
bool BindWithPack(const UnboundConversion* props,
|
||||
absl::Span<const FormatArgImpl> pack, BoundConversion* bound);
|
||||
|
||||
bool FormatUntyped(FormatRawSinkImpl raw_sink,
|
||||
UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args);
|
||||
|
||||
std::string& AppendPack(std::string* out, UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args);
|
||||
|
||||
std::string FormatPack(const UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args);
|
||||
|
||||
int FprintF(std::FILE* output, UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args);
|
||||
int SnprintF(char* output, size_t size, UntypedFormatSpecImpl format,
|
||||
absl::Span<const FormatArgImpl> args);
|
||||
|
||||
// Returned by Streamed(v). Converts via '%s' to the std::string created
|
||||
// by std::ostream << v.
|
||||
template <typename T>
|
||||
class StreamedWrapper {
|
||||
public:
|
||||
explicit StreamedWrapper(const T& v) : v_(v) { }
|
||||
|
||||
private:
|
||||
template <typename S>
|
||||
friend ArgConvertResult<FormatConversionCharSetInternal::s> FormatConvertImpl(
|
||||
const StreamedWrapper<S>& v, FormatConversionSpecImpl conv,
|
||||
FormatSinkImpl* out);
|
||||
const T& v_;
|
||||
};
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
|
||||
157
third_party/abseil-cpp/absl/strings/internal/str_format/bind_test.cc
vendored
Normal file
157
third_party/abseil-cpp/absl/strings/internal/str_format/bind_test.cc
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/bind.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <limits>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
namespace {
|
||||
|
||||
class FormatBindTest : public ::testing::Test {
|
||||
public:
|
||||
bool Extract(const char *s, UnboundConversion *props, int *next) const {
|
||||
return ConsumeUnboundConversion(s, s + strlen(s), props, next) ==
|
||||
s + strlen(s);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(FormatBindTest, BindSingle) {
|
||||
struct Expectation {
|
||||
int line;
|
||||
const char *fmt;
|
||||
int ok_phases;
|
||||
const FormatArgImpl *arg;
|
||||
int width;
|
||||
int precision;
|
||||
int next_arg;
|
||||
};
|
||||
const int no = -1;
|
||||
const int ia[] = { 10, 20, 30, 40};
|
||||
const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
|
||||
FormatArgImpl(ia[2]), FormatArgImpl(ia[3])};
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||
const Expectation kExpect[] = {
|
||||
{__LINE__, "d", 2, &args[0], no, no, 2},
|
||||
{__LINE__, "4d", 2, &args[0], 4, no, 2},
|
||||
{__LINE__, ".5d", 2, &args[0], no, 5, 2},
|
||||
{__LINE__, "4.5d", 2, &args[0], 4, 5, 2},
|
||||
{__LINE__, "*d", 2, &args[1], 10, no, 3},
|
||||
{__LINE__, ".*d", 2, &args[1], no, 10, 3},
|
||||
{__LINE__, "*.*d", 2, &args[2], 10, 20, 4},
|
||||
{__LINE__, "1$d", 2, &args[0], no, no, 0},
|
||||
{__LINE__, "2$d", 2, &args[1], no, no, 0},
|
||||
{__LINE__, "3$d", 2, &args[2], no, no, 0},
|
||||
{__LINE__, "4$d", 2, &args[3], no, no, 0},
|
||||
{__LINE__, "2$*1$d", 2, &args[1], 10, no, 0},
|
||||
{__LINE__, "2$*2$d", 2, &args[1], 20, no, 0},
|
||||
{__LINE__, "2$*3$d", 2, &args[1], 30, no, 0},
|
||||
{__LINE__, "2$.*1$d", 2, &args[1], no, 10, 0},
|
||||
{__LINE__, "2$.*2$d", 2, &args[1], no, 20, 0},
|
||||
{__LINE__, "2$.*3$d", 2, &args[1], no, 30, 0},
|
||||
{__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
|
||||
{__LINE__, "2$*2$.*2$d", 2, &args[1], 20, 20, 0},
|
||||
{__LINE__, "2$*1$.*3$d", 2, &args[1], 10, 30, 0},
|
||||
{__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
|
||||
{__LINE__, "1$*d", 0}, // indexed, then positional
|
||||
{__LINE__, "*2$d", 0}, // positional, then indexed
|
||||
{__LINE__, "6$d", 1}, // arg position out of bounds
|
||||
{__LINE__, "1$6$d", 0}, // width position incorrectly specified
|
||||
{__LINE__, "1$.6$d", 0}, // precision position incorrectly specified
|
||||
{__LINE__, "1$*6$d", 1}, // width position out of bounds
|
||||
{__LINE__, "1$.*6$d", 1}, // precision position out of bounds
|
||||
};
|
||||
#pragma GCC diagnostic pop
|
||||
for (const Expectation &e : kExpect) {
|
||||
SCOPED_TRACE(e.line);
|
||||
SCOPED_TRACE(e.fmt);
|
||||
UnboundConversion props;
|
||||
BoundConversion bound;
|
||||
int ok_phases = 0;
|
||||
int next = 0;
|
||||
if (Extract(e.fmt, &props, &next)) {
|
||||
++ok_phases;
|
||||
if (BindWithPack(&props, args, &bound)) {
|
||||
++ok_phases;
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(e.ok_phases, ok_phases);
|
||||
if (e.ok_phases < 2) continue;
|
||||
if (e.arg != nullptr) {
|
||||
EXPECT_EQ(e.arg, bound.arg());
|
||||
}
|
||||
EXPECT_EQ(e.width, bound.width());
|
||||
EXPECT_EQ(e.precision, bound.precision());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FormatBindTest, WidthUnderflowRegression) {
|
||||
UnboundConversion props;
|
||||
BoundConversion bound;
|
||||
int next = 0;
|
||||
const int args_i[] = {std::numeric_limits<int>::min(), 17};
|
||||
const FormatArgImpl args[] = {FormatArgImpl(args_i[0]),
|
||||
FormatArgImpl(args_i[1])};
|
||||
ASSERT_TRUE(Extract("*d", &props, &next));
|
||||
ASSERT_TRUE(BindWithPack(&props, args, &bound));
|
||||
|
||||
EXPECT_EQ(bound.width(), std::numeric_limits<int>::max());
|
||||
EXPECT_EQ(bound.arg(), args + 1);
|
||||
}
|
||||
|
||||
TEST_F(FormatBindTest, FormatPack) {
|
||||
struct Expectation {
|
||||
int line;
|
||||
const char *fmt;
|
||||
const char *summary;
|
||||
};
|
||||
const int ia[] = { 10, 20, 30, 40, -10 };
|
||||
const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
|
||||
FormatArgImpl(ia[2]), FormatArgImpl(ia[3]),
|
||||
FormatArgImpl(ia[4])};
|
||||
const Expectation kExpect[] = {
|
||||
{__LINE__, "a%4db%dc", "a{10:4d}b{20:d}c"},
|
||||
{__LINE__, "a%.4db%dc", "a{10:.4d}b{20:d}c"},
|
||||
{__LINE__, "a%4.5db%dc", "a{10:4.5d}b{20:d}c"},
|
||||
{__LINE__, "a%db%4.5dc", "a{10:d}b{20:4.5d}c"},
|
||||
{__LINE__, "a%db%*.*dc", "a{10:d}b{40:20.30d}c"},
|
||||
{__LINE__, "a%.*fb", "a{20:.10f}b"},
|
||||
{__LINE__, "a%1$db%2$*3$.*4$dc", "a{10:d}b{20:30.40d}c"},
|
||||
{__LINE__, "a%4$db%3$*2$.*1$dc", "a{40:d}b{30:20.10d}c"},
|
||||
{__LINE__, "a%04ldb", "a{10:04d}b"},
|
||||
{__LINE__, "a%-#04lldb", "a{10:-#04d}b"},
|
||||
{__LINE__, "a%1$*5$db", "a{10:-10d}b"},
|
||||
{__LINE__, "a%1$.*5$db", "a{10:d}b"},
|
||||
};
|
||||
for (const Expectation &e : kExpect) {
|
||||
absl::string_view fmt = e.fmt;
|
||||
SCOPED_TRACE(e.line);
|
||||
SCOPED_TRACE(e.fmt);
|
||||
UntypedFormatSpecImpl format(fmt);
|
||||
EXPECT_EQ(e.summary,
|
||||
str_format_internal::Summarize(format, absl::MakeSpan(args)))
|
||||
<< "line:" << e.line;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
333
third_party/abseil-cpp/absl/strings/internal/str_format/checker.h
vendored
Normal file
333
third_party/abseil-cpp/absl/strings/internal/str_format/checker.h
vendored
Normal file
@@ -0,0 +1,333 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/strings/internal/str_format/arg.h"
|
||||
#include "absl/strings/internal/str_format/extension.h"
|
||||
|
||||
// Compile time check support for entry points.
|
||||
|
||||
#ifndef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
#if ABSL_HAVE_ATTRIBUTE(enable_if) && !defined(__native_client__)
|
||||
#define ABSL_INTERNAL_ENABLE_FORMAT_CHECKER 1
|
||||
#endif // ABSL_HAVE_ATTRIBUTE(enable_if) && !defined(__native_client__)
|
||||
#endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
constexpr bool AllOf() { return true; }
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool AllOf(bool b, T... t) {
|
||||
return b && AllOf(t...);
|
||||
}
|
||||
|
||||
#ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
constexpr bool ContainsChar(const char* chars, char c) {
|
||||
return *chars == c || (*chars && ContainsChar(chars + 1, c));
|
||||
}
|
||||
|
||||
// A constexpr compatible list of Convs.
|
||||
struct ConvList {
|
||||
const FormatConversionCharSet* array;
|
||||
int count;
|
||||
|
||||
// We do the bound check here to avoid having to do it on the callers.
|
||||
// Returning an empty FormatConversionCharSet has the same effect as
|
||||
// short circuiting because it will never match any conversion.
|
||||
constexpr FormatConversionCharSet operator[](int i) const {
|
||||
return i < count ? array[i] : FormatConversionCharSet{};
|
||||
}
|
||||
|
||||
constexpr ConvList without_front() const {
|
||||
return count != 0 ? ConvList{array + 1, count - 1} : *this;
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t count>
|
||||
struct ConvListT {
|
||||
// Make sure the array has size > 0.
|
||||
FormatConversionCharSet list[count ? count : 1];
|
||||
};
|
||||
|
||||
constexpr char GetChar(string_view str, size_t index) {
|
||||
return index < str.size() ? str[index] : char{};
|
||||
}
|
||||
|
||||
constexpr string_view ConsumeFront(string_view str, size_t len = 1) {
|
||||
return len <= str.size() ? string_view(str.data() + len, str.size() - len)
|
||||
: string_view();
|
||||
}
|
||||
|
||||
constexpr string_view ConsumeAnyOf(string_view format, const char* chars) {
|
||||
return ContainsChar(chars, GetChar(format, 0))
|
||||
? ConsumeAnyOf(ConsumeFront(format), chars)
|
||||
: format;
|
||||
}
|
||||
|
||||
constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; }
|
||||
|
||||
// Helper class for the ParseDigits function.
|
||||
// It encapsulates the two return values we need there.
|
||||
struct Integer {
|
||||
string_view format;
|
||||
int value;
|
||||
|
||||
// If the next character is a '$', consume it.
|
||||
// Otherwise, make `this` an invalid positional argument.
|
||||
constexpr Integer ConsumePositionalDollar() const {
|
||||
return GetChar(format, 0) == '$' ? Integer{ConsumeFront(format), value}
|
||||
: Integer{format, 0};
|
||||
}
|
||||
};
|
||||
|
||||
constexpr Integer ParseDigits(string_view format, int value = 0) {
|
||||
return IsDigit(GetChar(format, 0))
|
||||
? ParseDigits(ConsumeFront(format),
|
||||
10 * value + GetChar(format, 0) - '0')
|
||||
: Integer{format, value};
|
||||
}
|
||||
|
||||
// Parse digits for a positional argument.
|
||||
// The parsing also consumes the '$'.
|
||||
constexpr Integer ParsePositional(string_view format) {
|
||||
return ParseDigits(format).ConsumePositionalDollar();
|
||||
}
|
||||
|
||||
// Parses a single conversion specifier.
|
||||
// See ConvParser::Run() for post conditions.
|
||||
class ConvParser {
|
||||
constexpr ConvParser SetFormat(string_view format) const {
|
||||
return ConvParser(format, args_, error_, arg_position_, is_positional_);
|
||||
}
|
||||
|
||||
constexpr ConvParser SetArgs(ConvList args) const {
|
||||
return ConvParser(format_, args, error_, arg_position_, is_positional_);
|
||||
}
|
||||
|
||||
constexpr ConvParser SetError(bool error) const {
|
||||
return ConvParser(format_, args_, error_ || error, arg_position_,
|
||||
is_positional_);
|
||||
}
|
||||
|
||||
constexpr ConvParser SetArgPosition(int arg_position) const {
|
||||
return ConvParser(format_, args_, error_, arg_position, is_positional_);
|
||||
}
|
||||
|
||||
// Consumes the next arg and verifies that it matches `conv`.
|
||||
// `error_` is set if there is no next arg or if it doesn't match `conv`.
|
||||
constexpr ConvParser ConsumeNextArg(char conv) const {
|
||||
return SetArgs(args_.without_front()).SetError(!Contains(args_[0], conv));
|
||||
}
|
||||
|
||||
// Verify that positional argument `i.value` matches `conv`.
|
||||
// `error_` is set if `i.value` is not a valid argument or if it doesn't
|
||||
// match.
|
||||
constexpr ConvParser VerifyPositional(Integer i, char conv) const {
|
||||
return SetFormat(i.format).SetError(!Contains(args_[i.value - 1], conv));
|
||||
}
|
||||
|
||||
// Parse the position of the arg and store it in `arg_position_`.
|
||||
constexpr ConvParser ParseArgPosition(Integer arg) const {
|
||||
return SetFormat(arg.format).SetArgPosition(arg.value);
|
||||
}
|
||||
|
||||
// Consume the flags.
|
||||
constexpr ConvParser ParseFlags() const {
|
||||
return SetFormat(ConsumeAnyOf(format_, "-+ #0"));
|
||||
}
|
||||
|
||||
// Consume the width.
|
||||
// If it is '*', we verify that it matches `args_`. `error_` is set if it
|
||||
// doesn't match.
|
||||
constexpr ConvParser ParseWidth() const {
|
||||
return IsDigit(GetChar(format_, 0))
|
||||
? SetFormat(ParseDigits(format_).format)
|
||||
: GetChar(format_, 0) == '*'
|
||||
? is_positional_
|
||||
? VerifyPositional(
|
||||
ParsePositional(ConsumeFront(format_)), '*')
|
||||
: SetFormat(ConsumeFront(format_))
|
||||
.ConsumeNextArg('*')
|
||||
: *this;
|
||||
}
|
||||
|
||||
// Consume the precision.
|
||||
// If it is '*', we verify that it matches `args_`. `error_` is set if it
|
||||
// doesn't match.
|
||||
constexpr ConvParser ParsePrecision() const {
|
||||
return GetChar(format_, 0) != '.'
|
||||
? *this
|
||||
: GetChar(format_, 1) == '*'
|
||||
? is_positional_
|
||||
? VerifyPositional(
|
||||
ParsePositional(ConsumeFront(format_, 2)), '*')
|
||||
: SetFormat(ConsumeFront(format_, 2))
|
||||
.ConsumeNextArg('*')
|
||||
: SetFormat(ParseDigits(ConsumeFront(format_)).format);
|
||||
}
|
||||
|
||||
// Consume the length characters.
|
||||
constexpr ConvParser ParseLength() const {
|
||||
return SetFormat(ConsumeAnyOf(format_, "lLhjztq"));
|
||||
}
|
||||
|
||||
// Consume the conversion character and verify that it matches `args_`.
|
||||
// `error_` is set if it doesn't match.
|
||||
constexpr ConvParser ParseConversion() const {
|
||||
return is_positional_
|
||||
? VerifyPositional({ConsumeFront(format_), arg_position_},
|
||||
GetChar(format_, 0))
|
||||
: ConsumeNextArg(GetChar(format_, 0))
|
||||
.SetFormat(ConsumeFront(format_));
|
||||
}
|
||||
|
||||
constexpr ConvParser(string_view format, ConvList args, bool error,
|
||||
int arg_position, bool is_positional)
|
||||
: format_(format),
|
||||
args_(args),
|
||||
error_(error),
|
||||
arg_position_(arg_position),
|
||||
is_positional_(is_positional) {}
|
||||
|
||||
public:
|
||||
constexpr ConvParser(string_view format, ConvList args, bool is_positional)
|
||||
: format_(format),
|
||||
args_(args),
|
||||
error_(false),
|
||||
arg_position_(0),
|
||||
is_positional_(is_positional) {}
|
||||
|
||||
// Consume the whole conversion specifier.
|
||||
// `format()` will be set to the character after the conversion character.
|
||||
// `error()` will be set if any of the arguments do not match.
|
||||
constexpr ConvParser Run() const {
|
||||
return (is_positional_ ? ParseArgPosition(ParsePositional(format_)) : *this)
|
||||
.ParseFlags()
|
||||
.ParseWidth()
|
||||
.ParsePrecision()
|
||||
.ParseLength()
|
||||
.ParseConversion();
|
||||
}
|
||||
|
||||
constexpr string_view format() const { return format_; }
|
||||
constexpr ConvList args() const { return args_; }
|
||||
constexpr bool error() const { return error_; }
|
||||
constexpr bool is_positional() const { return is_positional_; }
|
||||
|
||||
private:
|
||||
string_view format_;
|
||||
// Current list of arguments. If we are not in positional mode we will consume
|
||||
// from the front.
|
||||
ConvList args_;
|
||||
bool error_;
|
||||
// Holds the argument position of the conversion character, if we are in
|
||||
// positional mode. Otherwise, it is unspecified.
|
||||
int arg_position_;
|
||||
// Whether we are in positional mode.
|
||||
// It changes the behavior of '*' and where to find the converted argument.
|
||||
bool is_positional_;
|
||||
};
|
||||
|
||||
// Parses a whole format expression.
|
||||
// See FormatParser::Run().
|
||||
class FormatParser {
|
||||
static constexpr bool FoundPercent(string_view format) {
|
||||
return format.empty() ||
|
||||
(GetChar(format, 0) == '%' && GetChar(format, 1) != '%');
|
||||
}
|
||||
|
||||
// We use an inner function to increase the recursion limit.
|
||||
// The inner function consumes up to `limit` characters on every run.
|
||||
// This increases the limit from 512 to ~512*limit.
|
||||
static constexpr string_view ConsumeNonPercentInner(string_view format,
|
||||
int limit = 20) {
|
||||
return FoundPercent(format) || !limit
|
||||
? format
|
||||
: ConsumeNonPercentInner(
|
||||
ConsumeFront(format, GetChar(format, 0) == '%' &&
|
||||
GetChar(format, 1) == '%'
|
||||
? 2
|
||||
: 1),
|
||||
limit - 1);
|
||||
}
|
||||
|
||||
// Consume characters until the next conversion spec %.
|
||||
// It skips %%.
|
||||
static constexpr string_view ConsumeNonPercent(string_view format) {
|
||||
return FoundPercent(format)
|
||||
? format
|
||||
: ConsumeNonPercent(ConsumeNonPercentInner(format));
|
||||
}
|
||||
|
||||
static constexpr bool IsPositional(string_view format) {
|
||||
return IsDigit(GetChar(format, 0)) ? IsPositional(ConsumeFront(format))
|
||||
: GetChar(format, 0) == '$';
|
||||
}
|
||||
|
||||
constexpr bool RunImpl(bool is_positional) const {
|
||||
// In non-positional mode we require all arguments to be consumed.
|
||||
// In positional mode just reaching the end of the format without errors is
|
||||
// enough.
|
||||
return (format_.empty() && (is_positional || args_.count == 0)) ||
|
||||
(!format_.empty() &&
|
||||
ValidateArg(
|
||||
ConvParser(ConsumeFront(format_), args_, is_positional).Run()));
|
||||
}
|
||||
|
||||
constexpr bool ValidateArg(ConvParser conv) const {
|
||||
return !conv.error() && FormatParser(conv.format(), conv.args())
|
||||
.RunImpl(conv.is_positional());
|
||||
}
|
||||
|
||||
public:
|
||||
constexpr FormatParser(string_view format, ConvList args)
|
||||
: format_(ConsumeNonPercent(format)), args_(args) {}
|
||||
|
||||
// Runs the parser for `format` and `args`.
|
||||
// It verifies that the format is valid and that all conversion specifiers
|
||||
// match the arguments passed.
|
||||
// In non-positional mode it also verfies that all arguments are consumed.
|
||||
constexpr bool Run() const {
|
||||
return RunImpl(!format_.empty() && IsPositional(ConsumeFront(format_)));
|
||||
}
|
||||
|
||||
private:
|
||||
string_view format_;
|
||||
// Current list of arguments.
|
||||
// If we are not in positional mode we will consume from the front and will
|
||||
// have to be empty in the end.
|
||||
ConvList args_;
|
||||
};
|
||||
|
||||
template <FormatConversionCharSet... C>
|
||||
constexpr bool ValidFormatImpl(string_view format) {
|
||||
return FormatParser(format,
|
||||
{ConvListT<sizeof...(C)>{{C...}}.list, sizeof...(C)})
|
||||
.Run();
|
||||
}
|
||||
|
||||
#endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_
|
||||
170
third_party/abseil-cpp/absl/strings/internal/str_format/checker_test.cc
vendored
Normal file
170
third_party/abseil-cpp/absl/strings/internal/str_format/checker_test.cc
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
namespace {
|
||||
|
||||
std::string ConvToString(FormatConversionCharSet conv) {
|
||||
std::string out;
|
||||
#define CONV_SET_CASE(c) \
|
||||
if (Contains(conv, FormatConversionCharSetInternal::c)) { \
|
||||
out += #c; \
|
||||
}
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, )
|
||||
#undef CONV_SET_CASE
|
||||
if (Contains(conv, FormatConversionCharSetInternal::kStar)) {
|
||||
out += "*";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
TEST(StrFormatChecker, ArgumentToConv) {
|
||||
FormatConversionCharSet conv = ArgumentToConv<std::string>();
|
||||
EXPECT_EQ(ConvToString(conv), "s");
|
||||
|
||||
conv = ArgumentToConv<const char*>();
|
||||
EXPECT_EQ(ConvToString(conv), "sp");
|
||||
|
||||
conv = ArgumentToConv<double>();
|
||||
EXPECT_EQ(ConvToString(conv), "fFeEgGaA");
|
||||
|
||||
conv = ArgumentToConv<int>();
|
||||
EXPECT_EQ(ConvToString(conv), "cdiouxXfFeEgGaA*");
|
||||
|
||||
conv = ArgumentToConv<std::string*>();
|
||||
EXPECT_EQ(ConvToString(conv), "p");
|
||||
}
|
||||
|
||||
#ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
struct Case {
|
||||
bool result;
|
||||
const char* format;
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
constexpr Case ValidFormat(const char* format) {
|
||||
return {ValidFormatImpl<ArgumentToConv<Args>()...>(format), format};
|
||||
}
|
||||
|
||||
TEST(StrFormatChecker, ValidFormat) {
|
||||
// We want to make sure these expressions are constexpr and they have the
|
||||
// expected value.
|
||||
// If they are not constexpr the attribute will just ignore them and not give
|
||||
// a compile time error.
|
||||
enum e {};
|
||||
enum class e2 {};
|
||||
constexpr Case trues[] = {
|
||||
ValidFormat<>("abc"), //
|
||||
|
||||
ValidFormat<e>("%d"), //
|
||||
ValidFormat<e2>("%d"), //
|
||||
ValidFormat<int>("%% %d"), //
|
||||
ValidFormat<int>("%ld"), //
|
||||
ValidFormat<int>("%lld"), //
|
||||
ValidFormat<std::string>("%s"), //
|
||||
ValidFormat<std::string>("%10s"), //
|
||||
ValidFormat<int>("%.10x"), //
|
||||
ValidFormat<int, int>("%*.3x"), //
|
||||
ValidFormat<int>("%1.d"), //
|
||||
ValidFormat<int>("%.d"), //
|
||||
ValidFormat<int, double>("%d %g"), //
|
||||
ValidFormat<int, std::string>("%*s"), //
|
||||
ValidFormat<int, double>("%.*f"), //
|
||||
ValidFormat<void (*)(), volatile int*>("%p %p"), //
|
||||
ValidFormat<string_view, const char*, double, void*>(
|
||||
"string_view=%s const char*=%s double=%f void*=%p)"),
|
||||
|
||||
ValidFormat<int>("%% %1$d"), //
|
||||
ValidFormat<int>("%1$ld"), //
|
||||
ValidFormat<int>("%1$lld"), //
|
||||
ValidFormat<std::string>("%1$s"), //
|
||||
ValidFormat<std::string>("%1$10s"), //
|
||||
ValidFormat<int>("%1$.10x"), //
|
||||
ValidFormat<int>("%1$*1$.*1$d"), //
|
||||
ValidFormat<int, int>("%1$*2$.3x"), //
|
||||
ValidFormat<int>("%1$1.d"), //
|
||||
ValidFormat<int>("%1$.d"), //
|
||||
ValidFormat<double, int>("%2$d %1$g"), //
|
||||
ValidFormat<int, std::string>("%2$*1$s"), //
|
||||
ValidFormat<int, double>("%2$.*1$f"), //
|
||||
ValidFormat<void*, string_view, const char*, double>(
|
||||
"string_view=%2$s const char*=%3$s double=%4$f void*=%1$p "
|
||||
"repeat=%3$s)")};
|
||||
|
||||
for (Case c : trues) {
|
||||
EXPECT_TRUE(c.result) << c.format;
|
||||
}
|
||||
|
||||
constexpr Case falses[] = {
|
||||
ValidFormat<int>(""), //
|
||||
|
||||
ValidFormat<e>("%s"), //
|
||||
ValidFormat<e2>("%s"), //
|
||||
ValidFormat<>("%s"), //
|
||||
ValidFormat<>("%r"), //
|
||||
ValidFormat<int>("%s"), //
|
||||
ValidFormat<int>("%.1.d"), //
|
||||
ValidFormat<int>("%*1d"), //
|
||||
ValidFormat<int>("%1-d"), //
|
||||
ValidFormat<std::string, int>("%*s"), //
|
||||
ValidFormat<int>("%*d"), //
|
||||
ValidFormat<std::string>("%p"), //
|
||||
ValidFormat<int (*)(int)>("%d"), //
|
||||
|
||||
ValidFormat<>("%3$d"), //
|
||||
ValidFormat<>("%1$r"), //
|
||||
ValidFormat<int>("%1$s"), //
|
||||
ValidFormat<int>("%1$.1.d"), //
|
||||
ValidFormat<int>("%1$*2$1d"), //
|
||||
ValidFormat<int>("%1$1-d"), //
|
||||
ValidFormat<std::string, int>("%2$*1$s"), //
|
||||
ValidFormat<std::string>("%1$p"),
|
||||
|
||||
ValidFormat<int, int>("%d %2$d"), //
|
||||
};
|
||||
|
||||
for (Case c : falses) {
|
||||
EXPECT_FALSE(c.result) << c.format;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StrFormatChecker, LongFormat) {
|
||||
#define CHARS_X_40 "1234567890123456789012345678901234567890"
|
||||
#define CHARS_X_400 \
|
||||
CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 \
|
||||
CHARS_X_40 CHARS_X_40 CHARS_X_40
|
||||
#define CHARS_X_4000 \
|
||||
CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 \
|
||||
CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400
|
||||
constexpr char long_format[] =
|
||||
CHARS_X_4000 "%d" CHARS_X_4000 "%s" CHARS_X_4000;
|
||||
constexpr bool is_valid = ValidFormat<int, std::string>(long_format).result;
|
||||
EXPECT_TRUE(is_valid);
|
||||
}
|
||||
|
||||
#endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
|
||||
} // namespace
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
1243
third_party/abseil-cpp/absl/strings/internal/str_format/convert_test.cc
vendored
Normal file
1243
third_party/abseil-cpp/absl/strings/internal/str_format/convert_test.cc
vendored
Normal file
File diff suppressed because it is too large
Load Diff
75
third_party/abseil-cpp/absl/strings/internal/str_format/extension.cc
vendored
Normal file
75
third_party/abseil-cpp/absl/strings/internal/str_format/extension.cc
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/extension.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
std::string FlagsToString(Flags v) {
|
||||
std::string s;
|
||||
s.append(FlagsContains(v, Flags::kLeft) ? "-" : "");
|
||||
s.append(FlagsContains(v, Flags::kShowPos) ? "+" : "");
|
||||
s.append(FlagsContains(v, Flags::kSignCol) ? " " : "");
|
||||
s.append(FlagsContains(v, Flags::kAlt) ? "#" : "");
|
||||
s.append(FlagsContains(v, Flags::kZero) ? "0" : "");
|
||||
return s;
|
||||
}
|
||||
|
||||
#define ABSL_INTERNAL_X_VAL(id) \
|
||||
constexpr absl::FormatConversionChar FormatConversionCharInternal::id;
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
|
||||
#undef ABSL_INTERNAL_X_VAL
|
||||
// NOLINTNEXTLINE(readability-redundant-declaration)
|
||||
constexpr absl::FormatConversionChar FormatConversionCharInternal::kNone;
|
||||
|
||||
#define ABSL_INTERNAL_CHAR_SET_CASE(c) \
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetInternal::c;
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
|
||||
#undef ABSL_INTERNAL_CHAR_SET_CASE
|
||||
|
||||
// NOLINTNEXTLINE(readability-redundant-declaration)
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetInternal::kStar;
|
||||
// NOLINTNEXTLINE(readability-redundant-declaration)
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetInternal::kIntegral;
|
||||
// NOLINTNEXTLINE(readability-redundant-declaration)
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetInternal::kFloating;
|
||||
// NOLINTNEXTLINE(readability-redundant-declaration)
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetInternal::kNumeric;
|
||||
// NOLINTNEXTLINE(readability-redundant-declaration)
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetInternal::kPointer;
|
||||
|
||||
bool FormatSinkImpl::PutPaddedString(string_view value, int width,
|
||||
int precision, bool left) {
|
||||
size_t space_remaining = 0;
|
||||
if (width >= 0) space_remaining = width;
|
||||
size_t n = value.size();
|
||||
if (precision >= 0) n = std::min(n, static_cast<size_t>(precision));
|
||||
string_view shown(value.data(), n);
|
||||
space_remaining = Excess(shown.size(), space_remaining);
|
||||
if (!left) Append(space_remaining, ' ');
|
||||
Append(shown);
|
||||
if (left) Append(space_remaining, ' ');
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
445
third_party/abseil-cpp/absl/strings/internal/str_format/extension.h
vendored
Normal file
445
third_party/abseil-cpp/absl/strings/internal/str_format/extension.h
vendored
Normal file
@@ -0,0 +1,445 @@
|
||||
//
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <ostream>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/strings/internal/str_format/output.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
enum class FormatConversionChar : uint8_t;
|
||||
enum class FormatConversionCharSet : uint64_t;
|
||||
|
||||
namespace str_format_internal {
|
||||
|
||||
class FormatRawSinkImpl {
|
||||
public:
|
||||
// Implicitly convert from any type that provides the hook function as
|
||||
// described above.
|
||||
template <typename T, decltype(str_format_internal::InvokeFlush(
|
||||
std::declval<T*>(), string_view()))* = nullptr>
|
||||
FormatRawSinkImpl(T* raw) // NOLINT
|
||||
: sink_(raw), write_(&FormatRawSinkImpl::Flush<T>) {}
|
||||
|
||||
void Write(string_view s) { write_(sink_, s); }
|
||||
|
||||
template <typename T>
|
||||
static FormatRawSinkImpl Extract(T s) {
|
||||
return s.sink_;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
static void Flush(void* r, string_view s) {
|
||||
str_format_internal::InvokeFlush(static_cast<T*>(r), s);
|
||||
}
|
||||
|
||||
void* sink_;
|
||||
void (*write_)(void*, string_view);
|
||||
};
|
||||
|
||||
// An abstraction to which conversions write their string data.
|
||||
class FormatSinkImpl {
|
||||
public:
|
||||
explicit FormatSinkImpl(FormatRawSinkImpl raw) : raw_(raw) {}
|
||||
|
||||
~FormatSinkImpl() { Flush(); }
|
||||
|
||||
void Flush() {
|
||||
raw_.Write(string_view(buf_, pos_ - buf_));
|
||||
pos_ = buf_;
|
||||
}
|
||||
|
||||
void Append(size_t n, char c) {
|
||||
if (n == 0) return;
|
||||
size_ += n;
|
||||
auto raw_append = [&](size_t count) {
|
||||
memset(pos_, c, count);
|
||||
pos_ += count;
|
||||
};
|
||||
while (n > Avail()) {
|
||||
n -= Avail();
|
||||
if (Avail() > 0) {
|
||||
raw_append(Avail());
|
||||
}
|
||||
Flush();
|
||||
}
|
||||
raw_append(n);
|
||||
}
|
||||
|
||||
void Append(string_view v) {
|
||||
size_t n = v.size();
|
||||
if (n == 0) return;
|
||||
size_ += n;
|
||||
if (n >= Avail()) {
|
||||
Flush();
|
||||
raw_.Write(v);
|
||||
return;
|
||||
}
|
||||
memcpy(pos_, v.data(), n);
|
||||
pos_ += n;
|
||||
}
|
||||
|
||||
size_t size() const { return size_; }
|
||||
|
||||
// Put 'v' to 'sink' with specified width, precision, and left flag.
|
||||
bool PutPaddedString(string_view v, int width, int precision, bool left);
|
||||
|
||||
template <typename T>
|
||||
T Wrap() {
|
||||
return T(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FormatSinkImpl* Extract(T* s) {
|
||||
return s->sink_;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t Avail() const { return buf_ + sizeof(buf_) - pos_; }
|
||||
|
||||
FormatRawSinkImpl raw_;
|
||||
size_t size_ = 0;
|
||||
char* pos_ = buf_;
|
||||
char buf_[1024];
|
||||
};
|
||||
|
||||
enum class Flags : uint8_t {
|
||||
kBasic = 0,
|
||||
kLeft = 1 << 0,
|
||||
kShowPos = 1 << 1,
|
||||
kSignCol = 1 << 2,
|
||||
kAlt = 1 << 3,
|
||||
kZero = 1 << 4,
|
||||
// This is not a real flag. It just exists to turn off kBasic when no other
|
||||
// flags are set. This is for when width/precision are specified.
|
||||
kNonBasic = 1 << 5,
|
||||
};
|
||||
|
||||
constexpr Flags operator|(Flags a, Flags b) {
|
||||
return static_cast<Flags>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
|
||||
}
|
||||
|
||||
constexpr bool FlagsContains(Flags haystack, Flags needle) {
|
||||
return (static_cast<uint8_t>(haystack) & static_cast<uint8_t>(needle)) ==
|
||||
static_cast<uint8_t>(needle);
|
||||
}
|
||||
|
||||
std::string FlagsToString(Flags v);
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, Flags v) {
|
||||
return os << FlagsToString(v);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
#define ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP) \
|
||||
/* text */ \
|
||||
X_VAL(c) X_SEP X_VAL(s) X_SEP \
|
||||
/* ints */ \
|
||||
X_VAL(d) X_SEP X_VAL(i) X_SEP X_VAL(o) X_SEP \
|
||||
X_VAL(u) X_SEP X_VAL(x) X_SEP X_VAL(X) X_SEP \
|
||||
/* floats */ \
|
||||
X_VAL(f) X_SEP X_VAL(F) X_SEP X_VAL(e) X_SEP X_VAL(E) X_SEP \
|
||||
X_VAL(g) X_SEP X_VAL(G) X_SEP X_VAL(a) X_SEP X_VAL(A) X_SEP \
|
||||
/* misc */ \
|
||||
X_VAL(n) X_SEP X_VAL(p)
|
||||
// clang-format on
|
||||
|
||||
// This type should not be referenced, it exists only to provide labels
|
||||
// internally that match the values declared in FormatConversionChar in
|
||||
// str_format.h. This is meant to allow internal libraries to use the same
|
||||
// declared interface type as the public interface
|
||||
// (absl::StrFormatConversionChar) while keeping the definition in a public
|
||||
// header.
|
||||
// Internal libraries should use the form
|
||||
// `FormatConversionCharInternal::c`, `FormatConversionCharInternal::kNone` for
|
||||
// comparisons. Use in switch statements is not recommended due to a bug in how
|
||||
// gcc 4.9 -Wswitch handles declared but undefined enums.
|
||||
struct FormatConversionCharInternal {
|
||||
FormatConversionCharInternal() = delete;
|
||||
|
||||
private:
|
||||
// clang-format off
|
||||
enum class Enum : uint8_t {
|
||||
c, s, // text
|
||||
d, i, o, u, x, X, // int
|
||||
f, F, e, E, g, G, a, A, // float
|
||||
n, p, // misc
|
||||
kNone
|
||||
};
|
||||
// clang-format on
|
||||
public:
|
||||
#define ABSL_INTERNAL_X_VAL(id) \
|
||||
static constexpr FormatConversionChar id = \
|
||||
static_cast<FormatConversionChar>(Enum::id);
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
|
||||
#undef ABSL_INTERNAL_X_VAL
|
||||
static constexpr FormatConversionChar kNone =
|
||||
static_cast<FormatConversionChar>(Enum::kNone);
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
inline FormatConversionChar FormatConversionCharFromChar(char c) {
|
||||
switch (c) {
|
||||
#define ABSL_INTERNAL_X_VAL(id) \
|
||||
case #id[0]: \
|
||||
return FormatConversionCharInternal::id;
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
|
||||
#undef ABSL_INTERNAL_X_VAL
|
||||
}
|
||||
return FormatConversionCharInternal::kNone;
|
||||
}
|
||||
|
||||
inline bool FormatConversionCharIsUpper(FormatConversionChar c) {
|
||||
if (c == FormatConversionCharInternal::X ||
|
||||
c == FormatConversionCharInternal::F ||
|
||||
c == FormatConversionCharInternal::E ||
|
||||
c == FormatConversionCharInternal::G ||
|
||||
c == FormatConversionCharInternal::A) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool FormatConversionCharIsFloat(FormatConversionChar c) {
|
||||
if (c == FormatConversionCharInternal::a ||
|
||||
c == FormatConversionCharInternal::e ||
|
||||
c == FormatConversionCharInternal::f ||
|
||||
c == FormatConversionCharInternal::g ||
|
||||
c == FormatConversionCharInternal::A ||
|
||||
c == FormatConversionCharInternal::E ||
|
||||
c == FormatConversionCharInternal::F ||
|
||||
c == FormatConversionCharInternal::G) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline char FormatConversionCharToChar(FormatConversionChar c) {
|
||||
if (c == FormatConversionCharInternal::kNone) {
|
||||
return '\0';
|
||||
|
||||
#define ABSL_INTERNAL_X_VAL(e) \
|
||||
} else if (c == FormatConversionCharInternal::e) { \
|
||||
return #e[0];
|
||||
#define ABSL_INTERNAL_X_SEP
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL,
|
||||
ABSL_INTERNAL_X_SEP)
|
||||
} else {
|
||||
return '\0';
|
||||
}
|
||||
|
||||
#undef ABSL_INTERNAL_X_VAL
|
||||
#undef ABSL_INTERNAL_X_SEP
|
||||
}
|
||||
|
||||
// The associated char.
|
||||
inline std::ostream& operator<<(std::ostream& os, FormatConversionChar v) {
|
||||
char c = FormatConversionCharToChar(v);
|
||||
if (!c) c = '?';
|
||||
return os << c;
|
||||
}
|
||||
|
||||
struct FormatConversionSpecImplFriend;
|
||||
|
||||
class FormatConversionSpecImpl {
|
||||
public:
|
||||
// Width and precison are not specified, no flags are set.
|
||||
bool is_basic() const { return flags_ == Flags::kBasic; }
|
||||
bool has_left_flag() const { return FlagsContains(flags_, Flags::kLeft); }
|
||||
bool has_show_pos_flag() const {
|
||||
return FlagsContains(flags_, Flags::kShowPos);
|
||||
}
|
||||
bool has_sign_col_flag() const {
|
||||
return FlagsContains(flags_, Flags::kSignCol);
|
||||
}
|
||||
bool has_alt_flag() const { return FlagsContains(flags_, Flags::kAlt); }
|
||||
bool has_zero_flag() const { return FlagsContains(flags_, Flags::kZero); }
|
||||
|
||||
FormatConversionChar conversion_char() const {
|
||||
// Keep this field first in the struct . It generates better code when
|
||||
// accessing it when ConversionSpec is passed by value in registers.
|
||||
static_assert(offsetof(FormatConversionSpecImpl, conv_) == 0, "");
|
||||
return conv_;
|
||||
}
|
||||
|
||||
// Returns the specified width. If width is unspecfied, it returns a negative
|
||||
// value.
|
||||
int width() const { return width_; }
|
||||
// Returns the specified precision. If precision is unspecfied, it returns a
|
||||
// negative value.
|
||||
int precision() const { return precision_; }
|
||||
|
||||
template <typename T>
|
||||
T Wrap() {
|
||||
return T(*this);
|
||||
}
|
||||
|
||||
private:
|
||||
friend struct str_format_internal::FormatConversionSpecImplFriend;
|
||||
FormatConversionChar conv_ = FormatConversionCharInternal::kNone;
|
||||
Flags flags_;
|
||||
int width_;
|
||||
int precision_;
|
||||
};
|
||||
|
||||
struct FormatConversionSpecImplFriend final {
|
||||
static void SetFlags(Flags f, FormatConversionSpecImpl* conv) {
|
||||
conv->flags_ = f;
|
||||
}
|
||||
static void SetConversionChar(FormatConversionChar c,
|
||||
FormatConversionSpecImpl* conv) {
|
||||
conv->conv_ = c;
|
||||
}
|
||||
static void SetWidth(int w, FormatConversionSpecImpl* conv) {
|
||||
conv->width_ = w;
|
||||
}
|
||||
static void SetPrecision(int p, FormatConversionSpecImpl* conv) {
|
||||
conv->precision_ = p;
|
||||
}
|
||||
static std::string FlagsToString(const FormatConversionSpecImpl& spec) {
|
||||
return str_format_internal::FlagsToString(spec.flags_);
|
||||
}
|
||||
};
|
||||
|
||||
// Type safe OR operator.
|
||||
// We need this for two reasons:
|
||||
// 1. operator| on enums makes them decay to integers and the result is an
|
||||
// integer. We need the result to stay as an enum.
|
||||
// 2. We use "enum class" which would not work even if we accepted the decay.
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetUnion(
|
||||
FormatConversionCharSet a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
template <typename... CharSet>
|
||||
constexpr FormatConversionCharSet FormatConversionCharSetUnion(
|
||||
FormatConversionCharSet a, CharSet... rest) {
|
||||
return static_cast<FormatConversionCharSet>(
|
||||
static_cast<uint64_t>(a) |
|
||||
static_cast<uint64_t>(FormatConversionCharSetUnion(rest...)));
|
||||
}
|
||||
|
||||
constexpr uint64_t FormatConversionCharToConvInt(FormatConversionChar c) {
|
||||
return uint64_t{1} << (1 + static_cast<uint8_t>(c));
|
||||
}
|
||||
|
||||
constexpr uint64_t FormatConversionCharToConvInt(char conv) {
|
||||
return
|
||||
#define ABSL_INTERNAL_CHAR_SET_CASE(c) \
|
||||
conv == #c[0] \
|
||||
? FormatConversionCharToConvInt(FormatConversionCharInternal::c) \
|
||||
:
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
|
||||
#undef ABSL_INTERNAL_CHAR_SET_CASE
|
||||
conv == '*'
|
||||
? 1
|
||||
: 0;
|
||||
}
|
||||
|
||||
constexpr FormatConversionCharSet FormatConversionCharToConvValue(char conv) {
|
||||
return static_cast<FormatConversionCharSet>(
|
||||
FormatConversionCharToConvInt(conv));
|
||||
}
|
||||
|
||||
struct FormatConversionCharSetInternal {
|
||||
#define ABSL_INTERNAL_CHAR_SET_CASE(c) \
|
||||
static constexpr FormatConversionCharSet c = \
|
||||
FormatConversionCharToConvValue(#c[0]);
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
|
||||
#undef ABSL_INTERNAL_CHAR_SET_CASE
|
||||
|
||||
// Used for width/precision '*' specification.
|
||||
static constexpr FormatConversionCharSet kStar =
|
||||
FormatConversionCharToConvValue('*');
|
||||
|
||||
static constexpr FormatConversionCharSet kIntegral =
|
||||
FormatConversionCharSetUnion(d, i, u, o, x, X);
|
||||
static constexpr FormatConversionCharSet kFloating =
|
||||
FormatConversionCharSetUnion(a, e, f, g, A, E, F, G);
|
||||
static constexpr FormatConversionCharSet kNumeric =
|
||||
FormatConversionCharSetUnion(kIntegral, kFloating);
|
||||
static constexpr FormatConversionCharSet kPointer = p;
|
||||
};
|
||||
|
||||
// Type safe OR operator.
|
||||
// We need this for two reasons:
|
||||
// 1. operator| on enums makes them decay to integers and the result is an
|
||||
// integer. We need the result to stay as an enum.
|
||||
// 2. We use "enum class" which would not work even if we accepted the decay.
|
||||
constexpr FormatConversionCharSet operator|(FormatConversionCharSet a,
|
||||
FormatConversionCharSet b) {
|
||||
return FormatConversionCharSetUnion(a, b);
|
||||
}
|
||||
|
||||
// Overloaded conversion functions to support absl::ParsedFormat.
|
||||
// Get a conversion with a single character in it.
|
||||
constexpr FormatConversionCharSet ToFormatConversionCharSet(char c) {
|
||||
return static_cast<FormatConversionCharSet>(
|
||||
FormatConversionCharToConvValue(c));
|
||||
}
|
||||
|
||||
// Get a conversion with a single character in it.
|
||||
constexpr FormatConversionCharSet ToFormatConversionCharSet(
|
||||
FormatConversionCharSet c) {
|
||||
return c;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ToFormatConversionCharSet(T) = delete;
|
||||
|
||||
// Checks whether `c` exists in `set`.
|
||||
constexpr bool Contains(FormatConversionCharSet set, char c) {
|
||||
return (static_cast<uint64_t>(set) &
|
||||
static_cast<uint64_t>(FormatConversionCharToConvValue(c))) != 0;
|
||||
}
|
||||
|
||||
// Checks whether all the characters in `c` are contained in `set`
|
||||
constexpr bool Contains(FormatConversionCharSet set,
|
||||
FormatConversionCharSet c) {
|
||||
return (static_cast<uint64_t>(set) & static_cast<uint64_t>(c)) ==
|
||||
static_cast<uint64_t>(c);
|
||||
}
|
||||
|
||||
// Checks whether all the characters in `c` are contained in `set`
|
||||
constexpr bool Contains(FormatConversionCharSet set, FormatConversionChar c) {
|
||||
return (static_cast<uint64_t>(set) & FormatConversionCharToConvInt(c)) != 0;
|
||||
}
|
||||
|
||||
// Return capacity - used, clipped to a minimum of 0.
|
||||
inline size_t Excess(size_t used, size_t capacity) {
|
||||
return used < capacity ? capacity - used : 0;
|
||||
}
|
||||
|
||||
} // namespace str_format_internal
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_
|
||||
98
third_party/abseil-cpp/absl/strings/internal/str_format/extension_test.cc
vendored
Normal file
98
third_party/abseil-cpp/absl/strings/internal/str_format/extension_test.cc
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include "absl/strings/internal/str_format/extension.h"
|
||||
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace my_namespace {
|
||||
class UserDefinedType {
|
||||
public:
|
||||
UserDefinedType() = default;
|
||||
|
||||
void Append(absl::string_view str) { value_.append(str.data(), str.size()); }
|
||||
const std::string& Value() const { return value_; }
|
||||
|
||||
friend void AbslFormatFlush(UserDefinedType* x, absl::string_view str) {
|
||||
x->Append(str);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string value_;
|
||||
};
|
||||
} // namespace my_namespace
|
||||
|
||||
namespace {
|
||||
|
||||
std::string MakeRandomString(size_t len) {
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis('a', 'z');
|
||||
std::string s(len, '0');
|
||||
for (char& c : s) {
|
||||
c = dis(gen);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
TEST(FormatExtensionTest, SinkAppendSubstring) {
|
||||
for (size_t chunk_size : {1, 10, 100, 1000, 10000}) {
|
||||
std::string expected, actual;
|
||||
absl::str_format_internal::FormatSinkImpl sink(&actual);
|
||||
for (size_t chunks = 0; chunks < 10; ++chunks) {
|
||||
std::string rand = MakeRandomString(chunk_size);
|
||||
expected += rand;
|
||||
sink.Append(rand);
|
||||
}
|
||||
sink.Flush();
|
||||
EXPECT_EQ(actual, expected);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FormatExtensionTest, SinkAppendChars) {
|
||||
for (size_t chunk_size : {1, 10, 100, 1000, 10000}) {
|
||||
std::string expected, actual;
|
||||
absl::str_format_internal::FormatSinkImpl sink(&actual);
|
||||
for (size_t chunks = 0; chunks < 10; ++chunks) {
|
||||
std::string rand = MakeRandomString(1);
|
||||
expected.append(chunk_size, rand[0]);
|
||||
sink.Append(chunk_size, rand[0]);
|
||||
}
|
||||
sink.Flush();
|
||||
EXPECT_EQ(actual, expected);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FormatExtensionTest, VerifyEnumEquality) {
|
||||
#define X_VAL(id) \
|
||||
EXPECT_EQ(absl::FormatConversionChar::id, \
|
||||
absl::str_format_internal::FormatConversionCharInternal::id);
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, );
|
||||
#undef X_VAL
|
||||
|
||||
#define X_VAL(id) \
|
||||
EXPECT_EQ(absl::FormatConversionCharSet::id, \
|
||||
absl::str_format_internal::FormatConversionCharSetInternal::id);
|
||||
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, );
|
||||
#undef X_VAL
|
||||
}
|
||||
|
||||
} // namespace
|
||||
1423
third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.cc
vendored
Normal file
1423
third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.cc
vendored
Normal file
File diff suppressed because it is too large
Load Diff
37
third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.h
vendored
Normal file
37
third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.h
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_FLOAT_CONVERSION_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_FLOAT_CONVERSION_H_
|
||||
|
||||
#include "absl/strings/internal/str_format/extension.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
bool ConvertFloatImpl(float v, const FormatConversionSpecImpl &conv,
|
||||
FormatSinkImpl *sink);
|
||||
|
||||
bool ConvertFloatImpl(double v, const FormatConversionSpecImpl &conv,
|
||||
FormatSinkImpl *sink);
|
||||
|
||||
bool ConvertFloatImpl(long double v, const FormatConversionSpecImpl &conv,
|
||||
FormatSinkImpl *sink);
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_FLOAT_CONVERSION_H_
|
||||
72
third_party/abseil-cpp/absl/strings/internal/str_format/output.cc
vendored
Normal file
72
third_party/abseil-cpp/absl/strings/internal/str_format/output.cc
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/output.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
namespace {
|
||||
struct ClearErrnoGuard {
|
||||
ClearErrnoGuard() : old_value(errno) { errno = 0; }
|
||||
~ClearErrnoGuard() {
|
||||
if (!errno) errno = old_value;
|
||||
}
|
||||
int old_value;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
void BufferRawSink::Write(string_view v) {
|
||||
size_t to_write = std::min(v.size(), size_);
|
||||
std::memcpy(buffer_, v.data(), to_write);
|
||||
buffer_ += to_write;
|
||||
size_ -= to_write;
|
||||
total_written_ += v.size();
|
||||
}
|
||||
|
||||
void FILERawSink::Write(string_view v) {
|
||||
while (!v.empty() && !error_) {
|
||||
// Reset errno to zero in case the libc implementation doesn't set errno
|
||||
// when a failure occurs.
|
||||
ClearErrnoGuard guard;
|
||||
|
||||
if (size_t result = std::fwrite(v.data(), 1, v.size(), output_)) {
|
||||
// Some progress was made.
|
||||
count_ += result;
|
||||
v.remove_prefix(result);
|
||||
} else {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
} else if (errno) {
|
||||
error_ = errno;
|
||||
} else if (std::ferror(output_)) {
|
||||
// Non-POSIX compliant libc implementations may not set errno, so we
|
||||
// have check the streams error indicator.
|
||||
error_ = EBADF;
|
||||
} else {
|
||||
// We're likely on a non-POSIX system that encountered EINTR but had no
|
||||
// way of reporting it.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
96
third_party/abseil-cpp/absl/strings/internal/str_format/output.h
vendored
Normal file
96
third_party/abseil-cpp/absl/strings/internal/str_format/output.h
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Output extension hooks for the Format library.
|
||||
// `internal::InvokeFlush` calls the appropriate flush function for the
|
||||
// specified output argument.
|
||||
// `BufferRawSink` is a simple output sink for a char buffer. Used by SnprintF.
|
||||
// `FILERawSink` is a std::FILE* based sink. Used by PrintF and FprintF.
|
||||
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
|
||||
|
||||
#include <cstdio>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
// RawSink implementation that writes into a char* buffer.
|
||||
// It will not overflow the buffer, but will keep the total count of chars
|
||||
// that would have been written.
|
||||
class BufferRawSink {
|
||||
public:
|
||||
BufferRawSink(char* buffer, size_t size) : buffer_(buffer), size_(size) {}
|
||||
|
||||
size_t total_written() const { return total_written_; }
|
||||
void Write(string_view v);
|
||||
|
||||
private:
|
||||
char* buffer_;
|
||||
size_t size_;
|
||||
size_t total_written_ = 0;
|
||||
};
|
||||
|
||||
// RawSink implementation that writes into a FILE*.
|
||||
// It keeps track of the total number of bytes written and any error encountered
|
||||
// during the writes.
|
||||
class FILERawSink {
|
||||
public:
|
||||
explicit FILERawSink(std::FILE* output) : output_(output) {}
|
||||
|
||||
void Write(string_view v);
|
||||
|
||||
size_t count() const { return count_; }
|
||||
int error() const { return error_; }
|
||||
|
||||
private:
|
||||
std::FILE* output_;
|
||||
int error_ = 0;
|
||||
size_t count_ = 0;
|
||||
};
|
||||
|
||||
// Provide RawSink integration with common types from the STL.
|
||||
inline void AbslFormatFlush(std::string* out, string_view s) {
|
||||
out->append(s.data(), s.size());
|
||||
}
|
||||
inline void AbslFormatFlush(std::ostream* out, string_view s) {
|
||||
out->write(s.data(), s.size());
|
||||
}
|
||||
|
||||
inline void AbslFormatFlush(FILERawSink* sink, string_view v) {
|
||||
sink->Write(v);
|
||||
}
|
||||
|
||||
inline void AbslFormatFlush(BufferRawSink* sink, string_view v) {
|
||||
sink->Write(v);
|
||||
}
|
||||
|
||||
// This is a SFINAE to get a better compiler error message when the type
|
||||
// is not supported.
|
||||
template <typename T>
|
||||
auto InvokeFlush(T* out, string_view s) -> decltype(AbslFormatFlush(out, s)) {
|
||||
AbslFormatFlush(out, s);
|
||||
}
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
|
||||
79
third_party/abseil-cpp/absl/strings/internal/str_format/output_test.cc
vendored
Normal file
79
third_party/abseil-cpp/absl/strings/internal/str_format/output_test.cc
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2017 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/output.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/cord.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace {
|
||||
|
||||
TEST(InvokeFlush, String) {
|
||||
std::string str = "ABC";
|
||||
str_format_internal::InvokeFlush(&str, "DEF");
|
||||
EXPECT_EQ(str, "ABCDEF");
|
||||
}
|
||||
|
||||
TEST(InvokeFlush, Stream) {
|
||||
std::stringstream str;
|
||||
str << "ABC";
|
||||
str_format_internal::InvokeFlush(&str, "DEF");
|
||||
EXPECT_EQ(str.str(), "ABCDEF");
|
||||
}
|
||||
|
||||
TEST(InvokeFlush, Cord) {
|
||||
absl::Cord str("ABC");
|
||||
str_format_internal::InvokeFlush(&str, "DEF");
|
||||
EXPECT_EQ(str, "ABCDEF");
|
||||
}
|
||||
|
||||
TEST(BufferRawSink, Limits) {
|
||||
char buf[16];
|
||||
{
|
||||
std::fill(std::begin(buf), std::end(buf), 'x');
|
||||
str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
|
||||
str_format_internal::InvokeFlush(&bufsink, "Hello World237");
|
||||
EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World237xx");
|
||||
}
|
||||
{
|
||||
std::fill(std::begin(buf), std::end(buf), 'x');
|
||||
str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
|
||||
str_format_internal::InvokeFlush(&bufsink, "Hello World237237");
|
||||
EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World2372x");
|
||||
}
|
||||
{
|
||||
std::fill(std::begin(buf), std::end(buf), 'x');
|
||||
str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
|
||||
str_format_internal::InvokeFlush(&bufsink, "Hello World");
|
||||
str_format_internal::InvokeFlush(&bufsink, "237");
|
||||
EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World237xx");
|
||||
}
|
||||
{
|
||||
std::fill(std::begin(buf), std::end(buf), 'x');
|
||||
str_format_internal::BufferRawSink bufsink(buf, sizeof(buf) - 1);
|
||||
str_format_internal::InvokeFlush(&bufsink, "Hello World");
|
||||
str_format_internal::InvokeFlush(&bufsink, "237237");
|
||||
EXPECT_EQ(std::string(buf, sizeof(buf)), "Hello World2372x");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
339
third_party/abseil-cpp/absl/strings/internal/str_format/parser.cc
vendored
Normal file
339
third_party/abseil-cpp/absl/strings/internal/str_format/parser.cc
vendored
Normal file
@@ -0,0 +1,339 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/parser.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
|
||||
#include <algorithm>
|
||||
#include <initializer_list>
|
||||
#include <limits>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
using CC = FormatConversionCharInternal;
|
||||
using LM = LengthMod;
|
||||
|
||||
// Abbreviations to fit in the table below.
|
||||
constexpr auto f_sign = Flags::kSignCol;
|
||||
constexpr auto f_alt = Flags::kAlt;
|
||||
constexpr auto f_pos = Flags::kShowPos;
|
||||
constexpr auto f_left = Flags::kLeft;
|
||||
constexpr auto f_zero = Flags::kZero;
|
||||
|
||||
ABSL_CONST_INIT const ConvTag kTags[256] = {
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 00-07
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 08-0f
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 10-17
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 18-1f
|
||||
f_sign, {}, {}, f_alt, {}, {}, {}, {}, // !"#$%&'
|
||||
{}, {}, {}, f_pos, {}, f_left, {}, {}, // ()*+,-./
|
||||
f_zero, {}, {}, {}, {}, {}, {}, {}, // 01234567
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 89:;<=>?
|
||||
{}, CC::A, {}, {}, {}, CC::E, CC::F, CC::G, // @ABCDEFG
|
||||
{}, {}, {}, {}, LM::L, {}, {}, {}, // HIJKLMNO
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // PQRSTUVW
|
||||
CC::X, {}, {}, {}, {}, {}, {}, {}, // XYZ[\]^_
|
||||
{}, CC::a, {}, CC::c, CC::d, CC::e, CC::f, CC::g, // `abcdefg
|
||||
LM::h, CC::i, LM::j, {}, LM::l, {}, CC::n, CC::o, // hijklmno
|
||||
CC::p, LM::q, {}, CC::s, LM::t, CC::u, {}, {}, // pqrstuvw
|
||||
CC::x, {}, LM::z, {}, {}, {}, {}, {}, // xyz{|}!
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 80-87
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 88-8f
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 90-97
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // 98-9f
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // a0-a7
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // a8-af
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // b0-b7
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // b8-bf
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // c0-c7
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // c8-cf
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // d0-d7
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // d8-df
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // e0-e7
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // e8-ef
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // f0-f7
|
||||
{}, {}, {}, {}, {}, {}, {}, {}, // f8-ff
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
bool CheckFastPathSetting(const UnboundConversion& conv) {
|
||||
bool width_precision_needed =
|
||||
conv.width.value() >= 0 || conv.precision.value() >= 0;
|
||||
if (width_precision_needed && conv.flags == Flags::kBasic) {
|
||||
fprintf(stderr,
|
||||
"basic=%d left=%d show_pos=%d sign_col=%d alt=%d zero=%d "
|
||||
"width=%d precision=%d\n",
|
||||
conv.flags == Flags::kBasic ? 1 : 0,
|
||||
FlagsContains(conv.flags, Flags::kLeft) ? 1 : 0,
|
||||
FlagsContains(conv.flags, Flags::kShowPos) ? 1 : 0,
|
||||
FlagsContains(conv.flags, Flags::kSignCol) ? 1 : 0,
|
||||
FlagsContains(conv.flags, Flags::kAlt) ? 1 : 0,
|
||||
FlagsContains(conv.flags, Flags::kZero) ? 1 : 0, conv.width.value(),
|
||||
conv.precision.value());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <bool is_positional>
|
||||
const char *ConsumeConversion(const char *pos, const char *const end,
|
||||
UnboundConversion *conv, int *next_arg) {
|
||||
const char* const original_pos = pos;
|
||||
char c;
|
||||
// Read the next char into `c` and update `pos`. Returns false if there are
|
||||
// no more chars to read.
|
||||
#define ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR() \
|
||||
do { \
|
||||
if (ABSL_PREDICT_FALSE(pos == end)) return nullptr; \
|
||||
c = *pos++; \
|
||||
} while (0)
|
||||
|
||||
const auto parse_digits = [&] {
|
||||
int digits = c - '0';
|
||||
// We do not want to overflow `digits` so we consume at most digits10
|
||||
// digits. If there are more digits the parsing will fail later on when the
|
||||
// digit doesn't match the expected characters.
|
||||
int num_digits = std::numeric_limits<int>::digits10;
|
||||
for (;;) {
|
||||
if (ABSL_PREDICT_FALSE(pos == end)) break;
|
||||
c = *pos++;
|
||||
if (!std::isdigit(c)) break;
|
||||
--num_digits;
|
||||
if (ABSL_PREDICT_FALSE(!num_digits)) break;
|
||||
digits = 10 * digits + c - '0';
|
||||
}
|
||||
return digits;
|
||||
};
|
||||
|
||||
if (is_positional) {
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return nullptr;
|
||||
conv->arg_position = parse_digits();
|
||||
assert(conv->arg_position > 0);
|
||||
if (ABSL_PREDICT_FALSE(c != '$')) return nullptr;
|
||||
}
|
||||
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
|
||||
// We should start with the basic flag on.
|
||||
assert(conv->flags == Flags::kBasic);
|
||||
|
||||
// Any non alpha character makes this conversion not basic.
|
||||
// This includes flags (-+ #0), width (1-9, *) or precision (.).
|
||||
// All conversion characters and length modifiers are alpha characters.
|
||||
if (c < 'A') {
|
||||
while (c <= '0') {
|
||||
auto tag = GetTagForChar(c);
|
||||
if (tag.is_flags()) {
|
||||
conv->flags = conv->flags | tag.as_flags();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (c <= '9') {
|
||||
if (c >= '0') {
|
||||
int maybe_width = parse_digits();
|
||||
if (!is_positional && c == '$') {
|
||||
if (ABSL_PREDICT_FALSE(*next_arg != 0)) return nullptr;
|
||||
// Positional conversion.
|
||||
*next_arg = -1;
|
||||
return ConsumeConversion<true>(original_pos, end, conv, next_arg);
|
||||
}
|
||||
conv->flags = conv->flags | Flags::kNonBasic;
|
||||
conv->width.set_value(maybe_width);
|
||||
} else if (c == '*') {
|
||||
conv->flags = conv->flags | Flags::kNonBasic;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (is_positional) {
|
||||
if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return nullptr;
|
||||
conv->width.set_from_arg(parse_digits());
|
||||
if (ABSL_PREDICT_FALSE(c != '$')) return nullptr;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
conv->width.set_from_arg(++*next_arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (c == '.') {
|
||||
conv->flags = conv->flags | Flags::kNonBasic;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (std::isdigit(c)) {
|
||||
conv->precision.set_value(parse_digits());
|
||||
} else if (c == '*') {
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (is_positional) {
|
||||
if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return nullptr;
|
||||
conv->precision.set_from_arg(parse_digits());
|
||||
if (c != '$') return nullptr;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
conv->precision.set_from_arg(++*next_arg);
|
||||
}
|
||||
} else {
|
||||
conv->precision.set_value(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto tag = GetTagForChar(c);
|
||||
|
||||
if (ABSL_PREDICT_FALSE(!tag.is_conv())) {
|
||||
if (ABSL_PREDICT_FALSE(!tag.is_length())) return nullptr;
|
||||
|
||||
// It is a length modifier.
|
||||
using str_format_internal::LengthMod;
|
||||
LengthMod length_mod = tag.as_length();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (c == 'h' && length_mod == LengthMod::h) {
|
||||
conv->length_mod = LengthMod::hh;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else if (c == 'l' && length_mod == LengthMod::l) {
|
||||
conv->length_mod = LengthMod::ll;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
conv->length_mod = length_mod;
|
||||
}
|
||||
tag = GetTagForChar(c);
|
||||
if (ABSL_PREDICT_FALSE(!tag.is_conv())) return nullptr;
|
||||
}
|
||||
|
||||
assert(CheckFastPathSetting(*conv));
|
||||
(void)(&CheckFastPathSetting);
|
||||
|
||||
conv->conv = tag.as_conv();
|
||||
if (!is_positional) conv->arg_position = ++*next_arg;
|
||||
return pos;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string LengthModToString(LengthMod v) {
|
||||
switch (v) {
|
||||
case LengthMod::h:
|
||||
return "h";
|
||||
case LengthMod::hh:
|
||||
return "hh";
|
||||
case LengthMod::l:
|
||||
return "l";
|
||||
case LengthMod::ll:
|
||||
return "ll";
|
||||
case LengthMod::L:
|
||||
return "L";
|
||||
case LengthMod::j:
|
||||
return "j";
|
||||
case LengthMod::z:
|
||||
return "z";
|
||||
case LengthMod::t:
|
||||
return "t";
|
||||
case LengthMod::q:
|
||||
return "q";
|
||||
case LengthMod::none:
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
const char *ConsumeUnboundConversion(const char *p, const char *end,
|
||||
UnboundConversion *conv, int *next_arg) {
|
||||
if (*next_arg < 0) return ConsumeConversion<true>(p, end, conv, next_arg);
|
||||
return ConsumeConversion<false>(p, end, conv, next_arg);
|
||||
}
|
||||
|
||||
struct ParsedFormatBase::ParsedFormatConsumer {
|
||||
explicit ParsedFormatConsumer(ParsedFormatBase *parsedformat)
|
||||
: parsed(parsedformat), data_pos(parsedformat->data_.get()) {}
|
||||
|
||||
bool Append(string_view s) {
|
||||
if (s.empty()) return true;
|
||||
|
||||
size_t text_end = AppendText(s);
|
||||
|
||||
if (!parsed->items_.empty() && !parsed->items_.back().is_conversion) {
|
||||
// Let's extend the existing text run.
|
||||
parsed->items_.back().text_end = text_end;
|
||||
} else {
|
||||
// Let's make a new text run.
|
||||
parsed->items_.push_back({false, text_end, {}});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConvertOne(const UnboundConversion &conv, string_view s) {
|
||||
size_t text_end = AppendText(s);
|
||||
parsed->items_.push_back({true, text_end, conv});
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t AppendText(string_view s) {
|
||||
memcpy(data_pos, s.data(), s.size());
|
||||
data_pos += s.size();
|
||||
return static_cast<size_t>(data_pos - parsed->data_.get());
|
||||
}
|
||||
|
||||
ParsedFormatBase *parsed;
|
||||
char* data_pos;
|
||||
};
|
||||
|
||||
ParsedFormatBase::ParsedFormatBase(
|
||||
string_view format, bool allow_ignored,
|
||||
std::initializer_list<FormatConversionCharSet> convs)
|
||||
: data_(format.empty() ? nullptr : new char[format.size()]) {
|
||||
has_error_ = !ParseFormatString(format, ParsedFormatConsumer(this)) ||
|
||||
!MatchesConversions(allow_ignored, convs);
|
||||
}
|
||||
|
||||
bool ParsedFormatBase::MatchesConversions(
|
||||
bool allow_ignored,
|
||||
std::initializer_list<FormatConversionCharSet> convs) const {
|
||||
std::unordered_set<int> used;
|
||||
auto add_if_valid_conv = [&](int pos, char c) {
|
||||
if (static_cast<size_t>(pos) > convs.size() ||
|
||||
!Contains(convs.begin()[pos - 1], c))
|
||||
return false;
|
||||
used.insert(pos);
|
||||
return true;
|
||||
};
|
||||
for (const ConversionItem &item : items_) {
|
||||
if (!item.is_conversion) continue;
|
||||
auto &conv = item.conv;
|
||||
if (conv.precision.is_from_arg() &&
|
||||
!add_if_valid_conv(conv.precision.get_from_arg(), '*'))
|
||||
return false;
|
||||
if (conv.width.is_from_arg() &&
|
||||
!add_if_valid_conv(conv.width.get_from_arg(), '*'))
|
||||
return false;
|
||||
if (!add_if_valid_conv(conv.arg_position,
|
||||
FormatConversionCharToChar(conv.conv)))
|
||||
return false;
|
||||
}
|
||||
return used.size() == convs.size() || allow_ignored;
|
||||
}
|
||||
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
357
third_party/abseil-cpp/absl/strings/internal/str_format/parser.h
vendored
Normal file
357
third_party/abseil-cpp/absl/strings/internal/str_format/parser.h
vendored
Normal file
@@ -0,0 +1,357 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_
|
||||
#define ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <iosfwd>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/internal/str_format/checker.h"
|
||||
#include "absl/strings/internal/str_format/extension.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
enum class LengthMod : std::uint8_t { h, hh, l, ll, L, j, z, t, q, none };
|
||||
|
||||
std::string LengthModToString(LengthMod v);
|
||||
|
||||
// The analyzed properties of a single specified conversion.
|
||||
struct UnboundConversion {
|
||||
UnboundConversion() {}
|
||||
|
||||
class InputValue {
|
||||
public:
|
||||
void set_value(int value) {
|
||||
assert(value >= 0);
|
||||
value_ = value;
|
||||
}
|
||||
int value() const { return value_; }
|
||||
|
||||
// Marks the value as "from arg". aka the '*' format.
|
||||
// Requires `value >= 1`.
|
||||
// When set, is_from_arg() return true and get_from_arg() returns the
|
||||
// original value.
|
||||
// `value()`'s return value is unspecfied in this state.
|
||||
void set_from_arg(int value) {
|
||||
assert(value > 0);
|
||||
value_ = -value - 1;
|
||||
}
|
||||
bool is_from_arg() const { return value_ < -1; }
|
||||
int get_from_arg() const {
|
||||
assert(is_from_arg());
|
||||
return -value_ - 1;
|
||||
}
|
||||
|
||||
private:
|
||||
int value_ = -1;
|
||||
};
|
||||
|
||||
// No need to initialize. It will always be set in the parser.
|
||||
int arg_position;
|
||||
|
||||
InputValue width;
|
||||
InputValue precision;
|
||||
|
||||
Flags flags = Flags::kBasic;
|
||||
LengthMod length_mod = LengthMod::none;
|
||||
FormatConversionChar conv = FormatConversionCharInternal::kNone;
|
||||
};
|
||||
|
||||
// Consume conversion spec prefix (not including '%') of [p, end) if valid.
|
||||
// Examples of valid specs would be e.g.: "s", "d", "-12.6f".
|
||||
// If valid, it returns the first character following the conversion spec,
|
||||
// and the spec part is broken down and returned in 'conv'.
|
||||
// If invalid, returns nullptr.
|
||||
const char* ConsumeUnboundConversion(const char* p, const char* end,
|
||||
UnboundConversion* conv, int* next_arg);
|
||||
|
||||
// Helper tag class for the table below.
|
||||
// It allows fast `char -> ConversionChar/LengthMod/Flags` checking and
|
||||
// conversions.
|
||||
class ConvTag {
|
||||
public:
|
||||
constexpr ConvTag(FormatConversionChar conversion_char) // NOLINT
|
||||
: tag_(static_cast<uint8_t>(conversion_char)) {}
|
||||
constexpr ConvTag(LengthMod length_mod) // NOLINT
|
||||
: tag_(0x80 | static_cast<uint8_t>(length_mod)) {}
|
||||
constexpr ConvTag(Flags flags) // NOLINT
|
||||
: tag_(0xc0 | static_cast<uint8_t>(flags)) {}
|
||||
constexpr ConvTag() : tag_(0xFF) {}
|
||||
|
||||
bool is_conv() const { return (tag_ & 0x80) == 0; }
|
||||
bool is_length() const { return (tag_ & 0xC0) == 0x80; }
|
||||
bool is_flags() const { return (tag_ & 0xE0) == 0xC0; }
|
||||
|
||||
FormatConversionChar as_conv() const {
|
||||
assert(is_conv());
|
||||
assert(!is_length());
|
||||
assert(!is_flags());
|
||||
return static_cast<FormatConversionChar>(tag_);
|
||||
}
|
||||
LengthMod as_length() const {
|
||||
assert(!is_conv());
|
||||
assert(is_length());
|
||||
assert(!is_flags());
|
||||
return static_cast<LengthMod>(tag_ & 0x3F);
|
||||
}
|
||||
Flags as_flags() const {
|
||||
assert(!is_conv());
|
||||
assert(!is_length());
|
||||
assert(is_flags());
|
||||
return static_cast<Flags>(tag_ & 0x1F);
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t tag_;
|
||||
};
|
||||
|
||||
extern const ConvTag kTags[256];
|
||||
// Keep a single table for all the conversion chars and length modifiers.
|
||||
inline ConvTag GetTagForChar(char c) {
|
||||
return kTags[static_cast<unsigned char>(c)];
|
||||
}
|
||||
|
||||
// Parse the format string provided in 'src' and pass the identified items into
|
||||
// 'consumer'.
|
||||
// Text runs will be passed by calling
|
||||
// Consumer::Append(string_view);
|
||||
// ConversionItems will be passed by calling
|
||||
// Consumer::ConvertOne(UnboundConversion, string_view);
|
||||
// In the case of ConvertOne, the string_view that is passed is the
|
||||
// portion of the format string corresponding to the conversion, not including
|
||||
// the leading %. On success, it returns true. On failure, it stops and returns
|
||||
// false.
|
||||
template <typename Consumer>
|
||||
bool ParseFormatString(string_view src, Consumer consumer) {
|
||||
int next_arg = 0;
|
||||
const char* p = src.data();
|
||||
const char* const end = p + src.size();
|
||||
while (p != end) {
|
||||
const char* percent = static_cast<const char*>(memchr(p, '%', end - p));
|
||||
if (!percent) {
|
||||
// We found the last substring.
|
||||
return consumer.Append(string_view(p, end - p));
|
||||
}
|
||||
// We found a percent, so push the text run then process the percent.
|
||||
if (ABSL_PREDICT_FALSE(!consumer.Append(string_view(p, percent - p)))) {
|
||||
return false;
|
||||
}
|
||||
if (ABSL_PREDICT_FALSE(percent + 1 >= end)) return false;
|
||||
|
||||
auto tag = GetTagForChar(percent[1]);
|
||||
if (tag.is_conv()) {
|
||||
if (ABSL_PREDICT_FALSE(next_arg < 0)) {
|
||||
// This indicates an error in the format string.
|
||||
// The only way to get `next_arg < 0` here is to have a positional
|
||||
// argument first which sets next_arg to -1 and then a non-positional
|
||||
// argument.
|
||||
return false;
|
||||
}
|
||||
p = percent + 2;
|
||||
|
||||
// Keep this case separate from the one below.
|
||||
// ConvertOne is more efficient when the compiler can see that the `basic`
|
||||
// flag is set.
|
||||
UnboundConversion conv;
|
||||
conv.conv = tag.as_conv();
|
||||
conv.arg_position = ++next_arg;
|
||||
if (ABSL_PREDICT_FALSE(
|
||||
!consumer.ConvertOne(conv, string_view(percent + 1, 1)))) {
|
||||
return false;
|
||||
}
|
||||
} else if (percent[1] != '%') {
|
||||
UnboundConversion conv;
|
||||
p = ConsumeUnboundConversion(percent + 1, end, &conv, &next_arg);
|
||||
if (ABSL_PREDICT_FALSE(p == nullptr)) return false;
|
||||
if (ABSL_PREDICT_FALSE(!consumer.ConvertOne(
|
||||
conv, string_view(percent + 1, p - (percent + 1))))) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (ABSL_PREDICT_FALSE(!consumer.Append("%"))) return false;
|
||||
p = percent + 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Always returns true, or fails to compile in a constexpr context if s does not
|
||||
// point to a constexpr char array.
|
||||
constexpr bool EnsureConstexpr(string_view s) {
|
||||
return s.empty() || s[0] == s[0];
|
||||
}
|
||||
|
||||
class ParsedFormatBase {
|
||||
public:
|
||||
explicit ParsedFormatBase(
|
||||
string_view format, bool allow_ignored,
|
||||
std::initializer_list<FormatConversionCharSet> convs);
|
||||
|
||||
ParsedFormatBase(const ParsedFormatBase& other) { *this = other; }
|
||||
|
||||
ParsedFormatBase(ParsedFormatBase&& other) { *this = std::move(other); }
|
||||
|
||||
ParsedFormatBase& operator=(const ParsedFormatBase& other) {
|
||||
if (this == &other) return *this;
|
||||
has_error_ = other.has_error_;
|
||||
items_ = other.items_;
|
||||
size_t text_size = items_.empty() ? 0 : items_.back().text_end;
|
||||
data_.reset(new char[text_size]);
|
||||
memcpy(data_.get(), other.data_.get(), text_size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ParsedFormatBase& operator=(ParsedFormatBase&& other) {
|
||||
if (this == &other) return *this;
|
||||
has_error_ = other.has_error_;
|
||||
data_ = std::move(other.data_);
|
||||
items_ = std::move(other.items_);
|
||||
// Reset the vector to make sure the invariants hold.
|
||||
other.items_.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Consumer>
|
||||
bool ProcessFormat(Consumer consumer) const {
|
||||
const char* const base = data_.get();
|
||||
string_view text(base, 0);
|
||||
for (const auto& item : items_) {
|
||||
const char* const end = text.data() + text.size();
|
||||
text = string_view(end, (base + item.text_end) - end);
|
||||
if (item.is_conversion) {
|
||||
if (!consumer.ConvertOne(item.conv, text)) return false;
|
||||
} else {
|
||||
if (!consumer.Append(text)) return false;
|
||||
}
|
||||
}
|
||||
return !has_error_;
|
||||
}
|
||||
|
||||
bool has_error() const { return has_error_; }
|
||||
|
||||
private:
|
||||
// Returns whether the conversions match and if !allow_ignored it verifies
|
||||
// that all conversions are used by the format.
|
||||
bool MatchesConversions(
|
||||
bool allow_ignored,
|
||||
std::initializer_list<FormatConversionCharSet> convs) const;
|
||||
|
||||
struct ParsedFormatConsumer;
|
||||
|
||||
struct ConversionItem {
|
||||
bool is_conversion;
|
||||
// Points to the past-the-end location of this element in the data_ array.
|
||||
size_t text_end;
|
||||
UnboundConversion conv;
|
||||
};
|
||||
|
||||
bool has_error_;
|
||||
std::unique_ptr<char[]> data_;
|
||||
std::vector<ConversionItem> items_;
|
||||
};
|
||||
|
||||
|
||||
// A value type representing a preparsed format. These can be created, copied
|
||||
// around, and reused to speed up formatting loops.
|
||||
// The user must specify through the template arguments the conversion
|
||||
// characters used in the format. This will be checked at compile time.
|
||||
//
|
||||
// This class uses Conv enum values to specify each argument.
|
||||
// This allows for more flexibility as you can specify multiple possible
|
||||
// conversion characters for each argument.
|
||||
// ParsedFormat<char...> is a simplified alias for when the user only
|
||||
// needs to specify a single conversion character for each argument.
|
||||
//
|
||||
// Example:
|
||||
// // Extended format supports multiple characters per argument:
|
||||
// using MyFormat = ExtendedParsedFormat<Conv::d | Conv::x>;
|
||||
// MyFormat GetFormat(bool use_hex) {
|
||||
// if (use_hex) return MyFormat("foo %x bar");
|
||||
// return MyFormat("foo %d bar");
|
||||
// }
|
||||
// // 'format' can be used with any value that supports 'd' and 'x',
|
||||
// // like `int`.
|
||||
// auto format = GetFormat(use_hex);
|
||||
// value = StringF(format, i);
|
||||
//
|
||||
// This class also supports runtime format checking with the ::New() and
|
||||
// ::NewAllowIgnored() factory functions.
|
||||
// This is the only API that allows the user to pass a runtime specified format
|
||||
// string. These factory functions will return NULL if the format does not match
|
||||
// the conversions requested by the user.
|
||||
template <FormatConversionCharSet... C>
|
||||
class ExtendedParsedFormat : public str_format_internal::ParsedFormatBase {
|
||||
public:
|
||||
explicit ExtendedParsedFormat(string_view format)
|
||||
#ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
__attribute__((
|
||||
enable_if(str_format_internal::EnsureConstexpr(format),
|
||||
"Format string is not constexpr."),
|
||||
enable_if(str_format_internal::ValidFormatImpl<C...>(format),
|
||||
"Format specified does not match the template arguments.")))
|
||||
#endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
|
||||
: ExtendedParsedFormat(format, false) {
|
||||
}
|
||||
|
||||
// ExtendedParsedFormat factory function.
|
||||
// The user still has to specify the conversion characters, but they will not
|
||||
// be checked at compile time. Instead, it will be checked at runtime.
|
||||
// This delays the checking to runtime, but allows the user to pass
|
||||
// dynamically sourced formats.
|
||||
// It returns NULL if the format does not match the conversion characters.
|
||||
// The user is responsible for checking the return value before using it.
|
||||
//
|
||||
// The 'New' variant will check that all the specified arguments are being
|
||||
// consumed by the format and return NULL if any argument is being ignored.
|
||||
// The 'NewAllowIgnored' variant will not verify this and will allow formats
|
||||
// that ignore arguments.
|
||||
static std::unique_ptr<ExtendedParsedFormat> New(string_view format) {
|
||||
return New(format, false);
|
||||
}
|
||||
static std::unique_ptr<ExtendedParsedFormat> NewAllowIgnored(
|
||||
string_view format) {
|
||||
return New(format, true);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::unique_ptr<ExtendedParsedFormat> New(string_view format,
|
||||
bool allow_ignored) {
|
||||
std::unique_ptr<ExtendedParsedFormat> conv(
|
||||
new ExtendedParsedFormat(format, allow_ignored));
|
||||
if (conv->has_error()) return nullptr;
|
||||
return conv;
|
||||
}
|
||||
|
||||
ExtendedParsedFormat(string_view s, bool allow_ignored)
|
||||
: ParsedFormatBase(s, allow_ignored, {C...}) {}
|
||||
};
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_
|
||||
434
third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc
vendored
Normal file
434
third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc
vendored
Normal file
@@ -0,0 +1,434 @@
|
||||
// Copyright 2020 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/internal/str_format/parser.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/macros.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace str_format_internal {
|
||||
|
||||
namespace {
|
||||
|
||||
using testing::Pair;
|
||||
|
||||
TEST(LengthModTest, Names) {
|
||||
struct Expectation {
|
||||
int line;
|
||||
LengthMod mod;
|
||||
const char *name;
|
||||
};
|
||||
const Expectation kExpect[] = {
|
||||
{__LINE__, LengthMod::none, "" },
|
||||
{__LINE__, LengthMod::h, "h" },
|
||||
{__LINE__, LengthMod::hh, "hh"},
|
||||
{__LINE__, LengthMod::l, "l" },
|
||||
{__LINE__, LengthMod::ll, "ll"},
|
||||
{__LINE__, LengthMod::L, "L" },
|
||||
{__LINE__, LengthMod::j, "j" },
|
||||
{__LINE__, LengthMod::z, "z" },
|
||||
{__LINE__, LengthMod::t, "t" },
|
||||
{__LINE__, LengthMod::q, "q" },
|
||||
};
|
||||
EXPECT_EQ(ABSL_ARRAYSIZE(kExpect), 10);
|
||||
for (auto e : kExpect) {
|
||||
SCOPED_TRACE(e.line);
|
||||
EXPECT_EQ(e.name, LengthModToString(e.mod));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ConversionCharTest, Names) {
|
||||
struct Expectation {
|
||||
FormatConversionChar id;
|
||||
char name;
|
||||
};
|
||||
// clang-format off
|
||||
const Expectation kExpect[] = {
|
||||
#define X(c) {FormatConversionCharInternal::c, #c[0]}
|
||||
X(c), X(s), // text
|
||||
X(d), X(i), X(o), X(u), X(x), X(X), // int
|
||||
X(f), X(F), X(e), X(E), X(g), X(G), X(a), X(A), // float
|
||||
X(n), X(p), // misc
|
||||
#undef X
|
||||
{FormatConversionCharInternal::kNone, '\0'},
|
||||
};
|
||||
// clang-format on
|
||||
for (auto e : kExpect) {
|
||||
SCOPED_TRACE(e.name);
|
||||
FormatConversionChar v = e.id;
|
||||
EXPECT_EQ(e.name, FormatConversionCharToChar(v));
|
||||
}
|
||||
}
|
||||
|
||||
class ConsumeUnboundConversionTest : public ::testing::Test {
|
||||
public:
|
||||
std::pair<string_view, string_view> Consume(string_view src) {
|
||||
int next = 0;
|
||||
o = UnboundConversion(); // refresh
|
||||
const char* p = ConsumeUnboundConversion(
|
||||
src.data(), src.data() + src.size(), &o, &next);
|
||||
if (!p) return {{}, src};
|
||||
return {string_view(src.data(), p - src.data()),
|
||||
string_view(p, src.data() + src.size() - p)};
|
||||
}
|
||||
|
||||
bool Run(const char *fmt, bool force_positional = false) {
|
||||
int next = force_positional ? -1 : 0;
|
||||
o = UnboundConversion(); // refresh
|
||||
return ConsumeUnboundConversion(fmt, fmt + strlen(fmt), &o, &next) ==
|
||||
fmt + strlen(fmt);
|
||||
}
|
||||
UnboundConversion o;
|
||||
};
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, ConsumeSpecification) {
|
||||
struct Expectation {
|
||||
int line;
|
||||
string_view src;
|
||||
string_view out;
|
||||
string_view src_post;
|
||||
};
|
||||
const Expectation kExpect[] = {
|
||||
{__LINE__, "", "", "" },
|
||||
{__LINE__, "b", "", "b" }, // 'b' is invalid
|
||||
{__LINE__, "ba", "", "ba"}, // 'b' is invalid
|
||||
{__LINE__, "l", "", "l" }, // just length mod isn't okay
|
||||
{__LINE__, "d", "d", "" }, // basic
|
||||
{__LINE__, "d ", "d", " " }, // leave suffix
|
||||
{__LINE__, "dd", "d", "d" }, // don't be greedy
|
||||
{__LINE__, "d9", "d", "9" }, // leave non-space suffix
|
||||
{__LINE__, "dzz", "d", "zz"}, // length mod as suffix
|
||||
{__LINE__, "1$*2$d", "1$*2$d", "" }, // arg indexing and * allowed.
|
||||
{__LINE__, "0-14.3hhd", "0-14.3hhd", ""}, // precision, width
|
||||
{__LINE__, " 0-+#14.3hhd", " 0-+#14.3hhd", ""}, // flags
|
||||
};
|
||||
for (const auto& e : kExpect) {
|
||||
SCOPED_TRACE(e.line);
|
||||
EXPECT_THAT(Consume(e.src), Pair(e.out, e.src_post));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, BasicConversion) {
|
||||
EXPECT_FALSE(Run(""));
|
||||
EXPECT_FALSE(Run("z"));
|
||||
|
||||
EXPECT_FALSE(Run("dd")); // no excess allowed
|
||||
|
||||
EXPECT_TRUE(Run("d"));
|
||||
EXPECT_EQ('d', FormatConversionCharToChar(o.conv));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_LT(o.width.value(), 0);
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_LT(o.precision.value(), 0);
|
||||
EXPECT_EQ(1, o.arg_position);
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, ArgPosition) {
|
||||
EXPECT_TRUE(Run("d"));
|
||||
EXPECT_EQ(1, o.arg_position);
|
||||
EXPECT_TRUE(Run("3$d"));
|
||||
EXPECT_EQ(3, o.arg_position);
|
||||
EXPECT_TRUE(Run("1$d"));
|
||||
EXPECT_EQ(1, o.arg_position);
|
||||
EXPECT_TRUE(Run("1$d", true));
|
||||
EXPECT_EQ(1, o.arg_position);
|
||||
EXPECT_TRUE(Run("123$d"));
|
||||
EXPECT_EQ(123, o.arg_position);
|
||||
EXPECT_TRUE(Run("123$d", true));
|
||||
EXPECT_EQ(123, o.arg_position);
|
||||
EXPECT_TRUE(Run("10$d"));
|
||||
EXPECT_EQ(10, o.arg_position);
|
||||
EXPECT_TRUE(Run("10$d", true));
|
||||
EXPECT_EQ(10, o.arg_position);
|
||||
|
||||
// Position can't be zero.
|
||||
EXPECT_FALSE(Run("0$d"));
|
||||
EXPECT_FALSE(Run("0$d", true));
|
||||
EXPECT_FALSE(Run("1$*0$d"));
|
||||
EXPECT_FALSE(Run("1$.*0$d"));
|
||||
|
||||
// Position can't start with a zero digit at all. That is not a 'decimal'.
|
||||
EXPECT_FALSE(Run("01$p"));
|
||||
EXPECT_FALSE(Run("01$p", true));
|
||||
EXPECT_FALSE(Run("1$*01$p"));
|
||||
EXPECT_FALSE(Run("1$.*01$p"));
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, WidthAndPrecision) {
|
||||
EXPECT_TRUE(Run("14d"));
|
||||
EXPECT_EQ('d', FormatConversionCharToChar(o.conv));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_EQ(14, o.width.value());
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_LT(o.precision.value(), 0);
|
||||
|
||||
EXPECT_TRUE(Run("14.d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(14, o.width.value());
|
||||
EXPECT_EQ(0, o.precision.value());
|
||||
|
||||
EXPECT_TRUE(Run(".d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_LT(o.width.value(), 0);
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(0, o.precision.value());
|
||||
|
||||
EXPECT_TRUE(Run(".5d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_LT(o.width.value(), 0);
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(5, o.precision.value());
|
||||
|
||||
EXPECT_TRUE(Run(".0d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_LT(o.width.value(), 0);
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(0, o.precision.value());
|
||||
|
||||
EXPECT_TRUE(Run("14.5d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(14, o.width.value());
|
||||
EXPECT_EQ(5, o.precision.value());
|
||||
|
||||
EXPECT_TRUE(Run("*.*d"));
|
||||
EXPECT_TRUE(o.width.is_from_arg());
|
||||
EXPECT_EQ(1, o.width.get_from_arg());
|
||||
EXPECT_TRUE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(2, o.precision.get_from_arg());
|
||||
EXPECT_EQ(3, o.arg_position);
|
||||
|
||||
EXPECT_TRUE(Run("*d"));
|
||||
EXPECT_TRUE(o.width.is_from_arg());
|
||||
EXPECT_EQ(1, o.width.get_from_arg());
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_LT(o.precision.value(), 0);
|
||||
EXPECT_EQ(2, o.arg_position);
|
||||
|
||||
EXPECT_TRUE(Run(".*d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_LT(o.width.value(), 0);
|
||||
EXPECT_TRUE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(1, o.precision.get_from_arg());
|
||||
EXPECT_EQ(2, o.arg_position);
|
||||
|
||||
// mixed implicit and explicit: didn't specify arg position.
|
||||
EXPECT_FALSE(Run("*23$.*34$d"));
|
||||
|
||||
EXPECT_TRUE(Run("12$*23$.*34$d"));
|
||||
EXPECT_EQ(12, o.arg_position);
|
||||
EXPECT_TRUE(o.width.is_from_arg());
|
||||
EXPECT_EQ(23, o.width.get_from_arg());
|
||||
EXPECT_TRUE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(34, o.precision.get_from_arg());
|
||||
|
||||
EXPECT_TRUE(Run("2$*5$.*9$d"));
|
||||
EXPECT_EQ(2, o.arg_position);
|
||||
EXPECT_TRUE(o.width.is_from_arg());
|
||||
EXPECT_EQ(5, o.width.get_from_arg());
|
||||
EXPECT_TRUE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(9, o.precision.get_from_arg());
|
||||
|
||||
EXPECT_FALSE(Run(".*0$d")) << "no arg 0";
|
||||
|
||||
// Large values
|
||||
EXPECT_TRUE(Run("999999999.999999999d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_EQ(999999999, o.width.value());
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(999999999, o.precision.value());
|
||||
|
||||
EXPECT_FALSE(Run("1000000000.999999999d"));
|
||||
EXPECT_FALSE(Run("999999999.1000000000d"));
|
||||
EXPECT_FALSE(Run("9999999999d"));
|
||||
EXPECT_FALSE(Run(".9999999999d"));
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, Flags) {
|
||||
static const char kAllFlags[] = "-+ #0";
|
||||
static const int kNumFlags = ABSL_ARRAYSIZE(kAllFlags) - 1;
|
||||
for (int rev = 0; rev < 2; ++rev) {
|
||||
for (int i = 0; i < 1 << kNumFlags; ++i) {
|
||||
std::string fmt;
|
||||
for (int k = 0; k < kNumFlags; ++k)
|
||||
if ((i >> k) & 1) fmt += kAllFlags[k];
|
||||
// flag order shouldn't matter
|
||||
if (rev == 1) {
|
||||
std::reverse(fmt.begin(), fmt.end());
|
||||
}
|
||||
fmt += 'd';
|
||||
SCOPED_TRACE(fmt);
|
||||
EXPECT_TRUE(Run(fmt.c_str()));
|
||||
EXPECT_EQ(fmt.find('-') == std::string::npos,
|
||||
!FlagsContains(o.flags, Flags::kLeft));
|
||||
EXPECT_EQ(fmt.find('+') == std::string::npos,
|
||||
!FlagsContains(o.flags, Flags::kShowPos));
|
||||
EXPECT_EQ(fmt.find(' ') == std::string::npos,
|
||||
!FlagsContains(o.flags, Flags::kSignCol));
|
||||
EXPECT_EQ(fmt.find('#') == std::string::npos,
|
||||
!FlagsContains(o.flags, Flags::kAlt));
|
||||
EXPECT_EQ(fmt.find('0') == std::string::npos,
|
||||
!FlagsContains(o.flags, Flags::kZero));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, BasicFlag) {
|
||||
// Flag is on
|
||||
for (const char* fmt : {"d", "llx", "G", "1$X"}) {
|
||||
SCOPED_TRACE(fmt);
|
||||
EXPECT_TRUE(Run(fmt));
|
||||
EXPECT_EQ(o.flags, Flags::kBasic);
|
||||
}
|
||||
|
||||
// Flag is off
|
||||
for (const char* fmt : {"3d", ".llx", "-G", "1$#X"}) {
|
||||
SCOPED_TRACE(fmt);
|
||||
EXPECT_TRUE(Run(fmt));
|
||||
EXPECT_NE(o.flags, Flags::kBasic);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, LengthMod) {
|
||||
EXPECT_TRUE(Run("d"));
|
||||
EXPECT_EQ(LengthMod::none, o.length_mod);
|
||||
EXPECT_TRUE(Run("hd"));
|
||||
EXPECT_EQ(LengthMod::h, o.length_mod);
|
||||
EXPECT_TRUE(Run("hhd"));
|
||||
EXPECT_EQ(LengthMod::hh, o.length_mod);
|
||||
EXPECT_TRUE(Run("ld"));
|
||||
EXPECT_EQ(LengthMod::l, o.length_mod);
|
||||
EXPECT_TRUE(Run("lld"));
|
||||
EXPECT_EQ(LengthMod::ll, o.length_mod);
|
||||
EXPECT_TRUE(Run("Lf"));
|
||||
EXPECT_EQ(LengthMod::L, o.length_mod);
|
||||
EXPECT_TRUE(Run("qf"));
|
||||
EXPECT_EQ(LengthMod::q, o.length_mod);
|
||||
EXPECT_TRUE(Run("jd"));
|
||||
EXPECT_EQ(LengthMod::j, o.length_mod);
|
||||
EXPECT_TRUE(Run("zd"));
|
||||
EXPECT_EQ(LengthMod::z, o.length_mod);
|
||||
EXPECT_TRUE(Run("td"));
|
||||
EXPECT_EQ(LengthMod::t, o.length_mod);
|
||||
}
|
||||
|
||||
struct SummarizeConsumer {
|
||||
std::string* out;
|
||||
explicit SummarizeConsumer(std::string* out) : out(out) {}
|
||||
|
||||
bool Append(string_view s) {
|
||||
*out += "[" + std::string(s) + "]";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConvertOne(const UnboundConversion& conv, string_view s) {
|
||||
*out += "{";
|
||||
*out += std::string(s);
|
||||
*out += ":";
|
||||
*out += std::to_string(conv.arg_position) + "$";
|
||||
if (conv.width.is_from_arg()) {
|
||||
*out += std::to_string(conv.width.get_from_arg()) + "$*";
|
||||
}
|
||||
if (conv.precision.is_from_arg()) {
|
||||
*out += "." + std::to_string(conv.precision.get_from_arg()) + "$*";
|
||||
}
|
||||
*out += FormatConversionCharToChar(conv.conv);
|
||||
*out += "}";
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
std::string SummarizeParsedFormat(const ParsedFormatBase& pc) {
|
||||
std::string out;
|
||||
if (!pc.ProcessFormat(SummarizeConsumer(&out))) out += "!";
|
||||
return out;
|
||||
}
|
||||
|
||||
class ParsedFormatTest : public testing::Test {};
|
||||
|
||||
TEST_F(ParsedFormatTest, ValueSemantics) {
|
||||
ParsedFormatBase p1({}, true, {}); // empty format
|
||||
EXPECT_EQ("", SummarizeParsedFormat(p1));
|
||||
|
||||
ParsedFormatBase p2 = p1; // copy construct (empty)
|
||||
EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2));
|
||||
|
||||
p1 = ParsedFormatBase("hello%s", true,
|
||||
{FormatConversionCharSetInternal::s}); // move assign
|
||||
EXPECT_EQ("[hello]{s:1$s}", SummarizeParsedFormat(p1));
|
||||
|
||||
ParsedFormatBase p3 = p1; // copy construct (nonempty)
|
||||
EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p3));
|
||||
|
||||
using std::swap;
|
||||
swap(p1, p2);
|
||||
EXPECT_EQ("", SummarizeParsedFormat(p1));
|
||||
EXPECT_EQ("[hello]{s:1$s}", SummarizeParsedFormat(p2));
|
||||
swap(p1, p2); // undo
|
||||
|
||||
p2 = p1; // copy assign
|
||||
EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2));
|
||||
}
|
||||
|
||||
struct ExpectParse {
|
||||
const char* in;
|
||||
std::initializer_list<FormatConversionCharSet> conv_set;
|
||||
const char* out;
|
||||
};
|
||||
|
||||
TEST_F(ParsedFormatTest, Parsing) {
|
||||
// Parse should be equivalent to that obtained by ConversionParseIterator.
|
||||
// No need to retest the parsing edge cases here.
|
||||
const ExpectParse kExpect[] = {
|
||||
{"", {}, ""},
|
||||
{"ab", {}, "[ab]"},
|
||||
{"a%d", {FormatConversionCharSetInternal::d}, "[a]{d:1$d}"},
|
||||
{"a%+d", {FormatConversionCharSetInternal::d}, "[a]{+d:1$d}"},
|
||||
{"a% d", {FormatConversionCharSetInternal::d}, "[a]{ d:1$d}"},
|
||||
{"a%b %d", {}, "[a]!"}, // stop after error
|
||||
};
|
||||
for (const auto& e : kExpect) {
|
||||
SCOPED_TRACE(e.in);
|
||||
EXPECT_EQ(e.out,
|
||||
SummarizeParsedFormat(ParsedFormatBase(e.in, false, e.conv_set)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ParsedFormatTest, ParsingFlagOrder) {
|
||||
const ExpectParse kExpect[] = {
|
||||
{"a%+ 0d", {FormatConversionCharSetInternal::d}, "[a]{+ 0d:1$d}"},
|
||||
{"a%+0 d", {FormatConversionCharSetInternal::d}, "[a]{+0 d:1$d}"},
|
||||
{"a%0+ d", {FormatConversionCharSetInternal::d}, "[a]{0+ d:1$d}"},
|
||||
{"a% +0d", {FormatConversionCharSetInternal::d}, "[a]{ +0d:1$d}"},
|
||||
{"a%0 +d", {FormatConversionCharSetInternal::d}, "[a]{0 +d:1$d}"},
|
||||
{"a% 0+d", {FormatConversionCharSetInternal::d}, "[a]{ 0+d:1$d}"},
|
||||
{"a%+ 0+d", {FormatConversionCharSetInternal::d}, "[a]{+ 0+d:1$d}"},
|
||||
};
|
||||
for (const auto& e : kExpect) {
|
||||
SCOPED_TRACE(e.in);
|
||||
EXPECT_EQ(e.out,
|
||||
SummarizeParsedFormat(ParsedFormatBase(e.in, false, e.conv_set)));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace str_format_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
Reference in New Issue
Block a user