forked from fmtlib/fmt
Add fmt::to_string (#326)
This commit is contained in:
@@ -181,6 +181,8 @@ Utilities
|
|||||||
.. doxygenclass:: fmt::ArgList
|
.. doxygenclass:: fmt::ArgList
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
.. doxygenfunction:: fmt::to_string(const T&)
|
||||||
|
|
||||||
.. doxygenclass:: fmt::BasicStringRef
|
.. doxygenclass:: fmt::BasicStringRef
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
# Define the fmt library, its includes and the needed defines.
|
# Define the fmt library, its includes and the needed defines.
|
||||||
# format.cc is added to FMT_HEADERS for the header-only configuration.
|
# 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)
|
if (HAVE_OPEN)
|
||||||
set(FMT_HEADERS ${FMT_HEADERS} posix.h)
|
set(FMT_HEADERS ${FMT_HEADERS} posix.h)
|
||||||
set(FMT_SOURCES ${FMT_SOURCES} posix.cc)
|
set(FMT_SOURCES ${FMT_SOURCES} posix.cc)
|
||||||
endif ()
|
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)
|
option(FMT_CPPFORMAT "Build cppformat library for backward compatibility." OFF)
|
||||||
if (FMT_CPPFORMAT)
|
if (FMT_CPPFORMAT)
|
||||||
|
34
fmt/string.h
Normal file
34
fmt/string.h
Normal file
@@ -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 <typename T>
|
||||||
|
std::string to_string(const T &value) {
|
||||||
|
fmt::MemoryWriter w;
|
||||||
|
w << value;
|
||||||
|
return w.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // FMT_STRING_H_
|
@@ -77,6 +77,7 @@ add_fmt_test(format-test)
|
|||||||
add_fmt_test(format-impl-test)
|
add_fmt_test(format-impl-test)
|
||||||
add_fmt_test(ostream-test)
|
add_fmt_test(ostream-test)
|
||||||
add_fmt_test(printf-test)
|
add_fmt_test(printf-test)
|
||||||
|
add_fmt_test(string-test)
|
||||||
add_fmt_test(util-test mock-allocator.h)
|
add_fmt_test(util-test mock-allocator.h)
|
||||||
add_fmt_test(macro-test)
|
add_fmt_test(macro-test)
|
||||||
|
|
||||||
@@ -87,7 +88,8 @@ if (FMT_PEDANTIC AND MSVC)
|
|||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (HAVE_OPEN)
|
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_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR})
|
||||||
target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
|
target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
|
||||||
target_link_libraries(posix-mock-test gmock)
|
target_link_libraries(posix-mock-test gmock)
|
||||||
|
15
test/string-test.cc
Normal file
15
test/string-test.cc
Normal file
@@ -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));
|
||||||
|
}
|
Reference in New Issue
Block a user