prepare -> compile

This commit is contained in:
Victor Zverovich 2019-07-25 19:01:21 +03:00
parent b48ffc14a4
commit 9393fe26f6
5 changed files with 55 additions and 54 deletions

View File

@ -151,8 +151,9 @@ function(add_headers VAR)
endfunction() endfunction()
# Define the fmt library, its includes and the needed defines. # Define the fmt library, its includes and the needed defines.
add_headers(FMT_HEADERS chrono.h color.h core.h format.h format-inl.h locale.h add_headers(FMT_HEADERS chrono.h color.h compile.h core.h format.h format-inl.h
ostream.h prepare.h printf.h ranges.h safe-duration-cast.h) locale.h ostream.h printf.h ranges.h
safe-duration-cast.h)
set(FMT_SOURCES src/format.cc) set(FMT_SOURCES src/format.cc)
if (HAVE_OPEN) if (HAVE_OPEN)
add_headers(FMT_HEADERS posix.h) add_headers(FMT_HEADERS posix.h)

View File

@ -5,8 +5,8 @@
// //
// For the license information refer to format.h. // For the license information refer to format.h.
#ifndef FMT_PREPARE_H_ #ifndef FMT_COMPILE_H_
#define FMT_PREPARE_H_ #define FMT_COMPILE_H_
#ifndef FMT_HAS_CONSTRUCTIBLE_TRAITS #ifndef FMT_HAS_CONSTRUCTIBLE_TRAITS
# define FMT_HAS_CONSTRUCTIBLE_TRAITS \ # define FMT_HAS_CONSTRUCTIBLE_TRAITS \
@ -728,41 +728,41 @@ using wprepared_format_t =
#if FMT_USE_CONSTEXPR #if FMT_USE_CONSTEXPR
template <typename... Args, typename Format> template <typename... Args, typename Format>
FMT_CONSTEXPR auto prepare(Format format) { FMT_CONSTEXPR auto compile(Format format) {
return internal::do_prepare<Format, Args...>( return internal::do_prepare<Format, Args...>(
typename internal::format_tag<Format>::type{}, std::move(format)); typename internal::format_tag<Format>::type{}, std::move(format));
} }
#else #else
template <typename... Args, typename Format> template <typename... Args, typename Format>
auto prepare(Format format) -> auto compile(Format format) ->
typename internal::preparator<Format, Args...>::prepared_format_type { typename internal::preparator<Format, Args...>::prepared_format_type {
return internal::preparator<Format, Args...>::prepare(std::move(format)); return internal::preparator<Format, Args...>::prepare(std::move(format));
} }
#endif #endif
template <typename... Args, typename Char> template <typename... Args, typename Char>
auto prepare(const Char* format) -> auto compile(const Char* format) ->
typename internal::preparator<std::basic_string<Char>, typename internal::preparator<std::basic_string<Char>,
Args...>::prepared_format_type { Args...>::prepared_format_type {
return prepare<Args...>(internal::to_runtime_format(format)); return compile<Args...>(internal::to_runtime_format(format));
} }
template <typename... Args, typename Char, unsigned N> template <typename... Args, typename Char, unsigned N>
auto prepare(const Char(format)[N]) -> auto compile(const Char(format)[N]) ->
typename internal::preparator<std::basic_string<Char>, typename internal::preparator<std::basic_string<Char>,
Args...>::prepared_format_type { Args...>::prepared_format_type {
const auto view = basic_string_view<Char>(format, N); const auto view = basic_string_view<Char>(format, N);
return prepare<Args...>(internal::to_runtime_format(view)); return compile<Args...>(internal::to_runtime_format(view));
} }
template <typename... Args, typename Char> template <typename... Args, typename Char>
auto prepare(basic_string_view<Char> format) -> auto compile(basic_string_view<Char> format) ->
typename internal::preparator<std::basic_string<Char>, typename internal::preparator<std::basic_string<Char>,
Args...>::prepared_format_type { Args...>::prepared_format_type {
return prepare<Args...>(internal::to_runtime_format(format)); return compile<Args...>(internal::to_runtime_format(format));
} }
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif // FMT_PREPARE_H_ #endif // FMT_COMPILE_H_

View File

@ -101,7 +101,7 @@ if (NOT (MSVC AND BUILD_SHARED_LIBS))
endif () endif ()
add_fmt_test(locale-test) add_fmt_test(locale-test)
add_fmt_test(ostream-test) add_fmt_test(ostream-test)
add_fmt_test(prepare-test) add_fmt_test(compile-test)
add_fmt_test(printf-test) add_fmt_test(printf-test)
add_fmt_test(custom-formatter-test) add_fmt_test(custom-formatter-test)
add_fmt_test(ranges-test) add_fmt_test(ranges-test)
@ -180,10 +180,10 @@ if (FMT_PEDANTIC)
target_include_directories(no-windows-h-test SYSTEM PUBLIC gtest gmock) target_include_directories(no-windows-h-test SYSTEM PUBLIC gtest gmock)
endif () endif ()
add_test(compile-test ${CMAKE_CTEST_COMMAND} add_test(compile-error-test ${CMAKE_CTEST_COMMAND}
--build-and-test --build-and-test
"${CMAKE_CURRENT_SOURCE_DIR}/compile-test" "${CMAKE_CURRENT_SOURCE_DIR}/compile-error-test"
"${CMAKE_CURRENT_BINARY_DIR}/compile-test" "${CMAKE_CURRENT_BINARY_DIR}/compile-error-test"
--build-generator ${CMAKE_GENERATOR} --build-generator ${CMAKE_GENERATOR}
--build-makeprogram ${CMAKE_MAKE_PROGRAM} --build-makeprogram ${CMAKE_MAKE_PROGRAM}
--build-options --build-options

View File

@ -3,7 +3,7 @@
// Copyright (c) 2012 - present, Victor Zverovich // Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved. // All rights reserved.
// //
// For the license information refer to prepare.h. // For the license information refer to format.h.
#include <stdint.h> #include <stdint.h>
#include <cctype> #include <cctype>
@ -16,12 +16,12 @@
#include <memory> #include <memory>
#include <string> #include <string>
// Check if fmt/prepare.h compiles with windows.h included before it. // Check if fmt/compile.h compiles with windows.h included before it.
#ifdef _WIN32 #ifdef _WIN32
# include <windows.h> # include <windows.h>
#endif #endif
#include "fmt/prepare.h" #include "fmt/compile.h"
#include "gmock.h" #include "gmock.h"
#include "gtest-extra.h" #include "gtest-extra.h"
#include "mock-allocator.h" #include "mock-allocator.h"
@ -434,11 +434,11 @@ TEST(PrepareTest, CompileTimePreparedPartsTypeProvider) {
// Use the struct instead of a function to workaround GCC 4.4's 'sorry, // Use the struct instead of a function to workaround GCC 4.4's 'sorry,
// unimplemented: mangling template_id_expr' issue. // unimplemented: mangling template_id_expr' issue.
template <typename... Args> struct copied_prepared_format_creator { template <typename... Args> struct copied_prepared_format_creator {
static decltype(fmt::prepare<Args...>(std::declval<std::string>())) make( static decltype(fmt::compile<Args...>(std::declval<std::string>())) make(
std::string format_str) { std::string format_str) {
auto prepared_format = fmt::prepare<Args...>(std::move(format_str)); auto prepared_format = fmt::compile<Args...>(std::move(format_str));
auto copied_prepared_format = prepared_format; auto copied_prepared_format = prepared_format;
prepared_format = fmt::prepare<Args...>(""); prepared_format = fmt::compile<Args...>("");
return copied_prepared_format; return copied_prepared_format;
} }
@ -478,9 +478,9 @@ TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
TEST(PrepareTest, ReusedPreparedFormatType) { TEST(PrepareTest, ReusedPreparedFormatType) {
typedef fmt::prepared_format<std::string, int>::type prepared_format; typedef fmt::prepared_format<std::string, int>::type prepared_format;
prepared_format prepared = fmt::prepare<prepared_format>("The {} is {}."); prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42)); EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
prepared = fmt::prepare<prepared_format>("40 {} 2 = {}"); prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42)); EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
} }
@ -491,9 +491,9 @@ TEST(PrepareTest, UserProvidedPartsContainerUnderlyingContainer) {
typedef fmt::basic_prepared_format<std::string, parts_container, std::string, typedef fmt::basic_prepared_format<std::string, parts_container, std::string,
int>::type prepared_format; int>::type prepared_format;
prepared_format prepared = fmt::prepare<prepared_format>("The {} is {}."); prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42)); EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
prepared = fmt::prepare<prepared_format>("40 {} 2 = {}"); prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42)); EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
} }
@ -535,67 +535,67 @@ TEST(PrepareTest, UserProvidedPartsContainer) {
typedef fmt::basic_prepared_format<std::string, custom_parts_container, typedef fmt::basic_prepared_format<std::string, custom_parts_container,
std::string, int>::type prepared_format; std::string, int>::type prepared_format;
prepared_format prepared = fmt::prepare<prepared_format>("The {} is {}."); prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42)); EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
prepared = fmt::prepare<prepared_format>("40 {} 2 = {}"); prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42)); EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
} }
TEST(PrepareTest, PassConstCharPointerFormat) { TEST(PrepareTest, PassConstCharPointerFormat) {
const char* c_format = "test {}"; const char* c_format = "test {}";
const auto prepared = fmt::prepare<int>(c_format); const auto prepared = fmt::compile<int>(c_format);
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
const wchar_t* wc_format = L"test {}"; const wchar_t* wc_format = L"test {}";
const auto wprepared = fmt::prepare<int>(wc_format); const auto wprepared = fmt::compile<int>(wc_format);
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
TEST(PrepareTest, PassCharArrayFormat) { TEST(PrepareTest, PassCharArrayFormat) {
char c_format[] = "test {}"; char c_format[] = "test {}";
const auto prepared = fmt::prepare<int>(c_format); const auto prepared = fmt::compile<int>(c_format);
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
wchar_t wc_format[] = L"test {}"; wchar_t wc_format[] = L"test {}";
const auto wprepared = fmt::prepare<int>(wc_format); const auto wprepared = fmt::compile<int>(wc_format);
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
TEST(PrepareTest, PassConstCharArrayFormat) { TEST(PrepareTest, PassConstCharArrayFormat) {
const char c_format[] = "test {}"; const char c_format[] = "test {}";
const auto prepared = fmt::prepare<int>(c_format); const auto prepared = fmt::compile<int>(c_format);
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
const wchar_t wc_format[] = L"test {}"; const wchar_t wc_format[] = L"test {}";
const auto wprepared = fmt::prepare<int>(wc_format); const auto wprepared = fmt::compile<int>(wc_format);
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
TEST(PrepareTest, PassStringLiteralFormat) { TEST(PrepareTest, PassStringLiteralFormat) {
const auto prepared = fmt::prepare<int>("test {}"); const auto prepared = fmt::compile<int>("test {}");
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
const auto wprepared = fmt::prepare<int>(L"test {}"); const auto wprepared = fmt::compile<int>(L"test {}");
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
TEST(PrepareTest, PassStringViewFormat) { TEST(PrepareTest, PassStringViewFormat) {
const auto prepared = const auto prepared =
fmt::prepare<int>(fmt::basic_string_view<char>("test {}")); fmt::compile<int>(fmt::basic_string_view<char>("test {}"));
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
const auto wprepared = const auto wprepared =
fmt::prepare<int>(fmt::basic_string_view<wchar_t>(L"test {}")); fmt::compile<int>(fmt::basic_string_view<wchar_t>(L"test {}"));
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
TEST(PrepareTest, PassBasicStringFormat) { TEST(PrepareTest, PassBasicStringFormat) {
const auto prepared = fmt::prepare<int>(std::string("test {}")); const auto prepared = fmt::compile<int>(std::string("test {}"));
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
const auto wprepared = fmt::prepare<int>(std::wstring(L"test {}")); const auto wprepared = fmt::compile<int>(std::wstring(L"test {}"));
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
#if FMT_USE_CONSTEXPR #if FMT_USE_CONSTEXPR
TEST(PrepareTest, PassCompileString) { TEST(PrepareTest, PassCompileString) {
const auto prepared = fmt::prepare<int>(FMT_STRING("test {}")); const auto prepared = fmt::compile<int>(FMT_STRING("test {}"));
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
const auto wprepared = fmt::prepare<int>(FMT_STRING(L"test {}")); const auto wprepared = fmt::compile<int>(FMT_STRING(L"test {}"));
EXPECT_EQ(L"test 42", wprepared.format(42)); EXPECT_EQ(L"test 42", wprepared.format(42));
} }
#endif #endif
@ -634,61 +634,61 @@ template <typename T> struct user_allocator {
TEST(PrepareTest, PassUserTypeFormat) { TEST(PrepareTest, PassUserTypeFormat) {
typedef std::basic_string<char, std::char_traits<char>, user_allocator<char>> typedef std::basic_string<char, std::char_traits<char>, user_allocator<char>>
user_format; user_format;
const auto prepared = fmt::prepare<int>(user_format("test {}")); const auto prepared = fmt::compile<int>(user_format("test {}"));
EXPECT_EQ("test 42", prepared.format(42)); EXPECT_EQ("test 42", prepared.format(42));
} }
TEST(PrepareTest, FormatToArrayOfChars) { TEST(PrepareTest, FormatToArrayOfChars) {
char buffer[32] = {0}; char buffer[32] = {0};
const auto prepared = fmt::prepare<int>("4{}"); const auto prepared = fmt::compile<int>("4{}");
prepared.format_to(buffer, 2); prepared.format_to(buffer, 2);
EXPECT_EQ(std::string("42"), buffer); EXPECT_EQ(std::string("42"), buffer);
wchar_t wbuffer[32] = {0}; wchar_t wbuffer[32] = {0};
const auto wprepared = fmt::prepare<int>(L"4{}"); const auto wprepared = fmt::compile<int>(L"4{}");
wprepared.format_to(wbuffer, 2); wprepared.format_to(wbuffer, 2);
EXPECT_EQ(std::wstring(L"42"), wbuffer); EXPECT_EQ(std::wstring(L"42"), wbuffer);
} }
TEST(PrepareTest, FormatToIterator) { TEST(PrepareTest, FormatToIterator) {
std::string s(2, ' '); std::string s(2, ' ');
const auto prepared = fmt::prepare<int>("4{}"); const auto prepared = fmt::compile<int>("4{}");
prepared.format_to(s.begin(), 2); prepared.format_to(s.begin(), 2);
EXPECT_EQ("42", s); EXPECT_EQ("42", s);
std::wstring ws(2, L' '); std::wstring ws(2, L' ');
const auto wprepared = fmt::prepare<int>(L"4{}"); const auto wprepared = fmt::compile<int>(L"4{}");
wprepared.format_to(ws.begin(), 2); wprepared.format_to(ws.begin(), 2);
EXPECT_EQ(L"42", ws); EXPECT_EQ(L"42", ws);
} }
TEST(PrepareTest, FormatToBackInserter) { TEST(PrepareTest, FormatToBackInserter) {
std::string s; std::string s;
const auto prepared = fmt::prepare<int>("4{}"); const auto prepared = fmt::compile<int>("4{}");
prepared.format_to(std::back_inserter(s), 2); prepared.format_to(std::back_inserter(s), 2);
EXPECT_EQ("42", s); EXPECT_EQ("42", s);
std::wstring ws; std::wstring ws;
const auto wprepared = fmt::prepare<int>(L"4{}"); const auto wprepared = fmt::compile<int>(L"4{}");
wprepared.format_to(std::back_inserter(ws), 2); wprepared.format_to(std::back_inserter(ws), 2);
EXPECT_EQ(L"42", ws); EXPECT_EQ(L"42", ws);
} }
TEST(PrepareTest, FormatToBasicMemoryBuffer) { TEST(PrepareTest, FormatToBasicMemoryBuffer) {
fmt::basic_memory_buffer<char, 100> buffer; fmt::basic_memory_buffer<char, 100> buffer;
const auto prepared = fmt::prepare<int>("4{}"); const auto prepared = fmt::compile<int>("4{}");
prepared.format_to(buffer, 2); prepared.format_to(buffer, 2);
EXPECT_EQ("42", to_string(buffer)); EXPECT_EQ("42", to_string(buffer));
fmt::basic_memory_buffer<wchar_t, 100> wbuffer; fmt::basic_memory_buffer<wchar_t, 100> wbuffer;
const auto wprepared = fmt::prepare<int>(L"4{}"); const auto wprepared = fmt::compile<int>(L"4{}");
wprepared.format_to(wbuffer, 2); wprepared.format_to(wbuffer, 2);
EXPECT_EQ(L"42", to_string(wbuffer)); EXPECT_EQ(L"42", to_string(wbuffer));
} }
TEST(PrepareTest, FormatToMemoryBuffer) { TEST(PrepareTest, FormatToMemoryBuffer) {
fmt::memory_buffer buffer; fmt::memory_buffer buffer;
const auto prepared = fmt::prepare<int>("4{}"); const auto prepared = fmt::compile<int>("4{}");
prepared.format_to(buffer, 2); prepared.format_to(buffer, 2);
EXPECT_EQ("42", to_string(buffer)); EXPECT_EQ("42", to_string(buffer));
fmt::wmemory_buffer wbuffer; fmt::wmemory_buffer wbuffer;
const auto wprepared = fmt::prepare<int>(L"4{}"); const auto wprepared = fmt::compile<int>(L"4{}");
wprepared.format_to(wbuffer, 2); wprepared.format_to(wbuffer, 2);
EXPECT_EQ(L"42", to_string(wbuffer)); EXPECT_EQ(L"42", to_string(wbuffer));
} }