Clingo
Loading...
Searching...
No Matches
fstring.hh
1#pragma once
2
3#include <clingo/core/symbol.hh>
4
5#include <cstdint>
6#include <vector>
7
8namespace CppClingo {
9
12
14struct FormatSpec {
19 enum class Conversion : uint8_t {
20 str = 0,
21 repr = 1,
22 };
23
25 enum class Align : uint8_t {
26 none,
27 left,
28 right,
29 number,
30 center,
31 };
32
34 enum class Sign : uint8_t {
35 plus,
36 minus,
37 space,
38 };
39
41 enum class Grouping : uint8_t {
42 none,
43 comma,
45 };
46
48 enum class Type : uint8_t {
49 character,
50 binary,
51 octal,
52 decimal,
53 hex_lower,
54 hex_upper,
55 locale,
56 string,
57 };
58
60 FormatSpec() = default;
76 [[nodiscard]] static auto build(SymbolStore &store, std::string_view str) -> std::optional<FormatSpec>;
77
79 friend auto operator==(FormatSpec const &a, FormatSpec const &b) -> bool = default;
81 friend auto operator<=>(FormatSpec const &a, FormatSpec const &b) -> std::strong_ordering = default;
83 friend auto operator<<(std::ostream &out, FormatSpec const &spec) -> std::ostream &;
85 friend auto operator<<(Util::OutputBuffer &out, FormatSpec const &spec) -> Util::OutputBuffer &;
87 [[nodiscard]] auto hash() const -> size_t {
88 return Util::value_hash_record<FormatSpec>(accessors, width, fill, type, grouping, conversion, align, sign,
90 }
91
96 std::vector<std::variant<SharedString, size_t>> accessors;
98 uint32_t width = 0;
100 std::optional<char> fill;
104 Grouping grouping = Grouping::none;
112 bool alternate_form = false;
113};
114
116
117} // namespace CppClingo
A store for symbols.
Definition symbol.hh:454
Create an output buffer that bears some similarities with C++'s iostreams.
Definition print.hh:24
Format specification for a field.
Definition fstring.hh:14
friend auto operator<<(Util::OutputBuffer &out, FormatSpec const &spec) -> Util::OutputBuffer &
Output the format specification to a stream.
uint32_t width
The width of the field.
Definition fstring.hh:98
Grouping grouping
The grouping option.
Definition fstring.hh:104
friend auto operator<<(std::ostream &out, FormatSpec const &spec) -> std::ostream &
Output the format specification to a stream.
friend auto operator==(FormatSpec const &a, FormatSpec const &b) -> bool=default
Compare two format specifications for equality.
Type type
The type of the field.
Definition fstring.hh:102
Grouping
Enumeration of grouping options.
Definition fstring.hh:41
@ comma
Do not use thousands separator (default).
@ underscore
Use comma as thousands separator.
std::vector< std::variant< SharedString, size_t > > accessors
The vector of accessors.
Definition fstring.hh:96
Conversion
Enumeration of conversion options.
Definition fstring.hh:19
@ str
Use the string representation of a symbol.
@ repr
Use the clingo representation of a symbol.
auto hash() const -> size_t
Compute the hash of the format specification.
Definition fstring.hh:87
friend auto operator<=>(FormatSpec const &a, FormatSpec const &b) -> std::strong_ordering=default
Compare two format specifications.
Sign
The sign options.
Definition fstring.hh:34
@ plus
Use an explicit plus sign for positive numbers.
@ minus
Use a minus sign for negative numbers only (default).
@ space
Use a space for positive numbers.
Align
The alignment options.
Definition fstring.hh:25
@ none
No alignment specified (see defaults below).
@ right
Use right alignment (default for numbers).
@ left
Use left alignment (default for non-numbers).
@ center
Use center alignment.
@ number
Use number alignment (sign to the left/number to the right).
Type
Enumeration of type options.
Definition fstring.hh:48
@ octal
Output an integer in octal format.
@ binary
OUtput an integer in binary format.
@ character
Output the character with the given integer code.
@ string
Output symbol using their default string representation (default).
@ decimal
Output an integer in decimal format.
@ hex_lower
Output an integer in hexadecimal format with lower-case letters.
@ hex_upper
Output an integer in hexadecimal format with upper-case letters.
@ locale
Output an integer using the locale's conventions.
static auto build(SymbolStore &store, std::string_view str) -> std::optional< FormatSpec >
Parse a format specification from a string.
bool alternate_form
Whether to use the alternate form.
Definition fstring.hh:112
Sign sign
The sign option.
Definition fstring.hh:110
FormatSpec()=default
Construct a default format specification.
Align align
The alignment of the field.
Definition fstring.hh:108
std::optional< char > fill
The fill character for padding if the field is wider than the content.
Definition fstring.hh:100
Conversion conversion
The conversion option.
Definition fstring.hh:106