aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/string.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'libxsde/xsde/cxx/string.cxx')
-rw-r--r--libxsde/xsde/cxx/string.cxx75
1 files changed, 75 insertions, 0 deletions
diff --git a/libxsde/xsde/cxx/string.cxx b/libxsde/xsde/cxx/string.cxx
new file mode 100644
index 0000000..2edb3cb
--- /dev/null
+++ b/libxsde/xsde/cxx/string.cxx
@@ -0,0 +1,75 @@
+// file : xsde/cxx/string.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC
+// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+#include <string.h>
+
+#include <xsde/cxx/string.hxx>
+
+namespace xsde
+{
+ namespace cxx
+ {
+ string::error string::
+ assign (const char* s, size_t size)
+ {
+ if (size + 1 > capacity_)
+ if (error e = resize (size + 1, false))
+ return e;
+
+ if (size != 0)
+ memcpy (data_, s, size);
+
+ data_[size] = '\0';
+
+ size_ = size;
+
+ return error_none;
+ }
+
+ string::error string::
+ append (const char* s, size_t size)
+ {
+ if (size_ + size + 1 > capacity_)
+ if (error e = resize (size_ + size + 1, true))
+ return e;
+
+ if (size != 0)
+ memcpy (data_ + size_, s, size);
+
+ size_ += size;
+ data_[size_] = '\0';
+
+ return error_none;
+ }
+
+ string::error string::
+ resize (size_t new_cap, bool copy)
+ {
+ // Start with at least 64 chars (32 * 2).
+ //
+ size_t cap = capacity_ ? capacity_ : 32;
+
+ if (new_cap <= 2 * cap)
+ new_cap = 2 * cap;
+ else
+ new_cap += (new_cap & 1) ? 1 : 0; // Make even.
+
+ char* p = new char[new_cap];
+
+ if (p == 0)
+ return error_no_memory;
+
+ if (copy && size_ != 0)
+ memcpy (p, data_, size_ + 1);
+
+ delete[] data_;
+
+ data_ = p;
+ capacity_ = new_cap;
+
+ return error_none;
+ }
+ }
+}