Add a formatter for folly::StringPiece

This commit is contained in:
Victor Zverovich 2018-07-14 06:33:16 -07:00
parent ae4a3945f5
commit e0f6a2f8be
5 changed files with 56 additions and 2 deletions

View File

@ -148,8 +148,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 core.h folly.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)

22
include/fmt/folly.h Normal file
View File

@ -0,0 +1,22 @@
// Formatting library for C++ - folly::StringPiece formatter
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_FOLLY_H_
#define FMT_FOLLY_H_
#include <folly/Range.h>
#include "core.h"
FMT_BEGIN_NAMESPACE
template <typename Ctx>
inline internal::typed_value<Ctx, internal::string_type>
make_value(folly::StringPiece s) {
return string_view(s.data(), s.size());
}
FMT_END_NAMESPACE
#endif // FMT_FOLLY_H_

View File

@ -86,6 +86,7 @@ endfunction()
add_fmt_test(assert-test)
add_fmt_test(gtest-extra-test)
add_fmt_test(folly-test)
add_fmt_test(format-test)
add_fmt_test(format-impl-test)
add_fmt_test(ostream-test)

14
test/folly-test.cc Normal file
View File

@ -0,0 +1,14 @@
// Formatting library for C++ - folly::StringPiece formatter tests
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#include <fmt/folly.h>
#include "gtest.h"
TEST(FollyTest, FormatStringPiece) {
EXPECT_EQ(fmt::format("{}", "foo"), "foo");
EXPECT_EQ(fmt::format("{:>5}", "foo"), " foo");
}

17
test/folly/Range.h Normal file
View File

@ -0,0 +1,17 @@
// A folly::StringPiece stub.
#include <cstring>
namespace folly {
class StringPiece {
public:
explicit StringPiece(const char *s) : data_(s), size_(std::strlen(s)) {}
const char* data() const { return "foo"; }
size_t size() const { return 3; }
private:
const char *data_;
size_t size_;
};
}