[Groonga-commit] groonga/grnxx at d837813 [master] Move implementations from .hpp to .cpp.

Back to archive index

susumu.yata null+****@clear*****
Mon Jul 1 12:07:24 JST 2013


susumu.yata	2013-07-01 12:07:24 +0900 (Mon, 01 Jul 2013)

  New Revision: d837813a6c026a0d6f0b8ad8d8e986477c9d01b6
  https://github.com/groonga/grnxx/commit/d837813a6c026a0d6f0b8ad8d8e986477c9d01b6

  Message:
    Move implementations from .hpp to .cpp.

  Modified files:
    lib/grnxx/string_builder.cpp
    lib/grnxx/string_builder.hpp
    lib/grnxx/string_format.hpp

  Modified: lib/grnxx/string_builder.cpp (+128 -14)
===================================================================
--- lib/grnxx/string_builder.cpp    2013-07-01 11:14:31 +0900 (fc59064)
+++ lib/grnxx/string_builder.cpp    2013-07-01 12:07:24 +0900 (1a8f922)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2012  Brazil, Inc.
+  Copyright (C) 2012-2013  Brazil, Inc.
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -21,34 +21,149 @@
 #include <cstdio>
 #include <utility>
 
+#include "grnxx/exception.hpp"
 #include "grnxx/intrinsic.hpp"
+#include "grnxx/logger.hpp"
 
 namespace grnxx {
+namespace {
+
+constexpr size_t STRING_BUILDER_MIN_BUF_SIZE = 64;
+
+}  // namespace
+
+StringBuilder::StringBuilder(StringBuilderFlags flags)
+    : buf_(),
+      begin_(nullptr),
+      end_(nullptr),
+      ptr_(nullptr),
+      flags_(flags),
+      failed_(false) {}
 
 StringBuilder::StringBuilder(size_t size, StringBuilderFlags flags)
-    : buf_((size != 0) ? (new (std::nothrow) char[size]) : nullptr),
-      begin_(buf_.get()),
-      end_(buf_ ? (begin_ + size - 1) : nullptr),
-      ptr_(begin_),
+    : buf_(),
+      begin_(nullptr),
+      end_(nullptr),
+      ptr_(nullptr),
+      flags_(flags),
+      failed_(false) {
+  if (size != 0) {
+    buf_.reset(new (std::nothrow) char[size]);
+    if (!buf_) {
+      if (~flags_ & STRING_BUILDER_NOEXCEPT) {
+        GRNXX_ERROR() << "new char[" << size << "] failed";
+        throw MemoryError();
+      }
+      failed_ = true;
+    } else {
+      begin_ = buf_.get();
+      end_ = begin_ + size - 1;
+      ptr_ = begin_;
+      *ptr_ = '\0';
+    }
+  }
+}
+
+StringBuilder::StringBuilder(char *buf, size_t size, StringBuilderFlags flags)
+    : buf_(),
+      begin_(buf),
+      end_(buf + size - 1),
+      ptr_(buf),
       flags_(flags),
-      failed_(!buf_) {
-  if (buf_) {
-    *ptr_ = '\0';
+      failed_(false) {
+  *ptr_ = '\0';
+}
+
+StringBuilder::~StringBuilder() {}
+
+StringBuilder &StringBuilder::append(int byte) {
+  if (failed_) {
+    return *this;
+  }
+  if (ptr_ == end_) {
+    if (!auto_resize(length() + 2)) {
+      return *this;
+    }
+  }
+  *ptr_ = static_cast<char>(byte);
+  *++ptr_ = '\0';
+  return *this;
+}
+
+StringBuilder &StringBuilder::append(int byte, size_t length) {
+  if (failed_ || (length == 0)) {
+    return *this;
+  }
+  const size_t size_left = end_ - ptr_;
+  if (length > size_left) {
+    if (!auto_resize(this->length() + length + 1)) {
+      length = size_left;
+      if (length == 0) {
+        return *this;
+      }
+    }
+  }
+  std::memset(ptr_, byte, length);
+  ptr_ += length;
+  *ptr_ = '\0';
+  return *this;
+}
+
+StringBuilder &StringBuilder::append(const char *ptr, size_t length) {
+  if (failed_ || !ptr || (length == 0)) {
+    return *this;
   }
+  const size_t size_left = end_ - ptr_;
+  if (length > size_left) {
+    if (!auto_resize(this->length() + length + 1)) {
+      length = size_left;
+      if (length == 0) {
+        return *this;
+      }
+    }
+  }
+  std::memcpy(ptr_, ptr, length);
+  ptr_ += length;
+  *ptr_ = '\0';
+  return *this;
+}
+
+// TODO: To be removed if this is not used.
+StringBuilder &StringBuilder::resize(size_t length) {
+  const size_t size_left = end_ - ptr_;
+  if (length > size_left) {
+    if (!resize_buf(length + 1)) {
+      length = size_left;
+      failed_ = true;
+    }
+  }
+  ptr_ = begin_ + length;
+  *ptr_ = '\0';
+  return *this;
+}
+
+bool StringBuilder::auto_resize(size_t size) {
+  if (~flags_ & STRING_BUILDER_AUTO_RESIZE) {
+    failed_ = true;
+    return false;
+  }
+  return resize_buf(size);
 }
 
 bool StringBuilder::resize_buf(size_t size) {
-  if (size < STRING_BUILDER_BUF_SIZE_MIN) {
-    size = STRING_BUILDER_BUF_SIZE_MIN;
+  if (size < STRING_BUILDER_MIN_BUF_SIZE) {
+    size = STRING_BUILDER_MIN_BUF_SIZE;
   } else {
-    size = size_t(1) << (bit_scan_reverse(size - 1) + 1);
+    size = size_t(2) << bit_scan_reverse(size - 1);
   }
-
   std::unique_ptr<char[]> new_buf(new (std::nothrow) char[size]);
   if (!new_buf) {
+    if (~flags_ & STRING_BUILDER_NOEXCEPT) {
+      GRNXX_ERROR() << "new char [" << size << "] failed";
+      throw MemoryError();
+    }
     return false;
   }
-
   const size_t length = ptr_ - begin_;
   std::memcpy(new_buf.get(), begin_, length);
   ptr_ = new_buf.get() + length;
@@ -176,7 +291,6 @@ StringBuilder &operator<<(StringBuilder &builder, const void *value) {
 }
 
 StringBuilder &operator<<(StringBuilder &builder, const Bytes &bytes) {
-  // TODO: StringBuilder should support const uint_8 *.
   return builder.append(reinterpret_cast<const char *>(bytes.data()),
                         bytes.size());
 }

  Modified: lib/grnxx/string_builder.hpp (+12 -88)
===================================================================
--- lib/grnxx/string_builder.hpp    2013-07-01 11:14:31 +0900 (da8ef21)
+++ lib/grnxx/string_builder.hpp    2013-07-01 12:07:24 +0900 (cf46746)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2012  Brazil, Inc.
+  Copyright (C) 2012-2013  Brazil, Inc.
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -30,8 +30,6 @@
 
 namespace grnxx {
 
-constexpr size_t STRING_BUILDER_BUF_SIZE_MIN = 64;
-
 class StringBuilder;
 typedef FlagsImpl<StringBuilder> StringBuilderFlags;
 
@@ -41,20 +39,14 @@ constexpr StringBuilderFlags STRING_BUILDER_DEFAULT     =
 // Automatically resize the buffer.
 constexpr StringBuilderFlags STRING_BUILDER_AUTO_RESIZE =
     StringBuilderFlags::define(0x01);
-// Don't throw on failure.
+// Don't throw even if memory allocation fails.
 constexpr StringBuilderFlags STRING_BUILDER_NOEXCEPT    =
     StringBuilderFlags::define(0x02);
 
 class StringBuilder {
  public:
   // Create an empty StringBuilder.
-  explicit StringBuilder(StringBuilderFlags flags = STRING_BUILDER_DEFAULT)
-      : buf_(),
-        begin_(nullptr),
-        end_(nullptr),
-        ptr_(nullptr),
-        flags_(flags),
-        failed_(false) {}
+  explicit StringBuilder(StringBuilderFlags flags = STRING_BUILDER_DEFAULT);
   // Allocate "size" bytes to the internal buffer.
   explicit StringBuilder(size_t size,
                          StringBuilderFlags flags = STRING_BUILDER_DEFAULT);
@@ -72,16 +64,8 @@ class StringBuilder {
   }
   // Use "buf" ("size" bytes) as the internal buffer.
   StringBuilder(char *buf, size_t size,
-                StringBuilderFlags flags = STRING_BUILDER_DEFAULT)
-      : buf_(),
-        begin_(buf),
-        end_(buf + size - 1),
-        ptr_(buf),
-        flags_(flags),
-        failed_(false) {
-    *ptr_ = '\0';
-  }
-  ~StringBuilder() {}
+                StringBuilderFlags flags = STRING_BUILDER_DEFAULT);
+  ~StringBuilder();
 
   // TODO: To be removed if these are not used.
   StringBuilder(StringBuilder &&rhs)
@@ -112,77 +96,15 @@ class StringBuilder {
   }
 
   // Append a character.
-  StringBuilder &append(int byte) {
-    if (failed_) {
-      return *this;
-    }
-    if (ptr_ == end_) {
-      if ((~flags_ & STRING_BUILDER_AUTO_RESIZE) ||
-          !resize_buf(this->length() + 2)) {
-        failed_ = true;
-        return *this;
-      }
-    }
-    *ptr_ = static_cast<char>(byte);
-    *++ptr_ = '\0';
-    return *this;
-  }
-
+  StringBuilder &append(int byte);
   // Append a sequence of the same character.
-  StringBuilder &append(int byte, size_t length) {
-    if (failed_ || (length == 0)) {
-      return *this;
-    }
-    const size_t size_left = end_ - ptr_;
-    if (length > size_left) {
-      if ((~flags_ & STRING_BUILDER_AUTO_RESIZE) ||
-          !resize_buf(this->length() + length + 1)) {
-        length = size_left;
-        failed_ = true;
-        if (length == 0) {
-          return *this;
-        }
-      }
-    }
-    std::memset(ptr_, byte, length);
-    ptr_ += length;
-    *ptr_ = '\0';
-    return *this;
-  }
-
+  StringBuilder &append(int byte, size_t length);
   // Append a sequence of length characters.
-  StringBuilder &append(const char *ptr, size_t length) {
-    if (failed_ || !ptr || (length == 0)) {
-      return *this;
-    }
-    const size_t size_left = end_ - ptr_;
-    if (length > size_left) {
-      if ((~flags_ & STRING_BUILDER_AUTO_RESIZE) ||
-          !resize_buf(this->length() + length + 1)) {
-        length = size_left;
-        failed_ = true;
-      }
-    }
-    std::memcpy(ptr_, ptr, length);
-    ptr_ += length;
-    *ptr_ = '\0';
-    return *this;
-  }
+  StringBuilder &append(const char *ptr, size_t length);
 
+  // TODO: To be removed if this is not used.
   // Resize the internal buffer.
-  StringBuilder &resize(size_t length) {
-    const size_t size_left = end_ - ptr_;
-    if (length > size_left) {
-      if ((~flags_ & STRING_BUILDER_AUTO_RESIZE) ||
-          !resize_buf(length + 1)) {
-        length = size_left;
-        failed_ = true;
-      }
-    }
-    ptr_ = begin_ + length;
-    *ptr_ = '\0';
-    return *this;
-  }
+  StringBuilder &resize(size_t length);
 
   // Return the "i"-th byte.
   const char &operator[](size_t i) const {
@@ -226,6 +148,8 @@ class StringBuilder {
   StringBuilderFlags flags_;
   bool failed_;
 
+  // Resize the internal buffer.
+  bool auto_resize(size_t size);
   // Resize the internal buffer to greater than or equal to "size" bytes.
   bool resize_buf(size_t size);
 

  Modified: lib/grnxx/string_format.hpp (+4 -2)
===================================================================
--- lib/grnxx/string_format.hpp    2013-07-01 11:14:31 +0900 (6eabec7)
+++ lib/grnxx/string_format.hpp    2013-07-01 12:07:24 +0900 (60ffda8)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2012  Brazil, Inc.
+  Copyright (C) 2012-2013  Brazil, Inc.
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,8 @@
 
 namespace grnxx {
 
+constexpr size_t STRING_FORMAT_LOCAL_BUF_SIZE = 64;
+
 enum StringFormatAlignmentAttribute {
   STRING_FORMAT_ALIGNMENT_LEFT,
   STRING_FORMAT_ALIGNMENT_RIGHT,
@@ -96,7 +98,7 @@ class StringFormat {
 template <typename T>
 StringBuilder &operator<<(StringBuilder &builder,
                           const StringFormatAlignment<T> &alignment) {
-  char local_buf[STRING_BUILDER_BUF_SIZE_MIN];
+  char local_buf[STRING_FORMAT_LOCAL_BUF_SIZE];
   const StringBuilderFlags local_flags =
       (alignment.width() >= sizeof(local_buf)) ?
       STRING_BUILDER_AUTO_RESIZE : STRING_BUILDER_DEFAULT;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index