From b6c0cf9683736dffbad7a48016d2b08878102371 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 18 May 2016 19:54:52 -0700 Subject: [PATCH] Add fmt::to_string (#326) --- doc/api.rst | 2 ++ fmt/CMakeLists.txt | 4 ++-- fmt/string.h | 34 ++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 4 +++- test/string-test.cc | 15 +++++++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 fmt/string.h create mode 100644 test/string-test.cc diff --git a/doc/api.rst b/doc/api.rst index 75dc03ac..4ed9ae8a 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -181,6 +181,8 @@ Utilities .. doxygenclass:: fmt::ArgList :members: +.. doxygenfunction:: fmt::to_string(const T&) + .. doxygenclass:: fmt::BasicStringRef :members: diff --git a/fmt/CMakeLists.txt b/fmt/CMakeLists.txt index c0ef02e5..083652df 100644 --- a/fmt/CMakeLists.txt +++ b/fmt/CMakeLists.txt @@ -1,12 +1,12 @@ # Define the fmt library, its includes and the needed defines. # format.cc is added to FMT_HEADERS for the header-only configuration. -set(FMT_HEADERS format.h format.cc ostream.h ostream.cc time.h) +set(FMT_HEADERS format.h format.cc ostream.h ostream.cc string.h time.h) if (HAVE_OPEN) set(FMT_HEADERS ${FMT_HEADERS} posix.h) set(FMT_SOURCES ${FMT_SOURCES} posix.cc) endif () -add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS} ../ChangeLog.rst) +add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS} ../README.rst ../ChangeLog.rst) option(FMT_CPPFORMAT "Build cppformat library for backward compatibility." OFF) if (FMT_CPPFORMAT) diff --git a/fmt/string.h b/fmt/string.h new file mode 100644 index 00000000..f50d8b45 --- /dev/null +++ b/fmt/string.h @@ -0,0 +1,34 @@ +/* + Formatting library for C++ - string utilities + + Copyright (c) 2012 - 2016, Victor Zverovich + All rights reserved. + + For the license information refer to format.h. + */ + +#ifndef FMT_STRING_H_ +#define FMT_STRING_H_ + +#include "format.h" + +namespace fmt { + +/** + \rst + Converts *value* to ``std::string`` using the default format for type *T*. + + **Example**:: + + std::string answer = fmt::to_string(42); + \endrst + */ +template +std::string to_string(const T &value) { + fmt::MemoryWriter w; + w << value; + return w.str(); +} +} + +#endif // FMT_STRING_H_ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e4e868d5..618c1221 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -77,6 +77,7 @@ add_fmt_test(format-test) add_fmt_test(format-impl-test) add_fmt_test(ostream-test) add_fmt_test(printf-test) +add_fmt_test(string-test) add_fmt_test(util-test mock-allocator.h) add_fmt_test(macro-test) @@ -87,7 +88,8 @@ if (FMT_PEDANTIC AND MSVC) endif () if (HAVE_OPEN) - add_executable(posix-mock-test posix-mock-test.cc ../fmt/format.cc ${TEST_MAIN_SRC}) + add_executable(posix-mock-test + posix-mock-test.cc ../fmt/format.cc ${TEST_MAIN_SRC}) target_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR}) target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1) target_link_libraries(posix-mock-test gmock) diff --git a/test/string-test.cc b/test/string-test.cc new file mode 100644 index 00000000..5ef8877c --- /dev/null +++ b/test/string-test.cc @@ -0,0 +1,15 @@ +/* + Tests of string utilities + + Copyright (c) 2012 - 2016, Victor Zverovich + All rights reserved. + + For the license information refer to format.h. + */ + +#include "fmt/string.h" +#include "gtest/gtest.h" + +TEST(StringTest, ToString) { + EXPECT_EQ("42", fmt::to_string(42)); +}