Skip to content

Commit c3344e2

Browse files
committed
Cleanup base API
1 parent 5f438c9 commit c3344e2

File tree

4 files changed

+86
-78
lines changed

4 files changed

+86
-78
lines changed

Diff for: include/fmt/base.h

+9-17
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@
254254
#ifdef FMT_CLANG_PRAGMA
255255
// Use the provided definition.
256256
#elif FMT_CLANG_VERSION
257-
# define FMT_CLANG_PRAGMA(x) _Pragma(#x)
257+
# define FMT_CLANG_PRAGMA_IMPL(x) _Pragma(#x)
258+
# define FMT_CLANG_PRAGMA(x) FMT_CLANG_PRAGMA_IMPL(clang x)
258259
#else
259260
# define FMT_CLANG_PRAGMA(x)
260261
#endif
@@ -317,7 +318,7 @@ FMT_GCC_PRAGMA(GCC push_options)
317318
#if !defined(__OPTIMIZE__) && !defined(__CUDACC__)
318319
FMT_GCC_PRAGMA(GCC optimize("Og"))
319320
#endif
320-
FMT_CLANG_PRAGMA(clang diagnostic push)
321+
FMT_CLANG_PRAGMA(diagnostic push)
321322

322323
FMT_BEGIN_NAMESPACE
323324

@@ -432,7 +433,7 @@ template <typename T> auto convert_for_visit(T) -> monostate { return {}; }
432433
#endif
433434

434435
#if FMT_USE_BITINT
435-
# pragma clang diagnostic ignored "-Wbit-int-extension"
436+
FMT_CLANG_PRAGMA(diagnostic ignored "-Wbit-int-extension")
436437
template <int N> using bitint = _BitInt(N);
437438
template <int N> using ubitint = unsigned _BitInt(N);
438439
#else
@@ -2431,7 +2432,7 @@ template <typename T, typename Char, type TYPE> struct native_formatter {
24312432
specs_.set_type(set ? presentation_type::debug : presentation_type::none);
24322433
}
24332434

2434-
FMT_CLANG_PRAGMA(clang diagnostic ignored "-Wundefined-inline")
2435+
FMT_CLANG_PRAGMA(diagnostic ignored "-Wundefined-inline")
24352436
template <typename FormatContext>
24362437
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const
24372438
-> decltype(ctx.out());
@@ -2452,17 +2453,8 @@ FMT_CONSTEXPR inline auto is_locking() -> bool {
24522453
return locking<T1>::value || is_locking<T2, Tail...>();
24532454
}
24542455

2455-
// Use vformat_args and avoid type_identity to keep symbols short.
2456-
template <typename Char = char> struct vformat_args {
2457-
using type = basic_format_args<buffered_context<Char>>;
2458-
};
2459-
template <> struct vformat_args<char> {
2460-
using type = format_args;
2461-
};
2462-
2463-
template <typename Char>
2464-
void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
2465-
typename vformat_args<Char>::type args, locale_ref loc = {});
2456+
FMT_API void vformat_to(buffer<char>& buf, string_view fmt, format_args args,
2457+
locale_ref loc = {});
24662458

24672459
#ifdef _WIN32
24682460
FMT_API void vprint_mojibake(FILE*, string_view, format_args, bool);
@@ -2978,7 +2970,7 @@ template <typename... T>
29782970
FMT_NODISCARD FMT_INLINE auto formatted_size(format_string<T...> fmt,
29792971
T&&... args) -> size_t {
29802972
auto buf = detail::counting_buffer<>();
2981-
detail::vformat_to<char>(buf, fmt, fmt::make_format_args(args...), {});
2973+
detail::vformat_to(buf, fmt, fmt::make_format_args(args...), {});
29822974
return buf.count();
29832975
}
29842976

@@ -3036,7 +3028,7 @@ FMT_INLINE void println(format_string<T...> fmt, T&&... args) {
30363028
}
30373029

30383030
FMT_END_EXPORT
3039-
FMT_CLANG_PRAGMA(clang diagnostic pop)
3031+
FMT_CLANG_PRAGMA(diagnostic pop)
30403032
FMT_GCC_PRAGMA(GCC pop_options)
30413033
FMT_END_NAMESPACE
30423034

Diff for: include/fmt/format-inl.h

+9
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,15 @@ FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string {
14421442

14431443
namespace detail {
14441444

1445+
FMT_FUNC void vformat_to(buffer<char>& buf, string_view fmt, format_args args,
1446+
locale_ref loc) {
1447+
auto out = appender(buf);
1448+
if (fmt.size() == 2 && equal2(fmt.data(), "{}"))
1449+
return args.get(0).visit(default_arg_formatter<char>{out});
1450+
parse_format_string(
1451+
fmt, format_handler<char>{parse_context<char>(fmt), {out, args, loc}});
1452+
}
1453+
14451454
template <typename T> struct span {
14461455
T* data;
14471456
size_t size;

Diff for: include/fmt/format.h

+67-61
Original file line numberDiff line numberDiff line change
@@ -3808,6 +3808,71 @@ template <typename Char> struct udl_arg {
38083808
# endif
38093809
#endif // FMT_USE_USER_DEFINED_LITERALS
38103810

3811+
template <typename Char> struct format_handler {
3812+
parse_context<Char> parse_ctx;
3813+
buffered_context<Char> ctx;
3814+
3815+
void on_text(const Char* begin, const Char* end) {
3816+
copy_noinline<Char>(begin, end, ctx.out());
3817+
}
3818+
3819+
FMT_CONSTEXPR auto on_arg_id() -> int { return parse_ctx.next_arg_id(); }
3820+
FMT_CONSTEXPR auto on_arg_id(int id) -> int {
3821+
parse_ctx.check_arg_id(id);
3822+
return id;
3823+
}
3824+
FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
3825+
parse_ctx.check_arg_id(id);
3826+
int arg_id = ctx.arg_id(id);
3827+
if (arg_id < 0) report_error("argument not found");
3828+
return arg_id;
3829+
}
3830+
3831+
FMT_INLINE void on_replacement_field(int id, const Char*) {
3832+
ctx.arg(id).visit(default_arg_formatter<Char>{ctx.out()});
3833+
}
3834+
3835+
auto on_format_specs(int id, const Char* begin, const Char* end)
3836+
-> const Char* {
3837+
auto arg = get_arg(ctx, id);
3838+
// Not using a visitor for custom types gives better codegen.
3839+
if (arg.format_custom(begin, parse_ctx, ctx)) return parse_ctx.begin();
3840+
3841+
auto specs = dynamic_format_specs<Char>();
3842+
begin = parse_format_specs(begin, end, specs, parse_ctx, arg.type());
3843+
if (specs.dynamic()) {
3844+
handle_dynamic_spec(specs.dynamic_width(), specs.width, specs.width_ref,
3845+
ctx);
3846+
handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
3847+
specs.precision_ref, ctx);
3848+
}
3849+
3850+
arg.visit(arg_formatter<Char>{ctx.out(), specs, ctx.locale()});
3851+
return begin;
3852+
}
3853+
3854+
FMT_NORETURN void on_error(const char* message) { report_error(message); }
3855+
};
3856+
3857+
// DEPRECATED!
3858+
// Use vformat_args and avoid type_identity to keep symbols short.
3859+
template <typename Char = char> struct vformat_args {
3860+
using type = basic_format_args<buffered_context<Char>>;
3861+
};
3862+
template <> struct vformat_args<char> {
3863+
using type = format_args;
3864+
};
3865+
3866+
template <typename Char>
3867+
void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
3868+
typename vformat_args<Char>::type args, locale_ref loc = {}) {
3869+
auto out = basic_appender<Char>(buf);
3870+
if (fmt.size() == 2 && equal2(fmt.data(), "{}"))
3871+
return args.get(0).visit(default_arg_formatter<Char>{out});
3872+
parse_format_string(
3873+
fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
3874+
}
3875+
38113876
template <typename Locale, typename Char>
38123877
auto vformat(const Locale& loc, basic_string_view<Char> fmt,
38133878
typename detail::vformat_args<Char>::type args)
@@ -4188,68 +4253,9 @@ FMT_END_EXPORT
41884253

41894254
namespace detail {
41904255

4191-
template <typename Char> struct format_handler {
4192-
parse_context<Char> parse_ctx;
4193-
buffered_context<Char> ctx;
4194-
4195-
void on_text(const Char* begin, const Char* end) {
4196-
copy_noinline<Char>(begin, end, ctx.out());
4197-
}
4198-
4199-
FMT_CONSTEXPR auto on_arg_id() -> int { return parse_ctx.next_arg_id(); }
4200-
FMT_CONSTEXPR auto on_arg_id(int id) -> int {
4201-
parse_ctx.check_arg_id(id);
4202-
return id;
4203-
}
4204-
FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
4205-
parse_ctx.check_arg_id(id);
4206-
int arg_id = ctx.arg_id(id);
4207-
if (arg_id < 0) report_error("argument not found");
4208-
return arg_id;
4209-
}
4210-
4211-
FMT_INLINE void on_replacement_field(int id, const Char*) {
4212-
ctx.arg(id).visit(default_arg_formatter<Char>{ctx.out()});
4213-
}
4214-
4215-
auto on_format_specs(int id, const Char* begin, const Char* end)
4216-
-> const Char* {
4217-
auto arg = get_arg(ctx, id);
4218-
// Not using a visitor for custom types gives better codegen.
4219-
if (arg.format_custom(begin, parse_ctx, ctx)) return parse_ctx.begin();
4220-
4221-
auto specs = dynamic_format_specs<Char>();
4222-
begin = parse_format_specs(begin, end, specs, parse_ctx, arg.type());
4223-
if (specs.dynamic()) {
4224-
handle_dynamic_spec(specs.dynamic_width(), specs.width, specs.width_ref,
4225-
ctx);
4226-
handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
4227-
specs.precision_ref, ctx);
4228-
}
4229-
4230-
arg.visit(arg_formatter<Char>{ctx.out(), specs, ctx.locale()});
4231-
return begin;
4232-
}
4233-
4234-
FMT_NORETURN void on_error(const char* message) { report_error(message); }
4235-
};
4236-
4237-
template <typename Char>
4238-
void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
4239-
typename vformat_args<Char>::type args, locale_ref loc) {
4240-
auto out = basic_appender<Char>(buf);
4241-
if (fmt.size() == 2 && equal2(fmt.data(), "{}"))
4242-
return args.get(0).visit(default_arg_formatter<Char>{out});
4243-
parse_format_string(
4244-
fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
4245-
}
4246-
42474256
FMT_BEGIN_EXPORT
42484257

42494258
#ifndef FMT_HEADER_ONLY
4250-
extern template FMT_API void vformat_to(buffer<char>&, string_view,
4251-
typename vformat_args<>::type,
4252-
locale_ref);
42534259
extern template FMT_API auto thousands_sep_impl<char>(locale_ref)
42544260
-> thousands_sep_result<char>;
42554261
extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref)
@@ -4361,8 +4367,8 @@ FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
43614367
format_string<T...> fmt,
43624368
T&&... args) -> size_t {
43634369
auto buf = detail::counting_buffer<>();
4364-
detail::vformat_to<char>(buf, fmt, fmt::make_format_args(args...),
4365-
detail::locale_ref(loc));
4370+
detail::vformat_to(buf, fmt, fmt::make_format_args(args...),
4371+
detail::locale_ref(loc));
43664372
return buf.count();
43674373
}
43684374

Diff for: src/format.cc

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ template FMT_API auto decimal_point_impl(locale_ref) -> char;
2929
// DEPRECATED!
3030
template FMT_API void buffer<char>::append(const char*, const char*);
3131

32+
// DEPRECATED!
3233
template FMT_API void vformat_to(buffer<char>&, string_view,
3334
typename vformat_args<>::type, locale_ref);
3435

0 commit comments

Comments
 (0)