|
|
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:
1.2 ! paf 9: void String::construct(Pool *apool) {
1.1 paf 10: pool=apool;
1.2 ! paf 11: head.count=curr_chunk_rows=CR_PREALLOCATED_COUNT;
! 12: append_here=head.first;
! 13: head.preallocated_link=0;
! 14: link_row=&head.first[curr_chunk_rows];
1.1 paf 15: }
16:
17: void String::expand() {
1.2 ! paf 18: curr_chunk_rows=curr_chunk_rows*100/CR_GROW_PERCENT;
! 19: Chunk *chunk=static_cast<Chunk *>(
! 20: pool->calloc(sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
! 21: chunk->count=curr_chunk_rows;
! 22: link_row->link=chunk;
! 23: append_here=chunk->first;
! 24: link_row=&chunk->first[curr_chunk_rows];
1.1 paf 25: }
26:
27: String& String::operator += (char *src) {
28: if(chunk_is_full())
29: expand();
30:
31: append_here->item.ptr=src;
32: append_here->item.size=strlen(src);
33: append_here++;
34:
35: return *this;
36: }
37:
38: size_t String::size() {
39: int result=0;
1.2 ! paf 40: Chunk *chunk=&head;
! 41: do {
! 42: Chunk::Row *row=chunk->first;
! 43: for(int i=0; i<chunk->count; i++) {
1.1 paf 44: if(row==append_here)
45: goto break2;
46:
47: result+=row->item.size;
48: row++;
49: }
1.2 ! paf 50: chunk=row->link;
! 51: } while(chunk);
1.1 paf 52: break2:
53: return result;
54: }
55:
56: char *String::c_str() {
57: char *result=static_cast<char *>(pool->alloc(size()+1));
58:
59: char *copy_here=result;
1.2 ! paf 60: Chunk *chunk=&head;
! 61: do {
! 62: Chunk::Row *row=chunk->first;
! 63: for(int i=0; i<chunk->count; i++) {
1.1 paf 64: if(row==append_here)
65: goto break2;
66:
67: memcpy(copy_here, row->item.ptr, row->item.size);
68: copy_here+=row->item.size;
69: row++;
70: }
1.2 ! paf 71: chunk=row->link;
! 72: } while(chunk);
1.1 paf 73: break2:
74: *copy_here=0;
75: return result;
76: }
77: