Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
2d71d7e0
Commit
2d71d7e0
authored
Jun 12, 2020
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a simple format string compilation API
parent
d259fcfb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
37 deletions
+45
-37
include/fmt/compile.h
include/fmt/compile.h
+28
-29
include/fmt/core.h
include/fmt/core.h
+3
-3
include/fmt/format.h
include/fmt/format.h
+14
-5
No files found.
include/fmt/compile.h
View file @
2d71d7e0
...
@@ -15,6 +15,17 @@
...
@@ -15,6 +15,17 @@
FMT_BEGIN_NAMESPACE
FMT_BEGIN_NAMESPACE
namespace
detail
{
namespace
detail
{
// A compile-time string which is compiled into fast formatting code.
class
compiled_string
{};
template
<
typename
S
>
struct
is_compiled_string
:
std
::
is_base_of
<
compiled_string
,
S
>
{};
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
template
<
typename
T
,
typename
...
Tail
>
const
T
&
first
(
const
T
&
value
,
const
Tail
&
...)
{
return
value
;
}
// Part of a compiled format string. It can be either literal text or a
// Part of a compiled format string. It can be either literal text or a
// replacement field.
// replacement field.
template
<
typename
Char
>
struct
format_part
{
template
<
typename
Char
>
struct
format_part
{
...
@@ -381,31 +392,6 @@ constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
...
@@ -381,31 +392,6 @@ constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
return
{{
&
s
[
pos
],
size
}};
return
{{
&
s
[
pos
],
size
}};
}
}
template
<
typename
Char
,
typename
OutputIt
,
typename
T
,
std
::
enable_if_t
<
std
::
is_integral_v
<
T
>,
int
>
=
0
>
OutputIt
format_default
(
OutputIt
out
,
T
value
)
{
// TODO: reserve
format_int
fi
(
value
);
return
std
::
copy
(
fi
.
data
(),
fi
.
data
()
+
fi
.
size
(),
out
);
}
template
<
typename
Char
,
typename
OutputIt
>
OutputIt
format_default
(
OutputIt
out
,
double
value
)
{
return
detail
::
write
(
out
,
value
);
}
template
<
typename
Char
,
typename
OutputIt
>
OutputIt
format_default
(
OutputIt
out
,
Char
value
)
{
*
out
++
=
value
;
return
out
;
}
template
<
typename
Char
,
typename
OutputIt
>
OutputIt
format_default
(
OutputIt
out
,
const
Char
*
value
)
{
auto
length
=
std
::
char_traits
<
Char
>::
length
(
value
);
return
copy_str
<
Char
>
(
value
,
value
+
length
,
out
);
}
// A replacement field that refers to argument N.
// A replacement field that refers to argument N.
template
<
typename
Char
,
typename
T
,
int
N
>
struct
field
{
template
<
typename
Char
,
typename
T
,
int
N
>
struct
field
{
using
char_type
=
Char
;
using
char_type
=
Char
;
...
@@ -414,7 +400,7 @@ template <typename Char, typename T, int N> struct field {
...
@@ -414,7 +400,7 @@ template <typename Char, typename T, int N> struct field {
OutputIt
format
(
OutputIt
out
,
const
Args
&
...
args
)
const
{
OutputIt
format
(
OutputIt
out
,
const
Args
&
...
args
)
const
{
// This ensures that the argument type is convertile to `const T&`.
// This ensures that the argument type is convertile to `const T&`.
const
T
&
arg
=
get
<
N
>
(
args
...);
const
T
&
arg
=
get
<
N
>
(
args
...);
return
format_default
<
Char
>
(
out
,
arg
);
return
write
<
Char
>
(
out
,
arg
);
}
}
};
};
...
@@ -456,7 +442,8 @@ constexpr auto compile_format_string(S format_str);
...
@@ -456,7 +442,8 @@ constexpr auto compile_format_string(S format_str);
template
<
typename
Args
,
size_t
POS
,
int
ID
,
typename
T
,
typename
S
>
template
<
typename
Args
,
size_t
POS
,
int
ID
,
typename
T
,
typename
S
>
constexpr
auto
parse_tail
(
T
head
,
S
format_str
)
{
constexpr
auto
parse_tail
(
T
head
,
S
format_str
)
{
if
constexpr
(
POS
!=
to_string_view
(
format_str
).
size
())
{
if
constexpr
(
POS
!=
basic_string_view
<
typename
S
::
char_type
>
(
format_str
).
size
())
{
constexpr
auto
tail
=
compile_format_string
<
Args
,
POS
,
ID
>
(
format_str
);
constexpr
auto
tail
=
compile_format_string
<
Args
,
POS
,
ID
>
(
format_str
);
if
constexpr
(
std
::
is_same
<
remove_cvref_t
<
decltype
(
tail
)
>
,
if
constexpr
(
std
::
is_same
<
remove_cvref_t
<
decltype
(
tail
)
>
,
unknown_format
>
())
unknown_format
>
())
...
@@ -506,7 +493,8 @@ constexpr auto compile_format_string(S format_str) {
...
@@ -506,7 +493,8 @@ constexpr auto compile_format_string(S format_str) {
#if FMT_USE_CONSTEXPR
#if FMT_USE_CONSTEXPR
# ifdef __cpp_if_constexpr
# ifdef __cpp_if_constexpr
template
<
typename
...
Args
,
typename
S
,
template
<
typename
...
Args
,
typename
S
,
FMT_ENABLE_IF
(
is_compile_string
<
S
>
::
value
)
>
FMT_ENABLE_IF
(
is_compile_string
<
S
>
::
value
||
detail
::
is_compiled_string
<
S
>::
value
)
>
constexpr
auto
compile
(
S
format_str
)
{
constexpr
auto
compile
(
S
format_str
)
{
constexpr
basic_string_view
<
typename
S
::
char_type
>
str
=
format_str
;
constexpr
basic_string_view
<
typename
S
::
char_type
>
str
=
format_str
;
if
constexpr
(
str
.
size
()
==
0
)
{
if
constexpr
(
str
.
size
()
==
0
)
{
...
@@ -529,7 +517,8 @@ template <typename CompiledFormat, typename... Args,
...
@@ -529,7 +517,8 @@ template <typename CompiledFormat, typename... Args,
FMT_ENABLE_IF
(
detail
::
is_compiled_format
<
CompiledFormat
>
::
value
)
>
FMT_ENABLE_IF
(
detail
::
is_compiled_format
<
CompiledFormat
>
::
value
)
>
std
::
basic_string
<
Char
>
format
(
const
CompiledFormat
&
cf
,
const
Args
&
...
args
)
{
std
::
basic_string
<
Char
>
format
(
const
CompiledFormat
&
cf
,
const
Args
&
...
args
)
{
basic_memory_buffer
<
Char
>
buffer
;
basic_memory_buffer
<
Char
>
buffer
;
cf
.
format
(
std
::
back_inserter
(
buffer
),
args
...);
detail
::
buffer
<
Char
>&
base
=
buffer
;
cf
.
format
(
std
::
back_inserter
(
base
),
args
...);
return
to_string
(
buffer
);
return
to_string
(
buffer
);
}
}
...
@@ -569,6 +558,16 @@ std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
...
@@ -569,6 +558,16 @@ std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
return
to_string
(
buffer
);
return
to_string
(
buffer
);
}
}
template
<
typename
S
,
typename
...
Args
,
FMT_ENABLE_IF
(
detail
::
is_compiled_string
<
S
>
::
value
)
>
FMT_INLINE
std
::
basic_string
<
typename
S
::
char_type
>
format
(
S
,
Args
&&
...
args
)
{
constexpr
basic_string_view
<
typename
S
::
char_type
>
str
=
S
();
if
(
str
.
size
()
==
2
&&
str
[
0
]
==
'{'
&&
str
[
1
]
==
'}'
)
return
fmt
::
to_string
(
detail
::
first
(
args
...));
constexpr
auto
compiled
=
compile
<
Args
...
>
(
S
());
return
format
(
compiled
,
std
::
forward
<
Args
...
>
(
args
...));
}
template
<
typename
OutputIt
,
typename
CompiledFormat
,
typename
...
Args
,
template
<
typename
OutputIt
,
typename
CompiledFormat
,
typename
...
Args
,
FMT_ENABLE_IF
(
std
::
is_base_of
<
detail
::
basic_compiled_format
,
FMT_ENABLE_IF
(
std
::
is_base_of
<
detail
::
basic_compiled_format
,
CompiledFormat
>
::
value
)
>
CompiledFormat
>
::
value
)
>
...
...
include/fmt/core.h
View file @
2d71d7e0
...
@@ -481,7 +481,7 @@ inline basic_string_view<Char> to_string_view(detail::std_string_view<Char> s) {
...
@@ -481,7 +481,7 @@ inline basic_string_view<Char> to_string_view(detail::std_string_view<Char> s) {
}
}
// A base class for compile-time strings. It is defined in the fmt namespace to
// A base class for compile-time strings. It is defined in the fmt namespace to
// make formatting functions visible via ADL, e.g. format(
fmt
("{}"), 42).
// make formatting functions visible via ADL, e.g. format(
FMT_STRING
("{}"), 42).
struct
compile_string
{};
struct
compile_string
{};
template
<
typename
S
>
template
<
typename
S
>
...
@@ -1745,8 +1745,8 @@ template <typename..., typename S, FMT_ENABLE_IF(!is_compile_string<S>::value)>
...
@@ -1745,8 +1745,8 @@ template <typename..., typename S, FMT_ENABLE_IF(!is_compile_string<S>::value)>
FMT_INLINE
void
check_format_string
(
const
S
&
)
{
FMT_INLINE
void
check_format_string
(
const
S
&
)
{
#ifdef FMT_ENFORCE_COMPILE_STRING
#ifdef FMT_ENFORCE_COMPILE_STRING
static_assert
(
is_compile_string
<
S
>::
value
,
static_assert
(
is_compile_string
<
S
>::
value
,
"FMT_ENFORCE_COMPILE_STRING requires all format strings to "
"FMT_ENFORCE_COMPILE_STRING requires all format strings to
use
"
"
utilize FMT_STRING() or fmt()
."
);
"
FMT_STRING
."
);
#endif
#endif
}
}
template
<
typename
...
,
typename
S
,
FMT_ENABLE_IF
(
is_compile_string
<
S
>
::
value
)
>
template
<
typename
...
,
typename
S
,
FMT_ENABLE_IF
(
is_compile_string
<
S
>
::
value
)
>
...
...
include/fmt/format.h
View file @
2d71d7e0
...
@@ -700,6 +700,10 @@ void basic_memory_buffer<T, SIZE, Allocator>::grow(size_t size) {
...
@@ -700,6 +700,10 @@ void basic_memory_buffer<T, SIZE, Allocator>::grow(size_t size) {
using
memory_buffer
=
basic_memory_buffer
<
char
>
;
using
memory_buffer
=
basic_memory_buffer
<
char
>
;
using
wmemory_buffer
=
basic_memory_buffer
<
wchar_t
>
;
using
wmemory_buffer
=
basic_memory_buffer
<
wchar_t
>
;
template
<
typename
T
,
size_t
SIZE
,
typename
Allocator
>
struct
is_contiguous
<
basic_memory_buffer
<
T
,
SIZE
,
Allocator
>>
:
std
::
true_type
{
};
/** A formatting error such as invalid format string. */
/** A formatting error such as invalid format string. */
FMT_CLASS_API
FMT_CLASS_API
class
FMT_API
format_error
:
public
std
::
runtime_error
{
class
FMT_API
format_error
:
public
std
::
runtime_error
{
...
@@ -2746,12 +2750,12 @@ FMT_CONSTEXPR basic_string_view<Char> compile_string_to_view(
...
@@ -2746,12 +2750,12 @@ FMT_CONSTEXPR basic_string_view<Char> compile_string_to_view(
return
{
s
.
data
(),
s
.
size
()};
return
{
s
.
data
(),
s
.
size
()};
}
}
#define FMT_STRING_IMPL(s,
...)
\
#define FMT_STRING_IMPL(s,
base)
\
[] { \
[] { \
/* Use a macro-like name to avoid shadowing warnings. */
\
/* Use a macro-like name to avoid shadowing warnings. */
\
struct FMT_COMPILE_STRING :
fmt::compile_string {
\
struct FMT_COMPILE_STRING :
base {
\
using char_type = fmt::remove_cvref_t<decltype(s[0])>; \
using char_type = fmt::remove_cvref_t<decltype(s[0])>; \
FMT_MAYBE_UNUSED
__VA_ARGS__ FMT_CONSTEXPR
\
FMT_MAYBE_UNUSED
FMT_CONSTEXPR
\
operator fmt::basic_string_view<char_type>() const { \
operator fmt::basic_string_view<char_type>() const { \
return fmt::detail::compile_string_to_view<char_type>(s); \
return fmt::detail::compile_string_to_view<char_type>(s); \
} \
} \
...
@@ -2769,7 +2773,7 @@ FMT_CONSTEXPR basic_string_view<Char> compile_string_to_view(
...
@@ -2769,7 +2773,7 @@ FMT_CONSTEXPR basic_string_view<Char> compile_string_to_view(
std::string s = format(FMT_STRING("{:d}"), "foo");
std::string s = format(FMT_STRING("{:d}"), "foo");
\endrst
\endrst
*/
*/
#define FMT_STRING(s) FMT_STRING_IMPL(s, )
#define FMT_STRING(s) FMT_STRING_IMPL(s,
fmt::compile_string
)
template
<
typename
...
Args
,
typename
S
,
template
<
typename
...
Args
,
typename
S
,
enable_if_t
<
(
is_compile_string
<
S
>
::
value
),
int
>>
enable_if_t
<
(
is_compile_string
<
S
>
::
value
),
int
>>
...
@@ -3344,9 +3348,14 @@ join(const Range& range, wstring_view sep) {
...
@@ -3344,9 +3348,14 @@ join(const Range& range, wstring_view sep) {
std::string answer = fmt::to_string(42);
std::string answer = fmt::to_string(42);
\endrst
\endrst
*/
*/
template
<
typename
T
>
inline
std
::
string
to_string
(
const
T
&
value
)
{
template
<
typename
T
,
FMT_ENABLE_IF
(
!
std
::
is_integral
<
T
>
::
value
)
>
inline
std
::
string
to_string
(
const
T
&
value
)
{
return
format
(
"{}"
,
value
);
return
format
(
"{}"
,
value
);
}
}
template
<
typename
T
,
FMT_ENABLE_IF
(
std
::
is_integral
<
T
>
::
value
)
>
inline
std
::
string
to_string
(
T
value
)
{
return
format_int
(
value
).
str
();
}
/**
/**
Converts *value* to ``std::wstring`` using the default format for type *T*.
Converts *value* to ``std::wstring`` using the default format for type *T*.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment