Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
2a619d96
Commit
2a619d96
authored
Sep 20, 2017
by
Mário Feroldi
Committed by
Victor Zverovich
Sep 20, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make format work with C++17 std::string_view (#571)
Tests for C++17 std::string_view
parent
e051de37
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
0 deletions
+63
-0
fmt/format.h
fmt/format.h
+42
-0
test/format-test.cc
test/format-test.cc
+21
-0
No files found.
fmt/format.h
View file @
2a619d96
...
...
@@ -45,6 +45,19 @@
// The fmt library version in the form major * 10000 + minor * 100 + patch.
#define FMT_VERSION 40001
#if defined(__has_include)
# define FMT_HAS_INCLUDE(x) __has_include(x)
#else
# define FMT_HAS_INCLUDE(x) 0
#endif
#if FMT_HAS_INCLUDE(<string_view>) && __cplusplus > 201402L
# include <string_view>
# define FMT_HAS_STRING_VIEW 1
#else
# define FMT_HAS_STRING_VIEW 0
#endif
#if defined _SECURE_SCL && _SECURE_SCL
# define FMT_SECURE_SCL _SECURE_SCL
#else
...
...
@@ -521,6 +534,26 @@ class BasicStringRef {
const
std
::
basic_string
<
Char
,
std
::
char_traits
<
Char
>
,
Allocator
>
&
s
)
:
data_
(
s
.
c_str
()),
size_
(
s
.
size
())
{}
#if FMT_HAS_STRING_VIEW
/**
\rst
Constructs a string reference from a ``std::basic_string_view`` object.
\endrst
*/
BasicStringRef
(
const
std
::
basic_string_view
<
Char
,
std
::
char_traits
<
Char
>>
&
s
)
:
data_
(
s
.
data
()),
size_
(
s
.
size
())
{}
/**
\rst
Converts a string reference to an ``std::string_view`` object.
\endrst
*/
explicit
operator
std
::
basic_string_view
<
Char
>
()
const
FMT_NOEXCEPT
{
return
std
::
basic_string_view
<
Char
>
(
data_
,
size_
);
}
#endif
/**
\rst
Converts a string reference to an ``std::string`` object.
...
...
@@ -1299,6 +1332,9 @@ class MakeValue : public Arg {
MakeValue
(
typename
WCharHelper
<
wchar_t
*
,
Char
>::
Unsupported
);
MakeValue
(
typename
WCharHelper
<
const
wchar_t
*
,
Char
>::
Unsupported
);
MakeValue
(
typename
WCharHelper
<
const
std
::
wstring
&
,
Char
>::
Unsupported
);
#if FMT_HAS_STRING_VIEW
MakeValue
(
typename
WCharHelper
<
const
std
::
wstring_view
&
,
Char
>::
Unsupported
);
#endif
MakeValue
(
typename
WCharHelper
<
WStringRef
,
Char
>::
Unsupported
);
void
set_string
(
StringRef
str
)
{
...
...
@@ -1386,6 +1422,9 @@ class MakeValue : public Arg {
FMT_MAKE_VALUE
(
unsigned
char
*
,
ustring
.
value
,
CSTRING
)
FMT_MAKE_VALUE
(
const
unsigned
char
*
,
ustring
.
value
,
CSTRING
)
FMT_MAKE_STR_VALUE
(
const
std
::
string
&
,
STRING
)
#if FMT_HAS_STRING_VIEW
FMT_MAKE_STR_VALUE
(
const
std
::
string_view
&
,
STRING
)
#endif
FMT_MAKE_STR_VALUE
(
StringRef
,
STRING
)
FMT_MAKE_VALUE_
(
CStringRef
,
string
.
value
,
CSTRING
,
value
.
c_str
())
...
...
@@ -1398,6 +1437,9 @@ class MakeValue : public Arg {
FMT_MAKE_WSTR_VALUE
(
wchar_t
*
,
WSTRING
)
FMT_MAKE_WSTR_VALUE
(
const
wchar_t
*
,
WSTRING
)
FMT_MAKE_WSTR_VALUE
(
const
std
::
wstring
&
,
WSTRING
)
#if FMT_HAS_STRING_VIEW
FMT_MAKE_WSTR_VALUE
(
const
std
::
wstring_view
&
,
WSTRING
)
#endif
FMT_MAKE_WSTR_VALUE
(
WStringRef
,
WSTRING
)
FMT_MAKE_VALUE
(
void
*
,
pointer
,
POINTER
)
...
...
test/format-test.cc
View file @
2a619d96
...
...
@@ -151,16 +151,31 @@ TEST(StringRefTest, Ctor) {
EXPECT_STREQ
(
"defg"
,
StringRef
(
std
::
string
(
"defg"
)).
data
());
EXPECT_EQ
(
4u
,
StringRef
(
std
::
string
(
"defg"
)).
size
());
#if FMT_HAS_STRING_VIEW
EXPECT_STREQ
(
"hijk"
,
StringRef
(
std
::
string_view
(
"hijk"
)).
data
());
EXPECT_EQ
(
4u
,
StringRef
(
std
::
string_view
(
"hijk"
)).
size
());
#endif
}
TEST
(
StringRefTest
,
ConvertToString
)
{
std
::
string
s
=
StringRef
(
"abc"
).
to_string
();
EXPECT_EQ
(
"abc"
,
s
);
#if FMT_HAS_STRING_VIEW
StringRef
str_ref
(
"defg"
);
std
::
string_view
sv
=
static_cast
<
std
::
string_view
>
(
str_ref
);
EXPECT_EQ
(
"defg"
,
sv
);
#endif
}
TEST
(
CStringRefTest
,
Ctor
)
{
EXPECT_STREQ
(
"abc"
,
CStringRef
(
"abc"
).
c_str
());
EXPECT_STREQ
(
"defg"
,
CStringRef
(
std
::
string
(
"defg"
)).
c_str
());
#if FMT_HAS_STRING_VIEW
EXPECT_STREQ
(
"hijk"
,
CStringRef
(
std
::
string_view
(
"hijk"
)).
c_str
());
#endif
}
#if FMT_USE_TYPE_TRAITS
...
...
@@ -1378,6 +1393,12 @@ TEST(FormatterTest, FormatCStringRef) {
EXPECT_EQ
(
"test"
,
format
(
"{0}"
,
CStringRef
(
"test"
)));
}
#if FMT_HAS_STRING_VIEW
TEST
(
FormatterTest
,
FormatStringView
)
{
EXPECT_EQ
(
"test"
,
format
(
"{0}"
,
std
::
string_view
(
"test"
)));
}
#endif
void
format_arg
(
fmt
::
BasicFormatter
<
char
>
&
f
,
const
char
*
,
const
Date
&
d
)
{
f
.
writer
()
<<
d
.
year
()
<<
'-'
<<
d
.
month
()
<<
'-'
<<
d
.
day
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment