mirror of
https://github.com/fmtlib/fmt.git
synced 2025-12-24 07:48:18 +01:00
Compare commits
108 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e75ad9822 | ||
|
|
4f043f8e00 | ||
|
|
cc02cbc455 | ||
|
|
73c0238e3b | ||
|
|
cb122a4d03 | ||
|
|
dc69cc45d2 | ||
|
|
9d8021f0d6 | ||
|
|
9d2221b954 | ||
|
|
70a6a4bb01 | ||
|
|
e4fc856c2f | ||
|
|
3f4984fb36 | ||
|
|
d43665056d | ||
|
|
894b6fac8e | ||
|
|
59f555ad8f | ||
|
|
a7e356cc80 | ||
|
|
e758bfbae1 | ||
|
|
66381e308d | ||
|
|
295a0d84d9 | ||
|
|
1fb1c4c912 | ||
|
|
465a593536 | ||
|
|
d62f4c3bc1 | ||
|
|
a243490ad7 | ||
|
|
9e12ca6069 | ||
|
|
fbca830dd1 | ||
|
|
6146248cf4 | ||
|
|
bc26fbf1b0 | ||
|
|
97cc889374 | ||
|
|
7110b46076 | ||
|
|
c8a8464f7d | ||
|
|
8cbfb6e727 | ||
|
|
6ffc828a79 | ||
|
|
aeb6add336 | ||
|
|
5614289dd8 | ||
|
|
10c7f89351 | ||
|
|
59c268a5f8 | ||
|
|
918bb1ce8f | ||
|
|
a3ba6b4f62 | ||
|
|
8671689449 | ||
|
|
cc10b4607f | ||
|
|
981797f059 | ||
|
|
2117775747 | ||
|
|
be0e268468 | ||
|
|
fbc38b9083 | ||
|
|
8dc69b9da9 | ||
|
|
1489d3b7fa | ||
|
|
dd8c5ce442 | ||
|
|
46484da711 | ||
|
|
802ff8866e | ||
|
|
95a718992c | ||
|
|
e483a01a0f | ||
|
|
f51080916e | ||
|
|
2a952dd0b2 | ||
|
|
0de44a469a | ||
|
|
f0d0a1ebd7 | ||
|
|
569ac91e0b | ||
|
|
a11eb3a090 | ||
|
|
62010520ed | ||
|
|
987514761e | ||
|
|
ba95e36a58 | ||
|
|
abde38b4fb | ||
|
|
18400503da | ||
|
|
9de312112a | ||
|
|
8bbb0b48b4 | ||
|
|
5c0101ab2d | ||
|
|
fbe6410e53 | ||
|
|
8b9fb9fb7e | ||
|
|
0f04ec68a9 | ||
|
|
809073851f | ||
|
|
5d02041c59 | ||
|
|
4b868b8922 | ||
|
|
4061a0d35d | ||
|
|
c68bab7014 | ||
|
|
0c63d15ee9 | ||
|
|
ce19309d09 | ||
|
|
c684349195 | ||
|
|
8db14efa84 | ||
|
|
ffe414cad1 | ||
|
|
c178ab440f | ||
|
|
5befe6584d | ||
|
|
35538ca66c | ||
|
|
4f16409730 | ||
|
|
2a4e948864 | ||
|
|
d778bded95 | ||
|
|
7b4f170c94 | ||
|
|
b1d10a2884 | ||
|
|
cf2719bd12 | ||
|
|
50584f42b4 | ||
|
|
73bed45b7a | ||
|
|
6eaa507473 | ||
|
|
48dff9f3c5 | ||
|
|
a9e261599b | ||
|
|
efd8ee8a7f | ||
|
|
8615ff2acc | ||
|
|
916ed99dab | ||
|
|
e7e9578ed4 | ||
|
|
c99a259739 | ||
|
|
e0f6a2f8be | ||
|
|
ae4a3945f5 | ||
|
|
a317448bd4 | ||
|
|
0eb01b832c | ||
|
|
2a4cd6d05e | ||
|
|
9c32e73abf | ||
|
|
e5c93108e6 | ||
|
|
60c662b3a7 | ||
|
|
f66ba6508a | ||
|
|
f21268aa72 | ||
|
|
07b690a679 | ||
|
|
f9e9bf0231 |
64
.travis.yml
64
.travis.yml
@@ -84,38 +84,38 @@ matrix:
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
# Android
|
||||
- language: android
|
||||
android:
|
||||
addons:
|
||||
apt:
|
||||
update: true
|
||||
components:
|
||||
- tools
|
||||
- platform-tools
|
||||
- android-21
|
||||
- sys-img-armeabi-v7a-android-21
|
||||
env:
|
||||
- ANDROID=true
|
||||
before_install:
|
||||
- git submodule update --init --recursive
|
||||
- sudo apt-get install wget unzip tree
|
||||
install:
|
||||
# Accept SDK Licenses + Install NDK
|
||||
- yes | sdkmanager --update > /dev/null 2>&1
|
||||
- sdkmanager ndk-bundle > /dev/null 2>&1
|
||||
# Download Gradle 4.3.1
|
||||
- wget https://services.gradle.org/distributions/gradle-4.3.1-bin.zip
|
||||
- mkdir -p gradle
|
||||
- unzip -q -d ./gradle gradle-4.3.1-bin.zip
|
||||
- export GRADLE=${TRAVIS_BUILD_DIR}/gradle/gradle-4.3.1/bin/gradle
|
||||
before_script:
|
||||
- bash $GRADLE --version
|
||||
- cd ./support
|
||||
script:
|
||||
- bash $GRADLE clean assemble
|
||||
after_success:
|
||||
- cd ${TRAVIS_BUILD_DIR}
|
||||
- tree ./libs
|
||||
# - language: android
|
||||
# android:
|
||||
# addons:
|
||||
# apt:
|
||||
# update: true
|
||||
# components:
|
||||
# - tools
|
||||
# - platform-tools
|
||||
# - android-21
|
||||
# - sys-img-armeabi-v7a-android-21
|
||||
# env:
|
||||
# - ANDROID=true
|
||||
# before_install:
|
||||
# - git submodule update --init --recursive
|
||||
# - sudo apt-get install wget unzip tree
|
||||
# install:
|
||||
# # Accept SDK Licenses + Install NDK
|
||||
# - yes | sdkmanager --update > /dev/null 2>&1
|
||||
# - sdkmanager ndk-bundle > /dev/null 2>&1
|
||||
# # Download Gradle 4.3.1
|
||||
# - wget https://services.gradle.org/distributions/gradle-4.3.1-bin.zip
|
||||
# - mkdir -p gradle
|
||||
# - unzip -q -d ./gradle gradle-4.3.1-bin.zip
|
||||
# - export GRADLE=${TRAVIS_BUILD_DIR}/gradle/gradle-4.3.1/bin/gradle
|
||||
# before_script:
|
||||
# - bash $GRADLE --version
|
||||
# - cd ./support
|
||||
# script:
|
||||
# - bash $GRADLE clean assemble
|
||||
# after_success:
|
||||
# - cd ${TRAVIS_BUILD_DIR}
|
||||
# - tree ./libs
|
||||
allow_failures:
|
||||
# Errors
|
||||
- env: COMPILER=g++-4.4 BUILD=Debug STANDARD=11
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
message(STATUS "CMake version: ${CMAKE_VERSION}")
|
||||
|
||||
cmake_minimum_required(VERSION 3.1.0)
|
||||
|
||||
# Use newer policies if available, up to most recent tested version of CMake.
|
||||
if(${CMAKE_VERSION} VERSION_LESS 3.11)
|
||||
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
|
||||
else()
|
||||
cmake_policy(VERSION 3.11)
|
||||
endif()
|
||||
|
||||
# Determine if fmt is built as a subproject (using add_subdirectory)
|
||||
# or if it is the master project.
|
||||
set(MASTER_PROJECT OFF)
|
||||
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
||||
set(MASTER_PROJECT ON)
|
||||
message(STATUS "CMake version: ${CMAKE_VERSION}")
|
||||
endif ()
|
||||
|
||||
# Joins arguments and places the results in ${result_var}.
|
||||
@@ -67,7 +73,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
-Wcast-qual -Wformat=2 -Wmissing-include-dirs
|
||||
-Wcast-align -Wnon-virtual-dtor
|
||||
-Wctor-dtor-privacy -Wdisabled-optimization
|
||||
-Winvalid-pch -Wmissing-declarations -Woverloaded-virtual
|
||||
-Winvalid-pch -Woverloaded-virtual
|
||||
-Wno-ctor-dtor-privacy -Wno-dangling-else -Wno-float-equal
|
||||
-Wno-format-nonliteral -Wno-sign-conversion -Wno-shadow)
|
||||
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
|
||||
@@ -86,28 +92,24 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
set(WERROR_FLAG -Werror)
|
||||
endif ()
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
set(PEDANTIC_COMPILE_FLAGS -Weverything -Wpedantic
|
||||
-Wno-weak-vtables -Wno-padded -Wno-gnu-statement-expression
|
||||
-Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-reserved-id-macro
|
||||
-Wno-global-constructors -Wno-disabled-macro-expansion
|
||||
-Wno-switch-enum -Wno-documentation-unknown-command
|
||||
-Wno-gnu-string-literal-operator-template -Wno-unused-member-function
|
||||
-Wno-unused-member-function
|
||||
-Wno-format-nonliteral -Wno-missing-noreturn -Wno-undefined-func-template
|
||||
-Wno-shadow -Wno-sign-conversion -Wno-used-but-marked-unused
|
||||
-Wno-covered-switch-default -Wno-missing-variable-declarations
|
||||
-Wno-double-promotion)
|
||||
-Wno-covered-switch-default -Wno-missing-prototypes
|
||||
-Wno-missing-variable-declarations -Wno-double-promotion)
|
||||
|
||||
set(WERROR_FLAG -Werror)
|
||||
|
||||
check_cxx_compiler_flag(-Wno-zero-as-null-pointer-constant HAS_NULLPTR_WARNING)
|
||||
if (HAS_NULLPTR_WARNING)
|
||||
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-zero-as-null-pointer-constant)
|
||||
endif ()
|
||||
|
||||
check_cxx_compiler_flag(-Wno-gnu-string-literal-operator-template HAS_GNU_UDL_WARNING)
|
||||
if (HAS_GNU_UDL_WARNING)
|
||||
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-gnu-string-literal-operator-template)
|
||||
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
|
||||
-Wno-zero-as-null-pointer-constant)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
@@ -148,8 +150,8 @@ function(add_headers VAR)
|
||||
endfunction()
|
||||
|
||||
# Define the fmt library, its includes and the needed defines.
|
||||
add_headers(FMT_HEADERS core.h format.h format-inl.h ostream.h printf.h time.h
|
||||
ranges.h)
|
||||
add_headers(FMT_HEADERS color.h core.h format.h format-inl.h ostream.h printf.h
|
||||
time.h ranges.h)
|
||||
set(FMT_SOURCES src/format.cc)
|
||||
if (HAVE_OPEN)
|
||||
add_headers(FMT_HEADERS posix.h)
|
||||
|
||||
136
ChangeLog.rst
136
ChangeLog.rst
@@ -1,3 +1,137 @@
|
||||
5.2.1 - 2018-09-21
|
||||
------------------
|
||||
|
||||
* Fixed ``visit`` lookup issues on gcc 7 & 8
|
||||
(`#870 <https://github.com/fmtlib/fmt/pull/870>`_).
|
||||
Thanks `@medithe <https://github.com/medithe>`_.
|
||||
|
||||
* Fixed linkage errors on older gcc.
|
||||
|
||||
* Prevented ``fmt/range.h`` from specializing ``fmt::basic_string_view``
|
||||
(`#865 <https://github.com/fmtlib/fmt/issues/865>`_,
|
||||
`#868 <https://github.com/fmtlib/fmt/pull/868>`_).
|
||||
Thanks `@hhggit (dual) <https://github.com/hhggit>`_.
|
||||
|
||||
* Improved error message when formatting unknown types
|
||||
(`#872 <https://github.com/fmtlib/fmt/pull/872>`_).
|
||||
Thanks `@foonathan (Jonathan Müller) <https://github.com/foonathan>`_,
|
||||
|
||||
* Disabled templated user-defined literals when compiled under nvcc
|
||||
(`#875 <https://github.com/fmtlib/fmt/pull/875>`_).
|
||||
Thanks `@CandyGumdrop (Candy Gumdrop) <https://github.com/CandyGumdrop>`_,
|
||||
|
||||
* Fixed ``format_to`` formatting to ``wmemory_buffer``
|
||||
(`#874 <https://github.com/fmtlib/fmt/issues/874>`_).
|
||||
|
||||
5.2.0 - 2018-09-13
|
||||
------------------
|
||||
|
||||
* Optimized format string parsing and argument processing which resulted in up
|
||||
to 5x speed up on long format strings and significant performance boost on
|
||||
various benchmarks. For example, version 5.2 is 2.22x faster than 5.1 on
|
||||
decimal integer formatting with ``format_to`` (macOS, clang-902.0.39.2):
|
||||
|
||||
================== ======= =======
|
||||
Method Time, s Speedup
|
||||
================== ======= =======
|
||||
fmt::format 5.1 0.58
|
||||
fmt::format 5.2 0.35 1.66x
|
||||
fmt::format_to 5.1 0.51
|
||||
fmt::format_to 5.2 0.23 2.22x
|
||||
sprintf 0.71
|
||||
std::to_string 1.01
|
||||
std::stringstream 1.73
|
||||
================== ======= =======
|
||||
|
||||
* Changed the ``fmt`` macro from opt-out to opt-in to prevent name collisions.
|
||||
To enable it define the ``FMT_STRING_ALIAS`` macro to 1 before including
|
||||
``fmt/format.h``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
#define FMT_STRING_ALIAS 1
|
||||
#include <fmt/format.h>
|
||||
std::string answer = format(fmt("{}"), 42);
|
||||
|
||||
* Added compile-time format string checks to ``format_to`` overload that takes
|
||||
``fmt::memory_buffer`` (`#783 <https://github.com/fmtlib/fmt/issues/783>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::memory_buffer buf;
|
||||
// Compile-time error: invalid type specifier.
|
||||
fmt::format_to(buf, fmt("{:d}"), "foo");
|
||||
|
||||
* Moved experimental color support to ``fmt/color.h`` and enabled the
|
||||
new API by default. The old API can be enabled by defining the
|
||||
``FMT_DEPRECATED_COLORS`` macro.
|
||||
|
||||
* Added formatting support for types explicitly convertible to
|
||||
``fmt::string_view``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
struct foo {
|
||||
explicit operator fmt::string_view() const { return "foo"; }
|
||||
};
|
||||
auto s = format("{}", foo());
|
||||
|
||||
In particular, this makes formatting function work with
|
||||
``folly::StringPiece``.
|
||||
|
||||
* Implemented preliminary support for ``char*_t`` by replacing the ``format``
|
||||
function overloads with a single function template parameterized on the string
|
||||
type.
|
||||
|
||||
* Added support for dynamic argument lists
|
||||
(`#814 <https://github.com/fmtlib/fmt/issues/814>`_,
|
||||
`#819 <https://github.com/fmtlib/fmt/pull/819>`_).
|
||||
Thanks `@MikePopoloski (Michael Popoloski)
|
||||
<https://github.com/MikePopoloski>`_.
|
||||
|
||||
* Reduced executable size overhead for embedded targets using newlib nano by
|
||||
making locale dependency optional
|
||||
(`#839 <https://github.com/fmtlib/fmt/pull/839>`_).
|
||||
Thanks `@teajay-fr (Thomas Benard) <https://github.com/teajay-fr>`_.
|
||||
|
||||
* Keep ``noexcept`` specifier when exceptions are disabled
|
||||
(`#801 <https://github.com/fmtlib/fmt/issues/801>`_,
|
||||
`#810 <https://github.com/fmtlib/fmt/pull/810>`_).
|
||||
Thanks `@qis (Alexej Harm) <https://github.com/qis>`_.
|
||||
|
||||
* Fixed formatting of user-defined types providing ``operator<<`` with
|
||||
``format_to_n``
|
||||
(`#806 <https://github.com/fmtlib/fmt/pull/806>`_).
|
||||
Thanks `@mkurdej (Marek Kurdej) <https://github.com/mkurdej>`_.
|
||||
|
||||
* Fixed dynamic linkage of new symbols
|
||||
(`#808 <https://github.com/fmtlib/fmt/issues/808>`_).
|
||||
|
||||
* Fixed global initialization issue
|
||||
(`#807 <https://github.com/fmtlib/fmt/issues/807>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
// This works on compilers with constexpr support.
|
||||
static const std::string answer = fmt::format("{}", 42);
|
||||
|
||||
* Fixed various compiler warnings and errors
|
||||
(`#804 <https://github.com/fmtlib/fmt/pull/804>`_,
|
||||
`#809 <https://github.com/fmtlib/fmt/issues/809>`_,
|
||||
`#811 <https://github.com/fmtlib/fmt/pull/811>`_,
|
||||
`#822 <https://github.com/fmtlib/fmt/issues/822>`_,
|
||||
`#827 <https://github.com/fmtlib/fmt/pull/827>`_,
|
||||
`#830 <https://github.com/fmtlib/fmt/issues/830>`_,
|
||||
`#838 <https://github.com/fmtlib/fmt/pull/838>`_,
|
||||
`#843 <https://github.com/fmtlib/fmt/issues/843>`_,
|
||||
`#844 <https://github.com/fmtlib/fmt/pull/844>`_,
|
||||
`#851 <https://github.com/fmtlib/fmt/issues/851>`_,
|
||||
`#852 <https://github.com/fmtlib/fmt/pull/852>`_,
|
||||
`#854 <https://github.com/fmtlib/fmt/pull/854>`_).
|
||||
Thanks `@henryiii (Henry Schreiner) <https://github.com/henryiii>`_,
|
||||
`@medithe <https://github.com/medithe>`_, and
|
||||
`@eliasdaler (Elias Daler) <https://github.com/eliasdaler>`_.
|
||||
|
||||
5.1.0 - 2018-07-05
|
||||
------------------
|
||||
|
||||
@@ -16,7 +150,7 @@
|
||||
``color`` enum) is now deprecated.
|
||||
(`#762 <https://github.com/fmtlib/fmt/issues/762>`_
|
||||
`#767 <https://github.com/fmtlib/fmt/pull/767>`_).
|
||||
Thanks `@Remotion (Remo) <https://github.com/Remotion>`_.
|
||||
thanks `@remotion (remo) <https://github.com/remotion>`_.
|
||||
|
||||
* Added quotes to strings in ranges and tuples
|
||||
(`#766 <https://github.com/fmtlib/fmt/pull/766>`_).
|
||||
|
||||
83
README.rst
83
README.rst
@@ -80,6 +80,7 @@ Format strings can be checked at compile time:
|
||||
.. code:: c++
|
||||
|
||||
// test.cc
|
||||
#define FMT_STRING_ALIAS 1
|
||||
#include <fmt/format.h>
|
||||
std::string s = format(fmt("{2}"), 42);
|
||||
|
||||
@@ -87,7 +88,7 @@ Format strings can be checked at compile time:
|
||||
|
||||
$ c++ -Iinclude -std=c++14 test.cc
|
||||
...
|
||||
test.cc:3:17: note: in instantiation of function template specialization 'fmt::v5::format<S, int>' requested here
|
||||
test.cc:4:17: note: in instantiation of function template specialization 'fmt::v5::format<S, int>' requested here
|
||||
std::string s = format(fmt("{2}"), 42);
|
||||
^
|
||||
include/fmt/core.h:778:19: note: non-constexpr function 'on_error' cannot be used in a constant expression
|
||||
@@ -107,24 +108,29 @@ Format strings can be checked at compile time:
|
||||
format_to(buf, "{:x}", 42); // replaces itoa(42, buffer, 16)
|
||||
// access the string using to_string(buf) or buf.data()
|
||||
|
||||
An object of any user-defined type for which there is an overloaded
|
||||
:code:`std::ostream` insertion operator (``operator<<``) can be formatted:
|
||||
Formatting of user-defined types is supported via a simple
|
||||
`extension API <http://fmtlib.net/latest/api.html#formatting-user-defined-types>`_:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
#include "fmt/ostream.h"
|
||||
#include "fmt/format.h"
|
||||
|
||||
class Date {
|
||||
int year_, month_, day_;
|
||||
public:
|
||||
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
|
||||
struct date {
|
||||
int year, month, day;
|
||||
};
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Date &d) {
|
||||
return os << d.year_ << '-' << d.month_ << '-' << d.day_;
|
||||
template <>
|
||||
struct fmt::formatter<date> {
|
||||
template <typename ParseContext>
|
||||
constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
|
||||
|
||||
template <typename FormatContext>
|
||||
auto format(const date &d, FormatContext &ctx) {
|
||||
return format_to(ctx.begin(), "{}-{}-{}", d.year, d.month, d.day);
|
||||
}
|
||||
};
|
||||
|
||||
std::string s = fmt::format("The date is {}", Date(2012, 12, 9));
|
||||
std::string s = fmt::format("The date is {}", date{2012, 12, 9});
|
||||
// s == "The date is 2012-12-9"
|
||||
|
||||
You can create your own functions similar to `format
|
||||
@@ -272,7 +278,7 @@ Boost Format library
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This is a very powerful library which supports both printf-like format
|
||||
strings and positional arguments. The main its drawback is performance.
|
||||
strings and positional arguments. Its main drawback is performance.
|
||||
According to various benchmarks it is much slower than other methods
|
||||
considered here. Boost Format also has excessive build times and severe
|
||||
code bloat issues (see `Benchmarks`_).
|
||||
@@ -369,18 +375,18 @@ macOS Sierra, best of three) is shown in the following tables.
|
||||
============= =============== ==================== ==================
|
||||
Method Compile Time, s Executable size, KiB Stripped size, KiB
|
||||
============= =============== ==================== ==================
|
||||
printf 2.7 29 26
|
||||
printf+string 18.4 29 26
|
||||
IOStreams 34.6 59 55
|
||||
fmt 22.0 37 34
|
||||
tinyformat 51.8 103 97
|
||||
Boost Format 120.5 762 739
|
||||
Folly Format 158.7 102 87
|
||||
printf 2.6 29 26
|
||||
printf+string 16.4 29 26
|
||||
IOStreams 31.1 59 55
|
||||
fmt 19.0 37 34
|
||||
tinyformat 44.0 103 97
|
||||
Boost Format 91.9 226 203
|
||||
Folly Format 115.7 101 88
|
||||
============= =============== ==================== ==================
|
||||
|
||||
As you can see, fmt has 60% less overhead in terms of resulting binary code
|
||||
size compared to IOStreams and comes pretty close to ``printf``. Boost Format
|
||||
has by far the largest overheads.
|
||||
and Folly Format have the largest overheads.
|
||||
|
||||
``printf+string`` is the same as ``printf`` but with extra ``<string>``
|
||||
include to measure the overhead of the latter.
|
||||
@@ -390,13 +396,13 @@ include to measure the overhead of the latter.
|
||||
============= =============== ==================== ==================
|
||||
Method Compile Time, s Executable size, KiB Stripped size, KiB
|
||||
============= =============== ==================== ==================
|
||||
printf 2.4 33 30
|
||||
printf+string 18.5 33 30
|
||||
IOStreams 31.9 56 52
|
||||
fmt 20.9 56 51
|
||||
tinyformat 38.9 88 82
|
||||
Boost Format 64.8 366 304
|
||||
Folly Format 113.5 442 428
|
||||
printf 2.2 33 30
|
||||
printf+string 16.0 33 30
|
||||
IOStreams 28.3 56 52
|
||||
fmt 18.2 59 50
|
||||
tinyformat 32.6 88 82
|
||||
Boost Format 54.1 365 303
|
||||
Folly Format 79.9 445 430
|
||||
============= =============== ==================== ==================
|
||||
|
||||
``libc``, ``lib(std)c++`` and ``libfmt`` are all linked as shared
|
||||
@@ -429,6 +435,29 @@ or the bloat test::
|
||||
|
||||
$ make bloat-test
|
||||
|
||||
FAQ
|
||||
---
|
||||
|
||||
Q: how can I capture formatting arguments and format them later?
|
||||
|
||||
A: use ``std::tuple``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
template <typename... Args>
|
||||
auto capture(const Args&... args) {
|
||||
return std::make_tuple(args...);
|
||||
}
|
||||
|
||||
auto print_message = [](const auto&... args) {
|
||||
fmt::print(args...);
|
||||
};
|
||||
|
||||
// Capture and store arguments:
|
||||
auto args = capture("{} {}", 42, "foo");
|
||||
// Do formatting:
|
||||
std::apply(print_message, args);
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
|
||||
32
doc/api.rst
32
doc/api.rst
@@ -38,8 +38,8 @@ arguments in the resulting string.
|
||||
|
||||
.. _format:
|
||||
|
||||
.. doxygenfunction:: format(string_view, const Args&...)
|
||||
.. doxygenfunction:: vformat(string_view, format_args)
|
||||
.. doxygenfunction:: format(const String&, const Args&...)
|
||||
.. doxygenfunction:: vformat(const String&, basic_format_args<typename buffer_context<Char>::type>)
|
||||
|
||||
.. _print:
|
||||
|
||||
@@ -130,6 +130,25 @@ always be formatted in the same way. See ``formatter<tm>::parse`` in
|
||||
:file:`fmt/time.h` for an advanced example of how to parse the format string and
|
||||
customize the formatted output.
|
||||
|
||||
You can also reuse existing formatters, for example::
|
||||
|
||||
enum class color {red, green, blue};
|
||||
|
||||
template <>
|
||||
struct fmt::formatter<color>: formatter<string_view> {
|
||||
// parse is inherited from formatter<string_view>.
|
||||
template <typename FormatContext>
|
||||
auto format(color c, FormatContext &ctx) {
|
||||
string_view name = "unknown";
|
||||
switch (c) {
|
||||
case color::red: name = "red"; break;
|
||||
case color::green: name = "green"; break;
|
||||
case color::blue: name = "blue"; break;
|
||||
}
|
||||
return formatter<string_view>::format(name, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
This section shows how to define a custom format function for a user-defined
|
||||
type. The next section describes how to get ``fmt`` to use a conventional stream
|
||||
output ``operator<<`` when one is defined for a user-defined type.
|
||||
@@ -138,7 +157,7 @@ Output iterator support
|
||||
-----------------------
|
||||
|
||||
.. doxygenfunction:: fmt::format_to(OutputIt, string_view, const Args&...)
|
||||
.. doxygenfunction:: fmt::format_to_n(OutputIt, size_t, string_view, const Args&...)
|
||||
.. doxygenfunction:: fmt::format_to_n(OutputIt, std::size_t, string_view, const Args&...)
|
||||
.. doxygenstruct:: fmt::format_to_n_result
|
||||
:members:
|
||||
|
||||
@@ -234,11 +253,10 @@ custom argument formatter class::
|
||||
|
||||
using arg_formatter::operator();
|
||||
|
||||
void operator()(int value) {
|
||||
auto operator()(int value) {
|
||||
if (spec().type() == 'x')
|
||||
(*this)(static_cast<unsigned>(value)); // convert to unsigned and format
|
||||
else
|
||||
arg_formatter::operator()(value);
|
||||
return (*this)(static_cast<unsigned>(value)); // convert to unsigned and format
|
||||
return arg_formatter::operator()(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import errno, os, shutil, sys, tempfile
|
||||
from subprocess import check_call, check_output, CalledProcessError, Popen, PIPE
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
versions = ['1.0.0', '1.1.0', '2.0.0', '3.0.2', '4.0.0', '4.1.0', '5.0.0', '5.1.0']
|
||||
versions = ['1.0.0', '1.1.0', '2.0.0', '3.0.2', '4.0.0', '4.1.0', '5.0.0', '5.1.0', '5.2.0', '5.2.1']
|
||||
|
||||
def pip_install(package, commit=None, **kwargs):
|
||||
"Install package using pip."
|
||||
@@ -91,7 +91,8 @@ def build_docs(version='dev', **kwargs):
|
||||
FMT_USE_USER_DEFINED_LITERALS=1 \
|
||||
FMT_API= \
|
||||
"FMT_BEGIN_NAMESPACE=namespace fmt {{" \
|
||||
"FMT_END_NAMESPACE=}}"
|
||||
"FMT_END_NAMESPACE=}}" \
|
||||
"FMT_STRING_ALIAS=1"
|
||||
EXCLUDE_SYMBOLS = fmt::internal::* StringValue write_str
|
||||
'''.format(include_dir, doxyxml_dir).encode('UTF-8'))
|
||||
if p.returncode != 0:
|
||||
|
||||
@@ -76,7 +76,7 @@ The general form of a *standard format specifier* is:
|
||||
|
||||
.. productionlist:: sf
|
||||
format_spec: [[`fill`]`align`][`sign`]["#"]["0"][`width`]["." `precision`][`type`]
|
||||
fill: <a character other than '{' or '}'>
|
||||
fill: <a character other than '{', '}' or '\0'>
|
||||
align: "<" | ">" | "=" | "^"
|
||||
sign: "+" | "-" | " "
|
||||
width: `integer` | "{" `arg_id` "}"
|
||||
@@ -84,11 +84,11 @@ The general form of a *standard format specifier* is:
|
||||
type: `int_type` | "a" | "A" | "c" | "e" | "E" | "f" | "F" | "g" | "G" | "p" | "s"
|
||||
int_type: "b" | "B" | "d" | "n" | "o" | "x" | "X"
|
||||
|
||||
The *fill* character can be any character other than '{' or '}'. The presence
|
||||
of a fill character is signaled by the character following it, which must be
|
||||
one of the alignment options. If the second character of *format_spec* is not
|
||||
a valid alignment option, then it is assumed that both the fill character and
|
||||
the alignment option are absent.
|
||||
The *fill* character can be any character other than '{', '}' or '\\0'. The
|
||||
presence of a fill character is signaled by the character following it, which
|
||||
must be one of the alignment options. If the second character of *format_spec*
|
||||
is not a valid alignment option, then it is assumed that both the fill character
|
||||
and the alignment option are absent.
|
||||
|
||||
The meaning of the various alignment options is as follows:
|
||||
|
||||
|
||||
278
include/fmt/color.h
Normal file
278
include/fmt/color.h
Normal file
@@ -0,0 +1,278 @@
|
||||
// Formatting library for C++ - color support
|
||||
//
|
||||
// Copyright (c) 2018 - present, Victor Zverovich and fmt contributors
|
||||
// All rights reserved.
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#ifndef FMT_COLOR_H_
|
||||
#define FMT_COLOR_H_
|
||||
|
||||
#include "format.h"
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
|
||||
#ifdef FMT_DEPRECATED_COLORS
|
||||
|
||||
// color and (v)print_colored are deprecated.
|
||||
enum color { black, red, green, yellow, blue, magenta, cyan, white };
|
||||
FMT_API void vprint_colored(color c, string_view format, format_args args);
|
||||
FMT_API void vprint_colored(color c, wstring_view format, wformat_args args);
|
||||
template <typename... Args>
|
||||
inline void print_colored(color c, string_view format_str,
|
||||
const Args & ... args) {
|
||||
vprint_colored(c, format_str, make_format_args(args...));
|
||||
}
|
||||
template <typename... Args>
|
||||
inline void print_colored(color c, wstring_view format_str,
|
||||
const Args & ... args) {
|
||||
vprint_colored(c, format_str, make_format_args<wformat_context>(args...));
|
||||
}
|
||||
|
||||
inline void vprint_colored(color c, string_view format, format_args args) {
|
||||
char escape[] = "\x1b[30m";
|
||||
escape[3] = static_cast<char>('0' + c);
|
||||
std::fputs(escape, stdout);
|
||||
vprint(format, args);
|
||||
std::fputs(internal::data::RESET_COLOR, stdout);
|
||||
}
|
||||
|
||||
inline void vprint_colored(color c, wstring_view format, wformat_args args) {
|
||||
wchar_t escape[] = L"\x1b[30m";
|
||||
escape[3] = static_cast<wchar_t>('0' + c);
|
||||
std::fputws(escape, stdout);
|
||||
vprint(format, args);
|
||||
std::fputws(internal::data::WRESET_COLOR, stdout);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// Experimental color support.
|
||||
enum class color : uint32_t {
|
||||
alice_blue = 0xF0F8FF, // rgb(240,248,255)
|
||||
antique_white = 0xFAEBD7, // rgb(250,235,215)
|
||||
aqua = 0x00FFFF, // rgb(0,255,255)
|
||||
aquamarine = 0x7FFFD4, // rgb(127,255,212)
|
||||
azure = 0xF0FFFF, // rgb(240,255,255)
|
||||
beige = 0xF5F5DC, // rgb(245,245,220)
|
||||
bisque = 0xFFE4C4, // rgb(255,228,196)
|
||||
black = 0x000000, // rgb(0,0,0)
|
||||
blanched_almond = 0xFFEBCD, // rgb(255,235,205)
|
||||
blue = 0x0000FF, // rgb(0,0,255)
|
||||
blue_violet = 0x8A2BE2, // rgb(138,43,226)
|
||||
brown = 0xA52A2A, // rgb(165,42,42)
|
||||
burly_wood = 0xDEB887, // rgb(222,184,135)
|
||||
cadet_blue = 0x5F9EA0, // rgb(95,158,160)
|
||||
chartreuse = 0x7FFF00, // rgb(127,255,0)
|
||||
chocolate = 0xD2691E, // rgb(210,105,30)
|
||||
coral = 0xFF7F50, // rgb(255,127,80)
|
||||
cornflower_blue = 0x6495ED, // rgb(100,149,237)
|
||||
cornsilk = 0xFFF8DC, // rgb(255,248,220)
|
||||
crimson = 0xDC143C, // rgb(220,20,60)
|
||||
cyan = 0x00FFFF, // rgb(0,255,255)
|
||||
dark_blue = 0x00008B, // rgb(0,0,139)
|
||||
dark_cyan = 0x008B8B, // rgb(0,139,139)
|
||||
dark_golden_rod = 0xB8860B, // rgb(184,134,11)
|
||||
dark_gray = 0xA9A9A9, // rgb(169,169,169)
|
||||
dark_green = 0x006400, // rgb(0,100,0)
|
||||
dark_khaki = 0xBDB76B, // rgb(189,183,107)
|
||||
dark_magenta = 0x8B008B, // rgb(139,0,139)
|
||||
dark_olive_green = 0x556B2F, // rgb(85,107,47)
|
||||
dark_orange = 0xFF8C00, // rgb(255,140,0)
|
||||
dark_orchid = 0x9932CC, // rgb(153,50,204)
|
||||
dark_red = 0x8B0000, // rgb(139,0,0)
|
||||
dark_salmon = 0xE9967A, // rgb(233,150,122)
|
||||
dark_sea_green = 0x8FBC8F, // rgb(143,188,143)
|
||||
dark_slate_blue = 0x483D8B, // rgb(72,61,139)
|
||||
dark_slate_gray = 0x2F4F4F, // rgb(47,79,79)
|
||||
dark_turquoise = 0x00CED1, // rgb(0,206,209)
|
||||
dark_violet = 0x9400D3, // rgb(148,0,211)
|
||||
deep_pink = 0xFF1493, // rgb(255,20,147)
|
||||
deep_sky_blue = 0x00BFFF, // rgb(0,191,255)
|
||||
dim_gray = 0x696969, // rgb(105,105,105)
|
||||
dodger_blue = 0x1E90FF, // rgb(30,144,255)
|
||||
fire_brick = 0xB22222, // rgb(178,34,34)
|
||||
floral_white = 0xFFFAF0, // rgb(255,250,240)
|
||||
forest_green = 0x228B22, // rgb(34,139,34)
|
||||
fuchsia = 0xFF00FF, // rgb(255,0,255)
|
||||
gainsboro = 0xDCDCDC, // rgb(220,220,220)
|
||||
ghost_white = 0xF8F8FF, // rgb(248,248,255)
|
||||
gold = 0xFFD700, // rgb(255,215,0)
|
||||
golden_rod = 0xDAA520, // rgb(218,165,32)
|
||||
gray = 0x808080, // rgb(128,128,128)
|
||||
green = 0x008000, // rgb(0,128,0)
|
||||
green_yellow = 0xADFF2F, // rgb(173,255,47)
|
||||
honey_dew = 0xF0FFF0, // rgb(240,255,240)
|
||||
hot_pink = 0xFF69B4, // rgb(255,105,180)
|
||||
indian_red = 0xCD5C5C, // rgb(205,92,92)
|
||||
indigo = 0x4B0082, // rgb(75,0,130)
|
||||
ivory = 0xFFFFF0, // rgb(255,255,240)
|
||||
khaki = 0xF0E68C, // rgb(240,230,140)
|
||||
lavender = 0xE6E6FA, // rgb(230,230,250)
|
||||
lavender_blush = 0xFFF0F5, // rgb(255,240,245)
|
||||
lawn_green = 0x7CFC00, // rgb(124,252,0)
|
||||
lemon_chiffon = 0xFFFACD, // rgb(255,250,205)
|
||||
light_blue = 0xADD8E6, // rgb(173,216,230)
|
||||
light_coral = 0xF08080, // rgb(240,128,128)
|
||||
light_cyan = 0xE0FFFF, // rgb(224,255,255)
|
||||
light_golden_rod_yellow = 0xFAFAD2, // rgb(250,250,210)
|
||||
light_gray = 0xD3D3D3, // rgb(211,211,211)
|
||||
light_green = 0x90EE90, // rgb(144,238,144)
|
||||
light_pink = 0xFFB6C1, // rgb(255,182,193)
|
||||
light_salmon = 0xFFA07A, // rgb(255,160,122)
|
||||
light_sea_green = 0x20B2AA, // rgb(32,178,170)
|
||||
light_sky_blue = 0x87CEFA, // rgb(135,206,250)
|
||||
light_slate_gray = 0x778899, // rgb(119,136,153)
|
||||
light_steel_blue = 0xB0C4DE, // rgb(176,196,222)
|
||||
light_yellow = 0xFFFFE0, // rgb(255,255,224)
|
||||
lime = 0x00FF00, // rgb(0,255,0)
|
||||
lime_green = 0x32CD32, // rgb(50,205,50)
|
||||
linen = 0xFAF0E6, // rgb(250,240,230)
|
||||
magenta = 0xFF00FF, // rgb(255,0,255)
|
||||
maroon = 0x800000, // rgb(128,0,0)
|
||||
medium_aquamarine = 0x66CDAA, // rgb(102,205,170)
|
||||
medium_blue = 0x0000CD, // rgb(0,0,205)
|
||||
medium_orchid = 0xBA55D3, // rgb(186,85,211)
|
||||
medium_purple = 0x9370DB, // rgb(147,112,219)
|
||||
medium_sea_green = 0x3CB371, // rgb(60,179,113)
|
||||
medium_slate_blue = 0x7B68EE, // rgb(123,104,238)
|
||||
medium_spring_green = 0x00FA9A, // rgb(0,250,154)
|
||||
medium_turquoise = 0x48D1CC, // rgb(72,209,204)
|
||||
medium_violet_red = 0xC71585, // rgb(199,21,133)
|
||||
midnight_blue = 0x191970, // rgb(25,25,112)
|
||||
mint_cream = 0xF5FFFA, // rgb(245,255,250)
|
||||
misty_rose = 0xFFE4E1, // rgb(255,228,225)
|
||||
moccasin = 0xFFE4B5, // rgb(255,228,181)
|
||||
navajo_white = 0xFFDEAD, // rgb(255,222,173)
|
||||
navy = 0x000080, // rgb(0,0,128)
|
||||
old_lace = 0xFDF5E6, // rgb(253,245,230)
|
||||
olive = 0x808000, // rgb(128,128,0)
|
||||
olive_drab = 0x6B8E23, // rgb(107,142,35)
|
||||
orange = 0xFFA500, // rgb(255,165,0)
|
||||
orange_red = 0xFF4500, // rgb(255,69,0)
|
||||
orchid = 0xDA70D6, // rgb(218,112,214)
|
||||
pale_golden_rod = 0xEEE8AA, // rgb(238,232,170)
|
||||
pale_green = 0x98FB98, // rgb(152,251,152)
|
||||
pale_turquoise = 0xAFEEEE, // rgb(175,238,238)
|
||||
pale_violet_red = 0xDB7093, // rgb(219,112,147)
|
||||
papaya_whip = 0xFFEFD5, // rgb(255,239,213)
|
||||
peach_puff = 0xFFDAB9, // rgb(255,218,185)
|
||||
peru = 0xCD853F, // rgb(205,133,63)
|
||||
pink = 0xFFC0CB, // rgb(255,192,203)
|
||||
plum = 0xDDA0DD, // rgb(221,160,221)
|
||||
powder_blue = 0xB0E0E6, // rgb(176,224,230)
|
||||
purple = 0x800080, // rgb(128,0,128)
|
||||
rebecca_purple = 0x663399, // rgb(102,51,153)
|
||||
red = 0xFF0000, // rgb(255,0,0)
|
||||
rosy_brown = 0xBC8F8F, // rgb(188,143,143)
|
||||
royal_blue = 0x4169E1, // rgb(65,105,225)
|
||||
saddle_brown = 0x8B4513, // rgb(139,69,19)
|
||||
salmon = 0xFA8072, // rgb(250,128,114)
|
||||
sandy_brown = 0xF4A460, // rgb(244,164,96)
|
||||
sea_green = 0x2E8B57, // rgb(46,139,87)
|
||||
sea_shell = 0xFFF5EE, // rgb(255,245,238)
|
||||
sienna = 0xA0522D, // rgb(160,82,45)
|
||||
silver = 0xC0C0C0, // rgb(192,192,192)
|
||||
sky_blue = 0x87CEEB, // rgb(135,206,235)
|
||||
slate_blue = 0x6A5ACD, // rgb(106,90,205)
|
||||
slate_gray = 0x708090, // rgb(112,128,144)
|
||||
snow = 0xFFFAFA, // rgb(255,250,250)
|
||||
spring_green = 0x00FF7F, // rgb(0,255,127)
|
||||
steel_blue = 0x4682B4, // rgb(70,130,180)
|
||||
tan = 0xD2B48C, // rgb(210,180,140)
|
||||
teal = 0x008080, // rgb(0,128,128)
|
||||
thistle = 0xD8BFD8, // rgb(216,191,216)
|
||||
tomato = 0xFF6347, // rgb(255,99,71)
|
||||
turquoise = 0x40E0D0, // rgb(64,224,208)
|
||||
violet = 0xEE82EE, // rgb(238,130,238)
|
||||
wheat = 0xF5DEB3, // rgb(245,222,179)
|
||||
white = 0xFFFFFF, // rgb(255,255,255)
|
||||
white_smoke = 0xF5F5F5, // rgb(245,245,245)
|
||||
yellow = 0xFFFF00, // rgb(255,255,0)
|
||||
yellow_green = 0x9ACD32, // rgb(154,205,50)
|
||||
}; // enum class color
|
||||
|
||||
// rgb is a struct for red, green and blue colors.
|
||||
// We use rgb as name because some editors will show it as color direct in the
|
||||
// editor.
|
||||
struct rgb {
|
||||
FMT_CONSTEXPR_DECL rgb() : r(0), g(0), b(0) {}
|
||||
FMT_CONSTEXPR_DECL rgb(uint8_t r_, uint8_t g_, uint8_t b_)
|
||||
: r(r_), g(g_), b(b_) {}
|
||||
FMT_CONSTEXPR_DECL rgb(uint32_t hex)
|
||||
: r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b((hex) & 0xFF) {}
|
||||
FMT_CONSTEXPR_DECL rgb(color hex)
|
||||
: r((uint32_t(hex) >> 16) & 0xFF), g((uint32_t(hex) >> 8) & 0xFF),
|
||||
b(uint32_t(hex) & 0xFF) {}
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
};
|
||||
|
||||
void vprint_rgb(rgb fd, string_view format, format_args args);
|
||||
void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args);
|
||||
|
||||
/**
|
||||
Formats a string and prints it to stdout using ANSI escape sequences to
|
||||
specify foreground color 'fd'.
|
||||
Example:
|
||||
fmt::print(fmt::color::red, "Elapsed time: {0:.2f} seconds", 1.23);
|
||||
*/
|
||||
template <typename... Args>
|
||||
inline void print(rgb fd, string_view format_str, const Args & ... args) {
|
||||
vprint_rgb(fd, format_str, make_format_args(args...));
|
||||
}
|
||||
|
||||
/**
|
||||
Formats a string and prints it to stdout using ANSI escape sequences to
|
||||
specify foreground color 'fd' and background color 'bg'.
|
||||
Example:
|
||||
fmt::print(fmt::color::red, fmt::color::black,
|
||||
"Elapsed time: {0:.2f} seconds", 1.23);
|
||||
*/
|
||||
template <typename... Args>
|
||||
inline void print(rgb fd, rgb bg, string_view format_str,
|
||||
const Args & ... args) {
|
||||
vprint_rgb(fd, bg, format_str, make_format_args(args...));
|
||||
}
|
||||
namespace internal {
|
||||
FMT_CONSTEXPR void to_esc(uint8_t c, char out[], int offset) {
|
||||
out[offset + 0] = static_cast<char>('0' + c / 100);
|
||||
out[offset + 1] = static_cast<char>('0' + c / 10 % 10);
|
||||
out[offset + 2] = static_cast<char>('0' + c % 10);
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
inline void vprint_rgb(rgb fd, string_view format, format_args args) {
|
||||
char escape_fd[] = "\x1b[38;2;000;000;000m";
|
||||
internal::to_esc(fd.r, escape_fd, 7);
|
||||
internal::to_esc(fd.g, escape_fd, 11);
|
||||
internal::to_esc(fd.b, escape_fd, 15);
|
||||
|
||||
std::fputs(escape_fd, stdout);
|
||||
vprint(format, args);
|
||||
std::fputs(internal::data::RESET_COLOR, stdout);
|
||||
}
|
||||
|
||||
inline void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) {
|
||||
char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color
|
||||
char escape_bg[] = "\x1b[48;2;000;000;000m"; // background color
|
||||
internal::to_esc(fd.r, escape_fd, 7);
|
||||
internal::to_esc(fd.g, escape_fd, 11);
|
||||
internal::to_esc(fd.b, escape_fd, 15);
|
||||
|
||||
internal::to_esc(bg.r, escape_bg, 7);
|
||||
internal::to_esc(bg.g, escape_bg, 11);
|
||||
internal::to_esc(bg.b, escape_bg, 15);
|
||||
|
||||
std::fputs(escape_fd, stdout);
|
||||
std::fputs(escape_bg, stdout);
|
||||
vprint(format, args);
|
||||
std::fputs(internal::data::RESET_COLOR, stdout);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
#endif // FMT_COLOR_H_
|
||||
@@ -9,14 +9,14 @@
|
||||
#define FMT_CORE_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstdio> // std::FILE
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
// The fmt library version in the form major * 10000 + minor * 100 + patch.
|
||||
#define FMT_VERSION 50100
|
||||
#define FMT_VERSION 50201
|
||||
|
||||
#ifdef __has_feature
|
||||
# define FMT_HAS_FEATURE(x) __has_feature(x)
|
||||
@@ -24,7 +24,8 @@
|
||||
# define FMT_HAS_FEATURE(x) 0
|
||||
#endif
|
||||
|
||||
#ifdef __has_include
|
||||
#if defined(__has_include) && !defined(__INTELLISENSE__) && \
|
||||
(!defined(__INTEL_COMPILER) || __INTEL_COMPILER >= 1600)
|
||||
# define FMT_HAS_INCLUDE(x) __has_include(x)
|
||||
#else
|
||||
# define FMT_HAS_INCLUDE(x) 0
|
||||
@@ -54,7 +55,7 @@
|
||||
# define FMT_MSC_VER 0
|
||||
#endif
|
||||
|
||||
// Check if relaxed c++14 constexpr is supported.
|
||||
// Check if relaxed C++14 constexpr is supported.
|
||||
// GCC doesn't allow throw in constexpr until version 6 (bug 67371).
|
||||
#ifndef FMT_USE_CONSTEXPR
|
||||
# define FMT_USE_CONSTEXPR \
|
||||
@@ -69,18 +70,26 @@
|
||||
# define FMT_CONSTEXPR_DECL
|
||||
#endif
|
||||
|
||||
#ifndef FMT_USE_CONSTEXPR11
|
||||
# define FMT_USE_CONSTEXPR11 \
|
||||
(FMT_MSC_VER >= 1900 || FMT_GCC_VERSION >= 406 || FMT_USE_CONSTEXPR)
|
||||
#endif
|
||||
#if FMT_USE_CONSTEXPR11
|
||||
# define FMT_CONSTEXPR11 constexpr
|
||||
#else
|
||||
# define FMT_CONSTEXPR11
|
||||
#endif
|
||||
|
||||
#ifndef FMT_OVERRIDE
|
||||
# if FMT_HAS_FEATURE(cxx_override) || \
|
||||
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
|
||||
FMT_MSC_VER >= 1900
|
||||
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
|
||||
# define FMT_OVERRIDE override
|
||||
# else
|
||||
# define FMT_OVERRIDE
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if FMT_HAS_FEATURE(cxx_explicit_conversions) || \
|
||||
FMT_MSC_VER >= 1800
|
||||
#if FMT_HAS_FEATURE(cxx_explicit_conversions) || FMT_MSC_VER >= 1800
|
||||
# define FMT_EXPLICIT explicit
|
||||
#else
|
||||
# define FMT_EXPLICIT
|
||||
@@ -88,8 +97,7 @@
|
||||
|
||||
#ifndef FMT_NULL
|
||||
# if FMT_HAS_FEATURE(cxx_nullptr) || \
|
||||
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
|
||||
FMT_MSC_VER >= 1600
|
||||
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600
|
||||
# define FMT_NULL nullptr
|
||||
# define FMT_USE_NULLPTR 1
|
||||
# else
|
||||
@@ -123,15 +131,16 @@
|
||||
#endif
|
||||
|
||||
#if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
|
||||
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
|
||||
FMT_MSC_VER >= 1900
|
||||
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
|
||||
# define FMT_DETECTED_NOEXCEPT noexcept
|
||||
# define FMT_HAS_CXX11_NOEXCEPT 1
|
||||
#else
|
||||
# define FMT_DETECTED_NOEXCEPT throw()
|
||||
# define FMT_HAS_CXX11_NOEXCEPT 0
|
||||
#endif
|
||||
|
||||
#ifndef FMT_NOEXCEPT
|
||||
# if FMT_EXCEPTIONS
|
||||
# if FMT_EXCEPTIONS || FMT_HAS_CXX11_NOEXCEPT
|
||||
# define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
|
||||
# else
|
||||
# define FMT_NOEXCEPT
|
||||
@@ -173,13 +182,6 @@
|
||||
# define FMT_ASSERT(condition, message) assert((condition) && message)
|
||||
#endif
|
||||
|
||||
#define FMT_DELETED = delete
|
||||
|
||||
// A macro to disallow the copy construction and assignment.
|
||||
#define FMT_DISALLOW_COPY_AND_ASSIGN(Type) \
|
||||
Type(const Type &) FMT_DELETED; \
|
||||
void operator=(const Type &) FMT_DELETED
|
||||
|
||||
// libc++ supports string_view in pre-c++17.
|
||||
#if (FMT_HAS_INCLUDE(<string_view>) && \
|
||||
(__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \
|
||||
@@ -198,13 +200,22 @@
|
||||
#endif
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
|
||||
namespace internal {
|
||||
|
||||
// An implementation of declval for pre-C++11 compilers such as gcc 4.
|
||||
template <typename T>
|
||||
typename std::add_rvalue_reference<T>::type declval() FMT_NOEXCEPT;
|
||||
|
||||
template <typename>
|
||||
struct result_of;
|
||||
|
||||
template <typename F, typename... Args>
|
||||
struct result_of<F(Args...)> {
|
||||
// A workaround for gcc 4.4 that doesn't allow F to be a reference.
|
||||
typedef typename std::result_of<
|
||||
typename std::remove_reference<F>::type(Args...)>::type type;
|
||||
};
|
||||
|
||||
// Casts nonnegative integer to unsigned.
|
||||
template <typename Int>
|
||||
FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
|
||||
@@ -212,7 +223,17 @@ FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
|
||||
return static_cast<typename std::make_unsigned<Int>::type>(value);
|
||||
}
|
||||
|
||||
// A constexpr std::char_traits::length replacement for pre-C++17.
|
||||
template <typename Char>
|
||||
FMT_CONSTEXPR size_t length(const Char *s) {
|
||||
const Char *start = s;
|
||||
while (*s) ++s;
|
||||
return s - start;
|
||||
}
|
||||
#if FMT_GCC_VERSION
|
||||
FMT_CONSTEXPR size_t length(const char *s) { return std::strlen(s); }
|
||||
#endif
|
||||
} // namespace internal
|
||||
|
||||
/**
|
||||
An implementation of ``std::basic_string_view`` for pre-C++17. It provides a
|
||||
@@ -255,20 +276,20 @@ class basic_string_view {
|
||||
the size with ``std::char_traits<Char>::length``.
|
||||
\endrst
|
||||
*/
|
||||
basic_string_view(const Char *s)
|
||||
: data_(s), size_(std::char_traits<Char>::length(s)) {}
|
||||
FMT_CONSTEXPR basic_string_view(const Char *s)
|
||||
: data_(s), size_(internal::length(s)) {}
|
||||
|
||||
/** Constructs a string reference from a ``std::basic_string`` object. */
|
||||
template <typename Alloc>
|
||||
FMT_CONSTEXPR basic_string_view(
|
||||
const std::basic_string<Char, Alloc> &s) FMT_NOEXCEPT
|
||||
: data_(s.c_str()), size_(s.size()) {}
|
||||
: data_(s.data()), size_(s.size()) {}
|
||||
|
||||
FMT_CONSTEXPR basic_string_view(type s) FMT_NOEXCEPT
|
||||
: data_(s.data()), size_(s.size()) {}
|
||||
|
||||
/** Returns a pointer to the string data. */
|
||||
const Char *data() const { return data_; }
|
||||
FMT_CONSTEXPR const Char *data() const { return data_; }
|
||||
|
||||
/** Returns the string size. */
|
||||
FMT_CONSTEXPR size_t size() const { return size_; }
|
||||
@@ -319,9 +340,29 @@ class basic_format_arg;
|
||||
template <typename Context>
|
||||
class basic_format_args;
|
||||
|
||||
template <typename T>
|
||||
struct no_formatter_error : std::false_type {};
|
||||
|
||||
// A formatter for objects of type T.
|
||||
template <typename T, typename Char = char, typename Enable = void>
|
||||
struct formatter;
|
||||
struct formatter {
|
||||
static_assert(no_formatter_error<T>::value,
|
||||
"don't know how to format the type, include fmt/ostream.h if it provides "
|
||||
"an operator<< that should be used");
|
||||
|
||||
// The following functions are not defined intentionally.
|
||||
template <typename ParseContext>
|
||||
typename ParseContext::iterator parse(ParseContext &);
|
||||
template <typename FormatContext>
|
||||
auto format(const T &val, FormatContext &ctx) -> decltype(ctx.out());
|
||||
};
|
||||
|
||||
template <typename T, typename Char, typename Enable = void>
|
||||
struct convert_to_int {
|
||||
enum {
|
||||
value = !std::is_arithmetic<T>::value && std::is_convertible<T, int>::value
|
||||
};
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
|
||||
@@ -329,13 +370,17 @@ namespace internal {
|
||||
template <typename T>
|
||||
class basic_buffer {
|
||||
private:
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(basic_buffer);
|
||||
basic_buffer(const basic_buffer &) = delete;
|
||||
void operator=(const basic_buffer &) = delete;
|
||||
|
||||
T *ptr_;
|
||||
std::size_t size_;
|
||||
std::size_t capacity_;
|
||||
|
||||
protected:
|
||||
// Don't initialize ptr_ since it is not accessed to save a few cycles.
|
||||
basic_buffer(std::size_t sz) FMT_NOEXCEPT: size_(sz), capacity_(sz) {}
|
||||
|
||||
basic_buffer(T *p = FMT_NULL, std::size_t sz = 0, std::size_t cap = 0)
|
||||
FMT_NOEXCEPT: ptr_(p), size_(sz), capacity_(cap) {}
|
||||
|
||||
@@ -377,6 +422,9 @@ class basic_buffer {
|
||||
size_ = new_size;
|
||||
}
|
||||
|
||||
/** Clears this buffer. */
|
||||
void clear() { size_ = 0; }
|
||||
|
||||
/** Reserves space to store at least *capacity* elements. */
|
||||
void reserve(std::size_t new_capacity) {
|
||||
if (new_capacity > capacity_)
|
||||
@@ -413,8 +461,7 @@ class container_buffer : public basic_buffer<typename Container::value_type> {
|
||||
|
||||
public:
|
||||
explicit container_buffer(Container &c)
|
||||
: basic_buffer<typename Container::value_type>(&c[0], c.size(), c.size()),
|
||||
container_(c) {}
|
||||
: basic_buffer<typename Container::value_type>(c.size()), container_(c) {}
|
||||
};
|
||||
|
||||
struct error_handler {
|
||||
@@ -468,13 +515,6 @@ FMT_CONSTEXPR bool is_arithmetic(type t) {
|
||||
return t > internal::none_type && t <= internal::last_numeric_type;
|
||||
}
|
||||
|
||||
template <typename T, typename Char, bool ENABLE = true>
|
||||
struct convert_to_int {
|
||||
enum {
|
||||
value = !std::is_arithmetic<T>::value && std::is_convertible<T, int>::value
|
||||
};
|
||||
};
|
||||
|
||||
template <typename Char>
|
||||
struct string_value {
|
||||
const Char *value;
|
||||
@@ -554,12 +594,14 @@ class value {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Context, type TYPE>
|
||||
struct typed_value : value<Context> {
|
||||
// Value initializer used to delay conversion to value and reduce memory churn.
|
||||
template <typename Context, typename T, type TYPE>
|
||||
struct init {
|
||||
T val;
|
||||
static const type type_tag = TYPE;
|
||||
|
||||
template <typename T>
|
||||
FMT_CONSTEXPR typed_value(const T &val) : value<Context>(val) {}
|
||||
FMT_CONSTEXPR init(const T &v) : val(v) {}
|
||||
FMT_CONSTEXPR operator value<Context>() const { return value<Context>(val); }
|
||||
};
|
||||
|
||||
template <typename Context, typename T>
|
||||
@@ -567,15 +609,13 @@ FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T &value);
|
||||
|
||||
#define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \
|
||||
template <typename C> \
|
||||
FMT_CONSTEXPR typed_value<C, TAG> make_value(ArgType val) { \
|
||||
FMT_CONSTEXPR init<C, ValueType, TAG> make_value(ArgType val) { \
|
||||
return static_cast<ValueType>(val); \
|
||||
}
|
||||
|
||||
#define FMT_MAKE_VALUE_SAME(TAG, Type) \
|
||||
template <typename C> \
|
||||
FMT_CONSTEXPR typed_value<C, TAG> make_value(Type val) { \
|
||||
return val; \
|
||||
}
|
||||
FMT_CONSTEXPR init<C, Type, TAG> make_value(Type val) { return val; }
|
||||
|
||||
FMT_MAKE_VALUE(bool_type, bool, int)
|
||||
FMT_MAKE_VALUE(int_type, short, int)
|
||||
@@ -603,7 +643,7 @@ FMT_MAKE_VALUE(char_type, char, int)
|
||||
|
||||
#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
|
||||
template <typename C>
|
||||
inline typed_value<C, char_type> make_value(wchar_t val) {
|
||||
inline init<C, int, char_type> make_value(wchar_t val) {
|
||||
require_wchar<typename C::char_type>();
|
||||
return static_cast<int>(val);
|
||||
}
|
||||
@@ -650,20 +690,27 @@ typename std::enable_if<!std::is_same<T, typename C::char_type>::value>::type
|
||||
template <typename C, typename T>
|
||||
inline typename std::enable_if<
|
||||
std::is_enum<T>::value && convert_to_int<T, typename C::char_type>::value,
|
||||
typed_value<C, int_type>>::type
|
||||
init<C, int, int_type>>::type
|
||||
make_value(const T &val) { return static_cast<int>(val); }
|
||||
|
||||
template <typename C, typename T, typename Char = typename C::char_type>
|
||||
inline typename std::enable_if<
|
||||
std::is_constructible<basic_string_view<Char>, T>::value,
|
||||
init<C, basic_string_view<Char>, string_type>>::type
|
||||
make_value(const T &val) { return basic_string_view<Char>(val); }
|
||||
|
||||
template <typename C, typename T, typename Char = typename C::char_type>
|
||||
inline typename std::enable_if<
|
||||
!convert_to_int<T, Char>::value &&
|
||||
!std::is_convertible<T, basic_string_view<Char>>::value,
|
||||
!std::is_convertible<T, basic_string_view<Char>>::value &&
|
||||
!std::is_constructible<basic_string_view<Char>, T>::value,
|
||||
// Implicit conversion to std::string is not handled here because it's
|
||||
// unsafe: https://github.com/fmtlib/fmt/issues/729
|
||||
typed_value<C, custom_type>>::type
|
||||
init<C, const T &, custom_type>>::type
|
||||
make_value(const T &val) { return val; }
|
||||
|
||||
template <typename C, typename T>
|
||||
typed_value<C, named_arg_type>
|
||||
init<C, const void*, named_arg_type>
|
||||
make_value(const named_arg<T, typename C::char_type> &val) {
|
||||
basic_format_arg<C> arg = make_arg<C>(val.value);
|
||||
std::memcpy(val.data, &arg, sizeof(arg));
|
||||
@@ -675,17 +722,7 @@ enum { max_packed_args = 15 };
|
||||
|
||||
template <typename Context>
|
||||
class arg_map;
|
||||
|
||||
template <typename>
|
||||
struct result_of;
|
||||
|
||||
template <typename F, typename... Args>
|
||||
struct result_of<F(Args...)> {
|
||||
// A workaround for gcc 4.4 that doesn't allow F to be a reference.
|
||||
typedef typename std::result_of<
|
||||
typename std::remove_reference<F>::type(Args...)>::type type;
|
||||
};
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
// A formatting argument. It is a trivially copyable/constructible type to
|
||||
// allow storage in basic_memory_buffer.
|
||||
@@ -701,7 +738,7 @@ class basic_format_arg {
|
||||
|
||||
template <typename Visitor, typename Ctx>
|
||||
friend FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
|
||||
visit(Visitor &&vis, basic_format_arg<Ctx> arg);
|
||||
visit(Visitor &&vis, const basic_format_arg<Ctx> &arg);
|
||||
|
||||
friend class basic_format_args<Context>;
|
||||
friend class internal::arg_map<Context>;
|
||||
@@ -731,6 +768,54 @@ class basic_format_arg {
|
||||
bool is_arithmetic() const { return internal::is_arithmetic(type_); }
|
||||
};
|
||||
|
||||
struct monostate {};
|
||||
|
||||
/**
|
||||
\rst
|
||||
Visits an argument dispatching to the appropriate visit method based on
|
||||
the argument type. For example, if the argument type is ``double`` then
|
||||
``vis(value)`` will be called with the value of type ``double``.
|
||||
\endrst
|
||||
*/
|
||||
template <typename Visitor, typename Context>
|
||||
FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
|
||||
visit(Visitor &&vis, const basic_format_arg<Context> &arg) {
|
||||
typedef typename Context::char_type char_type;
|
||||
switch (arg.type_) {
|
||||
case internal::none_type:
|
||||
break;
|
||||
case internal::named_arg_type:
|
||||
FMT_ASSERT(false, "invalid argument type");
|
||||
break;
|
||||
case internal::int_type:
|
||||
return vis(arg.value_.int_value);
|
||||
case internal::uint_type:
|
||||
return vis(arg.value_.uint_value);
|
||||
case internal::long_long_type:
|
||||
return vis(arg.value_.long_long_value);
|
||||
case internal::ulong_long_type:
|
||||
return vis(arg.value_.ulong_long_value);
|
||||
case internal::bool_type:
|
||||
return vis(arg.value_.int_value != 0);
|
||||
case internal::char_type:
|
||||
return vis(static_cast<char_type>(arg.value_.int_value));
|
||||
case internal::double_type:
|
||||
return vis(arg.value_.double_value);
|
||||
case internal::long_double_type:
|
||||
return vis(arg.value_.long_double_value);
|
||||
case internal::cstring_type:
|
||||
return vis(arg.value_.string.value);
|
||||
case internal::string_type:
|
||||
return vis(basic_string_view<char_type>(
|
||||
arg.value_.string.value, arg.value_.string.size));
|
||||
case internal::pointer_type:
|
||||
return vis(arg.value_.pointer);
|
||||
case internal::custom_type:
|
||||
return vis(typename basic_format_arg<Context>::handle(arg.value_.custom));
|
||||
}
|
||||
return vis(monostate());
|
||||
}
|
||||
|
||||
// Parsing context consisting of a format string range being parsed and an
|
||||
// argument counter for automatic indexing.
|
||||
template <typename Char, typename ErrorHandler = internal::error_handler>
|
||||
@@ -789,7 +874,8 @@ namespace internal {
|
||||
template <typename Context>
|
||||
class arg_map {
|
||||
private:
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(arg_map);
|
||||
arg_map(const arg_map &) = delete;
|
||||
void operator=(const arg_map &) = delete;
|
||||
|
||||
typedef typename Context::char_type char_type;
|
||||
|
||||
@@ -904,7 +990,8 @@ class basic_format_context :
|
||||
private:
|
||||
internal::arg_map<basic_format_context> map_;
|
||||
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(basic_format_context);
|
||||
basic_format_context(const basic_format_context &) = delete;
|
||||
void operator=(const basic_format_context &) = delete;
|
||||
|
||||
typedef internal::context_base<OutputIt, basic_format_context, Char> base;
|
||||
typedef typename base::format_arg format_arg;
|
||||
@@ -948,10 +1035,10 @@ struct get_type {
|
||||
};
|
||||
|
||||
template <typename Context>
|
||||
FMT_CONSTEXPR unsigned long long get_types() { return 0; }
|
||||
FMT_CONSTEXPR11 unsigned long long get_types() { return 0; }
|
||||
|
||||
template <typename Context, typename Arg, typename... Args>
|
||||
FMT_CONSTEXPR unsigned long long get_types() {
|
||||
FMT_CONSTEXPR11 unsigned long long get_types() {
|
||||
return get_type<Context, Arg>::value | (get_types<Context, Args...>() << 4);
|
||||
}
|
||||
|
||||
@@ -974,7 +1061,7 @@ inline typename std::enable_if<!IS_PACKED, basic_format_arg<Context>>::type
|
||||
make_arg(const T &value) {
|
||||
return make_arg<Context>(value);
|
||||
}
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
/**
|
||||
\rst
|
||||
@@ -1001,15 +1088,15 @@ class format_arg_store {
|
||||
|
||||
friend class basic_format_args<Context>;
|
||||
|
||||
static FMT_CONSTEXPR long long get_types() {
|
||||
static FMT_CONSTEXPR11 long long get_types() {
|
||||
return IS_PACKED ?
|
||||
static_cast<long long>(internal::get_types<Context, Args...>()) :
|
||||
-static_cast<long long>(NUM_ARGS);
|
||||
}
|
||||
|
||||
public:
|
||||
#if FMT_USE_CONSTEXPR
|
||||
static constexpr long long TYPES = get_types();
|
||||
#if FMT_USE_CONSTEXPR11
|
||||
static FMT_CONSTEXPR11 long long TYPES = get_types();
|
||||
#else
|
||||
static const long long TYPES;
|
||||
#endif
|
||||
@@ -1028,7 +1115,7 @@ class format_arg_store {
|
||||
#endif
|
||||
};
|
||||
|
||||
#if !FMT_USE_CONSTEXPR
|
||||
#if !FMT_USE_CONSTEXPR11
|
||||
template <typename Context, typename ...Args>
|
||||
const long long format_arg_store<Context, Args...>::TYPES = get_types();
|
||||
#endif
|
||||
@@ -1036,8 +1123,8 @@ const long long format_arg_store<Context, Args...>::TYPES = get_types();
|
||||
/**
|
||||
\rst
|
||||
Constructs an `~fmt::format_arg_store` object that contains references to
|
||||
arguments and can be implicitly converted to `~fmt::format_args`. `Context` can
|
||||
be omitted in which case it defaults to `~fmt::context`.
|
||||
arguments and can be implicitly converted to `~fmt::format_args`. `Context`
|
||||
can be omitted in which case it defaults to `~fmt::context`.
|
||||
\endrst
|
||||
*/
|
||||
template <typename Context, typename ...Args>
|
||||
@@ -1086,12 +1173,15 @@ class basic_format_args {
|
||||
void set_data(const format_arg *args) { args_ = args; }
|
||||
|
||||
format_arg do_get(size_type index) const {
|
||||
format_arg arg;
|
||||
long long signed_types = static_cast<long long>(types_);
|
||||
if (signed_types < 0) {
|
||||
unsigned long long num_args = static_cast<unsigned long long>(-signed_types);
|
||||
return index < num_args ? args_[index] : format_arg();
|
||||
unsigned long long num_args =
|
||||
static_cast<unsigned long long>(-signed_types);
|
||||
if (index < num_args)
|
||||
arg = args_[index];
|
||||
return arg;
|
||||
}
|
||||
format_arg arg;
|
||||
if (index > internal::max_packed_args)
|
||||
return arg;
|
||||
arg.type_ = type(index);
|
||||
@@ -1116,11 +1206,22 @@ class basic_format_args {
|
||||
set_data(store.data_);
|
||||
}
|
||||
|
||||
/**
|
||||
\rst
|
||||
Constructs a `basic_format_args` object from a dynamic set of arguments.
|
||||
\endrst
|
||||
*/
|
||||
basic_format_args(const format_arg *args, size_type count)
|
||||
: types_(-static_cast<int64_t>(count)) {
|
||||
set_data(args);
|
||||
}
|
||||
|
||||
/** Returns the argument at specified index. */
|
||||
format_arg get(size_type index) const {
|
||||
format_arg arg = do_get(index);
|
||||
return arg.type_ == internal::named_arg_type ?
|
||||
arg.value_.as_named_arg().template deserialize<Context>() : arg;
|
||||
if (arg.type_ == internal::named_arg_type)
|
||||
arg = arg.value_.as_named_arg().template deserialize<Context>();
|
||||
return arg;
|
||||
}
|
||||
|
||||
unsigned max_size() const {
|
||||
@@ -1193,24 +1294,69 @@ inline internal::named_arg<T, wchar_t> arg(wstring_view name, const T &arg) {
|
||||
// This function template is deleted intentionally to disable nested named
|
||||
// arguments as in ``format("{}", arg("a", arg("b", 42)))``.
|
||||
template <typename S, typename T, typename Char>
|
||||
void arg(S, internal::named_arg<T, Char>) FMT_DELETED;
|
||||
void arg(S, internal::named_arg<T, Char>) = delete;
|
||||
|
||||
#ifndef FMT_EXTENDED_COLORS
|
||||
// color and (v)print_colored are deprecated.
|
||||
enum color { black, red, green, yellow, blue, magenta, cyan, white };
|
||||
FMT_API void vprint_colored(color c, string_view format, format_args args);
|
||||
FMT_API void vprint_colored(color c, wstring_view format, wformat_args args);
|
||||
template <typename... Args>
|
||||
inline void print_colored(color c, string_view format_str,
|
||||
const Args & ... args) {
|
||||
vprint_colored(c, format_str, make_format_args(args...));
|
||||
}
|
||||
template <typename... Args>
|
||||
inline void print_colored(color c, wstring_view format_str,
|
||||
const Args & ... args) {
|
||||
vprint_colored(c, format_str, make_format_args<wformat_context>(args...));
|
||||
}
|
||||
#endif
|
||||
// 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).
|
||||
struct compile_string {};
|
||||
|
||||
namespace internal {
|
||||
// If S is a format string type, format_string_traints<S>::char_type gives its
|
||||
// character type.
|
||||
template <typename S, typename Enable = void>
|
||||
struct format_string_traits {
|
||||
private:
|
||||
// Use constructability as a way to detect if format_string_traits is
|
||||
// specialized because other methods are broken on MSVC2013.
|
||||
format_string_traits();
|
||||
};
|
||||
|
||||
template <typename Char>
|
||||
struct format_string_traits_base { typedef Char char_type; };
|
||||
|
||||
template <typename Char>
|
||||
struct format_string_traits<Char *> : format_string_traits_base<Char> {};
|
||||
|
||||
template <typename Char>
|
||||
struct format_string_traits<const Char *> : format_string_traits_base<Char> {};
|
||||
|
||||
template <typename Char, std::size_t N>
|
||||
struct format_string_traits<Char[N]> : format_string_traits_base<Char> {};
|
||||
|
||||
template <typename Char, std::size_t N>
|
||||
struct format_string_traits<const Char[N]> : format_string_traits_base<Char> {};
|
||||
|
||||
template <typename Char>
|
||||
struct format_string_traits<std::basic_string<Char>> :
|
||||
format_string_traits_base<Char> {};
|
||||
|
||||
template <typename S>
|
||||
struct format_string_traits<
|
||||
S, typename std::enable_if<std::is_base_of<
|
||||
basic_string_view<typename S::char_type>, S>::value>::type> :
|
||||
format_string_traits_base<typename S::char_type> {};
|
||||
|
||||
template <typename S>
|
||||
struct is_format_string :
|
||||
std::integral_constant<
|
||||
bool, std::is_constructible<format_string_traits<S>>::value> {};
|
||||
|
||||
template <typename S>
|
||||
struct is_compile_string :
|
||||
std::integral_constant<bool, std::is_base_of<compile_string, S>::value> {};
|
||||
|
||||
template <typename... Args, typename S>
|
||||
inline typename std::enable_if<!is_compile_string<S>::value>::type
|
||||
check_format_string(const S &) {}
|
||||
template <typename... Args, typename S>
|
||||
typename std::enable_if<is_compile_string<S>::value>::type
|
||||
check_format_string(S);
|
||||
|
||||
template <typename Char>
|
||||
std::basic_string<Char> vformat(
|
||||
basic_string_view<Char> format_str,
|
||||
basic_format_args<typename buffer_context<Char>::type> args);
|
||||
} // namespace internal
|
||||
|
||||
format_context::iterator vformat_to(
|
||||
internal::buffer &buf, string_view format_str, format_args args);
|
||||
@@ -1232,10 +1378,9 @@ typename std::enable_if<
|
||||
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
||||
vformat_to(std::back_insert_iterator<Container> out,
|
||||
string_view format_str, format_args args) {
|
||||
auto& container = internal::get_container(out);
|
||||
internal::container_buffer<Container> buf(container);
|
||||
internal::container_buffer<Container> buf(internal::get_container(out));
|
||||
vformat_to(buf, format_str, args);
|
||||
return std::back_inserter(container);
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
@@ -1243,14 +1388,38 @@ typename std::enable_if<
|
||||
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
||||
vformat_to(std::back_insert_iterator<Container> out,
|
||||
wstring_view format_str, wformat_args args) {
|
||||
auto& container = internal::get_container(out);
|
||||
internal::container_buffer<Container> buf(container);
|
||||
internal::container_buffer<Container> buf(internal::get_container(out));
|
||||
vformat_to(buf, format_str, args);
|
||||
return std::back_inserter(container);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string vformat(string_view format_str, format_args args);
|
||||
std::wstring vformat(wstring_view format_str, wformat_args args);
|
||||
template <typename Container, typename... Args>
|
||||
inline typename std::enable_if<
|
||||
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
||||
format_to(std::back_insert_iterator<Container> out,
|
||||
string_view format_str, const Args & ... args) {
|
||||
format_arg_store<format_context, Args...> as{args...};
|
||||
return vformat_to(out, format_str, as);
|
||||
}
|
||||
|
||||
template <typename Container, typename... Args>
|
||||
inline typename std::enable_if<
|
||||
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
|
||||
format_to(std::back_insert_iterator<Container> out,
|
||||
wstring_view format_str, const Args & ... args) {
|
||||
return vformat_to(out, format_str,
|
||||
make_format_args<wformat_context>(args...));
|
||||
}
|
||||
|
||||
template <
|
||||
typename String,
|
||||
typename Char = typename internal::format_string_traits<String>::char_type>
|
||||
inline std::basic_string<Char> vformat(
|
||||
const String &format_str,
|
||||
basic_format_args<typename buffer_context<Char>::type> args) {
|
||||
// Convert format string to string_view to reduce the number of overloads.
|
||||
return internal::vformat(basic_string_view<Char>(format_str), args);
|
||||
}
|
||||
|
||||
/**
|
||||
\rst
|
||||
@@ -1262,18 +1431,19 @@ std::wstring vformat(wstring_view format_str, wformat_args args);
|
||||
std::string message = fmt::format("The answer is {}", 42);
|
||||
\endrst
|
||||
*/
|
||||
template <typename... Args>
|
||||
inline std::string format(string_view format_str, const Args & ... args) {
|
||||
template <typename String, typename... Args>
|
||||
inline std::basic_string<
|
||||
typename internal::format_string_traits<String>::char_type>
|
||||
format(const String &format_str, const Args & ... args) {
|
||||
internal::check_format_string<Args...>(format_str);
|
||||
// This should be just
|
||||
// return vformat(format_str, make_format_args(args...));
|
||||
// return vformat(format_str, make_format_args(args...));
|
||||
// but gcc has trouble optimizing the latter, so break it down.
|
||||
format_arg_store<format_context, Args...> as{args...};
|
||||
return vformat(format_str, as);
|
||||
}
|
||||
template <typename... Args>
|
||||
inline std::wstring format(wstring_view format_str, const Args & ... args) {
|
||||
format_arg_store<wformat_context, Args...> as{args...};
|
||||
return vformat(format_str, as);
|
||||
typedef typename internal::format_string_traits<String>::char_type char_t;
|
||||
typedef typename buffer_context<char_t>::type context_t;
|
||||
format_arg_store<context_t, Args...> as{args...};
|
||||
return internal::vformat(
|
||||
basic_string_view<char_t>(format_str), basic_format_args<context_t>(as));
|
||||
}
|
||||
|
||||
FMT_API void vprint(std::FILE *f, string_view format_str, format_args args);
|
||||
@@ -1294,11 +1464,12 @@ inline void print(std::FILE *f, string_view format_str, const Args & ... args) {
|
||||
vprint(f, format_str, as);
|
||||
}
|
||||
/**
|
||||
Prints formatted data to the file *f* which should be in wide-oriented mode set
|
||||
via ``fwide(f, 1)`` or ``_setmode(_fileno(f), _O_U8TEXT)`` on Windows.
|
||||
Prints formatted data to the file *f* which should be in wide-oriented mode
|
||||
set via ``fwide(f, 1)`` or ``_setmode(_fileno(f), _O_U8TEXT)`` on Windows.
|
||||
*/
|
||||
template <typename... Args>
|
||||
inline void print(std::FILE *f, wstring_view format_str, const Args & ... args) {
|
||||
inline void print(std::FILE *f, wstring_view format_str,
|
||||
const Args & ... args) {
|
||||
format_arg_store<wformat_context, Args...> as(args...);
|
||||
vprint(f, format_str, as);
|
||||
}
|
||||
|
||||
@@ -18,10 +18,9 @@
|
||||
#include <cmath>
|
||||
#include <cstdarg>
|
||||
#include <cstddef> // for std::ptrdiff_t
|
||||
#include <locale>
|
||||
|
||||
#if defined(_WIN32) && defined(__MINGW32__)
|
||||
# include <cstring>
|
||||
#include <cstring> // for std::memmove
|
||||
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
|
||||
# include <locale>
|
||||
#endif
|
||||
|
||||
#if FMT_USE_WINDOWS_H
|
||||
@@ -193,6 +192,7 @@ void report_error(FormatFunc func, int error_code,
|
||||
}
|
||||
} // namespace
|
||||
|
||||
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
|
||||
class locale {
|
||||
private:
|
||||
std::locale locale_;
|
||||
@@ -202,11 +202,27 @@ class locale {
|
||||
std::locale get() { return locale_; }
|
||||
};
|
||||
|
||||
FMT_FUNC size_t internal::count_code_points(u8string_view s) {
|
||||
const char8_t *data = s.data();
|
||||
int num_code_points = 0;
|
||||
for (size_t i = 0, size = s.size(); i != size; ++i) {
|
||||
if ((data[i].value & 0xc0) != 0x80)
|
||||
++num_code_points;
|
||||
}
|
||||
return num_code_points;
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
FMT_FUNC Char internal::thousands_sep(locale_provider *lp) {
|
||||
std::locale loc = lp ? lp->locale().get() : std::locale();
|
||||
return std::use_facet<std::numpunct<Char>>(loc).thousands_sep();
|
||||
}
|
||||
#else
|
||||
template <typename Char>
|
||||
FMT_FUNC Char internal::thousands_sep(locale_provider *lp) {
|
||||
return FMT_STATIC_THOUSANDS_SEPARATOR;
|
||||
}
|
||||
#endif
|
||||
|
||||
FMT_FUNC void system_error::init(
|
||||
int err_code, string_view format_str, format_args args) {
|
||||
@@ -256,11 +272,16 @@ const char basic_data<T>::DIGITS[] =
|
||||
|
||||
template <typename T>
|
||||
const uint32_t basic_data<T>::POWERS_OF_10_32[] = {
|
||||
1, FMT_POWERS_OF_10(1)
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
const uint32_t basic_data<T>::ZERO_OR_POWERS_OF_10_32[] = {
|
||||
0, FMT_POWERS_OF_10(1)
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
const uint64_t basic_data<T>::POWERS_OF_10_64[] = {
|
||||
const uint64_t basic_data<T>::ZERO_OR_POWERS_OF_10_64[] = {
|
||||
0,
|
||||
FMT_POWERS_OF_10(1),
|
||||
FMT_POWERS_OF_10(1000000000ull),
|
||||
@@ -271,28 +292,35 @@ const uint64_t basic_data<T>::POWERS_OF_10_64[] = {
|
||||
// These are generated by support/compute-powers.py.
|
||||
template <typename T>
|
||||
const uint64_t basic_data<T>::POW10_SIGNIFICANDS[] = {
|
||||
0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76, 0x8b16fb203055ac76, 0xcf42894a5dce35ea,
|
||||
0x9a6bb0aa55653b2d, 0xe61acf033d1a45df, 0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f,
|
||||
0xbe5691ef416bd60c, 0x8dd01fad907ffc3c, 0xd3515c2831559a83, 0x9d71ac8fada6c9b5,
|
||||
0xea9c227723ee8bcb, 0xaecc49914078536d, 0x823c12795db6ce57, 0xc21094364dfb5637,
|
||||
0x9096ea6f3848984f, 0xd77485cb25823ac7, 0xa086cfcd97bf97f4, 0xef340a98172aace5,
|
||||
0xb23867fb2a35b28e, 0x84c8d4dfd2c63f3b, 0xc5dd44271ad3cdba, 0x936b9fcebb25c996,
|
||||
0xdbac6c247d62a584, 0xa3ab66580d5fdaf6, 0xf3e2f893dec3f126, 0xb5b5ada8aaff80b8,
|
||||
0x87625f056c7c4a8b, 0xc9bcff6034c13053, 0x964e858c91ba2655, 0xdff9772470297ebd,
|
||||
0xa6dfbd9fb8e5b88f, 0xf8a95fcf88747d94, 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b,
|
||||
0xcdb02555653131b6, 0x993fe2c6d07b7fac, 0xe45c10c42a2b3b06, 0xaa242499697392d3,
|
||||
0xfd87b5f28300ca0e, 0xbce5086492111aeb, 0x8cbccc096f5088cc, 0xd1b71758e219652c,
|
||||
0x9c40000000000000, 0xe8d4a51000000000, 0xad78ebc5ac620000, 0x813f3978f8940984,
|
||||
0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70, 0xd5d238a4abe98068, 0x9f4f2726179a2245,
|
||||
0xed63a231d4c4fb27, 0xb0de65388cc8ada8, 0x83c7088e1aab65db, 0xc45d1df942711d9a,
|
||||
0x924d692ca61be758, 0xda01ee641a708dea, 0xa26da3999aef774a, 0xf209787bb47d6b85,
|
||||
0xb454e4a179dd1877, 0x865b86925b9bc5c2, 0xc83553c5c8965d3d, 0x952ab45cfa97a0b3,
|
||||
0xde469fbd99a05fe3, 0xa59bc234db398c25, 0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece,
|
||||
0x88fcf317f22241e2, 0xcc20ce9bd35c78a5, 0x98165af37b2153df, 0xe2a0b5dc971f303a,
|
||||
0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c, 0xbb764c4ca7a44410, 0x8bab8eefb6409c1a,
|
||||
0xd01fef10a657842c, 0x9b10a4e5e9913129, 0xe7109bfba19c0c9d, 0xac2820d9623bf429,
|
||||
0x80444b5e7aa7cf85, 0xbf21e44003acdd2d, 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841,
|
||||
0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9, 0xaf87023b9bf0ee6b
|
||||
0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76, 0x8b16fb203055ac76,
|
||||
0xcf42894a5dce35ea, 0x9a6bb0aa55653b2d, 0xe61acf033d1a45df,
|
||||
0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f, 0xbe5691ef416bd60c,
|
||||
0x8dd01fad907ffc3c, 0xd3515c2831559a83, 0x9d71ac8fada6c9b5,
|
||||
0xea9c227723ee8bcb, 0xaecc49914078536d, 0x823c12795db6ce57,
|
||||
0xc21094364dfb5637, 0x9096ea6f3848984f, 0xd77485cb25823ac7,
|
||||
0xa086cfcd97bf97f4, 0xef340a98172aace5, 0xb23867fb2a35b28e,
|
||||
0x84c8d4dfd2c63f3b, 0xc5dd44271ad3cdba, 0x936b9fcebb25c996,
|
||||
0xdbac6c247d62a584, 0xa3ab66580d5fdaf6, 0xf3e2f893dec3f126,
|
||||
0xb5b5ada8aaff80b8, 0x87625f056c7c4a8b, 0xc9bcff6034c13053,
|
||||
0x964e858c91ba2655, 0xdff9772470297ebd, 0xa6dfbd9fb8e5b88f,
|
||||
0xf8a95fcf88747d94, 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b,
|
||||
0xcdb02555653131b6, 0x993fe2c6d07b7fac, 0xe45c10c42a2b3b06,
|
||||
0xaa242499697392d3, 0xfd87b5f28300ca0e, 0xbce5086492111aeb,
|
||||
0x8cbccc096f5088cc, 0xd1b71758e219652c, 0x9c40000000000000,
|
||||
0xe8d4a51000000000, 0xad78ebc5ac620000, 0x813f3978f8940984,
|
||||
0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70, 0xd5d238a4abe98068,
|
||||
0x9f4f2726179a2245, 0xed63a231d4c4fb27, 0xb0de65388cc8ada8,
|
||||
0x83c7088e1aab65db, 0xc45d1df942711d9a, 0x924d692ca61be758,
|
||||
0xda01ee641a708dea, 0xa26da3999aef774a, 0xf209787bb47d6b85,
|
||||
0xb454e4a179dd1877, 0x865b86925b9bc5c2, 0xc83553c5c8965d3d,
|
||||
0x952ab45cfa97a0b3, 0xde469fbd99a05fe3, 0xa59bc234db398c25,
|
||||
0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece, 0x88fcf317f22241e2,
|
||||
0xcc20ce9bd35c78a5, 0x98165af37b2153df, 0xe2a0b5dc971f303a,
|
||||
0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c, 0xbb764c4ca7a44410,
|
||||
0x8bab8eefb6409c1a, 0xd01fef10a657842c, 0x9b10a4e5e9913129,
|
||||
0xe7109bfba19c0c9d, 0xac2820d9623bf429, 0x80444b5e7aa7cf85,
|
||||
0xbf21e44003acdd2d, 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841,
|
||||
0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9, 0xaf87023b9bf0ee6b,
|
||||
};
|
||||
|
||||
// Binary exponents of pow(10, k), for k = -348, -340, ..., 340, corresponding
|
||||
@@ -312,6 +340,96 @@ const int16_t basic_data<T>::POW10_EXPONENTS[] = {
|
||||
template <typename T> const char basic_data<T>::RESET_COLOR[] = "\x1b[0m";
|
||||
template <typename T> const wchar_t basic_data<T>::WRESET_COLOR[] = L"\x1b[0m";
|
||||
|
||||
// A handmade floating-point number f * pow(2, e).
|
||||
class fp {
|
||||
private:
|
||||
typedef uint64_t significand_type;
|
||||
|
||||
// All sizes are in bits.
|
||||
static FMT_CONSTEXPR_DECL const int char_size =
|
||||
std::numeric_limits<unsigned char>::digits;
|
||||
// Subtract 1 to account for an implicit most significant bit in the
|
||||
// normalized form.
|
||||
static FMT_CONSTEXPR_DECL const int double_significand_size =
|
||||
std::numeric_limits<double>::digits - 1;
|
||||
static FMT_CONSTEXPR_DECL const uint64_t implicit_bit =
|
||||
1ull << double_significand_size;
|
||||
|
||||
public:
|
||||
significand_type f;
|
||||
int e;
|
||||
|
||||
static FMT_CONSTEXPR_DECL const int significand_size =
|
||||
sizeof(significand_type) * char_size;
|
||||
|
||||
fp(): f(0), e(0) {}
|
||||
fp(uint64_t f, int e): f(f), e(e) {}
|
||||
|
||||
// Constructs fp from an IEEE754 double. It is a template to prevent compile
|
||||
// errors on platforms where double is not IEEE754.
|
||||
template <typename Double>
|
||||
explicit fp(Double d) {
|
||||
// Assume double is in the format [sign][exponent][significand].
|
||||
typedef std::numeric_limits<Double> limits;
|
||||
const int double_size = static_cast<int>(sizeof(Double) * char_size);
|
||||
const int exponent_size =
|
||||
double_size - double_significand_size - 1; // -1 for sign
|
||||
const uint64_t significand_mask = implicit_bit - 1;
|
||||
const uint64_t exponent_mask = (~0ull >> 1) & ~significand_mask;
|
||||
const int exponent_bias = (1 << exponent_size) - limits::max_exponent - 1;
|
||||
auto u = bit_cast<uint64_t>(d);
|
||||
auto biased_e = (u & exponent_mask) >> double_significand_size;
|
||||
f = u & significand_mask;
|
||||
if (biased_e != 0)
|
||||
f += implicit_bit;
|
||||
else
|
||||
biased_e = 1; // Subnormals use biased exponent 1 (min exponent).
|
||||
e = static_cast<int>(biased_e - exponent_bias - double_significand_size);
|
||||
}
|
||||
|
||||
// Normalizes the value converted from double and multiplied by (1 << SHIFT).
|
||||
template <int SHIFT = 0>
|
||||
void normalize() {
|
||||
// Handle subnormals.
|
||||
auto shifted_implicit_bit = implicit_bit << SHIFT;
|
||||
while ((f & shifted_implicit_bit) == 0) {
|
||||
f <<= 1;
|
||||
--e;
|
||||
}
|
||||
// Subtract 1 to account for hidden bit.
|
||||
auto offset = significand_size - double_significand_size - SHIFT - 1;
|
||||
f <<= offset;
|
||||
e -= offset;
|
||||
}
|
||||
|
||||
// Compute lower and upper boundaries (m^- and m^+ in the Grisu paper), where
|
||||
// a boundary is a value half way between the number and its predecessor
|
||||
// (lower) or successor (upper). The upper boundary is normalized and lower
|
||||
// has the same exponent but may be not normalized.
|
||||
void compute_boundaries(fp &lower, fp &upper) const {
|
||||
lower = f == implicit_bit ?
|
||||
fp((f << 2) - 1, e - 2) : fp((f << 1) - 1, e - 1);
|
||||
upper = fp((f << 1) + 1, e - 1);
|
||||
upper.normalize<1>(); // 1 is to account for the exponent shift above.
|
||||
lower.f <<= lower.e - upper.e;
|
||||
lower.e = upper.e;
|
||||
}
|
||||
};
|
||||
|
||||
// Returns an fp number representing x - y. Result may not be normalized.
|
||||
inline fp operator-(fp x, fp y) {
|
||||
FMT_ASSERT(x.f >= y.f && x.e == y.e, "invalid operands");
|
||||
return fp(x.f - y.f, x.e);
|
||||
}
|
||||
|
||||
// Computes an fp number r with r.f = x.f * y.f / pow(2, 64) rounded to nearest
|
||||
// with half-up tie breaking, r.e = x.e + y.e + 64. Result may not be normalized.
|
||||
FMT_API fp operator*(fp x, fp y);
|
||||
|
||||
// Returns cached power (of 10) c_k = c_k.f * pow(2, c_k.e) such that its
|
||||
// (binary) exponent satisfies min_exponent <= c_k.e <= min_exponent + 3.
|
||||
FMT_API fp get_cached_power(int min_exponent, int &pow10_exponent);
|
||||
|
||||
FMT_FUNC fp operator*(fp x, fp y) {
|
||||
// Multiply 32-bit parts of significands.
|
||||
uint64_t mask = (1ULL << 32) - 1;
|
||||
@@ -329,12 +447,248 @@ FMT_FUNC fp get_cached_power(int min_exponent, int &pow10_exponent) {
|
||||
(min_exponent + fp::significand_size - 1) * one_over_log2_10));
|
||||
// Decimal exponent of the first (smallest) cached power of 10.
|
||||
const int first_dec_exp = -348;
|
||||
// Difference between two consecutive decimal exponents in cached powers of 10.
|
||||
// Difference between 2 consecutive decimal exponents in cached powers of 10.
|
||||
const int dec_exp_step = 8;
|
||||
index = (index - first_dec_exp - 1) / dec_exp_step + 1;
|
||||
pow10_exponent = first_dec_exp + index * dec_exp_step;
|
||||
return fp(data::POW10_SIGNIFICANDS[index], data::POW10_EXPONENTS[index]);
|
||||
}
|
||||
|
||||
// Generates output using Grisu2 digit-gen algorithm.
|
||||
FMT_FUNC void grisu2_gen_digits(
|
||||
const fp &scaled_value, const fp &scaled_upper, uint64_t delta,
|
||||
char *buffer, size_t &size, int &dec_exp) {
|
||||
internal::fp one(1ull << -scaled_upper.e, scaled_upper.e);
|
||||
// hi (p1 in Grisu) contains the most significant digits of scaled_upper.
|
||||
// hi = floor(scaled_upper / one).
|
||||
uint32_t hi = static_cast<uint32_t>(scaled_upper.f >> -one.e);
|
||||
// lo (p2 in Grisu) contains the least significants digits of scaled_upper.
|
||||
// lo = scaled_upper mod 1.
|
||||
uint64_t lo = scaled_upper.f & (one.f - 1);
|
||||
size = 0;
|
||||
auto exp = count_digits(hi); // kappa in Grisu.
|
||||
while (exp > 0) {
|
||||
uint32_t digit = 0;
|
||||
// This optimization by miloyip reduces the number of integer divisions by
|
||||
// one per iteration.
|
||||
switch (exp) {
|
||||
case 10: digit = hi / 1000000000; hi %= 1000000000; break;
|
||||
case 9: digit = hi / 100000000; hi %= 100000000; break;
|
||||
case 8: digit = hi / 10000000; hi %= 10000000; break;
|
||||
case 7: digit = hi / 1000000; hi %= 1000000; break;
|
||||
case 6: digit = hi / 100000; hi %= 100000; break;
|
||||
case 5: digit = hi / 10000; hi %= 10000; break;
|
||||
case 4: digit = hi / 1000; hi %= 1000; break;
|
||||
case 3: digit = hi / 100; hi %= 100; break;
|
||||
case 2: digit = hi / 10; hi %= 10; break;
|
||||
case 1: digit = hi; hi = 0; break;
|
||||
default:
|
||||
FMT_ASSERT(false, "invalid number of digits");
|
||||
}
|
||||
if (digit != 0 || size != 0)
|
||||
buffer[size++] = static_cast<char>('0' + digit);
|
||||
--exp;
|
||||
uint64_t remainder = (static_cast<uint64_t>(hi) << -one.e) + lo;
|
||||
if (remainder <= delta) {
|
||||
dec_exp += exp;
|
||||
// TODO: use scaled_value
|
||||
(void)scaled_value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (;;) {
|
||||
lo *= 10;
|
||||
delta *= 10;
|
||||
char digit = static_cast<char>(lo >> -one.e);
|
||||
if (digit != 0 || size != 0)
|
||||
buffer[size++] = static_cast<char>('0' + digit);
|
||||
lo &= one.f - 1;
|
||||
--exp;
|
||||
if (lo < delta) {
|
||||
dec_exp += exp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FMT_FUNC void grisu2_format_positive(double value, char *buffer, size_t &size,
|
||||
int &dec_exp) {
|
||||
FMT_ASSERT(value > 0, "value is nonpositive");
|
||||
fp fp_value(value);
|
||||
fp lower, upper; // w^- and w^+ in the Grisu paper.
|
||||
fp_value.compute_boundaries(lower, upper);
|
||||
// Find a cached power of 10 close to 1 / upper.
|
||||
const int min_exp = -60; // alpha in Grisu.
|
||||
auto dec_pow = get_cached_power( // \tilde{c}_{-k} in Grisu.
|
||||
min_exp - (upper.e + fp::significand_size), dec_exp);
|
||||
dec_exp = -dec_exp;
|
||||
fp_value.normalize();
|
||||
fp scaled_value = fp_value * dec_pow;
|
||||
fp scaled_lower = lower * dec_pow; // \tilde{M}^- in Grisu.
|
||||
fp scaled_upper = upper * dec_pow; // \tilde{M}^+ in Grisu.
|
||||
++scaled_lower.f; // \tilde{M}^- + 1 ulp -> M^-_{\uparrow}.
|
||||
--scaled_upper.f; // \tilde{M}^+ - 1 ulp -> M^+_{\downarrow}.
|
||||
uint64_t delta = scaled_upper.f - scaled_lower.f;
|
||||
grisu2_gen_digits(scaled_value, scaled_upper, delta, buffer, size, dec_exp);
|
||||
}
|
||||
|
||||
FMT_FUNC void round(char *buffer, size_t &size, int &exp,
|
||||
int digits_to_remove) {
|
||||
size -= to_unsigned(digits_to_remove);
|
||||
exp += digits_to_remove;
|
||||
int digit = buffer[size] - '0';
|
||||
// TODO: proper rounding and carry
|
||||
if (digit > 5 || (digit == 5 && (digits_to_remove > 1 ||
|
||||
(buffer[size - 1] - '0') % 2) != 0)) {
|
||||
++buffer[size - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// Writes the exponent exp in the form "[+-]d{1,3}" to buffer.
|
||||
FMT_FUNC char *write_exponent(char *buffer, int exp) {
|
||||
FMT_ASSERT(-1000 < exp && exp < 1000, "exponent out of range");
|
||||
if (exp < 0) {
|
||||
*buffer++ = '-';
|
||||
exp = -exp;
|
||||
} else {
|
||||
*buffer++ = '+';
|
||||
}
|
||||
if (exp >= 100) {
|
||||
*buffer++ = static_cast<char>('0' + exp / 100);
|
||||
exp %= 100;
|
||||
const char *d = data::DIGITS + exp * 2;
|
||||
*buffer++ = d[0];
|
||||
*buffer++ = d[1];
|
||||
} else {
|
||||
const char *d = data::DIGITS + exp * 2;
|
||||
*buffer++ = d[0];
|
||||
*buffer++ = d[1];
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
FMT_FUNC void format_exp_notation(
|
||||
char *buffer, size_t &size, int exp, int precision, bool upper) {
|
||||
// Insert a decimal point after the first digit and add an exponent.
|
||||
std::memmove(buffer + 2, buffer + 1, size - 1);
|
||||
buffer[1] = '.';
|
||||
exp += static_cast<int>(size) - 1;
|
||||
int num_digits = precision - static_cast<int>(size) + 1;
|
||||
if (num_digits > 0) {
|
||||
std::uninitialized_fill_n(buffer + size + 1, num_digits, '0');
|
||||
size += to_unsigned(num_digits);
|
||||
} else if (num_digits < 0) {
|
||||
round(buffer, size, exp, -num_digits);
|
||||
}
|
||||
char *p = buffer + size + 1;
|
||||
*p++ = upper ? 'E' : 'e';
|
||||
size = to_unsigned(write_exponent(p, exp) - buffer);
|
||||
}
|
||||
|
||||
// Prettifies the output of the Grisu2 algorithm.
|
||||
// The number is given as v = buffer * 10^exp.
|
||||
FMT_FUNC void grisu2_prettify(char *buffer, size_t &size, int exp,
|
||||
int precision, bool upper) {
|
||||
// pow(10, full_exp - 1) <= v <= pow(10, full_exp).
|
||||
int int_size = static_cast<int>(size);
|
||||
int full_exp = int_size + exp;
|
||||
const int exp_threshold = 21;
|
||||
if (int_size <= full_exp && full_exp <= exp_threshold) {
|
||||
// 1234e7 -> 12340000000[.0+]
|
||||
std::uninitialized_fill_n(buffer + int_size, full_exp - int_size, '0');
|
||||
char *p = buffer + full_exp;
|
||||
if (precision > 0) {
|
||||
*p++ = '.';
|
||||
std::uninitialized_fill_n(p, precision, '0');
|
||||
p += precision;
|
||||
}
|
||||
size = to_unsigned(p - buffer);
|
||||
} else if (0 < full_exp && full_exp <= exp_threshold) {
|
||||
// 1234e-2 -> 12.34[0+]
|
||||
int fractional_size = -exp;
|
||||
std::memmove(buffer + full_exp + 1, buffer + full_exp,
|
||||
to_unsigned(fractional_size));
|
||||
buffer[full_exp] = '.';
|
||||
int num_zeros = precision - fractional_size;
|
||||
if (num_zeros > 0) {
|
||||
std::uninitialized_fill_n(buffer + size + 1, num_zeros, '0');
|
||||
size += to_unsigned(num_zeros);
|
||||
}
|
||||
++size;
|
||||
} else if (-6 < full_exp && full_exp <= 0) {
|
||||
// 1234e-6 -> 0.001234
|
||||
int offset = 2 - full_exp;
|
||||
std::memmove(buffer + offset, buffer, size);
|
||||
buffer[0] = '0';
|
||||
buffer[1] = '.';
|
||||
std::uninitialized_fill_n(buffer + 2, -full_exp, '0');
|
||||
size = to_unsigned(int_size + offset);
|
||||
} else {
|
||||
format_exp_notation(buffer, size, exp, precision, upper);
|
||||
}
|
||||
}
|
||||
|
||||
#if FMT_CLANG_VERSION
|
||||
# define FMT_FALLTHROUGH [[clang::fallthrough]];
|
||||
#elif FMT_GCC_VERSION >= 700
|
||||
# define FMT_FALLTHROUGH [[gnu::fallthrough]];
|
||||
#else
|
||||
# define FMT_FALLTHROUGH
|
||||
#endif
|
||||
|
||||
// Formats a nonnegative value using Grisu2 algorithm. Grisu2 doesn't give any
|
||||
// guarantees on the shortness of the result.
|
||||
FMT_FUNC void grisu2_format(double value, char *buffer, size_t &size, char type,
|
||||
int precision, bool write_decimal_point) {
|
||||
FMT_ASSERT(value >= 0, "value is negative");
|
||||
int dec_exp = 0; // K in Grisu.
|
||||
if (value > 0) {
|
||||
grisu2_format_positive(value, buffer, size, dec_exp);
|
||||
} else {
|
||||
*buffer = '0';
|
||||
size = 1;
|
||||
}
|
||||
const int default_precision = 6;
|
||||
if (precision < 0)
|
||||
precision = default_precision;
|
||||
bool upper = false;
|
||||
switch (type) {
|
||||
case 'G':
|
||||
upper = true;
|
||||
FMT_FALLTHROUGH
|
||||
case '\0': case 'g': {
|
||||
int digits_to_remove = static_cast<int>(size) - precision;
|
||||
if (digits_to_remove > 0) {
|
||||
round(buffer, size, dec_exp, digits_to_remove);
|
||||
// Remove trailing zeros.
|
||||
while (size > 0 && buffer[size - 1] == '0') {
|
||||
--size;
|
||||
++dec_exp;
|
||||
}
|
||||
}
|
||||
precision = 0;
|
||||
break;
|
||||
}
|
||||
case 'F':
|
||||
upper = true;
|
||||
FMT_FALLTHROUGH
|
||||
case 'f': {
|
||||
int digits_to_remove = -dec_exp - precision;
|
||||
if (digits_to_remove > 0) {
|
||||
if (digits_to_remove >= static_cast<int>(size))
|
||||
digits_to_remove = static_cast<int>(size) - 1;
|
||||
round(buffer, size, dec_exp, digits_to_remove);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'e': case 'E':
|
||||
format_exp_notation(buffer, size, dec_exp, precision, type == 'E');
|
||||
return;
|
||||
}
|
||||
if (write_decimal_point && precision < 1)
|
||||
precision = 1;
|
||||
grisu2_prettify(buffer, size, dec_exp, precision, upper);
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
#if FMT_USE_WINDOWS_H
|
||||
@@ -499,61 +853,9 @@ FMT_FUNC void vprint(wstring_view format_str, wformat_args args) {
|
||||
vprint(stdout, format_str, args);
|
||||
}
|
||||
|
||||
#ifndef FMT_EXTENDED_COLORS
|
||||
FMT_FUNC void vprint_colored(color c, string_view format, format_args args) {
|
||||
char escape[] = "\x1b[30m";
|
||||
escape[3] = static_cast<char>('0' + c);
|
||||
std::fputs(escape, stdout);
|
||||
vprint(format, args);
|
||||
std::fputs(internal::data::RESET_COLOR, stdout);
|
||||
}
|
||||
|
||||
FMT_FUNC void vprint_colored(color c, wstring_view format, wformat_args args) {
|
||||
wchar_t escape[] = L"\x1b[30m";
|
||||
escape[3] = static_cast<wchar_t>('0' + c);
|
||||
std::fputws(escape, stdout);
|
||||
vprint(format, args);
|
||||
std::fputws(internal::data::WRESET_COLOR, stdout);
|
||||
}
|
||||
#else
|
||||
namespace internal {
|
||||
FMT_CONSTEXPR void to_esc(uint8_t c, char out[], int offset) {
|
||||
out[offset + 0] = static_cast<char>('0' + c / 100);
|
||||
out[offset + 1] = static_cast<char>('0' + c / 10 % 10);
|
||||
out[offset + 2] = static_cast<char>('0' + c % 10);
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) {
|
||||
char escape_fd[] = "\x1b[38;2;000;000;000m";
|
||||
internal::to_esc(fd.r, escape_fd, 7);
|
||||
internal::to_esc(fd.g, escape_fd, 11);
|
||||
internal::to_esc(fd.b, escape_fd, 15);
|
||||
|
||||
std::fputs(escape_fd, stdout);
|
||||
vprint(format, args);
|
||||
std::fputs(internal::data::RESET_COLOR, stdout);
|
||||
}
|
||||
|
||||
FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) {
|
||||
char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color
|
||||
char escape_bg[] = "\x1b[48;2;000;000;000m"; // background color
|
||||
internal::to_esc(fd.r, escape_fd, 7);
|
||||
internal::to_esc(fd.g, escape_fd, 11);
|
||||
internal::to_esc(fd.b, escape_fd, 15);
|
||||
|
||||
internal::to_esc(bg.r, escape_bg, 7);
|
||||
internal::to_esc(bg.g, escape_bg, 11);
|
||||
internal::to_esc(bg.b, escape_bg, 15);
|
||||
|
||||
std::fputs(escape_fd, stdout);
|
||||
std::fputs(escape_bg, stdout);
|
||||
vprint(format, args);
|
||||
std::fputs(internal::data::RESET_COLOR, stdout);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
|
||||
FMT_FUNC locale locale_provider::locale() { return fmt::locale(); }
|
||||
#endif
|
||||
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -68,18 +68,7 @@ class is_streamable {
|
||||
typedef decltype(test<T>(0)) result;
|
||||
|
||||
public:
|
||||
// std::string operator<< is not considered user-defined because we handle strings
|
||||
// specially.
|
||||
static const bool value = result::value && !std::is_same<T, std::string>::value;
|
||||
};
|
||||
|
||||
// Disable conversion to int if T has an overloaded operator<< which is a free
|
||||
// function (not a member of std::ostream).
|
||||
template <typename T, typename Char>
|
||||
class convert_to_int<T, Char, true> {
|
||||
public:
|
||||
static const bool value =
|
||||
convert_to_int<T, Char, false>::value && !is_streamable<T, Char>::value;
|
||||
static const bool value = result::value;
|
||||
};
|
||||
|
||||
// Write the content of buf to os.
|
||||
@@ -106,17 +95,24 @@ void format_value(basic_buffer<Char> &buffer, const T &value) {
|
||||
output << value;
|
||||
buffer.resize(buffer.size());
|
||||
}
|
||||
|
||||
// Disable builtin formatting of enums and use operator<< instead.
|
||||
template <typename T>
|
||||
struct format_enum<T,
|
||||
typename std::enable_if<std::is_enum<T>::value>::type> : std::false_type {};
|
||||
} // namespace internal
|
||||
|
||||
// Disable conversion to int if T has an overloaded operator<< which is a free
|
||||
// function (not a member of std::ostream).
|
||||
template <typename T, typename Char>
|
||||
struct convert_to_int<T, Char, void> {
|
||||
static const bool value =
|
||||
convert_to_int<T, Char, int>::value &&
|
||||
!internal::is_streamable<T, Char>::value;
|
||||
};
|
||||
|
||||
// Formats an object of type T that has an overloaded ostream operator<<.
|
||||
template <typename T, typename Char>
|
||||
struct formatter<T, Char,
|
||||
typename std::enable_if<internal::is_streamable<T, Char>::value>::type>
|
||||
typename std::enable_if<
|
||||
internal::is_streamable<T, Char>::value &&
|
||||
!internal::format_type<
|
||||
typename buffer_context<Char>::type, T>::value>::type>
|
||||
: formatter<basic_string_view<Char>, Char> {
|
||||
|
||||
template <typename Context>
|
||||
@@ -124,8 +120,7 @@ struct formatter<T, Char,
|
||||
basic_memory_buffer<Char> buffer;
|
||||
internal::format_value(buffer, value);
|
||||
basic_string_view<Char> str(buffer.data(), buffer.size());
|
||||
formatter<basic_string_view<Char>, Char>::format(str, ctx);
|
||||
return ctx.out();
|
||||
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -139,52 +139,10 @@ class buffered_file {
|
||||
// Destroys the object closing the file it represents if any.
|
||||
FMT_API ~buffered_file() FMT_DTOR_NOEXCEPT;
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
// Emulate a move constructor and a move assignment operator if rvalue
|
||||
// references are not supported.
|
||||
|
||||
private:
|
||||
// A proxy object to emulate a move constructor.
|
||||
// It is private to make it impossible call operator Proxy directly.
|
||||
struct Proxy {
|
||||
FILE *file;
|
||||
};
|
||||
buffered_file(const buffered_file &) = delete;
|
||||
void operator=(const buffered_file &) = delete;
|
||||
|
||||
public:
|
||||
// A "move constructor" for moving from a temporary.
|
||||
buffered_file(Proxy p) FMT_NOEXCEPT : file_(p.file) {}
|
||||
|
||||
// A "move constructor" for moving from an lvalue.
|
||||
buffered_file(buffered_file &f) FMT_NOEXCEPT : file_(f.file_) {
|
||||
f.file_ = FMT_NULL;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from a temporary.
|
||||
buffered_file &operator=(Proxy p) {
|
||||
close();
|
||||
file_ = p.file;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from an lvalue.
|
||||
buffered_file &operator=(buffered_file &other) {
|
||||
close();
|
||||
file_ = other.file_;
|
||||
other.file_ = FMT_NULL;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Returns a proxy object for moving from a temporary:
|
||||
// buffered_file file = buffered_file(...);
|
||||
operator Proxy() FMT_NOEXCEPT {
|
||||
Proxy p = {file_};
|
||||
file_ = FMT_NULL;
|
||||
return p;
|
||||
}
|
||||
|
||||
#else
|
||||
private:
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(buffered_file);
|
||||
|
||||
public:
|
||||
buffered_file(buffered_file &&other) FMT_NOEXCEPT : file_(other.file_) {
|
||||
@@ -197,7 +155,6 @@ public:
|
||||
other.file_ = FMT_NULL;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Opens a file.
|
||||
FMT_API buffered_file(cstring_view filename, cstring_view mode);
|
||||
@@ -249,52 +206,9 @@ class file {
|
||||
// Opens a file and constructs a file object representing this file.
|
||||
FMT_API file(cstring_view path, int oflag);
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
// Emulate a move constructor and a move assignment operator if rvalue
|
||||
// references are not supported.
|
||||
|
||||
private:
|
||||
// A proxy object to emulate a move constructor.
|
||||
// It is private to make it impossible call operator Proxy directly.
|
||||
struct Proxy {
|
||||
int fd;
|
||||
};
|
||||
|
||||
public:
|
||||
// A "move constructor" for moving from a temporary.
|
||||
file(Proxy p) FMT_NOEXCEPT : fd_(p.fd) {}
|
||||
|
||||
// A "move constructor" for moving from an lvalue.
|
||||
file(file &other) FMT_NOEXCEPT : fd_(other.fd_) {
|
||||
other.fd_ = -1;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from a temporary.
|
||||
file &operator=(Proxy p) {
|
||||
close();
|
||||
fd_ = p.fd;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from an lvalue.
|
||||
file &operator=(file &other) {
|
||||
close();
|
||||
fd_ = other.fd_;
|
||||
other.fd_ = -1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Returns a proxy object for moving from a temporary:
|
||||
// file f = file(...);
|
||||
operator Proxy() FMT_NOEXCEPT {
|
||||
Proxy p = {fd_};
|
||||
fd_ = -1;
|
||||
return p;
|
||||
}
|
||||
|
||||
#else
|
||||
private:
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(file);
|
||||
file(const file &) = delete;
|
||||
void operator=(const file &) = delete;
|
||||
|
||||
public:
|
||||
file(file &&other) FMT_NOEXCEPT : fd_(other.fd_) {
|
||||
@@ -307,7 +221,6 @@ class file {
|
||||
other.fd_ = -1;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Destroys the object closing the file it represents if any.
|
||||
FMT_API ~file() FMT_DTOR_NOEXCEPT;
|
||||
@@ -353,7 +266,8 @@ class file {
|
||||
long getpagesize();
|
||||
|
||||
#if (defined(LC_NUMERIC_MASK) || defined(_MSC_VER)) && \
|
||||
!defined(__ANDROID__) && !defined(__CYGWIN__) && !defined(__OpenBSD__)
|
||||
!defined(__ANDROID__) && !defined(__CYGWIN__) && !defined(__OpenBSD__) && \
|
||||
!defined(__NEWLIB_H__)
|
||||
# define FMT_LOCALE
|
||||
#endif
|
||||
|
||||
@@ -381,7 +295,8 @@ class Locale {
|
||||
|
||||
locale_t locale_;
|
||||
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(Locale);
|
||||
Locale(const Locale &) = delete;
|
||||
void operator=(const Locale &) = delete;
|
||||
|
||||
public:
|
||||
typedef locale_t Type;
|
||||
@@ -406,12 +321,4 @@ class Locale {
|
||||
#endif // FMT_LOCALE
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
namespace std {
|
||||
// For compatibility with C++98.
|
||||
inline fmt::buffered_file &move(fmt::buffered_file &f) { return f; }
|
||||
inline fmt::file &move(fmt::file &f) { return f; }
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // FMT_POSIX_H_
|
||||
|
||||
@@ -133,7 +133,7 @@ class arg_converter: public function<void> {
|
||||
// unsigned).
|
||||
template <typename T, typename Context, typename Char>
|
||||
void convert_arg(basic_format_arg<Context> &arg, Char type) {
|
||||
visit(arg_converter<T, Context>(arg, type), arg);
|
||||
fmt::visit(arg_converter<T, Context>(arg, type), arg);
|
||||
}
|
||||
|
||||
// Converts an integer argument to char for printf.
|
||||
@@ -142,8 +142,6 @@ class char_converter: public function<void> {
|
||||
private:
|
||||
basic_format_arg<Context> &arg_;
|
||||
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(char_converter);
|
||||
|
||||
public:
|
||||
explicit char_converter(basic_format_arg<Context> &arg) : arg_(arg) {}
|
||||
|
||||
@@ -169,8 +167,6 @@ class printf_width_handler: public function<unsigned> {
|
||||
|
||||
format_specs &spec_;
|
||||
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(printf_width_handler);
|
||||
|
||||
public:
|
||||
explicit printf_width_handler(format_specs &spec) : spec_(spec) {}
|
||||
|
||||
@@ -226,12 +222,12 @@ class printf_arg_formatter:
|
||||
context_type &context_;
|
||||
|
||||
void write_null_pointer(char) {
|
||||
this->spec().type_ = 0;
|
||||
this->spec()->type_ = 0;
|
||||
this->write("(nil)");
|
||||
}
|
||||
|
||||
void write_null_pointer(wchar_t) {
|
||||
this->spec().type_ = 0;
|
||||
this->spec()->type_ = 0;
|
||||
this->write(L"(nil)");
|
||||
}
|
||||
|
||||
@@ -247,7 +243,7 @@ class printf_arg_formatter:
|
||||
*/
|
||||
printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
|
||||
format_specs &spec, context_type &ctx)
|
||||
: base(back_insert_range<internal::basic_buffer<char_type>>(buffer), spec),
|
||||
: base(back_insert_range<internal::basic_buffer<char_type>>(buffer), &spec),
|
||||
context_(ctx) {}
|
||||
|
||||
template <typename T>
|
||||
@@ -256,13 +252,13 @@ class printf_arg_formatter:
|
||||
// MSVC2013 fails to compile separate overloads for bool and char_type so
|
||||
// use std::is_same instead.
|
||||
if (std::is_same<T, bool>::value) {
|
||||
format_specs &fmt_spec = this->spec();
|
||||
format_specs &fmt_spec = *this->spec();
|
||||
if (fmt_spec.type_ != 's')
|
||||
return base::operator()(value ? 1 : 0);
|
||||
fmt_spec.type_ = 0;
|
||||
this->write(value != 0);
|
||||
} else if (std::is_same<T, char_type>::value) {
|
||||
format_specs &fmt_spec = this->spec();
|
||||
format_specs &fmt_spec = *this->spec();
|
||||
if (fmt_spec.type_ && fmt_spec.type_ != 'c')
|
||||
return (*this)(static_cast<int>(value));
|
||||
fmt_spec.flags_ = 0;
|
||||
@@ -284,7 +280,7 @@ class printf_arg_formatter:
|
||||
iterator operator()(const char *value) {
|
||||
if (value)
|
||||
base::operator()(value);
|
||||
else if (this->spec().type_ == 'p')
|
||||
else if (this->spec()->type_ == 'p')
|
||||
write_null_pointer(char_type());
|
||||
else
|
||||
this->write("(null)");
|
||||
@@ -295,7 +291,7 @@ class printf_arg_formatter:
|
||||
iterator operator()(const wchar_t *value) {
|
||||
if (value)
|
||||
base::operator()(value);
|
||||
else if (this->spec().type_ == 'p')
|
||||
else if (this->spec()->type_ == 'p')
|
||||
write_null_pointer(char_type());
|
||||
else
|
||||
this->write(L"(null)");
|
||||
@@ -314,7 +310,7 @@ class printf_arg_formatter:
|
||||
iterator operator()(const void *value) {
|
||||
if (value)
|
||||
return base::operator()(value);
|
||||
this->spec().type_ = 0;
|
||||
this->spec()->type_ = 0;
|
||||
write_null_pointer(char_type());
|
||||
return this->out();
|
||||
}
|
||||
@@ -341,7 +337,9 @@ struct printf_formatter {
|
||||
/** This template formats data and writes the output to a writer. */
|
||||
template <typename OutputIt, typename Char, typename ArgFormatter>
|
||||
class basic_printf_context :
|
||||
private internal::context_base<
|
||||
// Inherit publicly as a workaround for the icc bug
|
||||
// https://software.intel.com/en-us/forums/intel-c-compiler/topic/783476.
|
||||
public internal::context_base<
|
||||
OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
|
||||
public:
|
||||
/** The character type for the output. */
|
||||
@@ -456,7 +454,7 @@ unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
|
||||
} else if (*it == '*') {
|
||||
++it;
|
||||
spec.width_ =
|
||||
visit(internal::printf_width_handler<char_type>(spec), get_arg(it));
|
||||
fmt::visit(internal::printf_width_handler<char_type>(spec), get_arg(it));
|
||||
}
|
||||
return arg_index;
|
||||
}
|
||||
@@ -492,14 +490,14 @@ void basic_printf_context<OutputIt, Char, AF>::format() {
|
||||
} else if (*it == '*') {
|
||||
++it;
|
||||
spec.precision_ =
|
||||
visit(internal::printf_precision_handler(), get_arg(it));
|
||||
fmt::visit(internal::printf_precision_handler(), get_arg(it));
|
||||
} else {
|
||||
spec.precision_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
format_arg arg = get_arg(it, arg_index);
|
||||
if (spec.flag(HASH_FLAG) && visit(internal::is_zero_int(), arg))
|
||||
if (spec.flag(HASH_FLAG) && fmt::visit(internal::is_zero_int(), arg))
|
||||
spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
|
||||
if (spec.fill_ == '0') {
|
||||
if (arg.is_arithmetic())
|
||||
@@ -553,7 +551,7 @@ void basic_printf_context<OutputIt, Char, AF>::format() {
|
||||
break;
|
||||
case 'c':
|
||||
// TODO: handle wchar_t better?
|
||||
visit(internal::char_converter<basic_printf_context>(arg), arg);
|
||||
fmt::visit(internal::char_converter<basic_printf_context>(arg), arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -561,7 +559,7 @@ void basic_printf_context<OutputIt, Char, AF>::format() {
|
||||
start = it;
|
||||
|
||||
// Format argument.
|
||||
visit(AF(buffer, spec, *this), arg);
|
||||
fmt::visit(AF(buffer, spec, *this), arg);
|
||||
}
|
||||
buffer.append(pointer_from(start), pointer_from(it));
|
||||
}
|
||||
|
||||
@@ -87,6 +87,9 @@ class is_like_std_string {
|
||||
!std::is_void<decltype(check<T>(FMT_NULL))>::value;
|
||||
};
|
||||
|
||||
template <typename Char>
|
||||
struct is_like_std_string<fmt::basic_string_view<Char>> : std::true_type {};
|
||||
|
||||
template <typename... Ts>
|
||||
struct conditional_helper {};
|
||||
|
||||
|
||||
@@ -13,8 +13,12 @@
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
|
||||
// Prevents expansion of a preceding token as a function-style macro.
|
||||
// Usage: f FMT_NOMACRO()
|
||||
#define FMT_NOMACRO
|
||||
|
||||
namespace internal{
|
||||
inline null<> localtime_r(...) { return null<>(); }
|
||||
inline null<> localtime_r FMT_NOMACRO(...) { return null<>(); }
|
||||
inline null<> localtime_s(...) { return null<>(); }
|
||||
inline null<> gmtime_r(...) { return null<>(); }
|
||||
inline null<> gmtime_s(...) { return null<>(); }
|
||||
|
||||
@@ -8,45 +8,46 @@
|
||||
#include "fmt/format-inl.h"
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
namespace internal {
|
||||
// Force linking of inline functions into the library.
|
||||
std::string (*vformat_ref)(string_view, format_args) = vformat;
|
||||
std::wstring (*vformat_wref)(wstring_view, wformat_args) = vformat;
|
||||
}
|
||||
|
||||
template struct internal::basic_data<void>;
|
||||
|
||||
// Explicit instantiations for char.
|
||||
|
||||
template FMT_API char internal::thousands_sep(locale_provider *lp);
|
||||
|
||||
template void internal::basic_buffer<char>::append(const char *, const char *);
|
||||
|
||||
template void basic_fixed_buffer<char>::grow(std::size_t);
|
||||
|
||||
template void internal::arg_map<format_context>::init(
|
||||
const basic_format_args<format_context> &args);
|
||||
|
||||
template FMT_API int internal::char_traits<char>::format_float(
|
||||
char *buffer, std::size_t size, const char *format, int precision,
|
||||
double value);
|
||||
char *, std::size_t, const char *, int, double);
|
||||
|
||||
template FMT_API int internal::char_traits<char>::format_float(
|
||||
char *buffer, std::size_t size, const char *format, int precision,
|
||||
long double value);
|
||||
char *, std::size_t, const char *, int, long double);
|
||||
|
||||
template FMT_API std::string internal::vformat<char>(
|
||||
string_view, basic_format_args<format_context>);
|
||||
|
||||
// Explicit instantiations for wchar_t.
|
||||
|
||||
template FMT_API wchar_t internal::thousands_sep(locale_provider *lp);
|
||||
template FMT_API wchar_t internal::thousands_sep(locale_provider *);
|
||||
|
||||
template void internal::basic_buffer<wchar_t>::append(
|
||||
const wchar_t *, const wchar_t *);
|
||||
|
||||
template void basic_fixed_buffer<wchar_t>::grow(std::size_t);
|
||||
|
||||
template void internal::arg_map<wformat_context>::init(
|
||||
const basic_format_args<wformat_context> &args);
|
||||
const basic_format_args<wformat_context> &);
|
||||
|
||||
template FMT_API int internal::char_traits<wchar_t>::format_float(
|
||||
wchar_t *buffer, std::size_t size, const wchar_t *format,
|
||||
int precision, double value);
|
||||
wchar_t *, std::size_t, const wchar_t *, int, double);
|
||||
|
||||
template FMT_API int internal::char_traits<wchar_t>::format_float(
|
||||
wchar_t *buffer, std::size_t size, const wchar_t *format,
|
||||
int precision, long double value);
|
||||
wchar_t *, std::size_t, const wchar_t *, int, long double);
|
||||
|
||||
template FMT_API std::wstring internal::vformat<wchar_t>(
|
||||
wstring_view, basic_format_args<wformat_context>);
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
@@ -39,7 +39,7 @@ for i, exp in enumerate(range(min_exponent, max_exponent + 1, step)):
|
||||
|
||||
print('Significands:', end='')
|
||||
for i, fp in enumerate(powers):
|
||||
if i % 4 == 0:
|
||||
if i % 3 == 0:
|
||||
print(end='\n ')
|
||||
print(' {:0<#16x}'.format(fp.f, ), end=',')
|
||||
|
||||
|
||||
@@ -247,7 +247,8 @@ def release(args):
|
||||
package = 'fmt-{}.zip'.format(version)
|
||||
r = requests.post(
|
||||
'{}/{}/assets?name={}'.format(uploads_url, id, package),
|
||||
params=params, files={package: open('build/fmt/' + package, 'rb')})
|
||||
headers={'Content-Type': 'application/zip'},
|
||||
params=params, data=open('build/fmt/' + package, 'rb'))
|
||||
if r.status_code != 201:
|
||||
raise Exception('Failed to upload an asset ' + str(r))
|
||||
|
||||
|
||||
@@ -109,6 +109,30 @@ class Translator(nodes.NodeVisitor):
|
||||
def depart_image(self, node):
|
||||
pass
|
||||
|
||||
def write_row(self, row, widths):
|
||||
for i, entry in enumerate(row):
|
||||
text = entry[0][0] if len(entry) > 0 else ''
|
||||
if i != 0:
|
||||
self.write('|')
|
||||
self.write('{:{}}'.format(text, widths[i]))
|
||||
self.write('\n')
|
||||
|
||||
def visit_table(self, node):
|
||||
table = node.children[0]
|
||||
colspecs = table[:-2]
|
||||
thead = table[-2]
|
||||
tbody = table[-1]
|
||||
widths = [int(cs['colwidth']) for cs in colspecs]
|
||||
sep = '|'.join(['-' * w for w in widths]) + '\n'
|
||||
self.write('\n\n')
|
||||
self.write_row(thead[0], widths)
|
||||
self.write(sep)
|
||||
for row in tbody:
|
||||
self.write_row(row, widths)
|
||||
raise nodes.SkipChildren
|
||||
|
||||
def depart_table(self, node):
|
||||
pass
|
||||
|
||||
class MDWriter(writers.Writer):
|
||||
"""GitHub-flavored markdown writer"""
|
||||
|
||||
@@ -85,13 +85,13 @@ function(add_fmt_test name)
|
||||
endfunction()
|
||||
|
||||
add_fmt_test(assert-test)
|
||||
add_fmt_test(core-test)
|
||||
add_fmt_test(gtest-extra-test)
|
||||
add_fmt_test(format-test)
|
||||
add_fmt_test(format-test mock-allocator.h)
|
||||
add_fmt_test(format-impl-test)
|
||||
add_fmt_test(ostream-test)
|
||||
add_fmt_test(printf-test)
|
||||
add_fmt_test(time-test)
|
||||
add_fmt_test(util-test mock-allocator.h)
|
||||
add_fmt_test(custom-formatter-test)
|
||||
add_fmt_test(ranges-test)
|
||||
|
||||
@@ -123,7 +123,10 @@ else ()
|
||||
endif ()
|
||||
|
||||
# Test that the library can be compiled with exceptions disabled.
|
||||
check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
|
||||
# -fno-exception is broken in icc: https://github.com/fmtlib/fmt/issues/822.
|
||||
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
|
||||
check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
|
||||
endif ()
|
||||
if (HAVE_FNO_EXCEPTIONS_FLAG)
|
||||
add_library(noexception-test ../src/format.cc)
|
||||
target_include_directories(
|
||||
|
||||
460
test/core-test.cc
Normal file
460
test/core-test.cc
Normal file
@@ -0,0 +1,460 @@
|
||||
// Formatting library for C++ - core tests
|
||||
//
|
||||
// Copyright (c) 2012 - present, Victor Zverovich
|
||||
// All rights reserved.
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "test-assert.h"
|
||||
|
||||
#include "gmock.h"
|
||||
|
||||
// Check if fmt/core.h compiles with windows.h included before it.
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "fmt/core.h"
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
using fmt::basic_format_arg;
|
||||
using fmt::internal::basic_buffer;
|
||||
using fmt::internal::value;
|
||||
using fmt::string_view;
|
||||
|
||||
using testing::_;
|
||||
using testing::StrictMock;
|
||||
|
||||
namespace {
|
||||
|
||||
struct test_struct {};
|
||||
|
||||
template <typename Context, typename T>
|
||||
basic_format_arg<Context> make_arg(const T &value) {
|
||||
return fmt::internal::make_arg<Context>(value);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
template <typename Char>
|
||||
struct formatter<test_struct, Char> {
|
||||
template <typename ParseContext>
|
||||
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
typedef std::back_insert_iterator<basic_buffer<Char>> iterator;
|
||||
|
||||
auto format(test_struct, basic_format_context<iterator, char> &ctx)
|
||||
-> decltype(ctx.out()) {
|
||||
const Char *test = "test";
|
||||
return std::copy_n(test, std::strlen(test), ctx.out());
|
||||
}
|
||||
};
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
TEST(BufferTest, Noncopyable) {
|
||||
EXPECT_FALSE(std::is_copy_constructible<basic_buffer<char> >::value);
|
||||
#if !FMT_MSC_VER
|
||||
// std::is_copy_assignable is broken in MSVC2013.
|
||||
EXPECT_FALSE(std::is_copy_assignable<basic_buffer<char> >::value);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(BufferTest, Nonmoveable) {
|
||||
EXPECT_FALSE(std::is_move_constructible<basic_buffer<char> >::value);
|
||||
#if !FMT_MSC_VER
|
||||
// std::is_move_assignable is broken in MSVC2013.
|
||||
EXPECT_FALSE(std::is_move_assignable<basic_buffer<char> >::value);
|
||||
#endif
|
||||
}
|
||||
|
||||
// A test buffer with a dummy grow method.
|
||||
template <typename T>
|
||||
struct test_buffer : basic_buffer<T> {
|
||||
void grow(std::size_t capacity) { this->set(nullptr, capacity); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct mock_buffer : basic_buffer<T> {
|
||||
MOCK_METHOD1(do_grow, void (std::size_t capacity));
|
||||
|
||||
void grow(std::size_t capacity) {
|
||||
this->set(this->data(), capacity);
|
||||
do_grow(capacity);
|
||||
}
|
||||
|
||||
mock_buffer() {}
|
||||
mock_buffer(T *data) { this->set(data, 0); }
|
||||
mock_buffer(T *data, std::size_t capacity) { this->set(data, capacity); }
|
||||
};
|
||||
|
||||
TEST(BufferTest, Ctor) {
|
||||
{
|
||||
mock_buffer<int> buffer;
|
||||
EXPECT_EQ(nullptr, &buffer[0]);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
|
||||
}
|
||||
{
|
||||
int dummy;
|
||||
mock_buffer<int> buffer(&dummy);
|
||||
EXPECT_EQ(&dummy, &buffer[0]);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
|
||||
}
|
||||
{
|
||||
int dummy;
|
||||
std::size_t capacity = std::numeric_limits<std::size_t>::max();
|
||||
mock_buffer<int> buffer(&dummy, capacity);
|
||||
EXPECT_EQ(&dummy, &buffer[0]);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(capacity, buffer.capacity());
|
||||
}
|
||||
}
|
||||
|
||||
struct dying_buffer : test_buffer<int> {
|
||||
MOCK_METHOD0(die, void());
|
||||
~dying_buffer() { die(); }
|
||||
};
|
||||
|
||||
TEST(BufferTest, VirtualDtor) {
|
||||
typedef StrictMock<dying_buffer> stict_mock_buffer;
|
||||
stict_mock_buffer *mock_buffer = new stict_mock_buffer();
|
||||
EXPECT_CALL(*mock_buffer, die());
|
||||
basic_buffer<int> *buffer = mock_buffer;
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
TEST(BufferTest, Access) {
|
||||
char data[10];
|
||||
mock_buffer<char> buffer(data, sizeof(data));
|
||||
buffer[0] = 11;
|
||||
EXPECT_EQ(11, buffer[0]);
|
||||
buffer[3] = 42;
|
||||
EXPECT_EQ(42, *(&buffer[0] + 3));
|
||||
const basic_buffer<char> &const_buffer = buffer;
|
||||
EXPECT_EQ(42, const_buffer[3]);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Resize) {
|
||||
char data[123];
|
||||
mock_buffer<char> buffer(data, sizeof(data));
|
||||
buffer[10] = 42;
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
buffer.resize(20);
|
||||
EXPECT_EQ(20u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
buffer.resize(5);
|
||||
EXPECT_EQ(5u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
// Check if resize calls grow.
|
||||
EXPECT_CALL(buffer, do_grow(124));
|
||||
buffer.resize(124);
|
||||
EXPECT_CALL(buffer, do_grow(200));
|
||||
buffer.resize(200);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Clear) {
|
||||
test_buffer<char> buffer;
|
||||
buffer.resize(20);
|
||||
buffer.resize(0);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(20u, buffer.capacity());
|
||||
}
|
||||
|
||||
TEST(BufferTest, Append) {
|
||||
char data[15];
|
||||
mock_buffer<char> buffer(data, 10);
|
||||
const char *test = "test";
|
||||
buffer.append(test, test + 5);
|
||||
EXPECT_STREQ(test, &buffer[0]);
|
||||
EXPECT_EQ(5u, buffer.size());
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(12));
|
||||
buffer.append(test, test + 2);
|
||||
EXPECT_EQ('t', buffer[10]);
|
||||
EXPECT_EQ('e', buffer[11]);
|
||||
EXPECT_EQ(12u, buffer.size());
|
||||
}
|
||||
|
||||
TEST(BufferTest, AppendAllocatesEnoughStorage) {
|
||||
char data[19];
|
||||
mock_buffer<char> buffer(data, 10);
|
||||
const char *test = "abcdefgh";
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(19));
|
||||
buffer.append(test, test + 9);
|
||||
}
|
||||
|
||||
TEST(ArgTest, FormatArgs) {
|
||||
fmt::format_args args;
|
||||
EXPECT_FALSE(args.get(1));
|
||||
}
|
||||
|
||||
struct custom_context {
|
||||
typedef char char_type;
|
||||
|
||||
template <typename T>
|
||||
struct formatter_type {
|
||||
struct type {
|
||||
template <typename ParseContext>
|
||||
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
const char *format(const T &, custom_context& ctx) {
|
||||
ctx.called = true;
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
bool called;
|
||||
|
||||
fmt::parse_context parse_context() { return fmt::parse_context(""); }
|
||||
void advance_to(const char *) {}
|
||||
};
|
||||
|
||||
TEST(ArgTest, MakeValueWithCustomContext) {
|
||||
test_struct t;
|
||||
fmt::internal::value<custom_context> arg =
|
||||
fmt::internal::make_value<custom_context>(t);
|
||||
custom_context ctx = {false};
|
||||
arg.custom.format(&t, ctx);
|
||||
EXPECT_TRUE(ctx.called);
|
||||
}
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
namespace internal {
|
||||
template <typename Char>
|
||||
bool operator==(custom_value<Char> lhs, custom_value<Char> rhs) {
|
||||
return lhs.value == rhs.value;
|
||||
}
|
||||
}
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
// Use a unique result type to make sure that there are no undesirable
|
||||
// conversions.
|
||||
struct test_result {};
|
||||
|
||||
template <typename T>
|
||||
struct mock_visitor {
|
||||
template <typename U>
|
||||
struct result { typedef test_result type; };
|
||||
|
||||
mock_visitor() {
|
||||
ON_CALL(*this, visit(_)).WillByDefault(testing::Return(test_result()));
|
||||
}
|
||||
|
||||
MOCK_METHOD1_T(visit, test_result (T value));
|
||||
MOCK_METHOD0_T(unexpected, void ());
|
||||
|
||||
test_result operator()(T value) { return visit(value); }
|
||||
|
||||
template <typename U>
|
||||
test_result operator()(U) {
|
||||
unexpected();
|
||||
return test_result();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct visit_type { typedef T Type; };
|
||||
|
||||
#define VISIT_TYPE(Type_, visit_type_) \
|
||||
template <> \
|
||||
struct visit_type<Type_> { typedef visit_type_ Type; }
|
||||
|
||||
VISIT_TYPE(signed char, int);
|
||||
VISIT_TYPE(unsigned char, unsigned);
|
||||
VISIT_TYPE(short, int);
|
||||
VISIT_TYPE(unsigned short, unsigned);
|
||||
|
||||
#if LONG_MAX == INT_MAX
|
||||
VISIT_TYPE(long, int);
|
||||
VISIT_TYPE(unsigned long, unsigned);
|
||||
#else
|
||||
VISIT_TYPE(long, long long);
|
||||
VISIT_TYPE(unsigned long, unsigned long long);
|
||||
#endif
|
||||
|
||||
VISIT_TYPE(float, double);
|
||||
|
||||
#define CHECK_ARG_(Char, expected, value) { \
|
||||
testing::StrictMock<mock_visitor<decltype(expected)>> visitor; \
|
||||
EXPECT_CALL(visitor, visit(expected)); \
|
||||
typedef std::back_insert_iterator<basic_buffer<Char>> iterator; \
|
||||
fmt::visit(visitor, \
|
||||
make_arg<fmt::basic_format_context<iterator, Char>>(value)); \
|
||||
}
|
||||
|
||||
#define CHECK_ARG(value, typename_) { \
|
||||
typedef decltype(value) value_type; \
|
||||
typename_ visit_type<value_type>::Type expected = value; \
|
||||
CHECK_ARG_(char, expected, value) \
|
||||
CHECK_ARG_(wchar_t, expected, value) \
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class NumericArgTest : public testing::Test {};
|
||||
|
||||
typedef ::testing::Types<
|
||||
bool, signed char, unsigned char, signed, unsigned short,
|
||||
int, unsigned, long, unsigned long, long long, unsigned long long,
|
||||
float, double, long double> Types;
|
||||
TYPED_TEST_CASE(NumericArgTest, Types);
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type test_value() {
|
||||
return static_cast<T>(42);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type
|
||||
test_value() {
|
||||
return static_cast<T>(4.2);
|
||||
}
|
||||
|
||||
TYPED_TEST(NumericArgTest, MakeAndVisit) {
|
||||
CHECK_ARG(test_value<TypeParam>(), typename);
|
||||
CHECK_ARG(std::numeric_limits<TypeParam>::min(), typename);
|
||||
CHECK_ARG(std::numeric_limits<TypeParam>::max(), typename);
|
||||
}
|
||||
|
||||
TEST(ArgTest, CharArg) {
|
||||
CHECK_ARG_(char, 'a', 'a');
|
||||
CHECK_ARG_(wchar_t, L'a', 'a');
|
||||
CHECK_ARG_(wchar_t, L'a', L'a');
|
||||
}
|
||||
|
||||
TEST(ArgTest, StringArg) {
|
||||
char str_data[] = "test";
|
||||
char *str = str_data;
|
||||
const char *cstr = str;
|
||||
CHECK_ARG_(char, cstr, str);
|
||||
|
||||
string_view sref(str);
|
||||
CHECK_ARG_(char, sref, std::string(str));
|
||||
}
|
||||
|
||||
TEST(ArgTest, WStringArg) {
|
||||
wchar_t str_data[] = L"test";
|
||||
wchar_t *str = str_data;
|
||||
const wchar_t *cstr = str;
|
||||
|
||||
fmt::wstring_view sref(str);
|
||||
CHECK_ARG_(wchar_t, cstr, str);
|
||||
CHECK_ARG_(wchar_t, cstr, cstr);
|
||||
CHECK_ARG_(wchar_t, sref, std::wstring(str));
|
||||
CHECK_ARG_(wchar_t, sref, fmt::wstring_view(str));
|
||||
}
|
||||
|
||||
TEST(ArgTest, PointerArg) {
|
||||
void *p = nullptr;
|
||||
const void *cp = nullptr;
|
||||
CHECK_ARG_(char, cp, p);
|
||||
CHECK_ARG_(wchar_t, cp, p);
|
||||
CHECK_ARG(cp, );
|
||||
}
|
||||
|
||||
struct check_custom {
|
||||
test_result operator()(
|
||||
fmt::basic_format_arg<fmt::format_context>::handle h) const {
|
||||
struct test_buffer : fmt::internal::basic_buffer<char> {
|
||||
char data[10];
|
||||
test_buffer() : basic_buffer(data, 0, 10) {}
|
||||
void grow(std::size_t) {}
|
||||
} buffer;
|
||||
fmt::internal::basic_buffer<char> &base = buffer;
|
||||
fmt::format_context ctx(std::back_inserter(base), "", fmt::format_args());
|
||||
h.format(ctx);
|
||||
EXPECT_EQ("test", std::string(buffer.data, buffer.size()));
|
||||
return test_result();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ArgTest, CustomArg) {
|
||||
test_struct test;
|
||||
typedef mock_visitor<fmt::basic_format_arg<fmt::format_context>::handle>
|
||||
visitor;
|
||||
testing::StrictMock<visitor> v;
|
||||
EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke(check_custom()));
|
||||
fmt::visit(v, make_arg<fmt::format_context>(test));
|
||||
}
|
||||
|
||||
TEST(ArgTest, VisitInvalidArg) {
|
||||
testing::StrictMock< mock_visitor<fmt::monostate> > visitor;
|
||||
EXPECT_CALL(visitor, visit(_));
|
||||
fmt::basic_format_arg<fmt::format_context> arg;
|
||||
visit(visitor, arg);
|
||||
}
|
||||
|
||||
TEST(StringViewTest, Length) {
|
||||
// Test that StringRef::size() returns string length, not buffer size.
|
||||
char str[100] = "some string";
|
||||
EXPECT_EQ(std::strlen(str), string_view(str).size());
|
||||
EXPECT_LT(std::strlen(str), sizeof(str));
|
||||
}
|
||||
|
||||
// Check string_view's comparison operator.
|
||||
template <template <typename> class Op>
|
||||
void check_op() {
|
||||
const char *inputs[] = {"foo", "fop", "fo"};
|
||||
std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
|
||||
for (std::size_t i = 0; i < num_inputs; ++i) {
|
||||
for (std::size_t j = 0; j < num_inputs; ++j) {
|
||||
string_view lhs(inputs[i]), rhs(inputs[j]);
|
||||
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StringViewTest, Compare) {
|
||||
EXPECT_EQ(string_view("foo").compare(string_view("foo")), 0);
|
||||
EXPECT_GT(string_view("fop").compare(string_view("foo")), 0);
|
||||
EXPECT_LT(string_view("foo").compare(string_view("fop")), 0);
|
||||
EXPECT_GT(string_view("foo").compare(string_view("fo")), 0);
|
||||
EXPECT_LT(string_view("fo").compare(string_view("foo")), 0);
|
||||
check_op<std::equal_to>();
|
||||
check_op<std::not_equal_to>();
|
||||
check_op<std::less>();
|
||||
check_op<std::less_equal>();
|
||||
check_op<std::greater>();
|
||||
check_op<std::greater_equal>();
|
||||
}
|
||||
|
||||
enum basic_enum {};
|
||||
|
||||
TEST(CoreTest, ConvertToInt) {
|
||||
EXPECT_FALSE((fmt::convert_to_int<char, char>::value));
|
||||
EXPECT_FALSE((fmt::convert_to_int<const char *, char>::value));
|
||||
EXPECT_TRUE((fmt::convert_to_int<basic_enum, char>::value));
|
||||
}
|
||||
|
||||
enum enum_with_underlying_type : char {};
|
||||
|
||||
TEST(CoreTest, IsEnumConvertibleToInt) {
|
||||
EXPECT_TRUE((fmt::convert_to_int<enum_with_underlying_type, char>::value));
|
||||
}
|
||||
|
||||
TEST(CoreTest, Format) {
|
||||
// This should work without including fmt/format.h.
|
||||
#ifdef FMT_FORMAT_H_
|
||||
# error fmt/format.h must not be included in the core test
|
||||
#endif
|
||||
EXPECT_EQ(fmt::format("{}", 42), "42");
|
||||
}
|
||||
@@ -19,14 +19,15 @@ class custom_arg_formatter :
|
||||
typedef fmt::back_insert_range<fmt::internal::buffer> range;
|
||||
typedef fmt::arg_formatter<range> base;
|
||||
|
||||
custom_arg_formatter(fmt::format_context &ctx, fmt::format_specs &s)
|
||||
custom_arg_formatter(
|
||||
fmt::format_context &ctx, fmt::format_specs *s = FMT_NULL)
|
||||
: base(ctx, s) {}
|
||||
|
||||
using base::operator();
|
||||
|
||||
iterator operator()(double value) {
|
||||
// Comparing a float to 0.0 is safe
|
||||
if (round(value * pow(10, spec().precision())) == 0.0)
|
||||
if (round(value * pow(10, spec()->precision())) == 0.0)
|
||||
value = 0;
|
||||
return base::operator()(value);
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#define FMT_EXTENDED_COLORS
|
||||
#define FMT_NOEXCEPT
|
||||
#undef FMT_SHARED
|
||||
#include "test-assert.h"
|
||||
|
||||
// Include format.cc instead of format.h to test implementation.
|
||||
#include "../src/format.cc"
|
||||
#include "fmt/color.h"
|
||||
#include "fmt/printf.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -24,6 +24,78 @@
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
using fmt::internal::fp;
|
||||
|
||||
template <bool is_iec559>
|
||||
void test_construct_from_double() {
|
||||
fmt::print("warning: double is not IEC559, skipping FP tests\n");
|
||||
}
|
||||
|
||||
template <>
|
||||
void test_construct_from_double<true>() {
|
||||
auto v = fp(1.23);
|
||||
EXPECT_EQ(v.f, 0x13ae147ae147aeu);
|
||||
EXPECT_EQ(v.e, -52);
|
||||
}
|
||||
|
||||
TEST(FPTest, ConstructFromDouble) {
|
||||
test_construct_from_double<std::numeric_limits<double>::is_iec559>();
|
||||
}
|
||||
|
||||
TEST(FPTest, Normalize) {
|
||||
auto v = fp(0xbeef, 42);
|
||||
v.normalize();
|
||||
EXPECT_EQ(0xbeef000000000000, v.f);
|
||||
EXPECT_EQ(-6, v.e);
|
||||
}
|
||||
|
||||
TEST(FPTest, ComputeBoundariesSubnormal) {
|
||||
auto v = fp(0xbeef, 42);
|
||||
fp lower, upper;
|
||||
v.compute_boundaries(lower, upper);
|
||||
EXPECT_EQ(0xbeee800000000000, lower.f);
|
||||
EXPECT_EQ(-6, lower.e);
|
||||
EXPECT_EQ(0xbeef800000000000, upper.f);
|
||||
EXPECT_EQ(-6, upper.e);
|
||||
}
|
||||
|
||||
TEST(FPTest, ComputeBoundaries) {
|
||||
auto v = fp(0x10000000000000, 42);
|
||||
fp lower, upper;
|
||||
v.compute_boundaries(lower, upper);
|
||||
EXPECT_EQ(0x7ffffffffffffe00, lower.f);
|
||||
EXPECT_EQ(31, lower.e);
|
||||
EXPECT_EQ(0x8000000000000400, upper.f);
|
||||
EXPECT_EQ(31, upper.e);
|
||||
}
|
||||
|
||||
TEST(FPTest, Subtract) {
|
||||
auto v = fp(123, 1) - fp(102, 1);
|
||||
EXPECT_EQ(v.f, 21u);
|
||||
EXPECT_EQ(v.e, 1);
|
||||
}
|
||||
|
||||
TEST(FPTest, Multiply) {
|
||||
auto v = fp(123ULL << 32, 4) * fp(56ULL << 32, 7);
|
||||
EXPECT_EQ(v.f, 123u * 56u);
|
||||
EXPECT_EQ(v.e, 4 + 7 + 64);
|
||||
v = fp(123ULL << 32, 4) * fp(567ULL << 31, 8);
|
||||
EXPECT_EQ(v.f, (123 * 567 + 1u) / 2);
|
||||
EXPECT_EQ(v.e, 4 + 8 + 64);
|
||||
}
|
||||
|
||||
TEST(FPTest, GetCachedPower) {
|
||||
typedef std::numeric_limits<double> limits;
|
||||
for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) {
|
||||
int dec_exp = 0;
|
||||
auto fp = fmt::internal::get_cached_power(exp, dec_exp);
|
||||
EXPECT_LE(exp, fp.e);
|
||||
int dec_exp_step = 8;
|
||||
EXPECT_LE(fp.e, exp + dec_exp_step * log2(10));
|
||||
EXPECT_DOUBLE_EQ(pow(10, dec_exp), ldexp(fp.f, fp.e));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct ValueExtractor: fmt::internal::function<T> {
|
||||
T operator()(T value) {
|
||||
@@ -118,9 +190,13 @@ TEST(FormatTest, FormatErrorCode) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FormatTest, CountCodePoints) {
|
||||
EXPECT_EQ(4, fmt::internal::count_code_points(fmt::u8string_view("ёжик")));
|
||||
}
|
||||
|
||||
TEST(ColorsTest, Colors) {
|
||||
EXPECT_WRITE(stdout, fmt::print(fmt::rgb(255,20,30), "rgb(255,20,30)"),
|
||||
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
|
||||
EXPECT_WRITE(stdout, fmt::print(fmt::color::blue,"blue"),
|
||||
EXPECT_WRITE(stdout, fmt::print(fmt::color::blue, "blue"),
|
||||
"\x1b[38;2;000;000;255mblue\x1b[0m");
|
||||
}
|
||||
|
||||
@@ -14,17 +14,24 @@
|
||||
#include <memory>
|
||||
#include <stdint.h>
|
||||
|
||||
// Check if fmt/format.h compiles with windows.h included before it.
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "fmt/format.h"
|
||||
#include "gmock.h"
|
||||
#include "gtest-extra.h"
|
||||
#include "mock-allocator.h"
|
||||
#include "util.h"
|
||||
|
||||
#undef ERROR
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
using std::size_t;
|
||||
|
||||
using fmt::basic_memory_buffer;
|
||||
using fmt::basic_writer;
|
||||
using fmt::format;
|
||||
using fmt::format_error;
|
||||
@@ -32,6 +39,9 @@ using fmt::string_view;
|
||||
using fmt::memory_buffer;
|
||||
using fmt::wmemory_buffer;
|
||||
|
||||
using testing::Return;
|
||||
using testing::StrictMock;
|
||||
|
||||
namespace {
|
||||
|
||||
// Format value using the standard library.
|
||||
@@ -101,6 +111,451 @@ struct WriteChecker {
|
||||
EXPECT_PRED_FORMAT1(WriteChecker<wchar_t>(), value)
|
||||
} // namespace
|
||||
|
||||
// Tests fmt::internal::count_digits for integer type Int.
|
||||
template <typename Int>
|
||||
void test_count_digits() {
|
||||
for (Int i = 0; i < 10; ++i)
|
||||
EXPECT_EQ(1u, fmt::internal::count_digits(i));
|
||||
for (Int i = 1, n = 1,
|
||||
end = std::numeric_limits<Int>::max() / 10; n <= end; ++i) {
|
||||
n *= 10;
|
||||
EXPECT_EQ(i, fmt::internal::count_digits(n - 1));
|
||||
EXPECT_EQ(i + 1, fmt::internal::count_digits(n));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UtilTest, CountDigits) {
|
||||
test_count_digits<uint32_t>();
|
||||
test_count_digits<uint64_t>();
|
||||
}
|
||||
|
||||
struct uint32_pair {
|
||||
uint32_t u[2];
|
||||
};
|
||||
|
||||
TEST(UtilTest, BitCast) {
|
||||
auto s = fmt::internal::bit_cast<uint32_pair>(uint64_t{42});
|
||||
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), 42ull);
|
||||
s = fmt::internal::bit_cast<uint32_pair>(uint64_t(~0ull));
|
||||
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), ~0ull);
|
||||
}
|
||||
|
||||
TEST(UtilTest, Increment) {
|
||||
char s[10] = "123";
|
||||
increment(s);
|
||||
EXPECT_STREQ("124", s);
|
||||
s[2] = '8';
|
||||
increment(s);
|
||||
EXPECT_STREQ("129", s);
|
||||
increment(s);
|
||||
EXPECT_STREQ("130", s);
|
||||
s[1] = s[2] = '9';
|
||||
increment(s);
|
||||
EXPECT_STREQ("200", s);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ParseNonnegativeInt) {
|
||||
if (std::numeric_limits<int>::max() !=
|
||||
static_cast<int>(static_cast<unsigned>(1) << 31)) {
|
||||
fmt::print("Skipping parse_nonnegative_int test\n");
|
||||
return;
|
||||
}
|
||||
const char *s = "10000000000";
|
||||
EXPECT_THROW_MSG(
|
||||
parse_nonnegative_int(s, fmt::internal::error_handler()),
|
||||
fmt::format_error, "number is too big");
|
||||
s = "2147483649";
|
||||
EXPECT_THROW_MSG(
|
||||
parse_nonnegative_int(s, fmt::internal::error_handler()),
|
||||
fmt::format_error, "number is too big");
|
||||
}
|
||||
|
||||
TEST(IteratorTest, CountingIterator) {
|
||||
fmt::internal::counting_iterator<char> it;
|
||||
auto prev = it++;
|
||||
EXPECT_EQ(prev.count(), 0);
|
||||
EXPECT_EQ(it.count(), 1);
|
||||
}
|
||||
|
||||
TEST(IteratorTest, TruncatingIterator) {
|
||||
char *p = FMT_NULL;
|
||||
fmt::internal::truncating_iterator<char*> it(p, 3);
|
||||
auto prev = it++;
|
||||
EXPECT_EQ(prev.base(), p);
|
||||
EXPECT_EQ(it.base(), p + 1);
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Ctor) {
|
||||
basic_memory_buffer<char, 123> buffer;
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
}
|
||||
|
||||
static void check_forwarding(
|
||||
mock_allocator<int> &alloc, allocator_ref<mock_allocator<int>> &ref) {
|
||||
int mem;
|
||||
// Check if value_type is properly defined.
|
||||
allocator_ref< mock_allocator<int> >::value_type *ptr = &mem;
|
||||
// Check forwarding.
|
||||
EXPECT_CALL(alloc, allocate(42)).WillOnce(testing::Return(ptr));
|
||||
ref.allocate(42);
|
||||
EXPECT_CALL(alloc, deallocate(ptr, 42));
|
||||
ref.deallocate(ptr, 42);
|
||||
}
|
||||
|
||||
TEST(AllocatorTest, allocator_ref) {
|
||||
StrictMock< mock_allocator<int> > alloc;
|
||||
typedef allocator_ref< mock_allocator<int> > test_allocator_ref;
|
||||
test_allocator_ref ref(&alloc);
|
||||
// Check if allocator_ref forwards to the underlying allocator.
|
||||
check_forwarding(alloc, ref);
|
||||
test_allocator_ref ref2(ref);
|
||||
check_forwarding(alloc, ref2);
|
||||
test_allocator_ref ref3;
|
||||
EXPECT_EQ(nullptr, ref3.get());
|
||||
ref3 = ref;
|
||||
check_forwarding(alloc, ref3);
|
||||
}
|
||||
|
||||
typedef allocator_ref< std::allocator<char> > TestAllocator;
|
||||
|
||||
static void check_move_buffer(const char *str,
|
||||
basic_memory_buffer<char, 5, TestAllocator> &buffer) {
|
||||
std::allocator<char> *alloc = buffer.get_allocator().get();
|
||||
basic_memory_buffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
||||
// Move shouldn't destroy the inline content of the first buffer.
|
||||
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
||||
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_EQ(5u, buffer2.capacity());
|
||||
// Move should transfer allocator.
|
||||
EXPECT_EQ(nullptr, buffer.get_allocator().get());
|
||||
EXPECT_EQ(alloc, buffer2.get_allocator().get());
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, MoveCtor) {
|
||||
std::allocator<char> alloc;
|
||||
basic_memory_buffer<char, 5, TestAllocator> buffer((TestAllocator(&alloc)));
|
||||
const char test[] = "test";
|
||||
buffer.append(test, test + 4);
|
||||
check_move_buffer("test", buffer);
|
||||
// Adding one more character fills the inline buffer, but doesn't cause
|
||||
// dynamic allocation.
|
||||
buffer.push_back('a');
|
||||
check_move_buffer("testa", buffer);
|
||||
const char *inline_buffer_ptr = &buffer[0];
|
||||
// Adding one more character causes the content to move from the inline to
|
||||
// a dynamically allocated buffer.
|
||||
buffer.push_back('b');
|
||||
basic_memory_buffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
||||
// Move should rip the guts of the first buffer.
|
||||
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
||||
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_GT(buffer2.capacity(), 5u);
|
||||
}
|
||||
|
||||
static void check_move_assign_buffer(
|
||||
const char *str, basic_memory_buffer<char, 5> &buffer) {
|
||||
basic_memory_buffer<char, 5> buffer2;
|
||||
buffer2 = std::move(buffer);
|
||||
// Move shouldn't destroy the inline content of the first buffer.
|
||||
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
||||
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_EQ(5u, buffer2.capacity());
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, MoveAssignment) {
|
||||
basic_memory_buffer<char, 5> buffer;
|
||||
const char test[] = "test";
|
||||
buffer.append(test, test + 4);
|
||||
check_move_assign_buffer("test", buffer);
|
||||
// Adding one more character fills the inline buffer, but doesn't cause
|
||||
// dynamic allocation.
|
||||
buffer.push_back('a');
|
||||
check_move_assign_buffer("testa", buffer);
|
||||
const char *inline_buffer_ptr = &buffer[0];
|
||||
// Adding one more character causes the content to move from the inline to
|
||||
// a dynamically allocated buffer.
|
||||
buffer.push_back('b');
|
||||
basic_memory_buffer<char, 5> buffer2;
|
||||
buffer2 = std::move(buffer);
|
||||
// Move should rip the guts of the first buffer.
|
||||
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
||||
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_GT(buffer2.capacity(), 5u);
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Grow) {
|
||||
typedef allocator_ref< mock_allocator<int> > Allocator;
|
||||
typedef basic_memory_buffer<int, 10, Allocator> Base;
|
||||
mock_allocator<int> alloc;
|
||||
struct TestMemoryBuffer : Base {
|
||||
TestMemoryBuffer(Allocator alloc) : Base(alloc) {}
|
||||
void grow(std::size_t size) { Base::grow(size); }
|
||||
} buffer((Allocator(&alloc)));
|
||||
buffer.resize(7);
|
||||
using fmt::internal::to_unsigned;
|
||||
for (int i = 0; i < 7; ++i)
|
||||
buffer[to_unsigned(i)] = i * i;
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
int mem[20];
|
||||
mem[7] = 0xdead;
|
||||
EXPECT_CALL(alloc, allocate(20)).WillOnce(Return(mem));
|
||||
buffer.grow(20);
|
||||
EXPECT_EQ(20u, buffer.capacity());
|
||||
// Check if size elements have been copied
|
||||
for (int i = 0; i < 7; ++i)
|
||||
EXPECT_EQ(i * i, buffer[to_unsigned(i)]);
|
||||
// and no more than that.
|
||||
EXPECT_EQ(0xdead, buffer[7]);
|
||||
EXPECT_CALL(alloc, deallocate(mem, 20));
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Allocator) {
|
||||
typedef allocator_ref< mock_allocator<char> > TestAllocator;
|
||||
basic_memory_buffer<char, 10, TestAllocator> buffer;
|
||||
EXPECT_EQ(nullptr, buffer.get_allocator().get());
|
||||
StrictMock< mock_allocator<char> > alloc;
|
||||
char mem;
|
||||
{
|
||||
basic_memory_buffer<char, 10, TestAllocator> buffer2((TestAllocator(&alloc)));
|
||||
EXPECT_EQ(&alloc, buffer2.get_allocator().get());
|
||||
std::size_t size = 2 * fmt::inline_buffer_size;
|
||||
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem));
|
||||
buffer2.reserve(size);
|
||||
EXPECT_CALL(alloc, deallocate(&mem, size));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, ExceptionInDeallocate) {
|
||||
typedef allocator_ref< mock_allocator<char> > TestAllocator;
|
||||
StrictMock< mock_allocator<char> > alloc;
|
||||
basic_memory_buffer<char, 10, TestAllocator> buffer((TestAllocator(&alloc)));
|
||||
std::size_t size = 2 * fmt::inline_buffer_size;
|
||||
std::vector<char> mem(size);
|
||||
{
|
||||
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0]));
|
||||
buffer.resize(size);
|
||||
std::fill(&buffer[0], &buffer[0] + size, 'x');
|
||||
}
|
||||
std::vector<char> mem2(2 * size);
|
||||
{
|
||||
EXPECT_CALL(alloc, allocate(2 * size)).WillOnce(Return(&mem2[0]));
|
||||
std::exception e;
|
||||
EXPECT_CALL(alloc, deallocate(&mem[0], size)).WillOnce(testing::Throw(e));
|
||||
EXPECT_THROW(buffer.reserve(2 * size), std::exception);
|
||||
EXPECT_EQ(&mem2[0], &buffer[0]);
|
||||
// Check that the data has been copied.
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
EXPECT_EQ('x', buffer[i]);
|
||||
}
|
||||
EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size));
|
||||
}
|
||||
|
||||
TEST(FixedBufferTest, Ctor) {
|
||||
char array[10] = "garbage";
|
||||
fmt::basic_fixed_buffer<char> buffer(array, sizeof(array));
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
EXPECT_EQ(array, buffer.data());
|
||||
}
|
||||
|
||||
TEST(FixedBufferTest, CompileTimeSizeCtor) {
|
||||
char array[10] = "garbage";
|
||||
fmt::basic_fixed_buffer<char> buffer(array);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
EXPECT_EQ(array, buffer.data());
|
||||
}
|
||||
|
||||
TEST(FixedBufferTest, BufferOverflow) {
|
||||
char array[10];
|
||||
fmt::basic_fixed_buffer<char> buffer(array);
|
||||
buffer.resize(10);
|
||||
EXPECT_THROW_MSG(buffer.resize(11), std::runtime_error, "buffer overflow");
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TEST(UtilTest, UTF16ToUTF8) {
|
||||
std::string s = "ёжик";
|
||||
fmt::internal::utf16_to_utf8 u(L"\x0451\x0436\x0438\x043A");
|
||||
EXPECT_EQ(s, u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8EmptyString) {
|
||||
std::string s = "";
|
||||
fmt::internal::utf16_to_utf8 u(L"");
|
||||
EXPECT_EQ(s, u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16) {
|
||||
std::string s = "лошадка";
|
||||
fmt::internal::utf8_to_utf16 u(s.c_str());
|
||||
EXPECT_EQ(L"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u.str());
|
||||
EXPECT_EQ(7, u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16EmptyString) {
|
||||
std::string s = "";
|
||||
fmt::internal::utf8_to_utf16 u(s.c_str());
|
||||
EXPECT_EQ(L"", u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
template <typename Converter, typename Char>
|
||||
void check_utf_conversion_error(
|
||||
const char *message,
|
||||
fmt::basic_string_view<Char> str = fmt::basic_string_view<Char>(0, 1)) {
|
||||
fmt::memory_buffer out;
|
||||
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
|
||||
fmt::system_error error(0, "");
|
||||
try {
|
||||
(Converter)(str);
|
||||
} catch (const fmt::system_error &e) {
|
||||
error = e;
|
||||
}
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, error.error_code());
|
||||
EXPECT_EQ(fmt::to_string(out), error.what());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Error) {
|
||||
check_utf_conversion_error<fmt::internal::utf16_to_utf8, wchar_t>(
|
||||
"cannot convert string from UTF-16 to UTF-8");
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16Error) {
|
||||
const char *message = "cannot convert string from UTF-8 to UTF-16";
|
||||
check_utf_conversion_error<fmt::internal::utf8_to_utf16, char>(message);
|
||||
check_utf_conversion_error<fmt::internal::utf8_to_utf16, char>(
|
||||
message, fmt::string_view("foo", INT_MAX + 1u));
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Convert) {
|
||||
fmt::internal::utf16_to_utf8 u;
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::wstring_view(0, 1)));
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER,
|
||||
u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u)));
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
typedef void (*FormatErrorMessage)(
|
||||
fmt::internal::buffer &out, int error_code, string_view message);
|
||||
|
||||
template <typename Error>
|
||||
void check_throw_error(int error_code, FormatErrorMessage format) {
|
||||
fmt::system_error error(0, "");
|
||||
try {
|
||||
throw Error(error_code, "test {}", "error");
|
||||
} catch (const fmt::system_error &e) {
|
||||
error = e;
|
||||
}
|
||||
fmt::memory_buffer message;
|
||||
format(message, error_code, "test error");
|
||||
EXPECT_EQ(to_string(message), error.what());
|
||||
EXPECT_EQ(error_code, error.error_code());
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatSystemError) {
|
||||
fmt::memory_buffer message;
|
||||
fmt::format_system_error(message, EDOM, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)),
|
||||
to_string(message));
|
||||
message = fmt::memory_buffer();
|
||||
|
||||
// Check if std::allocator throws on allocating max size_t / 2 chars.
|
||||
size_t max_size = std::numeric_limits<size_t>::max() / 2;
|
||||
bool throws_on_alloc = false;
|
||||
try {
|
||||
std::allocator<char> alloc;
|
||||
alloc.deallocate(alloc.allocate(max_size), max_size);
|
||||
} catch (const std::bad_alloc&) {
|
||||
throws_on_alloc = true;
|
||||
}
|
||||
if (!throws_on_alloc) {
|
||||
fmt::print("warning: std::allocator allocates {} chars", max_size);
|
||||
return;
|
||||
}
|
||||
fmt::format_system_error(message, EDOM, fmt::string_view(nullptr, max_size));
|
||||
EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message));
|
||||
}
|
||||
|
||||
TEST(UtilTest, SystemError) {
|
||||
fmt::system_error e(EDOM, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), e.what());
|
||||
EXPECT_EQ(EDOM, e.error_code());
|
||||
check_throw_error<fmt::system_error>(EDOM, fmt::format_system_error);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ReportSystemError) {
|
||||
fmt::memory_buffer out;
|
||||
fmt::format_system_error(out, EDOM, "test error");
|
||||
out.push_back('\n');
|
||||
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"),
|
||||
to_string(out));
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
TEST(UtilTest, FormatWindowsError) {
|
||||
LPWSTR message = 0;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||
ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
||||
fmt::internal::utf16_to_utf8 utf8_message(message);
|
||||
LocalFree(message);
|
||||
fmt::memory_buffer actual_message;
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, ERROR_FILE_EXISTS, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
||||
fmt::to_string(actual_message));
|
||||
actual_message.resize(0);
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, ERROR_FILE_EXISTS,
|
||||
fmt::string_view(0, std::numeric_limits<size_t>::max()));
|
||||
EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS),
|
||||
fmt::to_string(actual_message));
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatLongWindowsError) {
|
||||
LPWSTR message = 0;
|
||||
// this error code is not available on all Windows platforms and
|
||||
// Windows SDKs, so do not fail the test if the error string cannot
|
||||
// be retrieved.
|
||||
const int provisioning_not_allowed = 0x80284013L /*TBS_E_PROVISIONING_NOT_ALLOWED*/;
|
||||
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||
static_cast<DWORD>(provisioning_not_allowed),
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPWSTR>(&message), 0, 0) == 0) {
|
||||
return;
|
||||
}
|
||||
fmt::internal::utf16_to_utf8 utf8_message(message);
|
||||
LocalFree(message);
|
||||
fmt::memory_buffer actual_message;
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, provisioning_not_allowed, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
||||
fmt::to_string(actual_message));
|
||||
}
|
||||
|
||||
TEST(UtilTest, WindowsError) {
|
||||
check_throw_error<fmt::windows_error>(
|
||||
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ReportWindowsError) {
|
||||
fmt::memory_buffer out;
|
||||
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
||||
out.push_back('\n');
|
||||
EXPECT_WRITE(stderr,
|
||||
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"),
|
||||
fmt::to_string(out));
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
||||
|
||||
TEST(StringViewTest, Ctor) {
|
||||
EXPECT_STREQ("abc", string_view("abc").data());
|
||||
EXPECT_EQ(3u, string_view("abc").size());
|
||||
@@ -237,6 +692,9 @@ TEST(FormatToTest, FormatToMemoryBuffer) {
|
||||
fmt::basic_memory_buffer<char, 100> buffer;
|
||||
fmt::format_to(buffer, "{}", "foo");
|
||||
EXPECT_EQ("foo", to_string(buffer));
|
||||
fmt::wmemory_buffer wbuffer;
|
||||
fmt::format_to(wbuffer, L"{}", L"foo");
|
||||
EXPECT_EQ(L"foo", to_string(wbuffer));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, Escape) {
|
||||
@@ -555,7 +1013,10 @@ TEST(FormatterTest, HashFlag) {
|
||||
EXPECT_EQ("0x42", format("{0:#x}", 0x42ull));
|
||||
EXPECT_EQ("042", format("{0:#o}", 042ull));
|
||||
|
||||
EXPECT_EQ("-42.0000", format("{0:#}", -42.0));
|
||||
if (fmt::internal::use_grisu())
|
||||
EXPECT_EQ("-42.0", format("{0:#}", -42.0));
|
||||
else
|
||||
EXPECT_EQ("-42.0000", format("{0:#}", -42.0));
|
||||
EXPECT_EQ("-42.0000", format("{0:#}", -42.0l));
|
||||
EXPECT_THROW_MSG(format("{0:#", 'c'),
|
||||
format_error, "missing '}' in format string");
|
||||
@@ -1085,14 +1546,45 @@ TEST(FormatterTest, FormatStdStringView) {
|
||||
}
|
||||
#endif
|
||||
|
||||
struct ConvertibleToStringView {
|
||||
struct implicitly_convertible_to_string_view {
|
||||
operator fmt::string_view() const { return "foo"; }
|
||||
};
|
||||
|
||||
TEST(FormatterTest, FormatConvertibleToStringView) {
|
||||
EXPECT_EQ("foo", format("{}", ConvertibleToStringView()));
|
||||
TEST(FormatterTest, FormatImplicitlyConvertibleToStringView) {
|
||||
EXPECT_EQ("foo", format("{}", implicitly_convertible_to_string_view()));
|
||||
}
|
||||
|
||||
// std::is_constructible is broken in MSVC until version 2015.
|
||||
#if !FMT_MSC_VER || FMT_MSC_VER >= 1900
|
||||
struct explicitly_convertible_to_string_view {
|
||||
explicit operator fmt::string_view() const { return "foo"; }
|
||||
};
|
||||
|
||||
TEST(FormatterTest, FormatExplicitlyConvertibleToStringView) {
|
||||
EXPECT_EQ("foo", format("{}", explicitly_convertible_to_string_view()));
|
||||
}
|
||||
|
||||
struct explicitly_convertible_to_wstring_view {
|
||||
explicit operator fmt::wstring_view() const { return L"foo"; }
|
||||
};
|
||||
|
||||
TEST(FormatterTest, FormatExplicitlyConvertibleToWStringView) {
|
||||
EXPECT_EQ(L"foo", format(L"{}", explicitly_convertible_to_wstring_view()));
|
||||
}
|
||||
|
||||
struct explicitly_convertible_to_string_like {
|
||||
template <
|
||||
typename String,
|
||||
typename = typename std::enable_if<
|
||||
std::is_constructible<String, const char*, std::size_t>::value>::type>
|
||||
explicit operator String() const { return String("foo", 3u); }
|
||||
};
|
||||
|
||||
TEST(FormatterTest, FormatExplicitlyConvertibleToStringLike) {
|
||||
EXPECT_EQ("foo", format("{}", explicitly_convertible_to_string_like()));
|
||||
}
|
||||
#endif
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
template <>
|
||||
struct formatter<Date> {
|
||||
@@ -1285,6 +1777,21 @@ TEST(FormatTest, Variadic) {
|
||||
EXPECT_EQ(L"abc1", format(L"{}c{}", L"ab", 1));
|
||||
}
|
||||
|
||||
TEST(FormatTest, Dynamic) {
|
||||
using ctx = fmt::format_context;
|
||||
std::vector<fmt::basic_format_arg<ctx>> args;
|
||||
args.emplace_back(fmt::internal::make_arg<ctx>(42));
|
||||
args.emplace_back(fmt::internal::make_arg<ctx>("abc1"));
|
||||
args.emplace_back(fmt::internal::make_arg<ctx>(1.2f));
|
||||
|
||||
std::string result = fmt::vformat("{} and {} and {}",
|
||||
fmt::basic_format_args<ctx>(
|
||||
args.data(),
|
||||
static_cast<unsigned>(args.size())));
|
||||
|
||||
EXPECT_EQ("42 and abc1 and 1.2", result);
|
||||
}
|
||||
|
||||
TEST(FormatTest, JoinArg) {
|
||||
using fmt::join;
|
||||
int v1[3] = { 1, 2, 3 };
|
||||
@@ -1397,6 +1904,10 @@ TEST(FormatTest, Enum) {
|
||||
EXPECT_EQ("0", fmt::format("{}", A));
|
||||
}
|
||||
|
||||
TEST(FormatTest, EnumFormatterUnambiguous) {
|
||||
fmt::formatter<TestEnum> f;
|
||||
}
|
||||
|
||||
#if FMT_HAS_FEATURE(cxx_strong_enums)
|
||||
enum TestFixedEnum : short { B };
|
||||
|
||||
@@ -1412,13 +1923,13 @@ class mock_arg_formatter:
|
||||
fmt::internal::arg_formatter_base<buffer_range>::iterator>,
|
||||
public fmt::internal::arg_formatter_base<buffer_range> {
|
||||
private:
|
||||
MOCK_METHOD1(call, void (int value));
|
||||
MOCK_METHOD1(call, void (long long value));
|
||||
|
||||
public:
|
||||
typedef fmt::internal::arg_formatter_base<buffer_range> base;
|
||||
typedef buffer_range range;
|
||||
|
||||
mock_arg_formatter(fmt::format_context &ctx, fmt::format_specs &s)
|
||||
mock_arg_formatter(fmt::format_context &ctx, fmt::format_specs *s = FMT_NULL)
|
||||
: base(fmt::internal::get_container(ctx.out()), s) {
|
||||
EXPECT_CALL(*this, call(42));
|
||||
}
|
||||
@@ -1760,18 +2271,20 @@ struct test_format_string_handler {
|
||||
template <typename T>
|
||||
FMT_CONSTEXPR void on_arg_id(T) {}
|
||||
|
||||
FMT_CONSTEXPR void on_replacement_field(const char *) {}
|
||||
template <typename Iterator>
|
||||
FMT_CONSTEXPR void on_replacement_field(Iterator) {}
|
||||
|
||||
FMT_CONSTEXPR const char *on_format_specs(const char *s) { return s; }
|
||||
template <typename Iterator>
|
||||
FMT_CONSTEXPR Iterator on_format_specs(Iterator it) { return it; }
|
||||
|
||||
FMT_CONSTEXPR void on_error(const char *) { error = true; }
|
||||
|
||||
bool error = false;
|
||||
};
|
||||
|
||||
FMT_CONSTEXPR bool parse_string(const char *s) {
|
||||
FMT_CONSTEXPR bool parse_string(fmt::string_view s) {
|
||||
test_format_string_handler h;
|
||||
fmt::internal::parse_format_string(s, h);
|
||||
fmt::internal::parse_format_string<true>(s, h);
|
||||
return !h.error;
|
||||
}
|
||||
|
||||
@@ -1884,3 +2397,30 @@ TEST(FormatTest, FormatStringErrors) {
|
||||
}
|
||||
#endif // FMT_USE_CONSTEXPR
|
||||
|
||||
TEST(FormatTest, ConstructU8StringViewFromCString) {
|
||||
fmt::u8string_view s("ab");
|
||||
EXPECT_EQ(s.size(), 2u);
|
||||
const fmt::char8_t *data = s.data();
|
||||
EXPECT_EQ(data[0].value, 'a');
|
||||
EXPECT_EQ(data[1].value, 'b');
|
||||
}
|
||||
|
||||
TEST(FormatTest, ConstructU8StringViewFromDataAndSize) {
|
||||
fmt::u8string_view s("foobar", 3);
|
||||
EXPECT_EQ(s.size(), 3u);
|
||||
const fmt::char8_t *data = s.data();
|
||||
EXPECT_EQ(data[0].value, 'f');
|
||||
EXPECT_EQ(data[1].value, 'o');
|
||||
EXPECT_EQ(data[2].value, 'o');
|
||||
}
|
||||
|
||||
#if FMT_USE_USER_DEFINED_LITERALS
|
||||
TEST(FormatTest, U8StringViewLiteral) {
|
||||
using namespace fmt::literals;
|
||||
fmt::u8string_view s = "ab"_u;
|
||||
EXPECT_EQ(s.size(), 2u);
|
||||
const fmt::char8_t *data = s.data();
|
||||
EXPECT_EQ(data[0].value, 'a');
|
||||
EXPECT_EQ(data[1].value, 'b');
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -9,53 +9,48 @@
|
||||
#define FMT_MOCK_ALLOCATOR_H_
|
||||
|
||||
#include "gmock.h"
|
||||
#include "fmt/format.h"
|
||||
|
||||
template <typename T>
|
||||
class MockAllocator {
|
||||
class mock_allocator {
|
||||
public:
|
||||
MockAllocator() {}
|
||||
MockAllocator(const MockAllocator &) {}
|
||||
mock_allocator() {}
|
||||
mock_allocator(const mock_allocator &) {}
|
||||
typedef T value_type;
|
||||
MOCK_METHOD1_T(allocate, T* (std::size_t n));
|
||||
MOCK_METHOD2_T(deallocate, void (T* p, std::size_t n));
|
||||
};
|
||||
|
||||
template <typename Allocator>
|
||||
class AllocatorRef {
|
||||
class allocator_ref {
|
||||
private:
|
||||
Allocator *alloc_;
|
||||
|
||||
public:
|
||||
typedef typename Allocator::value_type value_type;
|
||||
|
||||
explicit AllocatorRef(Allocator *alloc = nullptr) : alloc_(alloc) {}
|
||||
|
||||
AllocatorRef(const AllocatorRef &other) : alloc_(other.alloc_) {}
|
||||
|
||||
AllocatorRef& operator=(const AllocatorRef &other) {
|
||||
alloc_ = other.alloc_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if FMT_USE_RVALUE_REFERENCES
|
||||
private:
|
||||
void move(AllocatorRef &other) {
|
||||
void move(allocator_ref &other) {
|
||||
alloc_ = other.alloc_;
|
||||
other.alloc_ = nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
AllocatorRef(AllocatorRef &&other) {
|
||||
move(other);
|
||||
}
|
||||
typedef typename Allocator::value_type value_type;
|
||||
|
||||
AllocatorRef& operator=(AllocatorRef &&other) {
|
||||
explicit allocator_ref(Allocator *alloc = nullptr) : alloc_(alloc) {}
|
||||
|
||||
allocator_ref(const allocator_ref &other) : alloc_(other.alloc_) {}
|
||||
allocator_ref(allocator_ref &&other) { move(other); }
|
||||
|
||||
allocator_ref& operator=(allocator_ref &&other) {
|
||||
assert(this != &other);
|
||||
move(other);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
allocator_ref& operator=(const allocator_ref &other) {
|
||||
alloc_ = other.alloc_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
public:
|
||||
Allocator *get() const { return alloc_; }
|
||||
|
||||
value_type* allocate(std::size_t n) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#define FMT_STRING_ALIAS 1
|
||||
#include "fmt/ostream.h"
|
||||
|
||||
#include <sstream>
|
||||
@@ -37,10 +38,10 @@ static std::wostream &operator<<(std::wostream &os, TestEnum) {
|
||||
enum TestEnum2 {A};
|
||||
|
||||
TEST(OStreamTest, Enum) {
|
||||
EXPECT_FALSE((fmt::internal::convert_to_int<TestEnum, char>::value));
|
||||
EXPECT_FALSE((fmt::convert_to_int<TestEnum, char>::value));
|
||||
EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum()));
|
||||
EXPECT_EQ("0", fmt::format("{}", A));
|
||||
EXPECT_FALSE((fmt::internal::convert_to_int<TestEnum, wchar_t>::value));
|
||||
EXPECT_FALSE((fmt::convert_to_int<TestEnum, wchar_t>::value));
|
||||
EXPECT_EQ(L"TestEnum", fmt::format(L"{}", TestEnum()));
|
||||
EXPECT_EQ(L"0", fmt::format(L"{}", A));
|
||||
}
|
||||
@@ -49,7 +50,7 @@ typedef fmt::back_insert_range<fmt::internal::buffer> range;
|
||||
|
||||
struct test_arg_formatter: fmt::arg_formatter<range> {
|
||||
test_arg_formatter(fmt::format_context &ctx, fmt::format_specs &s)
|
||||
: fmt::arg_formatter<range>(ctx, s) {}
|
||||
: fmt::arg_formatter<range>(ctx, &s) {}
|
||||
};
|
||||
|
||||
TEST(OStreamTest, CustomArg) {
|
||||
@@ -169,3 +170,32 @@ TEST(OStreamTest, ConstexprString) {
|
||||
EXPECT_EQ("42", format(fmt("{}"), std::string("42")));
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace fmt_test {
|
||||
struct ABC {};
|
||||
|
||||
template <typename Output> Output &operator<<(Output &out, ABC) {
|
||||
out << "ABC";
|
||||
return out;
|
||||
}
|
||||
} // namespace fmt_test
|
||||
|
||||
TEST(FormatTest, FormatToN) {
|
||||
char buffer[4];
|
||||
buffer[3] = 'x';
|
||||
auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::ABC());
|
||||
EXPECT_EQ(3u, result.size);
|
||||
EXPECT_EQ(buffer + 3, result.out);
|
||||
EXPECT_EQ("ABCx", fmt::string_view(buffer, 4));
|
||||
result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::ABC());
|
||||
EXPECT_EQ(5u, result.size);
|
||||
EXPECT_EQ(buffer + 3, result.out);
|
||||
EXPECT_EQ("xABx", fmt::string_view(buffer, 4));
|
||||
}
|
||||
|
||||
#if FMT_USE_USER_DEFINED_LITERALS
|
||||
TEST(FormatTest, UDL) {
|
||||
using namespace fmt::literals;
|
||||
EXPECT_EQ("{}"_format("test"), "test");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -180,8 +180,13 @@ TEST(PrintfTest, HashFlag) {
|
||||
safe_sprintf(buffer, "%#E", -42.0);
|
||||
EXPECT_PRINTF(buffer, "%#E", -42.0);
|
||||
|
||||
EXPECT_PRINTF("-42.0000", "%#g", -42.0);
|
||||
EXPECT_PRINTF("-42.0000", "%#G", -42.0);
|
||||
if (fmt::internal::use_grisu()) {
|
||||
EXPECT_PRINTF("-42.0", "%#g", -42.0);
|
||||
EXPECT_PRINTF("-42.0", "%#G", -42.0);
|
||||
} else {
|
||||
EXPECT_PRINTF("-42.0000", "%#g", -42.0);
|
||||
EXPECT_PRINTF("-42.0000", "%#G", -42.0);
|
||||
}
|
||||
|
||||
safe_sprintf(buffer, "%#a", 16.0);
|
||||
EXPECT_PRINTF(buffer, "%#a", 16.0);
|
||||
|
||||
@@ -5,23 +5,22 @@
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#ifndef FMT_TEST_ASSERT_H
|
||||
#define FMT_TEST_ASSERT_H
|
||||
#ifndef FMT_TEST_ASSERT_H_
|
||||
#define FMT_TEST_ASSERT_H_
|
||||
|
||||
#include <stdexcept>
|
||||
#include "gtest.h"
|
||||
|
||||
class AssertionFailure : public std::logic_error {
|
||||
class assertion_failure : public std::logic_error {
|
||||
public:
|
||||
explicit AssertionFailure(const char *message) : std::logic_error(message) {}
|
||||
explicit assertion_failure(const char *message) : std::logic_error(message) {}
|
||||
};
|
||||
|
||||
#define FMT_ASSERT(condition, message) \
|
||||
if (!(condition)) throw AssertionFailure(message);
|
||||
|
||||
#include "gtest-extra.h"
|
||||
if (!(condition)) throw assertion_failure(message);
|
||||
|
||||
// Expects an assertion failure.
|
||||
#define EXPECT_ASSERT(stmt, message) \
|
||||
EXPECT_THROW_MSG(stmt, AssertionFailure, message)
|
||||
FMT_TEST_THROW_(stmt, assertion_failure, message, GTEST_NONFATAL_FAILURE_)
|
||||
|
||||
#endif // FMT_TEST_ASSERT_H
|
||||
#endif // FMT_TEST_ASSERT_H_
|
||||
|
||||
@@ -1,948 +0,0 @@
|
||||
// Formatting library for C++ - utility tests
|
||||
//
|
||||
// Copyright (c) 2012 - present, Victor Zverovich
|
||||
// All rights reserved.
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#include "test-assert.h"
|
||||
|
||||
#include <cfloat>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
|
||||
#if FMT_USE_TYPE_TRAITS
|
||||
# include <type_traits>
|
||||
#endif
|
||||
|
||||
#include "gmock.h"
|
||||
#include "gtest-extra.h"
|
||||
#include "mock-allocator.h"
|
||||
#include "util.h"
|
||||
|
||||
// Check if format.h compiles with windows.h included.
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "fmt/core.h"
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
using fmt::basic_format_arg;
|
||||
using fmt::internal::basic_buffer;
|
||||
using fmt::basic_memory_buffer;
|
||||
using fmt::string_view;
|
||||
using fmt::internal::fp;
|
||||
using fmt::internal::value;
|
||||
|
||||
using testing::_;
|
||||
using testing::Return;
|
||||
using testing::StrictMock;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Test {};
|
||||
|
||||
template <typename Context, typename T>
|
||||
basic_format_arg<Context> make_arg(const T &value) {
|
||||
return fmt::internal::make_arg<Context>(value);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
template <typename Char>
|
||||
struct formatter<Test, Char> {
|
||||
template <typename ParseContext>
|
||||
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
typedef std::back_insert_iterator<basic_buffer<Char>> iterator;
|
||||
|
||||
auto format(Test, basic_format_context<iterator, char> &ctx)
|
||||
-> decltype(ctx.out()) {
|
||||
const Char *test = "test";
|
||||
return std::copy_n(test, std::strlen(test), ctx.out());
|
||||
}
|
||||
};
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
static void CheckForwarding(
|
||||
MockAllocator<int> &alloc, AllocatorRef<MockAllocator<int>> &ref) {
|
||||
int mem;
|
||||
// Check if value_type is properly defined.
|
||||
AllocatorRef< MockAllocator<int> >::value_type *ptr = &mem;
|
||||
// Check forwarding.
|
||||
EXPECT_CALL(alloc, allocate(42)).WillOnce(Return(ptr));
|
||||
ref.allocate(42);
|
||||
EXPECT_CALL(alloc, deallocate(ptr, 42));
|
||||
ref.deallocate(ptr, 42);
|
||||
}
|
||||
|
||||
TEST(AllocatorTest, AllocatorRef) {
|
||||
StrictMock< MockAllocator<int> > alloc;
|
||||
typedef AllocatorRef< MockAllocator<int> > TestAllocatorRef;
|
||||
TestAllocatorRef ref(&alloc);
|
||||
// Check if AllocatorRef forwards to the underlying allocator.
|
||||
CheckForwarding(alloc, ref);
|
||||
TestAllocatorRef ref2(ref);
|
||||
CheckForwarding(alloc, ref2);
|
||||
TestAllocatorRef ref3;
|
||||
EXPECT_EQ(nullptr, ref3.get());
|
||||
ref3 = ref;
|
||||
CheckForwarding(alloc, ref3);
|
||||
}
|
||||
|
||||
#if FMT_USE_TYPE_TRAITS
|
||||
TEST(BufferTest, Noncopyable) {
|
||||
EXPECT_FALSE(std::is_copy_constructible<basic_buffer<char> >::value);
|
||||
EXPECT_FALSE(std::is_copy_assignable<basic_buffer<char> >::value);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Nonmoveable) {
|
||||
EXPECT_FALSE(std::is_move_constructible<basic_buffer<char> >::value);
|
||||
EXPECT_FALSE(std::is_move_assignable<basic_buffer<char> >::value);
|
||||
}
|
||||
#endif
|
||||
|
||||
// A test buffer with a dummy grow method.
|
||||
template <typename T>
|
||||
struct TestBuffer : basic_buffer<T> {
|
||||
void grow(std::size_t capacity) { this->set(nullptr, capacity); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct MockBuffer : basic_buffer<T> {
|
||||
MOCK_METHOD1(do_grow, void (std::size_t capacity));
|
||||
|
||||
void grow(std::size_t capacity) {
|
||||
this->set(this->data(), capacity);
|
||||
do_grow(capacity);
|
||||
}
|
||||
|
||||
MockBuffer() {}
|
||||
MockBuffer(T *data) { this->set(data, 0); }
|
||||
MockBuffer(T *data, std::size_t capacity) { this->set(data, capacity); }
|
||||
};
|
||||
|
||||
TEST(BufferTest, Ctor) {
|
||||
{
|
||||
MockBuffer<int> buffer;
|
||||
EXPECT_EQ(nullptr, &buffer[0]);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
|
||||
}
|
||||
{
|
||||
int dummy;
|
||||
MockBuffer<int> buffer(&dummy);
|
||||
EXPECT_EQ(&dummy, &buffer[0]);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
|
||||
}
|
||||
{
|
||||
int dummy;
|
||||
std::size_t capacity = std::numeric_limits<std::size_t>::max();
|
||||
MockBuffer<int> buffer(&dummy, capacity);
|
||||
EXPECT_EQ(&dummy, &buffer[0]);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(capacity, buffer.capacity());
|
||||
}
|
||||
}
|
||||
|
||||
struct DyingBuffer : TestBuffer<int> {
|
||||
MOCK_METHOD0(die, void());
|
||||
~DyingBuffer() { die(); }
|
||||
};
|
||||
|
||||
TEST(BufferTest, VirtualDtor) {
|
||||
typedef StrictMock<DyingBuffer> StictMockBuffer;
|
||||
StictMockBuffer *mock_buffer = new StictMockBuffer();
|
||||
EXPECT_CALL(*mock_buffer, die());
|
||||
basic_buffer<int> *buffer = mock_buffer;
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
TEST(BufferTest, Access) {
|
||||
char data[10];
|
||||
MockBuffer<char> buffer(data, sizeof(data));
|
||||
buffer[0] = 11;
|
||||
EXPECT_EQ(11, buffer[0]);
|
||||
buffer[3] = 42;
|
||||
EXPECT_EQ(42, *(&buffer[0] + 3));
|
||||
const basic_buffer<char> &const_buffer = buffer;
|
||||
EXPECT_EQ(42, const_buffer[3]);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Resize) {
|
||||
char data[123];
|
||||
MockBuffer<char> buffer(data, sizeof(data));
|
||||
buffer[10] = 42;
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
buffer.resize(20);
|
||||
EXPECT_EQ(20u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
buffer.resize(5);
|
||||
EXPECT_EQ(5u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
// Check if resize calls grow.
|
||||
EXPECT_CALL(buffer, do_grow(124));
|
||||
buffer.resize(124);
|
||||
EXPECT_CALL(buffer, do_grow(200));
|
||||
buffer.resize(200);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Clear) {
|
||||
TestBuffer<char> buffer;
|
||||
buffer.resize(20);
|
||||
buffer.resize(0);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(20u, buffer.capacity());
|
||||
}
|
||||
|
||||
TEST(BufferTest, Append) {
|
||||
char data[15];
|
||||
MockBuffer<char> buffer(data, 10);
|
||||
const char *test = "test";
|
||||
buffer.append(test, test + 5);
|
||||
EXPECT_STREQ(test, &buffer[0]);
|
||||
EXPECT_EQ(5u, buffer.size());
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(12));
|
||||
buffer.append(test, test + 2);
|
||||
EXPECT_EQ('t', buffer[10]);
|
||||
EXPECT_EQ('e', buffer[11]);
|
||||
EXPECT_EQ(12u, buffer.size());
|
||||
}
|
||||
|
||||
TEST(BufferTest, AppendAllocatesEnoughStorage) {
|
||||
char data[19];
|
||||
MockBuffer<char> buffer(data, 10);
|
||||
const char *test = "abcdefgh";
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(19));
|
||||
buffer.append(test, test + 9);
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Ctor) {
|
||||
basic_memory_buffer<char, 123> buffer;
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
}
|
||||
|
||||
#if FMT_USE_RVALUE_REFERENCES
|
||||
|
||||
typedef AllocatorRef< std::allocator<char> > TestAllocator;
|
||||
|
||||
static void check_move_buffer(const char *str,
|
||||
basic_memory_buffer<char, 5, TestAllocator> &buffer) {
|
||||
std::allocator<char> *alloc = buffer.get_allocator().get();
|
||||
basic_memory_buffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
||||
// Move shouldn't destroy the inline content of the first buffer.
|
||||
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
||||
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_EQ(5u, buffer2.capacity());
|
||||
// Move should transfer allocator.
|
||||
EXPECT_EQ(nullptr, buffer.get_allocator().get());
|
||||
EXPECT_EQ(alloc, buffer2.get_allocator().get());
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, MoveCtor) {
|
||||
std::allocator<char> alloc;
|
||||
basic_memory_buffer<char, 5, TestAllocator> buffer((TestAllocator(&alloc)));
|
||||
const char test[] = "test";
|
||||
buffer.append(test, test + 4);
|
||||
check_move_buffer("test", buffer);
|
||||
// Adding one more character fills the inline buffer, but doesn't cause
|
||||
// dynamic allocation.
|
||||
buffer.push_back('a');
|
||||
check_move_buffer("testa", buffer);
|
||||
const char *inline_buffer_ptr = &buffer[0];
|
||||
// Adding one more character causes the content to move from the inline to
|
||||
// a dynamically allocated buffer.
|
||||
buffer.push_back('b');
|
||||
basic_memory_buffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
||||
// Move should rip the guts of the first buffer.
|
||||
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
||||
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_GT(buffer2.capacity(), 5u);
|
||||
}
|
||||
|
||||
static void check_move_assign_buffer(
|
||||
const char *str, basic_memory_buffer<char, 5> &buffer) {
|
||||
basic_memory_buffer<char, 5> buffer2;
|
||||
buffer2 = std::move(buffer);
|
||||
// Move shouldn't destroy the inline content of the first buffer.
|
||||
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
||||
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_EQ(5u, buffer2.capacity());
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, MoveAssignment) {
|
||||
basic_memory_buffer<char, 5> buffer;
|
||||
const char test[] = "test";
|
||||
buffer.append(test, test + 4);
|
||||
check_move_assign_buffer("test", buffer);
|
||||
// Adding one more character fills the inline buffer, but doesn't cause
|
||||
// dynamic allocation.
|
||||
buffer.push_back('a');
|
||||
check_move_assign_buffer("testa", buffer);
|
||||
const char *inline_buffer_ptr = &buffer[0];
|
||||
// Adding one more character causes the content to move from the inline to
|
||||
// a dynamically allocated buffer.
|
||||
buffer.push_back('b');
|
||||
basic_memory_buffer<char, 5> buffer2;
|
||||
buffer2 = std::move(buffer);
|
||||
// Move should rip the guts of the first buffer.
|
||||
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
||||
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_GT(buffer2.capacity(), 5u);
|
||||
}
|
||||
|
||||
#endif // FMT_USE_RVALUE_REFERENCES
|
||||
|
||||
TEST(MemoryBufferTest, Grow) {
|
||||
typedef AllocatorRef< MockAllocator<int> > Allocator;
|
||||
typedef basic_memory_buffer<int, 10, Allocator> Base;
|
||||
MockAllocator<int> alloc;
|
||||
struct TestMemoryBuffer : Base {
|
||||
TestMemoryBuffer(Allocator alloc) : Base(alloc) {}
|
||||
void grow(std::size_t size) { Base::grow(size); }
|
||||
} buffer((Allocator(&alloc)));
|
||||
buffer.resize(7);
|
||||
using fmt::internal::to_unsigned;
|
||||
for (int i = 0; i < 7; ++i)
|
||||
buffer[to_unsigned(i)] = i * i;
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
int mem[20];
|
||||
mem[7] = 0xdead;
|
||||
EXPECT_CALL(alloc, allocate(20)).WillOnce(Return(mem));
|
||||
buffer.grow(20);
|
||||
EXPECT_EQ(20u, buffer.capacity());
|
||||
// Check if size elements have been copied
|
||||
for (int i = 0; i < 7; ++i)
|
||||
EXPECT_EQ(i * i, buffer[to_unsigned(i)]);
|
||||
// and no more than that.
|
||||
EXPECT_EQ(0xdead, buffer[7]);
|
||||
EXPECT_CALL(alloc, deallocate(mem, 20));
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Allocator) {
|
||||
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
|
||||
basic_memory_buffer<char, 10, TestAllocator> buffer;
|
||||
EXPECT_EQ(nullptr, buffer.get_allocator().get());
|
||||
StrictMock< MockAllocator<char> > alloc;
|
||||
char mem;
|
||||
{
|
||||
basic_memory_buffer<char, 10, TestAllocator> buffer2((TestAllocator(&alloc)));
|
||||
EXPECT_EQ(&alloc, buffer2.get_allocator().get());
|
||||
std::size_t size = 2 * fmt::inline_buffer_size;
|
||||
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem));
|
||||
buffer2.reserve(size);
|
||||
EXPECT_CALL(alloc, deallocate(&mem, size));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, ExceptionInDeallocate) {
|
||||
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
|
||||
StrictMock< MockAllocator<char> > alloc;
|
||||
basic_memory_buffer<char, 10, TestAllocator> buffer((TestAllocator(&alloc)));
|
||||
std::size_t size = 2 * fmt::inline_buffer_size;
|
||||
std::vector<char> mem(size);
|
||||
{
|
||||
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0]));
|
||||
buffer.resize(size);
|
||||
std::fill(&buffer[0], &buffer[0] + size, 'x');
|
||||
}
|
||||
std::vector<char> mem2(2 * size);
|
||||
{
|
||||
EXPECT_CALL(alloc, allocate(2 * size)).WillOnce(Return(&mem2[0]));
|
||||
std::exception e;
|
||||
EXPECT_CALL(alloc, deallocate(&mem[0], size)).WillOnce(testing::Throw(e));
|
||||
EXPECT_THROW(buffer.reserve(2 * size), std::exception);
|
||||
EXPECT_EQ(&mem2[0], &buffer[0]);
|
||||
// Check that the data has been copied.
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
EXPECT_EQ('x', buffer[i]);
|
||||
}
|
||||
EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size));
|
||||
}
|
||||
|
||||
TEST(FixedBufferTest, Ctor) {
|
||||
char array[10] = "garbage";
|
||||
fmt::basic_fixed_buffer<char> buffer(array, sizeof(array));
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
EXPECT_EQ(array, buffer.data());
|
||||
}
|
||||
|
||||
TEST(FixedBufferTest, CompileTimeSizeCtor) {
|
||||
char array[10] = "garbage";
|
||||
fmt::basic_fixed_buffer<char> buffer(array);
|
||||
EXPECT_EQ(static_cast<size_t>(0), buffer.size());
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
EXPECT_EQ(array, buffer.data());
|
||||
}
|
||||
|
||||
TEST(FixedBufferTest, BufferOverflow) {
|
||||
char array[10];
|
||||
fmt::basic_fixed_buffer<char> buffer(array);
|
||||
buffer.resize(10);
|
||||
EXPECT_THROW_MSG(buffer.resize(11), std::runtime_error, "buffer overflow");
|
||||
}
|
||||
|
||||
struct uint32_pair {
|
||||
uint32_t u[2];
|
||||
};
|
||||
|
||||
TEST(UtilTest, BitCast) {
|
||||
auto s = fmt::internal::bit_cast<uint32_pair>(uint64_t{42});
|
||||
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), 42ull);
|
||||
s = fmt::internal::bit_cast<uint32_pair>(uint64_t(~0ull));
|
||||
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), ~0ull);
|
||||
}
|
||||
|
||||
TEST(UtilTest, Increment) {
|
||||
char s[10] = "123";
|
||||
increment(s);
|
||||
EXPECT_STREQ("124", s);
|
||||
s[2] = '8';
|
||||
increment(s);
|
||||
EXPECT_STREQ("129", s);
|
||||
increment(s);
|
||||
EXPECT_STREQ("130", s);
|
||||
s[1] = s[2] = '9';
|
||||
increment(s);
|
||||
EXPECT_STREQ("200", s);
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatArgs) {
|
||||
fmt::format_args args;
|
||||
EXPECT_FALSE(args.get(1));
|
||||
}
|
||||
|
||||
struct custom_context {
|
||||
typedef char char_type;
|
||||
|
||||
template <typename T>
|
||||
struct formatter_type {
|
||||
struct type {
|
||||
template <typename ParseContext>
|
||||
auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
const char *format(const T &, custom_context& ctx) {
|
||||
ctx.called = true;
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
bool called;
|
||||
|
||||
fmt::parse_context parse_context() { return fmt::parse_context(""); }
|
||||
void advance_to(const char *) {}
|
||||
};
|
||||
|
||||
TEST(UtilTest, MakeValueWithCustomFormatter) {
|
||||
::Test t;
|
||||
fmt::internal::value<custom_context> arg =
|
||||
fmt::internal::make_value<custom_context>(t);
|
||||
custom_context ctx = {false};
|
||||
arg.custom.format(&t, ctx);
|
||||
EXPECT_TRUE(ctx.called);
|
||||
}
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
namespace internal {
|
||||
|
||||
template <typename Char>
|
||||
bool operator==(custom_value<Char> lhs, custom_value<Char> rhs) {
|
||||
return lhs.value == rhs.value;
|
||||
}
|
||||
}
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
// Use a unique result type to make sure that there are no undesirable
|
||||
// conversions.
|
||||
struct Result {};
|
||||
|
||||
template <typename T>
|
||||
struct MockVisitor: fmt::internal::function<Result> {
|
||||
MockVisitor() {
|
||||
ON_CALL(*this, visit(_)).WillByDefault(Return(Result()));
|
||||
}
|
||||
|
||||
MOCK_METHOD1_T(visit, Result (T value));
|
||||
MOCK_METHOD0_T(unexpected, void ());
|
||||
|
||||
Result operator()(T value) { return visit(value); }
|
||||
|
||||
template <typename U>
|
||||
Result operator()(U) {
|
||||
unexpected();
|
||||
return Result();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct VisitType { typedef T Type; };
|
||||
|
||||
#define VISIT_TYPE(Type_, VisitType_) \
|
||||
template <> \
|
||||
struct VisitType<Type_> { typedef VisitType_ Type; }
|
||||
|
||||
VISIT_TYPE(signed char, int);
|
||||
VISIT_TYPE(unsigned char, unsigned);
|
||||
VISIT_TYPE(short, int);
|
||||
VISIT_TYPE(unsigned short, unsigned);
|
||||
|
||||
#if LONG_MAX == INT_MAX
|
||||
VISIT_TYPE(long, int);
|
||||
VISIT_TYPE(unsigned long, unsigned);
|
||||
#else
|
||||
VISIT_TYPE(long, long long);
|
||||
VISIT_TYPE(unsigned long, unsigned long long);
|
||||
#endif
|
||||
|
||||
VISIT_TYPE(float, double);
|
||||
|
||||
#define CHECK_ARG_(Char, expected, value) { \
|
||||
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
|
||||
EXPECT_CALL(visitor, visit(expected)); \
|
||||
typedef std::back_insert_iterator<basic_buffer<Char>> iterator; \
|
||||
fmt::visit(visitor, \
|
||||
make_arg<fmt::basic_format_context<iterator, Char>>(value)); \
|
||||
}
|
||||
|
||||
#define CHECK_ARG(value, typename_) { \
|
||||
typedef decltype(value) value_type; \
|
||||
typename_ VisitType<value_type>::Type expected = value; \
|
||||
CHECK_ARG_(char, expected, value) \
|
||||
CHECK_ARG_(wchar_t, expected, value) \
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class NumericArgTest : public testing::Test {};
|
||||
|
||||
typedef ::testing::Types<
|
||||
bool, signed char, unsigned char, signed, unsigned short,
|
||||
int, unsigned, long, unsigned long, long long, unsigned long long,
|
||||
float, double, long double> Types;
|
||||
TYPED_TEST_CASE(NumericArgTest, Types);
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type test_value() {
|
||||
return static_cast<T>(42);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type
|
||||
test_value() {
|
||||
return static_cast<T>(4.2);
|
||||
}
|
||||
|
||||
TYPED_TEST(NumericArgTest, MakeAndVisit) {
|
||||
CHECK_ARG(test_value<TypeParam>(), typename);
|
||||
CHECK_ARG(std::numeric_limits<TypeParam>::min(), typename);
|
||||
CHECK_ARG(std::numeric_limits<TypeParam>::max(), typename);
|
||||
}
|
||||
|
||||
TEST(UtilTest, CharArg) {
|
||||
CHECK_ARG_(char, 'a', 'a');
|
||||
CHECK_ARG_(wchar_t, L'a', 'a');
|
||||
CHECK_ARG_(wchar_t, L'a', L'a');
|
||||
}
|
||||
|
||||
TEST(UtilTest, StringArg) {
|
||||
char str_data[] = "test";
|
||||
char *str = str_data;
|
||||
const char *cstr = str;
|
||||
CHECK_ARG_(char, cstr, str);
|
||||
|
||||
string_view sref(str);
|
||||
CHECK_ARG_(char, sref, std::string(str));
|
||||
}
|
||||
|
||||
TEST(UtilTest, WStringArg) {
|
||||
wchar_t str_data[] = L"test";
|
||||
wchar_t *str = str_data;
|
||||
const wchar_t *cstr = str;
|
||||
|
||||
fmt::wstring_view sref(str);
|
||||
CHECK_ARG_(wchar_t, cstr, str);
|
||||
CHECK_ARG_(wchar_t, cstr, cstr);
|
||||
CHECK_ARG_(wchar_t, sref, std::wstring(str));
|
||||
CHECK_ARG_(wchar_t, sref, fmt::wstring_view(str));
|
||||
}
|
||||
|
||||
TEST(UtilTest, PointerArg) {
|
||||
void *p = nullptr;
|
||||
const void *cp = nullptr;
|
||||
CHECK_ARG_(char, cp, p);
|
||||
CHECK_ARG_(wchar_t, cp, p);
|
||||
CHECK_ARG(cp, );
|
||||
}
|
||||
|
||||
struct check_custom {
|
||||
Result operator()(fmt::basic_format_arg<fmt::format_context>::handle h) const {
|
||||
fmt::memory_buffer buffer;
|
||||
fmt::internal::basic_buffer<char> &base = buffer;
|
||||
fmt::format_context ctx(std::back_inserter(base), "", fmt::format_args());
|
||||
h.format(ctx);
|
||||
EXPECT_EQ("test", std::string(buffer.data(), buffer.size()));
|
||||
return Result();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(UtilTest, CustomArg) {
|
||||
::Test test;
|
||||
typedef MockVisitor<fmt::basic_format_arg<fmt::format_context>::handle>
|
||||
visitor;
|
||||
testing::StrictMock<visitor> v;
|
||||
EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke(check_custom()));
|
||||
fmt::visit(v, make_arg<fmt::format_context>(test));
|
||||
}
|
||||
|
||||
TEST(ArgVisitorTest, VisitInvalidArg) {
|
||||
typedef MockVisitor<fmt::monostate> Visitor;
|
||||
testing::StrictMock<Visitor> visitor;
|
||||
EXPECT_CALL(visitor, visit(_));
|
||||
fmt::basic_format_arg<fmt::format_context> arg;
|
||||
visit(visitor, arg);
|
||||
}
|
||||
|
||||
// Tests fmt::internal::count_digits for integer type Int.
|
||||
template <typename Int>
|
||||
void test_count_digits() {
|
||||
for (Int i = 0; i < 10; ++i)
|
||||
EXPECT_EQ(1u, fmt::internal::count_digits(i));
|
||||
for (Int i = 1, n = 1,
|
||||
end = std::numeric_limits<Int>::max() / 10; n <= end; ++i) {
|
||||
n *= 10;
|
||||
EXPECT_EQ(i, fmt::internal::count_digits(n - 1));
|
||||
EXPECT_EQ(i + 1, fmt::internal::count_digits(n));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UtilTest, StringRef) {
|
||||
// Test that StringRef::size() returns string length, not buffer size.
|
||||
char str[100] = "some string";
|
||||
EXPECT_EQ(std::strlen(str), string_view(str).size());
|
||||
EXPECT_LT(std::strlen(str), sizeof(str));
|
||||
}
|
||||
|
||||
// Check StringRef's comparison operator.
|
||||
template <template <typename> class Op>
|
||||
void CheckOp() {
|
||||
const char *inputs[] = {"foo", "fop", "fo"};
|
||||
std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
|
||||
for (std::size_t i = 0; i < num_inputs; ++i) {
|
||||
for (std::size_t j = 0; j < num_inputs; ++j) {
|
||||
string_view lhs(inputs[i]), rhs(inputs[j]);
|
||||
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UtilTest, StringRefCompare) {
|
||||
EXPECT_EQ(0, string_view("foo").compare(string_view("foo")));
|
||||
EXPECT_GT(string_view("fop").compare(string_view("foo")), 0);
|
||||
EXPECT_LT(string_view("foo").compare(string_view("fop")), 0);
|
||||
EXPECT_GT(string_view("foo").compare(string_view("fo")), 0);
|
||||
EXPECT_LT(string_view("fo").compare(string_view("foo")), 0);
|
||||
CheckOp<std::equal_to>();
|
||||
CheckOp<std::not_equal_to>();
|
||||
CheckOp<std::less>();
|
||||
CheckOp<std::less_equal>();
|
||||
CheckOp<std::greater>();
|
||||
CheckOp<std::greater_equal>();
|
||||
}
|
||||
|
||||
TEST(UtilTest, CountDigits) {
|
||||
test_count_digits<uint32_t>();
|
||||
test_count_digits<uint64_t>();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TEST(UtilTest, UTF16ToUTF8) {
|
||||
std::string s = "ёжик";
|
||||
fmt::internal::utf16_to_utf8 u(L"\x0451\x0436\x0438\x043A");
|
||||
EXPECT_EQ(s, u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8EmptyString) {
|
||||
std::string s = "";
|
||||
fmt::internal::utf16_to_utf8 u(L"");
|
||||
EXPECT_EQ(s, u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16) {
|
||||
std::string s = "лошадка";
|
||||
fmt::internal::utf8_to_utf16 u(s.c_str());
|
||||
EXPECT_EQ(L"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u.str());
|
||||
EXPECT_EQ(7, u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16EmptyString) {
|
||||
std::string s = "";
|
||||
fmt::internal::utf8_to_utf16 u(s.c_str());
|
||||
EXPECT_EQ(L"", u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
template <typename Converter, typename Char>
|
||||
void check_utf_conversion_error(
|
||||
const char *message,
|
||||
fmt::basic_string_view<Char> str = fmt::basic_string_view<Char>(0, 1)) {
|
||||
fmt::memory_buffer out;
|
||||
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
|
||||
fmt::system_error error(0, "");
|
||||
try {
|
||||
(Converter)(str);
|
||||
} catch (const fmt::system_error &e) {
|
||||
error = e;
|
||||
}
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, error.error_code());
|
||||
EXPECT_EQ(fmt::to_string(out), error.what());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Error) {
|
||||
check_utf_conversion_error<fmt::internal::utf16_to_utf8, wchar_t>(
|
||||
"cannot convert string from UTF-16 to UTF-8");
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16Error) {
|
||||
const char *message = "cannot convert string from UTF-8 to UTF-16";
|
||||
check_utf_conversion_error<fmt::internal::utf8_to_utf16, char>(message);
|
||||
check_utf_conversion_error<fmt::internal::utf8_to_utf16, char>(
|
||||
message, fmt::string_view("foo", INT_MAX + 1u));
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Convert) {
|
||||
fmt::internal::utf16_to_utf8 u;
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::wstring_view(0, 1)));
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER,
|
||||
u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u)));
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
typedef void (*FormatErrorMessage)(
|
||||
fmt::internal::buffer &out, int error_code, string_view message);
|
||||
|
||||
template <typename Error>
|
||||
void check_throw_error(int error_code, FormatErrorMessage format) {
|
||||
fmt::system_error error(0, "");
|
||||
try {
|
||||
throw Error(error_code, "test {}", "error");
|
||||
} catch (const fmt::system_error &e) {
|
||||
error = e;
|
||||
}
|
||||
fmt::memory_buffer message;
|
||||
format(message, error_code, "test error");
|
||||
EXPECT_EQ(to_string(message), error.what());
|
||||
EXPECT_EQ(error_code, error.error_code());
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatSystemError) {
|
||||
fmt::memory_buffer message;
|
||||
fmt::format_system_error(message, EDOM, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)),
|
||||
to_string(message));
|
||||
message = fmt::memory_buffer();
|
||||
|
||||
// Check if std::allocator throws on allocating max size_t / 2 chars.
|
||||
size_t max_size = std::numeric_limits<size_t>::max() / 2;
|
||||
bool throws_on_alloc = false;
|
||||
try {
|
||||
std::allocator<char> alloc;
|
||||
alloc.deallocate(alloc.allocate(max_size), max_size);
|
||||
} catch (const std::bad_alloc&) {
|
||||
throws_on_alloc = true;
|
||||
}
|
||||
if (!throws_on_alloc) {
|
||||
fmt::print("warning: std::allocator allocates {} chars", max_size);
|
||||
return;
|
||||
}
|
||||
fmt::format_system_error(message, EDOM, fmt::string_view(nullptr, max_size));
|
||||
EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message));
|
||||
}
|
||||
|
||||
TEST(UtilTest, SystemError) {
|
||||
fmt::system_error e(EDOM, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), e.what());
|
||||
EXPECT_EQ(EDOM, e.error_code());
|
||||
check_throw_error<fmt::system_error>(EDOM, fmt::format_system_error);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ReportSystemError) {
|
||||
fmt::memory_buffer out;
|
||||
fmt::format_system_error(out, EDOM, "test error");
|
||||
out.push_back('\n');
|
||||
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"),
|
||||
to_string(out));
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
TEST(UtilTest, FormatWindowsError) {
|
||||
LPWSTR message = 0;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||
ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
||||
fmt::internal::utf16_to_utf8 utf8_message(message);
|
||||
LocalFree(message);
|
||||
fmt::memory_buffer actual_message;
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, ERROR_FILE_EXISTS, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
||||
fmt::to_string(actual_message));
|
||||
actual_message.resize(0);
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, ERROR_FILE_EXISTS,
|
||||
fmt::string_view(0, std::numeric_limits<size_t>::max()));
|
||||
EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS),
|
||||
fmt::to_string(actual_message));
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatLongWindowsError) {
|
||||
LPWSTR message = 0;
|
||||
// this error code is not available on all Windows platforms and
|
||||
// Windows SDKs, so do not fail the test if the error string cannot
|
||||
// be retrieved.
|
||||
const int provisioning_not_allowed = 0x80284013L /*TBS_E_PROVISIONING_NOT_ALLOWED*/;
|
||||
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||
static_cast<DWORD>(provisioning_not_allowed),
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPWSTR>(&message), 0, 0) == 0) {
|
||||
return;
|
||||
}
|
||||
fmt::internal::utf16_to_utf8 utf8_message(message);
|
||||
LocalFree(message);
|
||||
fmt::memory_buffer actual_message;
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, provisioning_not_allowed, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
||||
fmt::to_string(actual_message));
|
||||
}
|
||||
|
||||
TEST(UtilTest, WindowsError) {
|
||||
check_throw_error<fmt::windows_error>(
|
||||
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ReportWindowsError) {
|
||||
fmt::memory_buffer out;
|
||||
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
||||
out.push_back('\n');
|
||||
EXPECT_WRITE(stderr,
|
||||
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"),
|
||||
fmt::to_string(out));
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
||||
|
||||
enum TestEnum2 {};
|
||||
|
||||
TEST(UtilTest, ConvertToInt) {
|
||||
EXPECT_FALSE((fmt::internal::convert_to_int<char, char>::value));
|
||||
EXPECT_FALSE((fmt::internal::convert_to_int<const char *, char>::value));
|
||||
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum2, char>::value));
|
||||
}
|
||||
|
||||
#if FMT_USE_ENUM_BASE
|
||||
enum TestEnum : char {TestValue};
|
||||
TEST(UtilTest, IsEnumConvertibleToInt) {
|
||||
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum, char>::value));
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UtilTest, ParseNonnegativeInt) {
|
||||
if (std::numeric_limits<int>::max() != static_cast<int>(static_cast<unsigned>(1) << 31)) {
|
||||
fmt::print("Skipping parse_nonnegative_int test\n");
|
||||
return;
|
||||
}
|
||||
const char *s = "10000000000";
|
||||
EXPECT_THROW_MSG(
|
||||
parse_nonnegative_int(s, fmt::internal::error_handler()),
|
||||
fmt::format_error, "number is too big");
|
||||
s = "2147483649";
|
||||
EXPECT_THROW_MSG(
|
||||
parse_nonnegative_int(s, fmt::internal::error_handler()),
|
||||
fmt::format_error, "number is too big");
|
||||
}
|
||||
|
||||
template <bool is_iec559>
|
||||
void test_construct_from_double() {
|
||||
fmt::print("warning: double is not IEC559, skipping FP tests\n");
|
||||
}
|
||||
|
||||
template <>
|
||||
void test_construct_from_double<true>() {
|
||||
auto v = fp(1.23);
|
||||
EXPECT_EQ(v.f, 0x13ae147ae147aeu);
|
||||
EXPECT_EQ(v.e, -52);
|
||||
}
|
||||
|
||||
TEST(FPTest, ConstructFromDouble) {
|
||||
test_construct_from_double<std::numeric_limits<double>::is_iec559>();
|
||||
}
|
||||
|
||||
TEST(FPTest, Normalize) {
|
||||
auto v = fp(0xbeef, 42);
|
||||
v.normalize();
|
||||
EXPECT_EQ(0xbeef000000000000, v.f);
|
||||
EXPECT_EQ(-6, v.e);
|
||||
}
|
||||
|
||||
TEST(FPTest, Subtract) {
|
||||
auto v = fp(123, 1) - fp(102, 1);
|
||||
EXPECT_EQ(v.f, 21u);
|
||||
EXPECT_EQ(v.e, 1);
|
||||
}
|
||||
|
||||
TEST(FPTest, Multiply) {
|
||||
auto v = fp(123ULL << 32, 4) * fp(56ULL << 32, 7);
|
||||
EXPECT_EQ(v.f, 123u * 56u);
|
||||
EXPECT_EQ(v.e, 4 + 7 + 64);
|
||||
v = fp(123ULL << 32, 4) * fp(567ULL << 31, 8);
|
||||
EXPECT_EQ(v.f, (123 * 567 + 1u) / 2);
|
||||
EXPECT_EQ(v.e, 4 + 8 + 64);
|
||||
}
|
||||
|
||||
TEST(FPTest, GetCachedPower) {
|
||||
typedef std::numeric_limits<double> limits;
|
||||
for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) {
|
||||
int dec_exp = 0;
|
||||
auto fp = fmt::internal::get_cached_power(exp, dec_exp);
|
||||
EXPECT_LE(exp, fp.e);
|
||||
int dec_exp_step = 8;
|
||||
EXPECT_LE(fp.e, exp + dec_exp_step * log2(10));
|
||||
EXPECT_DOUBLE_EQ(pow(10, dec_exp), ldexp(fp.f, fp.e));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(IteratorTest, CountingIterator) {
|
||||
fmt::internal::counting_iterator<char> it;
|
||||
auto prev = it++;
|
||||
EXPECT_EQ(prev.count(), 0);
|
||||
EXPECT_EQ(it.count(), 1);
|
||||
}
|
||||
|
||||
TEST(IteratorTest, TruncatingIterator) {
|
||||
char *p = FMT_NULL;
|
||||
fmt::internal::truncating_iterator<char*> it(p, 3);
|
||||
auto prev = it++;
|
||||
EXPECT_EQ(prev.base(), p);
|
||||
EXPECT_EQ(it.base(), p + 1);
|
||||
}
|
||||
Reference in New Issue
Block a user