blob: 67126205757a8ffc5d6d4f13614f685b5b2029db (
plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// file : xsde/cxx/string.cxx
// copyright : Copyright (c) 2005-2014 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.
#ifndef XSDE_CUSTOM_ALLOCATOR
char* p = new char[new_cap];
#else
char* p = static_cast<char*> (alloc (new_cap));
#endif
if (p == 0)
return error_no_memory;
if (copy && size_ != 0)
memcpy (p, data_, size_ + 1);
#ifndef XSDE_CUSTOM_ALLOCATOR
delete[] data_;
#else
cxx::free (data_);
#endif
data_ = p;
capacity_ = new_cap;
return error_none;
}
}
}
|