From 91aaa723851b148b5cac3aa0a505605d26ec08fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Mon, 9 Jul 2018 15:37:50 +0900 Subject: [PATCH] [pp] Introduce IndentedStringBuilder (#563) * [pp] Introduce IndentedStringBuilder This commit extracts IndentedStringBuilder from IndentedDocumentBuilder class. Signed-off-by: Jonghyun Park * Update local variable names --- contrib/pp/include/pp/IndentedDocumentBuilder.h | 5 ++-- contrib/pp/include/pp/IndentedStringBuilder.h | 36 +++++++++++++++++++++++++ contrib/pp/src/IndentedDocumentBuilder.cpp | 19 +++---------- contrib/pp/src/IndentedStringBuilder.cpp | 32 ++++++++++++++++++++++ contrib/pp/src/IndentedStringBuilder.test.cpp | 14 ++++++++++ 5 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 contrib/pp/include/pp/IndentedStringBuilder.h create mode 100644 contrib/pp/src/IndentedStringBuilder.cpp create mode 100644 contrib/pp/src/IndentedStringBuilder.test.cpp diff --git a/contrib/pp/include/pp/IndentedDocumentBuilder.h b/contrib/pp/include/pp/IndentedDocumentBuilder.h index c7e7dde..dc1a408 100644 --- a/contrib/pp/include/pp/IndentedDocumentBuilder.h +++ b/contrib/pp/include/pp/IndentedDocumentBuilder.h @@ -2,6 +2,7 @@ #define __PP_INDENTED_DOCUMENT_BUILDER_H__ #include "pp/Format.h" +#include "pp/IndentedStringBuilder.h" namespace pp { @@ -9,7 +10,7 @@ namespace pp class IndentedDocumentBuilder { public: - IndentedDocumentBuilder(std::ostream &os) : _os(os), _level{0} + IndentedDocumentBuilder(std::ostream &os) : _os(os) { // DO NOTHING } @@ -29,7 +30,7 @@ private: std::ostream &_os; private: - uint32_t _level; + IndentedStringBuilder _builder; }; } // namespace pp diff --git a/contrib/pp/include/pp/IndentedStringBuilder.h b/contrib/pp/include/pp/IndentedStringBuilder.h new file mode 100644 index 0000000..ae80185 --- /dev/null +++ b/contrib/pp/include/pp/IndentedStringBuilder.h @@ -0,0 +1,36 @@ +#ifndef __PP_INDENTED_STRING_BUILDER_H__ +#define __PP_INDENTED_STRING_BUILDER_H__ + +#include "pp/Format.h" + +namespace pp +{ + +class IndentedStringBuilder +{ +public: + IndentedStringBuilder() : _level{0} + { + // DO NOTHING + } + +public: + void increase(void); + void decrease(void); + +public: + std::string build(const std::string &content); + +public: + template std::string build(const Args &... args) + { + return build(fmt(args...)); + } + +private: + uint32_t _level; +}; + +} // namespace pp + +#endif // __PP_INDENTED_STRING_BUILDER_H__ diff --git a/contrib/pp/src/IndentedDocumentBuilder.cpp b/contrib/pp/src/IndentedDocumentBuilder.cpp index fe68bd8..a03739c 100644 --- a/contrib/pp/src/IndentedDocumentBuilder.cpp +++ b/contrib/pp/src/IndentedDocumentBuilder.cpp @@ -6,28 +6,15 @@ namespace pp { -void IndentedDocumentBuilder::indent(void) -{ - // TODO Check overflow - ++_level; -} +void IndentedDocumentBuilder::indent(void) { _builder.increase(); } -void IndentedDocumentBuilder::unindent(void) -{ - assert(_level > 0); - --_level; -} +void IndentedDocumentBuilder::unindent(void) { _builder.decrease(); } void IndentedDocumentBuilder::empty(void) { _os << std::endl; } void IndentedDocumentBuilder::line(const std::string &content) { - assert(std::find(content.begin(), content.end(), '\n') == content.end()); - - const char c = ' '; - const size_t n = 2 * _level; - - _os << std::string(n, c) << content << std::endl; + _os << _builder.build(content) << std::endl; } } // namespace pp diff --git a/contrib/pp/src/IndentedStringBuilder.cpp b/contrib/pp/src/IndentedStringBuilder.cpp new file mode 100644 index 0000000..71a75e1 --- /dev/null +++ b/contrib/pp/src/IndentedStringBuilder.cpp @@ -0,0 +1,32 @@ +#include "pp/IndentedStringBuilder.h" + +#include +#include + +namespace pp +{ + +void IndentedStringBuilder::increase(void) +{ + // TODO Check overflow + ++_level; +} + +void IndentedStringBuilder::decrease(void) +{ + assert(_level > 0); + --_level; +} + +std::string IndentedStringBuilder::build(const std::string &content) +{ + assert(std::find(content.begin(), content.end(), '\n') == content.end()); + + const char c = ' '; + const size_t space_per_indent_level = 2; + const size_t space_count = space_per_indent_level * _level; + + return std::string(space_count, c) + content; +} + +} // namespace pp diff --git a/contrib/pp/src/IndentedStringBuilder.test.cpp b/contrib/pp/src/IndentedStringBuilder.test.cpp new file mode 100644 index 0000000..efbb620 --- /dev/null +++ b/contrib/pp/src/IndentedStringBuilder.test.cpp @@ -0,0 +1,14 @@ +#include "pp/IndentedStringBuilder.h" + +#include + +TEST(INDENTED_STRING_BUILDER, usage) +{ + pp::IndentedStringBuilder builder{}; + + ASSERT_EQ(builder.build("A"), "A"); + builder.increase(); + ASSERT_EQ(builder.build("B"), " B"); + builder.decrease(); + ASSERT_EQ(builder.build("C"), "C"); +} -- 2.7.4