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
c0954453
Commit
c0954453
authored
Jan 06, 2018
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace buffer with range
parent
c3d6c5fc
Changes
14
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
544 additions
and
555 deletions
+544
-555
include/fmt/core.h
include/fmt/core.h
+199
-127
include/fmt/format.cc
include/fmt/format.cc
+13
-35
include/fmt/format.h
include/fmt/format.h
+177
-224
include/fmt/locale.h
include/fmt/locale.h
+6
-8
include/fmt/ostream.h
include/fmt/ostream.h
+9
-11
include/fmt/posix.cc
include/fmt/posix.cc
+6
-8
include/fmt/posix.h
include/fmt/posix.h
+6
-8
include/fmt/printf.h
include/fmt/printf.h
+68
-65
include/fmt/string.h
include/fmt/string.h
+6
-8
include/fmt/time.h
include/fmt/time.h
+8
-9
test/custom-formatter-test.cc
test/custom-formatter-test.cc
+5
-5
test/format-test.cc
test/format-test.cc
+30
-36
test/ostream-test.cc
test/ostream-test.cc
+3
-3
test/util-test.cc
test/util-test.cc
+8
-8
No files found.
include/fmt/core.h
View file @
c0954453
This diff is collapsed.
Click to expand it.
include/fmt/format.cc
View file @
c0954453
/*
Formatting library for C++
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Formatting library for C++
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#include "fmt/format.h"
#include "fmt/locale.h"
...
...
@@ -348,18 +328,18 @@ FMT_FUNC void windows_error::init(
FMT_FUNC
void
internal
::
format_windows_error
(
buffer
&
out
,
int
error_code
,
string_view
message
)
FMT_NOEXCEPT
{
FMT_TRY
{
wmemory_buffer
buf
fer
;
buf
fer
.
resize
(
INLINE_BUFFER_SIZE
);
wmemory_buffer
buf
;
buf
.
resize
(
INLINE_BUFFER_SIZE
);
for
(;;)
{
wchar_t
*
system_message
=
&
buf
fer
[
0
];
wchar_t
*
system_message
=
&
buf
[
0
];
int
result
=
FormatMessageW
(
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
0
,
error_code
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
system_message
,
static_cast
<
uint32_t
>
(
buf
fer
.
size
()),
0
);
system_message
,
static_cast
<
uint32_t
>
(
buf
.
size
()),
0
);
if
(
result
!=
0
)
{
utf16_to_utf8
utf8_message
;
if
(
utf8_message
.
convert
(
system_message
)
==
ERROR_SUCCESS
)
{
basic_writer
<
cha
r
>
w
(
out
);
basic_writer
<
buffe
r
>
w
(
out
);
w
.
write
(
message
);
w
.
write
(
": "
);
w
.
write
(
utf8_message
);
...
...
@@ -369,10 +349,10 @@ FMT_FUNC void internal::format_windows_error(
}
if
(
GetLastError
()
!=
ERROR_INSUFFICIENT_BUFFER
)
break
;
// Can't get error message, report error code instead.
buf
fer
.
resize
(
buffer
.
size
()
*
2
);
buf
.
resize
(
buf
.
size
()
*
2
);
}
}
FMT_CATCH
(...)
{}
f
mt
::
format_error_code
(
out
,
error_code
,
message
);
// 'fmt::' is for bcc32.
f
ormat_error_code
(
out
,
error_code
,
message
);
}
#endif // FMT_USE_WINDOWS_H
...
...
@@ -467,8 +447,6 @@ template int internal::char_traits<char>::format_float(
template
wchar_t
internal
::
thousands_sep
(
locale_provider
*
lp
);
template
class
basic_context
<
wchar_t
>;
template
void
basic_fixed_buffer
<
wchar_t
>
::
grow
(
std
::
size_t
);
template
void
internal
::
arg_map
<
wcontext
>
::
init
(
const
wformat_args
&
args
);
...
...
include/fmt/format.h
View file @
c0954453
This diff is collapsed.
Click to expand it.
include/fmt/locale.h
View file @
c0954453
/*
Formatting library for C++ - locale support
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - locale support
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#include "fmt/format.h"
#include <locale>
...
...
include/fmt/ostream.h
View file @
c0954453
/*
Formatting library for C++ - std::ostream support
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - std::ostream support
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_OSTREAM_H_
#define FMT_OSTREAM_H_
...
...
@@ -104,12 +102,12 @@ struct formatter<T, Char,
typename
std
::
enable_if
<!
internal
::
format_type
<
T
>::
value
>::
type
>
:
formatter
<
basic_string_view
<
Char
>
,
Char
>
{
void
format
(
basic_buffer
<
Char
>
&
buf
,
const
T
&
value
,
basic_context
<
Char
>
&
ctx
)
{
template
<
typename
Context
>
void
format
(
const
T
&
value
,
Context
&
ctx
)
{
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
(
buf
,
str
,
ctx
);
formatter
<
basic_string_view
<
Char
>
,
Char
>::
format
(
str
,
ctx
);
}
};
...
...
include/fmt/posix.cc
View file @
c0954453
/*
A C++ interface to POSIX functions.
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// A C++ interface to POSIX functions.
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
// Disable bogus MSVC warnings.
#ifndef _CRT_SECURE_NO_WARNINGS
...
...
include/fmt/posix.h
View file @
c0954453
/*
A C++ interface to POSIX functions.
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// A C++ interface to POSIX functions.
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_POSIX_H_
#define FMT_POSIX_H_
...
...
include/fmt/printf.h
View file @
c0954453
This diff is collapsed.
Click to expand it.
include/fmt/string.h
View file @
c0954453
/*
Formatting library for C++ - string utilities
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// 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/fmt/time.h
View file @
c0954453
/*
Formatting library for C++ - time formatting
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
// Formatting library for C++ - time formatting
//
// Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
#ifndef FMT_TIME_H_
#define FMT_TIME_H_
...
...
@@ -32,7 +30,8 @@ struct formatter<std::tm> {
return
pointer_from
(
end
);
}
void
format
(
buffer
&
buf
,
const
std
::
tm
&
tm
,
context
&
)
{
void
format
(
const
std
::
tm
&
tm
,
context
&
ctx
)
{
buffer
&
buf
=
ctx
.
range
();
std
::
size_t
start
=
buf
.
size
();
for
(;;)
{
std
::
size_t
size
=
buf
.
capacity
()
-
start
;
...
...
test/custom-formatter-test.cc
View file @
c0954453
...
...
@@ -14,18 +14,18 @@ using fmt::printf_arg_formatter;
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class
CustomArgFormatter
:
public
fmt
::
arg_formatter
<
cha
r
>
{
class
CustomArgFormatter
:
public
fmt
::
arg_formatter
<
fmt
::
buffe
r
>
{
public:
CustomArgFormatter
(
fmt
::
buffer
&
buf
,
fmt
::
basic_context
<
cha
r
>
&
ctx
,
CustomArgFormatter
(
fmt
::
buffer
&
buf
,
fmt
::
basic_context
<
fmt
::
buffe
r
>
&
ctx
,
fmt
::
format_specs
&
s
)
:
fmt
::
arg_formatter
<
cha
r
>
(
buf
,
ctx
,
s
)
{}
:
fmt
::
arg_formatter
<
fmt
::
buffe
r
>
(
buf
,
ctx
,
s
)
{}
using
fmt
::
arg_formatter
<
cha
r
>::
operator
();
using
fmt
::
arg_formatter
<
fmt
::
buffe
r
>::
operator
();
void
operator
()(
double
value
)
{
if
(
round
(
value
*
pow
(
10
,
spec
().
precision
()))
==
0
)
value
=
0
;
fmt
::
arg_formatter
<
cha
r
>::
operator
()(
value
);
fmt
::
arg_formatter
<
fmt
::
buffe
r
>::
operator
()(
value
);
}
};
...
...
test/format-test.cc
View file @
c0954453
...
...
@@ -31,7 +31,6 @@
#include <cmath>
#include <cstring>
#include <memory>
#include <type_traits>
#include <stdint.h>
#include "gmock/gmock.h"
...
...
@@ -91,7 +90,7 @@ template <typename Char, typename T>
fmt
::
basic_memory_buffer
<
Char
>
buffer
;
fmt
::
basic_writer
<
fmt
::
basic_buffer
<
Char
>>
writer
(
buffer
);
writer
.
write
(
value
);
std
::
basic_string
<
Char
>
actual
=
writer
.
str
(
);
std
::
basic_string
<
Char
>
actual
=
to_string
(
buffer
);
std
::
basic_string
<
Char
>
expected
;
std_format
(
value
,
expected
);
if
(
expected
==
actual
)
...
...
@@ -149,19 +148,11 @@ TEST(WriterTest, NotCopyAssignable) {
EXPECT_FALSE
(
std
::
is_copy_assignable
<
basic_writer
<
fmt
::
buffer
>>::
value
);
}
TEST
(
WriterTest
,
Ctor
)
{
memory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
buffer
>
w
(
buf
);
EXPECT_EQ
(
0u
,
w
.
size
());
EXPECT_STREQ
(
""
,
w
.
c_str
());
EXPECT_EQ
(
""
,
w
.
str
());
}
TEST
(
WriterTest
,
Data
)
{
memory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
buffer
>
w
(
buf
);
w
.
write
(
42
);
EXPECT_EQ
(
"42"
,
std
::
string
(
w
.
data
(),
w
.
size
()
));
EXPECT_EQ
(
"42"
,
to_string
(
buf
));
}
TEST
(
WriterTest
,
WriteInt
)
{
...
...
@@ -224,7 +215,9 @@ TEST(WriterTest, WriteDoubleWithFilledBuffer) {
for
(
int
i
=
0
;
i
<
fmt
::
internal
::
INLINE_BUFFER_SIZE
;
++
i
)
writer
.
write
(
' '
);
writer
.
write
(
1.2
);
EXPECT_STREQ
(
"1.2"
,
writer
.
c_str
()
+
fmt
::
internal
::
INLINE_BUFFER_SIZE
);
fmt
::
string_view
sv
(
buf
.
data
(),
buf
.
size
());
sv
.
remove_prefix
(
fmt
::
internal
::
INLINE_BUFFER_SIZE
);
EXPECT_EQ
(
"1.2"
,
sv
);
}
TEST
(
WriterTest
,
WriteChar
)
{
...
...
@@ -239,31 +232,29 @@ TEST(WriterTest, WriteString) {
CHECK_WRITE_CHAR
(
"abc"
);
CHECK_WRITE_WCHAR
(
"abc"
);
// The following line shouldn't compile:
//
MemoryWriter() << L"abc"
;
//
std::declval<fmt::basic_writer<fmt::buffer>>().write(L"abc")
;
}
TEST
(
WriterTest
,
WriteWideString
)
{
CHECK_WRITE_WCHAR
(
L"abc"
);
// The following line shouldn't compile:
//
fmt::WMemoryWriter() << "abc"
;
//
std::declval<fmt::basic_writer<fmt::wbuffer>>().write("abc")
;
}
template
<
typename
...
T
>
std
::
string
write_str
(
T
...
args
)
{
memory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
buffer
>
writer
(
buf
);
using
namespace
fmt
;
writer
.
write
(
args
...);
return
writer
.
str
(
);
return
to_string
(
buf
);
}
template
<
typename
...
T
>
std
::
wstring
write_wstr
(
T
...
args
)
{
wmemory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
wbuffer
>
writer
(
buf
);
using
namespace
fmt
;
writer
.
write
(
args
...);
return
writer
.
str
(
);
return
to_string
(
buf
);
}
TEST
(
WriterTest
,
bin
)
{
...
...
@@ -350,17 +341,20 @@ TEST(WriterTest, pad) {
EXPECT_EQ
(
" 33"
,
write_str
(
33ll
,
width
=
7
));
EXPECT_EQ
(
" 44"
,
write_str
(
44ull
,
width
=
7
));
memory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
buffer
>
w
(
buf
);
w
.
clear
();
w
.
write
(
42
,
fmt
::
width
=
5
,
fmt
::
fill
=
'0'
);
EXPECT_EQ
(
"00042"
,
w
.
str
());
w
.
clear
();
w
<<
Date
(
2012
,
12
,
9
);
EXPECT_EQ
(
"2012-12-9"
,
w
.
str
());
w
.
clear
();
w
<<
iso8601
(
Date
(
2012
,
1
,
9
));
EXPECT_EQ
(
"2012-01-09"
,
w
.
str
());
EXPECT_EQ
(
"00042"
,
write_str
(
42
,
fmt
::
width
=
5
,
fmt
::
fill
=
'0'
));
{
memory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
buffer
>
w
(
buf
);
w
<<
Date
(
2012
,
12
,
9
);
EXPECT_EQ
(
"2012-12-9"
,
to_string
(
buf
));
}
{
memory_buffer
buf
;
fmt
::
basic_writer
<
fmt
::
buffer
>
w
(
buf
);
w
<<
iso8601
(
Date
(
2012
,
1
,
9
));
EXPECT_EQ
(
"2012-01-09"
,
to_string
(
buf
));
}
}
TEST
(
WriterTest
,
PadString
)
{
...
...
@@ -1228,8 +1222,8 @@ struct formatter<Date> {
return
it
;
}
void
format
(
buffer
&
buf
,
const
Date
&
d
,
context
&
)
{
format_to
(
buf
,
"{}-{}-{}"
,
d
.
year
(),
d
.
month
(),
d
.
day
());
void
format
(
const
Date
&
d
,
context
&
ctx
)
{
format_to
(
ctx
.
range
()
,
"{}-{}-{}"
,
d
.
year
(),
d
.
month
(),
d
.
day
());
}
};
}
...
...
@@ -1245,8 +1239,8 @@ class Answer {};
namespace
fmt
{
template
<
>
struct
formatter
<
Answer
>
:
formatter
<
int
>
{
void
format
(
fmt
::
buffer
&
buf
,
Answer
,
fmt
::
context
&
ctx
)
{
formatter
<
int
>::
format
(
buf
,
42
,
ctx
);
void
format
(
Answer
,
fmt
::
context
&
ctx
)
{
formatter
<
int
>::
format
(
42
,
ctx
);
}
};
}
...
...
@@ -1535,11 +1529,11 @@ struct variant {
namespace
fmt
{
template
<
>
struct
formatter
<
variant
>
:
dynamic_formatter
<>
{
void
format
(
buffer
&
buf
,
variant
value
,
context
&
ctx
)
{
void
format
(
variant
value
,
context
&
ctx
)
{
if
(
value
.
type
==
variant
::
INT
)
dynamic_formatter
::
format
(
buf
,
42
,
ctx
);
dynamic_formatter
::
format
(
ctx
.
range
()
,
42
,
ctx
);
else
dynamic_formatter
::
format
(
buf
,
"foo"
,
ctx
);
dynamic_formatter
::
format
(
ctx
.
range
()
,
"foo"
,
ctx
);
}
};
}
...
...
test/ostream-test.cc
View file @
c0954453
...
...
@@ -58,14 +58,14 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ
(
"0"
,
fmt
::
format
(
"{}"
,
A
));
}
struct
TestArgFormatter
:
fmt
::
arg_formatter
<
cha
r
>
{
struct
TestArgFormatter
:
fmt
::
arg_formatter
<
fmt
::
buffe
r
>
{
TestArgFormatter
(
fmt
::
buffer
&
buf
,
fmt
::
context
&
ctx
,
fmt
::
format_specs
&
s
)
:
fmt
::
arg_formatter
<
cha
r
>
(
buf
,
ctx
,
s
)
{}
:
fmt
::
arg_formatter
<
fmt
::
buffe
r
>
(
buf
,
ctx
,
s
)
{}
};
TEST
(
OStreamTest
,
CustomArg
)
{
fmt
::
memory_buffer
buffer
;
fmt
::
context
ctx
(
""
,
fmt
::
format_args
());
fmt
::
context
ctx
(
buffer
,
""
,
fmt
::
format_args
());
fmt
::
format_specs
spec
;
TestArgFormatter
af
(
buffer
,
ctx
,
spec
);
visit
(
af
,
fmt
::
internal
::
make_arg
<
fmt
::
context
>
(
TestEnum
()));
...
...
test/util-test.cc
View file @
c0954453
...
...
@@ -81,9 +81,9 @@ struct formatter<Test, Char> {
return
ctx
.
begin
();
}
void
format
(
basic_buffer
<
Char
>
&
b
,
Test
,
basic_context
<
Char
>
&
)
{
void
format
(
Test
,
basic_context
<
basic_buffer
<
Char
>>
&
ctx
)
{
const
Char
*
test
=
"test"
;
b
.
append
(
test
,
test
+
std
::
strlen
(
test
));
ctx
.
range
()
.
append
(
test
,
test
+
std
::
strlen
(
test
));
}
};
}
...
...
@@ -442,7 +442,7 @@ struct CustomContext {
return
ctx
.
begin
();
}
void
format
(
fmt
::
buffer
&
,
const
T
&
,
CustomContext
&
ctx
)
{
void
format
(
const
T
&
,
CustomContext
&
ctx
)
{
ctx
.
called
=
true
;
}
};
...
...
@@ -456,8 +456,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
::
Test
t
;
fmt
::
internal
::
value
<
CustomContext
>
arg
(
t
);
CustomContext
ctx
=
{
false
};
fmt
::
memory_buffer
buffer
;
arg
.
custom
.
format
(
buffer
,
&
t
,
ctx
);
arg
.
custom
.
format
(
&
t
,
ctx
);
EXPECT_TRUE
(
ctx
.
called
);
}
...
...
@@ -518,7 +517,8 @@ VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) { \
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \
fmt::visit(visitor, make_arg<fmt::basic_context<Char>>(value)); \
fmt::visit(visitor, \
make_arg<fmt::basic_context<basic_buffer<Char>>>(value)); \
}
#define CHECK_ARG(value) { \
...
...
@@ -596,8 +596,8 @@ TEST(UtilTest, CustomArg) {
testing
::
StrictMock
<
visitor
>
v
;
EXPECT_CALL
(
v
,
visit
(
_
)).
WillOnce
(
testing
::
Invoke
([
&
](
handle
h
)
{
fmt
::
memory_buffer
buffer
;
fmt
::
context
ctx
(
""
,
fmt
::
format_args
());
h
.
format
(
buffer
,
ctx
);
fmt
::
context
ctx
(
buffer
,
""
,
fmt
::
format_args
());
h
.
format
(
ctx
);
EXPECT_EQ
(
"test"
,
std
::
string
(
buffer
.
data
(),
buffer
.
size
()));
return
visitor
::
Result
();
}));
...
...
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