2018-06-21 21:55:12 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2019-03-08 16:27:53 +01:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2018-06-21 21:55:12 +02:00
|
|
|
//
|
|
|
|
// 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>
|
2020-01-28 20:50:11 +01:00
|
|
|
|
2018-09-12 20:03:25 +02:00
|
|
|
#include <cstddef>
|
2018-06-21 21:55:12 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <ostream>
|
|
|
|
|
2020-01-28 20:50:11 +01:00
|
|
|
#include "absl/base/config.h"
|
2018-06-21 21:55:12 +02:00
|
|
|
#include "absl/base/port.h"
|
2020-03-16 17:06:23 +01:00
|
|
|
#include "absl/meta/type_traits.h"
|
2018-06-21 21:55:12 +02:00
|
|
|
#include "absl/strings/internal/str_format/output.h"
|
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
|
|
|
|
namespace absl {
|
2019-12-12 19:36:03 +01:00
|
|
|
ABSL_NAMESPACE_BEGIN
|
2020-05-01 02:44:10 +02:00
|
|
|
|
2018-06-21 21:55:12 +02:00
|
|
|
namespace str_format_internal {
|
2020-05-01 02:44:10 +02:00
|
|
|
enum class FormatConversionCharSet : uint64_t;
|
|
|
|
enum class FormatConversionChar : uint8_t;
|
2018-06-21 21:55:12 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2018-08-21 20:31:02 +02:00
|
|
|
// An abstraction to which conversions write their string data.
|
2018-06-21 21:55:12 +02:00
|
|
|
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 w, int p, bool l);
|
|
|
|
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Flags {
|
|
|
|
bool basic : 1; // fastest conversion: no flags, width, or precision
|
|
|
|
bool left : 1; // "-"
|
|
|
|
bool show_pos : 1; // "+"
|
|
|
|
bool sign_col : 1; // " "
|
|
|
|
bool alt : 1; // "#"
|
|
|
|
bool zero : 1; // "0"
|
|
|
|
std::string ToString() const;
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const Flags& v) {
|
|
|
|
return os << v.ToString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format off
|
2020-02-25 17:48:42 +01:00
|
|
|
#define ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP) \
|
2018-06-21 21:55:12 +02:00
|
|
|
/* text */ \
|
2020-05-06 23:06:16 +02:00
|
|
|
X_VAL(c) X_SEP X_VAL(s) X_SEP \
|
2018-06-21 21:55:12 +02:00
|
|
|
/* 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)
|
2020-05-01 02:44:10 +02:00
|
|
|
// clang-format on
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
// 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 {
|
2020-05-06 23:06:16 +02:00
|
|
|
c, s, // text
|
2018-06-21 21:55:12 +02:00
|
|
|
d, i, o, u, x, X, // int
|
|
|
|
f, F, e, E, g, G, a, A, // float
|
|
|
|
n, p, // misc
|
2020-03-31 21:32:35 +02:00
|
|
|
kNone
|
2020-05-01 02:44:10 +02:00
|
|
|
};
|
|
|
|
// 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);
|
2020-02-14 18:41:25 +01:00
|
|
|
};
|
|
|
|
// clang-format on
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-02-14 18:41:25 +01:00
|
|
|
inline FormatConversionChar FormatConversionCharFromChar(char c) {
|
|
|
|
switch (c) {
|
2020-02-25 17:48:42 +01:00
|
|
|
#define ABSL_INTERNAL_X_VAL(id) \
|
|
|
|
case #id[0]: \
|
2020-05-01 02:44:10 +02:00
|
|
|
return FormatConversionCharInternal::id;
|
2020-02-25 17:48:42 +01:00
|
|
|
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL, )
|
|
|
|
#undef ABSL_INTERNAL_X_VAL
|
2018-06-21 21:55:12 +02:00
|
|
|
}
|
2020-05-01 02:44:10 +02:00
|
|
|
return FormatConversionCharInternal::kNone;
|
2020-02-14 18:41:25 +01:00
|
|
|
}
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-02-14 18:41:25 +01:00
|
|
|
inline bool FormatConversionCharIsUpper(FormatConversionChar c) {
|
2020-05-01 02:44:10 +02:00
|
|
|
if (c == FormatConversionCharInternal::X ||
|
|
|
|
c == FormatConversionCharInternal::F ||
|
|
|
|
c == FormatConversionCharInternal::E ||
|
|
|
|
c == FormatConversionCharInternal::G ||
|
|
|
|
c == FormatConversionCharInternal::A) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2018-06-21 21:55:12 +02:00
|
|
|
}
|
2020-02-14 18:41:25 +01:00
|
|
|
}
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-02-14 18:41:25 +01:00
|
|
|
inline bool FormatConversionCharIsFloat(FormatConversionChar c) {
|
2020-05-01 02:44:10 +02:00
|
|
|
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;
|
2018-06-21 21:55:12 +02:00
|
|
|
}
|
2020-02-14 18:41:25 +01:00
|
|
|
}
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-02-14 18:41:25 +01:00
|
|
|
inline char FormatConversionCharToChar(FormatConversionChar c) {
|
2020-05-01 02:44:10 +02:00
|
|
|
if (c == FormatConversionCharInternal::kNone) {
|
|
|
|
return '\0';
|
|
|
|
|
|
|
|
#define ABSL_INTERNAL_X_VAL(e) \
|
|
|
|
} else if (c == FormatConversionCharInternal::e) { \
|
2020-02-14 18:41:25 +01:00
|
|
|
return #e[0];
|
2020-02-25 17:48:42 +01:00
|
|
|
#define ABSL_INTERNAL_X_SEP
|
2020-05-01 02:44:10 +02:00
|
|
|
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_X_VAL,
|
|
|
|
ABSL_INTERNAL_X_SEP)
|
|
|
|
} else {
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:48:42 +01:00
|
|
|
#undef ABSL_INTERNAL_X_VAL
|
|
|
|
#undef ABSL_INTERNAL_X_SEP
|
2020-02-14 18:41:25 +01:00
|
|
|
}
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-02-14 18:41:25 +01:00
|
|
|
// The associated char.
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, FormatConversionChar v) {
|
|
|
|
char c = FormatConversionCharToChar(v);
|
|
|
|
if (!c) c = '?';
|
|
|
|
return os << c;
|
|
|
|
}
|
2018-06-21 21:55:12 +02:00
|
|
|
|
2020-02-20 21:34:37 +01:00
|
|
|
struct FormatConversionSpecImplFriend;
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
class FormatConversionSpecImpl {
|
2018-06-21 21:55:12 +02:00
|
|
|
public:
|
2020-02-20 21:34:37 +01:00
|
|
|
// Width and precison are not specified, no flags are set.
|
|
|
|
bool is_basic() const { return flags_.basic; }
|
|
|
|
bool has_left_flag() const { return flags_.left; }
|
|
|
|
bool has_show_pos_flag() const { return flags_.show_pos; }
|
|
|
|
bool has_sign_col_flag() const { return flags_.sign_col; }
|
|
|
|
bool has_alt_flag() const { return flags_.alt; }
|
|
|
|
bool has_zero_flag() const { return flags_.zero; }
|
|
|
|
|
2020-02-25 17:48:42 +01:00
|
|
|
FormatConversionChar conversion_char() const {
|
2018-09-12 20:03:25 +02:00
|
|
|
// Keep this field first in the struct . It generates better code when
|
|
|
|
// accessing it when ConversionSpec is passed by value in registers.
|
2020-05-01 02:44:10 +02:00
|
|
|
static_assert(offsetof(FormatConversionSpecImpl, conv_) == 0, "");
|
2018-09-12 20:03:25 +02:00
|
|
|
return conv_;
|
|
|
|
}
|
2018-06-21 21:55:12 +02:00
|
|
|
|
|
|
|
// 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_; }
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
template <typename T>
|
|
|
|
T Wrap() {
|
|
|
|
return T(*this);
|
|
|
|
}
|
|
|
|
|
2018-06-21 21:55:12 +02:00
|
|
|
private:
|
2020-02-20 21:34:37 +01:00
|
|
|
friend struct str_format_internal::FormatConversionSpecImplFriend;
|
2020-05-01 02:44:10 +02:00
|
|
|
FormatConversionChar conv_ = FormatConversionCharInternal::kNone;
|
2018-06-21 21:55:12 +02:00
|
|
|
Flags flags_;
|
|
|
|
int width_;
|
|
|
|
int precision_;
|
|
|
|
};
|
|
|
|
|
2020-02-20 21:34:37 +01:00
|
|
|
struct FormatConversionSpecImplFriend final {
|
2020-05-01 02:44:10 +02:00
|
|
|
static void SetFlags(Flags f, FormatConversionSpecImpl* conv) {
|
2020-02-25 17:48:42 +01:00
|
|
|
conv->flags_ = f;
|
|
|
|
}
|
|
|
|
static void SetConversionChar(FormatConversionChar c,
|
2020-05-01 02:44:10 +02:00
|
|
|
FormatConversionSpecImpl* conv) {
|
2020-02-20 21:34:37 +01:00
|
|
|
conv->conv_ = c;
|
|
|
|
}
|
2020-05-01 02:44:10 +02:00
|
|
|
static void SetWidth(int w, FormatConversionSpecImpl* conv) {
|
|
|
|
conv->width_ = w;
|
|
|
|
}
|
|
|
|
static void SetPrecision(int p, FormatConversionSpecImpl* conv) {
|
2020-02-20 21:34:37 +01:00
|
|
|
conv->precision_ = p;
|
|
|
|
}
|
2020-05-01 02:44:10 +02:00
|
|
|
static std::string FlagsToString(const FormatConversionSpecImpl& spec) {
|
2020-02-20 21:34:37 +01:00
|
|
|
return spec.flags_.ToString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
// 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...)));
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:36:00 +02:00
|
|
|
constexpr uint64_t FormatConversionCharToConvInt(FormatConversionChar c) {
|
|
|
|
return uint64_t{1} << (1 + static_cast<uint8_t>(c));
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
constexpr uint64_t FormatConversionCharToConvInt(char conv) {
|
2018-06-21 21:55:12 +02:00
|
|
|
return
|
2020-05-08 19:36:00 +02:00
|
|
|
#define ABSL_INTERNAL_CHAR_SET_CASE(c) \
|
|
|
|
conv == #c[0] \
|
|
|
|
? FormatConversionCharToConvInt(FormatConversionCharInternal::c) \
|
|
|
|
:
|
2020-02-25 17:48:42 +01:00
|
|
|
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
|
|
|
|
#undef ABSL_INTERNAL_CHAR_SET_CASE
|
2018-06-21 21:55:12 +02:00
|
|
|
conv == '*'
|
|
|
|
? 1
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
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]);
|
2020-02-25 17:48:42 +01:00
|
|
|
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(ABSL_INTERNAL_CHAR_SET_CASE, )
|
|
|
|
#undef ABSL_INTERNAL_CHAR_SET_CASE
|
2018-06-21 21:55:12 +02:00
|
|
|
|
|
|
|
// Used for width/precision '*' specification.
|
2020-05-01 02:44:10 +02:00
|
|
|
static constexpr FormatConversionCharSet kStar =
|
|
|
|
FormatConversionCharToConvValue('*');
|
|
|
|
|
|
|
|
// Some predefined values (TODO(matthewbr), delete any that are unused).
|
|
|
|
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 kString = s;
|
|
|
|
static constexpr FormatConversionCharSet kPointer = p;
|
2018-06-21 21:55:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
2020-02-25 17:48:42 +01:00
|
|
|
constexpr FormatConversionCharSet operator|(FormatConversionCharSet a,
|
|
|
|
FormatConversionCharSet b) {
|
2020-05-01 02:44:10 +02:00
|
|
|
return FormatConversionCharSetUnion(a, b);
|
2018-06-21 21:55:12 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 17:06:23 +01:00
|
|
|
// Overloaded conversion functions to support absl::ParsedFormat.
|
2018-06-21 21:55:12 +02:00
|
|
|
// Get a conversion with a single character in it.
|
2020-03-16 17:06:23 +01:00
|
|
|
constexpr FormatConversionCharSet ToFormatConversionCharSet(char c) {
|
|
|
|
return static_cast<FormatConversionCharSet>(
|
|
|
|
FormatConversionCharToConvValue(c));
|
2018-06-21 21:55:12 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 17:06:23 +01:00
|
|
|
// Get a conversion with a single character in it.
|
|
|
|
constexpr FormatConversionCharSet ToFormatConversionCharSet(
|
|
|
|
FormatConversionCharSet c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ToFormatConversionCharSet(T) = delete;
|
|
|
|
|
2018-06-21 21:55:12 +02:00
|
|
|
// Checks whether `c` exists in `set`.
|
2020-02-25 17:48:42 +01:00
|
|
|
constexpr bool Contains(FormatConversionCharSet set, char c) {
|
2020-05-01 02:44:10 +02:00
|
|
|
return (static_cast<uint64_t>(set) &
|
|
|
|
static_cast<uint64_t>(FormatConversionCharToConvValue(c))) != 0;
|
2018-06-21 21:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether all the characters in `c` are contained in `set`
|
2020-02-25 17:48:42 +01:00
|
|
|
constexpr bool Contains(FormatConversionCharSet set,
|
|
|
|
FormatConversionCharSet c) {
|
2018-06-21 21:55:12 +02:00
|
|
|
return (static_cast<uint64_t>(set) & static_cast<uint64_t>(c)) ==
|
|
|
|
static_cast<uint64_t>(c);
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:36:00 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-06-21 21:55:12 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:44:10 +02:00
|
|
|
class FormatConversionSpec {
|
|
|
|
public:
|
|
|
|
// Width and precison are not specified, no flags are set.
|
|
|
|
bool is_basic() const { return impl_.is_basic(); }
|
|
|
|
bool has_left_flag() const { return impl_.has_left_flag(); }
|
|
|
|
bool has_show_pos_flag() const { return impl_.has_show_pos_flag(); }
|
|
|
|
bool has_sign_col_flag() const { return impl_.has_sign_col_flag(); }
|
|
|
|
bool has_alt_flag() const { return impl_.has_alt_flag(); }
|
|
|
|
bool has_zero_flag() const { return impl_.has_zero_flag(); }
|
|
|
|
|
|
|
|
FormatConversionChar conversion_char() const {
|
|
|
|
return impl_.conversion_char();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the specified width. If width is unspecfied, it returns a negative
|
|
|
|
// value.
|
|
|
|
int width() const { return impl_.width(); }
|
|
|
|
// Returns the specified precision. If precision is unspecfied, it returns a
|
|
|
|
// negative value.
|
|
|
|
int precision() const { return impl_.precision(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit FormatConversionSpec(
|
|
|
|
str_format_internal::FormatConversionSpecImpl impl)
|
|
|
|
: impl_(impl) {}
|
|
|
|
|
|
|
|
friend str_format_internal::FormatConversionSpecImpl;
|
|
|
|
|
|
|
|
absl::str_format_internal::FormatConversionSpecImpl impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
enum class FormatConversionChar : uint8_t {
|
2020-05-06 23:06:16 +02:00
|
|
|
c, s, // text
|
2020-05-01 02:44:10 +02:00
|
|
|
d, i, o, u, x, X, // int
|
|
|
|
f, F, e, E, g, G, a, A, // float
|
|
|
|
n, p // misc
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
enum class FormatConversionCharSet : uint64_t {
|
|
|
|
// text
|
|
|
|
c = str_format_internal::FormatConversionCharToConvInt('c'),
|
|
|
|
s = str_format_internal::FormatConversionCharToConvInt('s'),
|
|
|
|
// integer
|
|
|
|
d = str_format_internal::FormatConversionCharToConvInt('d'),
|
|
|
|
i = str_format_internal::FormatConversionCharToConvInt('i'),
|
|
|
|
o = str_format_internal::FormatConversionCharToConvInt('o'),
|
|
|
|
u = str_format_internal::FormatConversionCharToConvInt('u'),
|
|
|
|
x = str_format_internal::FormatConversionCharToConvInt('x'),
|
|
|
|
X = str_format_internal::FormatConversionCharToConvInt('X'),
|
|
|
|
// Float
|
|
|
|
f = str_format_internal::FormatConversionCharToConvInt('f'),
|
|
|
|
F = str_format_internal::FormatConversionCharToConvInt('F'),
|
|
|
|
e = str_format_internal::FormatConversionCharToConvInt('e'),
|
|
|
|
E = str_format_internal::FormatConversionCharToConvInt('E'),
|
|
|
|
g = str_format_internal::FormatConversionCharToConvInt('g'),
|
|
|
|
G = str_format_internal::FormatConversionCharToConvInt('G'),
|
|
|
|
a = str_format_internal::FormatConversionCharToConvInt('a'),
|
|
|
|
A = str_format_internal::FormatConversionCharToConvInt('A'),
|
|
|
|
// misc
|
|
|
|
n = str_format_internal::FormatConversionCharToConvInt('n'),
|
|
|
|
p = str_format_internal::FormatConversionCharToConvInt('p'),
|
|
|
|
|
|
|
|
// Used for width/precision '*' specification.
|
|
|
|
kStar = str_format_internal::FormatConversionCharToConvInt('*'),
|
|
|
|
|
|
|
|
// Some predefined values:
|
|
|
|
kIntegral = d | i | u | o | x | X,
|
|
|
|
kFloating = a | e | f | g | A | E | F | G,
|
|
|
|
kNumeric = kIntegral | kFloating,
|
|
|
|
kString = s,
|
|
|
|
kPointer = p,
|
|
|
|
};
|
2020-02-14 18:41:25 +01:00
|
|
|
|
2018-06-21 21:55:12 +02:00
|
|
|
} // namespace str_format_internal
|
|
|
|
|
2019-12-12 19:36:03 +01:00
|
|
|
ABSL_NAMESPACE_END
|
2018-06-21 21:55:12 +02:00
|
|
|
} // namespace absl
|
|
|
|
|
2018-11-06 22:01:25 +01:00
|
|
|
#endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_
|