Skip to content

Commit 025a92a

Browse files
committed
Issue #418 - Minimal supported library subset
1 parent f5b1c16 commit 025a92a

File tree

7 files changed

+66
-40
lines changed

7 files changed

+66
-40
lines changed

fmt/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Define the fmt library, its includes and the needed defines.
2-
# format.cc is added to FMT_HEADERS for the header-only configuration.
3-
set(FMT_HEADERS format.h format.cc ostream.h ostream.cc printf.h
2+
# *.cc are added to FMT_HEADERS for the header-only configuration.
3+
set(FMT_HEADERS format.h format.cc ostream.h ostream.cc printf.h printf.cc
44
string.h time.h)
55
if (HAVE_OPEN)
66
set(FMT_HEADERS ${FMT_HEADERS} posix.h)

fmt/format.cc

-35
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*/
2727

2828
#include "format.h"
29-
#include "printf.h"
3029

3130
#include <string.h>
3231

@@ -106,8 +105,6 @@ inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) {
106105
# define FMT_SWPRINTF swprintf
107106
#endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT)
108107

109-
const char RESET_COLOR[] = "\x1b[0m";
110-
111108
typedef void (*FormatFunc)(Writer &, int, StringRef);
112109

113110
// Portable thread-safe version of strerror.
@@ -486,34 +483,6 @@ FMT_FUNC void report_windows_error(
486483
}
487484
#endif
488485

489-
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args) {
490-
MemoryWriter w;
491-
w.write(format_str, args);
492-
std::fwrite(w.data(), 1, w.size(), f);
493-
}
494-
495-
FMT_FUNC void print(CStringRef format_str, ArgList args) {
496-
print(stdout, format_str, args);
497-
}
498-
499-
FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) {
500-
char escape[] = "\x1b[30m";
501-
escape[3] = static_cast<char>('0' + c);
502-
std::fputs(escape, stdout);
503-
print(format, args);
504-
std::fputs(RESET_COLOR, stdout);
505-
}
506-
507-
template <typename Char>
508-
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args);
509-
510-
FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) {
511-
MemoryWriter w;
512-
printf(w, format, args);
513-
std::size_t size = w.size();
514-
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
515-
}
516-
517486
#ifndef FMT_HEADER_ONLY
518487

519488
template struct internal::BasicData<void>;
@@ -524,8 +493,6 @@ template void internal::FixedBuffer<char>::grow(std::size_t);
524493

525494
template void internal::ArgMap<char>::init(const ArgList &args);
526495

527-
template void PrintfFormatter<char>::format(CStringRef format);
528-
529496
template int internal::CharTraits<char>::format_float(
530497
char *buffer, std::size_t size, const char *format,
531498
unsigned width, int precision, double value);
@@ -540,8 +507,6 @@ template void internal::FixedBuffer<wchar_t>::grow(std::size_t);
540507

541508
template void internal::ArgMap<wchar_t>::init(const ArgList &args);
542509

543-
template void PrintfFormatter<wchar_t>::format(WCStringRef format);
544-
545510
template int internal::CharTraits<wchar_t>::format_float(
546511
wchar_t *buffer, std::size_t size, const wchar_t *format,
547512
unsigned width, int precision, double value);

fmt/printf.cc

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Formatting library for C++
3+
4+
Copyright (c) 2012 - 2016, Victor Zverovich
5+
All rights reserved.
6+
7+
For the license information refer to format.h.
8+
*/
9+
10+
#include "format.h"
11+
#include "printf.h"
12+
13+
namespace fmt {
14+
15+
namespace {
16+
17+
const char RESET_COLOR[] = "\x1b[0m";
18+
19+
} // namespace
20+
21+
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args) {
22+
MemoryWriter w;
23+
w.write(format_str, args);
24+
std::fwrite(w.data(), 1, w.size(), f);
25+
}
26+
27+
FMT_FUNC void print(CStringRef format_str, ArgList args) {
28+
print(stdout, format_str, args);
29+
}
30+
31+
FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) {
32+
char escape[] = "\x1b[30m";
33+
escape[3] = static_cast<char>('0' + c);
34+
std::fputs(escape, stdout);
35+
print(format, args);
36+
std::fputs(RESET_COLOR, stdout);
37+
}
38+
39+
template <typename Char>
40+
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args);
41+
42+
FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) {
43+
MemoryWriter w;
44+
printf(w, format, args);
45+
std::size_t size = w.size();
46+
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
47+
}
48+
49+
#ifndef FMT_HEADER_ONLY
50+
51+
template void PrintfFormatter<char>::format(CStringRef format);
52+
template void PrintfFormatter<wchar_t>::format(WCStringRef format);
53+
54+
#endif // FMT_HEADER_ONLY
55+
56+
} // namespace fmt

fmt/printf.h

+4
Original file line numberDiff line numberDiff line change
@@ -555,4 +555,8 @@ inline int fprintf(std::ostream &os, CStringRef format_str, ArgList args) {
555555
FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
556556
} // namespace fmt
557557

558+
#ifdef FMT_HEADER_ONLY
559+
# include "printf.cc"
560+
#endif
561+
558562
#endif // FMT_PRINTF_H_

test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ endif ()
9999

100100
if (HAVE_OPEN)
101101
add_fmt_executable(posix-mock-test
102-
posix-mock-test.cc ../fmt/format.cc ${TEST_MAIN_SRC})
102+
posix-mock-test.cc ../fmt/format.cc ../fmt/printf.cc ${TEST_MAIN_SRC})
103103
target_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR})
104104
target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
105105
target_link_libraries(posix-mock-test gmock)

test/find-package-test/main.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "fmt/format.h"
1+
#include "fmt/printf.h"
22

33
int main(int argc, char** argv) {
44
for(int i = 0; i < argc; ++i)

test/format-impl-test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
#define FMT_NOEXCEPT
2929
#include "test-assert.h"
3030

31-
// Include format.cc instead of format.h to test implementation-specific stuff.
31+
// Include *.cc instead of *.h to test implementation-specific stuff.
3232
#include "fmt/format.cc"
33+
#include "fmt/printf.cc"
3334

3435
#include <algorithm>
3536
#include <cstring>

0 commit comments

Comments
 (0)