00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "chars_seq.H"
00031
00032 typedef chars_seq::chars chars;
00033 typedef chars_seq_impl::rep rep;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 inline int round_up(int v, int m) { return (((v + m - 1) / m) * m); }
00044
00045 rep *chars_seq_impl::allocate_buffer(int len_hint, int bytes)
00046 {
00047
00048
00049
00050 int size = chars_seq::min_size(len_hint) + round_up(bytes, sizeof(chars));
00051 char* buffer = (char *) NEW_PTRFREE_ARRAY(chars, size/sizeof(chars));
00052 rep *p = (rep *) buffer;
00053 p->h.storage = grow;
00054 p->h.len = 0;
00055 p->h.bodies = p->h.limit = &buffer[size];
00056 return p;
00057 }
00058
00059 void chars_seq_impl::expand(rep *&p, int len_hint, int min_body_add)
00060 {
00061 if (p->h.storage == manual)
00062 throw(chars_seq::failure("Buffer has insufficient space."));
00063
00064
00065
00066
00067 int new_len = len_hint < 0 ? p->h.len : len_hint;
00068 int old_body_sz = p->h.limit - p->h.bodies;
00069 int old_char_cnt = old_body_sz - p->h.len;
00070 int new_char_cnt = old_char_cnt +
00071 ((min_body_add >= 0) ? max(old_char_cnt, min_body_add) : 0);
00072
00073
00074 rep *new_p = allocate_buffer(new_len, new_char_cnt);
00075
00076 new_p->h.storage = p->h.storage;
00077 new_p->h.len = p->h.len;
00078 new_p->h.bodies -= old_body_sz;
00079
00080
00081 memcpy(new_p->h.bodies, p->h.bodies, old_body_sz);
00082
00083
00084 chars *seq = &p->base;
00085 chars *new_seq = &new_p->base;
00086 for (int i = 0; i < p->h.len; i++) {
00087 assert(seq[i] >= p->h.bodies && seq[i] < p->h.limit);
00088
00089
00090 int offset = p->h.limit - seq[i];
00091
00092
00093 new_seq[i] = new_p->h.limit - offset;
00094
00095
00096
00097 assert((new_seq[i] >= new_p->h.bodies) &&
00098 (new_seq[i] < new_p->h.limit));
00099
00100 seq[i] = (char *)NULL;
00101 }
00102 delete[] (chars *)p;
00103 p = new_p;
00104 }
00105
00106
00107
00108
00109
00110
00111 chars_seq::chars_seq(int len_hint, int bytes)
00112 {
00113 if (len_hint == 0 && bytes == 0)
00114
00115
00116
00117 p = NULL;
00118 else p = chars_seq_impl::allocate_buffer(len_hint, bytes);
00119 }
00120
00121 chars_seq::chars_seq(void **buffer, int bytes) throw (chars_seq::failure)
00122 {
00123 p = (rep *) buffer;
00124 p->h.storage = chars_seq_impl::manual;
00125 p->h.len = 0;
00126 p->h.bodies = p->h.limit = (char *)buffer + bytes;
00127 }
00128
00129 chars_seq::chars_seq(const char **seq, int len) throw(chars_seq::failure)
00130 {
00131 int total_len = 0;
00132 for (int i = 0; i < len; i++) total_len += strlen(seq[i]);
00133 p = chars_seq_impl::allocate_buffer(len, total_len);
00134 for (int i = 0; i < len; i++) append(seq[i]);
00135 }
00136
00137 chars_seq::~chars_seq()
00138 {
00139 if (p == NULL || p->h.storage == chars_seq_impl::manual) return;
00140 delete[] (chars *)p;
00141 }
00142
00143 int chars_seq::min_size(int len)
00144 {
00145
00146
00147
00148
00149
00150 return
00151 round_up(sizeof(chars_seq_impl::rep) + (len-1)*sizeof(chars) + len*1,
00152 sizeof(chars));
00153 }
00154
00155
00156
00157
00158
00159 void chars_seq::append(const chars s) throw(chars_seq::failure)
00160 {
00161 if (p == NULL) p = chars_seq_impl::allocate_buffer();
00162 int s_len = strlen(s);
00163 int bytes_for_body = s_len + 1;
00164 chars *seq = &p->base;
00165 char *s_copy = p->h.bodies - bytes_for_body;
00166
00167
00168
00169 if (&seq[p->h.len+1] > (chars *)s_copy) {
00170
00171 chars_seq_impl::expand(p, 1+2*p->h.len, s_len);
00172
00173 seq = &p->base;
00174 s_copy = p->h.bodies - bytes_for_body;
00175 }
00176
00177 assert(&seq[p->h.len+1] <= (chars *)s_copy);
00178
00179 memcpy(s_copy, s, bytes_for_body);
00180 seq[p->h.len] = p->h.bodies = s_copy;
00181 p->h.len++;
00182 }
00183
00184 void chars_seq::append(const Text &t) throw(chars_seq::failure)
00185 {
00186 append(t.chars());
00187 }