/
string_buffer.cpp
65 lines (56 loc) · 1.34 KB
/
string_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
//
//
// Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
// Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
#include <cstdio>
#include <cstring>
#include "common.h"
#include "string_buffer.h"
#define DEFAULT_ALLOC_SIZE BUF_SIZE
namespace MeCab {
bool StringBuffer::reserve(size_t length) {
if (!is_delete_) {
error_ = (size_ + length >= alloc_size_);
return (!error_);
}
if (size_ + length >= alloc_size_) {
if (alloc_size_ == 0) {
alloc_size_ = DEFAULT_ALLOC_SIZE;
ptr_ = new char[alloc_size_];
}
size_t len = size_ + length;
do {
alloc_size_ *= 2;
} while (len >= alloc_size_);
char *new_ptr = new char[alloc_size_];
std::memcpy(new_ptr, ptr_, size_);
delete [] ptr_;
ptr_ = new_ptr;
}
return true;
}
StringBuffer::~StringBuffer() {
if (is_delete_) {
delete [] ptr_;
ptr_ = 0;
}
}
StringBuffer& StringBuffer::write(char str) {
if (reserve(1)) {
ptr_[size_] = str;
++size_;
}
return *this;
}
StringBuffer& StringBuffer::write(const char* str) {
return this->write(str, std::strlen(str));
}
StringBuffer& StringBuffer::write(const char* str, size_t length) {
if (reserve(length)) {
std::memcpy(ptr_ + size_ , str, length);
size_ += length;
}
return *this;
}
}