Annotation of parser3/src/main/pa_string.C, revision 1.4
1.4 ! paf 1: /*
! 2: $Id: pa_string.C,v 1.3 2001/01/26 15:32:52 paf Exp $
! 3: */
! 4:
1.1 paf 5: #include <string.h>
6:
7: #include "pa_pool.h"
8:
9: void *String::operator new(size_t size, Pool *apool) {
10: return apool->alloc(size);
11: }
12:
1.2 paf 13: void String::construct(Pool *apool) {
1.1 paf 14: pool=apool;
1.2 paf 15: head.count=curr_chunk_rows=CR_PREALLOCATED_COUNT;
16: append_here=head.first;
17: head.preallocated_link=0;
18: link_row=&head.first[curr_chunk_rows];
1.1 paf 19: }
20:
21: void String::expand() {
1.2 paf 22: curr_chunk_rows=curr_chunk_rows*100/CR_GROW_PERCENT;
23: Chunk *chunk=static_cast<Chunk *>(
24: pool->calloc(sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
25: chunk->count=curr_chunk_rows;
26: link_row->link=chunk;
27: append_here=chunk->first;
28: link_row=&chunk->first[curr_chunk_rows];
1.1 paf 29: }
30:
31: String& String::operator += (char *src) {
32: if(chunk_is_full())
33: expand();
34:
35: append_here->item.ptr=src;
36: append_here->item.size=strlen(src);
37: append_here++;
38:
39: return *this;
40: }
41:
42: size_t String::size() {
43: int result=0;
1.2 paf 44: Chunk *chunk=&head;
45: do {
46: Chunk::Row *row=chunk->first;
47: for(int i=0; i<chunk->count; i++) {
1.1 paf 48: if(row==append_here)
49: goto break2;
50:
51: result+=row->item.size;
52: row++;
53: }
1.2 paf 54: chunk=row->link;
55: } while(chunk);
1.1 paf 56: break2:
57: return result;
58: }
59:
60: char *String::c_str() {
61: char *result=static_cast<char *>(pool->alloc(size()+1));
62:
63: char *copy_here=result;
1.2 paf 64: Chunk *chunk=&head;
65: do {
66: Chunk::Row *row=chunk->first;
67: for(int i=0; i<chunk->count; i++) {
1.1 paf 68: if(row==append_here)
69: goto break2;
70:
71: memcpy(copy_here, row->item.ptr, row->item.size);
72: copy_here+=row->item.size;
73: row++;
74: }
1.2 paf 75: chunk=row->link;
76: } while(chunk);
1.1 paf 77: break2:
78: *copy_here=0;
79: return result;
80: }
81:
E-mail: