[pp] Introduce IndentedStringBuilder (#563)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 9 Jul 2018 06:37:50 +0000 (15:37 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 9 Jul 2018 06:37:50 +0000 (15:37 +0900)
* [pp] Introduce IndentedStringBuilder

This commit extracts IndentedStringBuilder from IndentedDocumentBuilder
class.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Update local variable names

contrib/pp/include/pp/IndentedDocumentBuilder.h
contrib/pp/include/pp/IndentedStringBuilder.h [new file with mode: 0644]
contrib/pp/src/IndentedDocumentBuilder.cpp
contrib/pp/src/IndentedStringBuilder.cpp [new file with mode: 0644]
contrib/pp/src/IndentedStringBuilder.test.cpp [new file with mode: 0644]

index c7e7dde..dc1a408 100644 (file)
@@ -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 (file)
index 0000000..ae80185
--- /dev/null
@@ -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 <typename... Args> std::string build(const Args &... args)
+  {
+    return build(fmt(args...));
+  }
+
+private:
+  uint32_t _level;
+};
+
+} // namespace pp
+
+#endif // __PP_INDENTED_STRING_BUILDER_H__
index fe68bd8..a03739c 100644 (file)
@@ -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 (file)
index 0000000..71a75e1
--- /dev/null
@@ -0,0 +1,32 @@
+#include "pp/IndentedStringBuilder.h"
+
+#include <algorithm>
+#include <cassert>
+
+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 (file)
index 0000000..efbb620
--- /dev/null
@@ -0,0 +1,14 @@
+#include "pp/IndentedStringBuilder.h"
+
+#include <gtest/gtest.h>
+
+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");
+}