|
|
1.1 paf 1: #include <string.h>
2:
3: #include "pa_pool.h"
4:
5: void *String::operator new(size_t size, Pool *apool) {
6: return apool->alloc(size);
7: }
8:
9: void String::construct(Pool *apool, int achunk_items) {
10: pool=apool;
11: chunk_items=achunk_items;
12:
13: head_chunk=static_cast<Chunk *>(
14: pool->calloc(sizeof(Chunk::Row)*chunk_items+sizeof(Chunk *)));
15: append_here=&head_chunk->first;
16: chunk_link_row=&head_chunk->first+chunk_items;
17: }
18:
19: void String::expand() {
20: chunk_link_row->link=static_cast<Chunk *>(
21: pool->calloc(sizeof(Chunk::Row)*chunk_items+sizeof(Chunk *)));
22: append_here=&chunk_link_row->link->first;
23: chunk_link_row=&chunk_link_row->link->first+chunk_items;
24: }
25:
26: String& String::operator += (char *src) {
27: if(chunk_is_full())
28: expand();
29:
30: append_here->item.ptr=src;
31: append_here->item.size=strlen(src);
32: append_here++;
33:
34: return *this;
35: }
36:
37: size_t String::size() {
38: int result=0;
39: for(Chunk::Row *row=&head_chunk->first; row; row=&row->link->first)
40: for(int i=0; i<chunk_items; i++) {
41: if(row==append_here)
42: goto break2;
43:
44: result+=row->item.size;
45: row++;
46: }
47: break2:
48: return result;
49: }
50:
51: char *String::c_str() {
52: char *result=static_cast<char *>(pool->alloc(size()+1));
53:
54: char *copy_here=result;
55: for(Chunk::Row *row=&head_chunk->first; row; row=&row->link->first)
56: for(int i=0; i<chunk_items; i++) {
57: if(row==append_here)
58: goto break2;
59:
60: memcpy(copy_here, row->item.ptr, row->item.size);
61: copy_here+=row->item.size;
62: row++;
63: }
64: break2:
65: *copy_here=0;
66: return result;
67: }
68: