Annotation of parser3/src/main/pa_string.C, revision 1.5
1.4 paf 1: /*
1.5 ! paf 2: $Id: pa_string.C,v 1.4 2001/01/26 15:38:34 paf Exp $
1.4 paf 3: */
4:
1.1 paf 5: #include <string.h>
6:
7: #include "pa_pool.h"
1.5 ! paf 8: #include "pa_hash.h"
1.1 paf 9:
10: void *String::operator new(size_t size, Pool *apool) {
1.5 ! paf 11: return apool->malloc(size);
1.1 paf 12: }
13:
1.2 paf 14: void String::construct(Pool *apool) {
1.1 paf 15: pool=apool;
1.2 paf 16: head.count=curr_chunk_rows=CR_PREALLOCATED_COUNT;
1.5 ! paf 17: append_here=head.rows;
1.2 paf 18: head.preallocated_link=0;
1.5 ! paf 19: link_row=&head.rows[curr_chunk_rows];
1.1 paf 20: }
21:
22: void String::expand() {
1.2 paf 23: curr_chunk_rows=curr_chunk_rows*100/CR_GROW_PERCENT;
24: Chunk *chunk=static_cast<Chunk *>(
25: pool->calloc(sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
26: chunk->count=curr_chunk_rows;
27: link_row->link=chunk;
1.5 ! paf 28: append_here=chunk->rows;
! 29: link_row=&chunk->rows[curr_chunk_rows];
1.1 paf 30: }
31:
1.5 ! paf 32: String::String(String& src) {/*
! 33: int src_used_rows=src.used_rows(); {
! 34: curr_chunk_rows=src_total_rows
! 35: Chunk *chunk=static_cast<Chunk *>(
! 36: pool->calloc(sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
! 37: chunk->count=curr_chunk_rows;
! 38: link_row->link=chunk;
! 39: append_here=chunk->rows;
! 40: link_row=&chunk->rows[curr_chunk_rows];
! 41: }
! 42:
! 43: Chunk *chunk=&head;
! 44: do {
! 45: result+=chunk->count;
! 46: chunk=row->link;
! 47: } while(chunk);
! 48: break2:
! 49: return result;*/
! 50:
! 51: }
! 52:
! 53:
1.1 paf 54: String& String::operator += (char *src) {
55: if(chunk_is_full())
56: expand();
57:
58: append_here->item.ptr=src;
59: append_here->item.size=strlen(src);
60: append_here++;
61:
62: return *this;
63: }
64:
65: size_t String::size() {
66: int result=0;
1.5 ! paf 67:
1.2 paf 68: Chunk *chunk=&head;
69: do {
1.5 ! paf 70: Chunk::Row *row=chunk->rows;
1.2 paf 71: for(int i=0; i<chunk->count; i++) {
1.1 paf 72: if(row==append_here)
73: goto break2;
74:
75: result+=row->item.size;
76: row++;
77: }
1.2 paf 78: chunk=row->link;
79: } while(chunk);
1.1 paf 80: break2:
81: return result;
82: }
83:
84: char *String::c_str() {
1.5 ! paf 85: char *result=static_cast<char *>(pool->malloc(size()+1));
1.1 paf 86:
87: char *copy_here=result;
1.2 paf 88: Chunk *chunk=&head;
89: do {
1.5 ! paf 90: Chunk::Row *row=chunk->rows;
1.2 paf 91: for(int i=0; i<chunk->count; i++) {
1.1 paf 92: if(row==append_here)
93: goto break2;
94:
95: memcpy(copy_here, row->item.ptr, row->item.size);
96: copy_here+=row->item.size;
97: row++;
98: }
1.2 paf 99: chunk=row->link;
100: } while(chunk);
1.1 paf 101: break2:
102: *copy_here=0;
103: return result;
104: }
105:
1.5 ! paf 106: int String::used_rows() {
! 107: int result=0;
! 108:
! 109: Chunk *chunk=&head;
! 110: do {
! 111: int count=chunk->count;
! 112: result+=count;
! 113: chunk=chunk->rows[count].link;
! 114: } while(chunk);
! 115:
! 116: result-=link_row-append_here;
! 117:
! 118: return result;
! 119: }
! 120:
! 121: unsigned int String::hash_code() {
! 122: unsigned int result=0;
! 123:
! 124: Chunk *chunk=&head;
! 125: do {
! 126: Chunk::Row *row=chunk->rows;
! 127: for(int i=0; i<chunk->count; i++) {
! 128: if(row==append_here)
! 129: goto break2;
! 130:
! 131: result=Hash<String, String>::generic_code(result, row->item.ptr, row->item.size);
! 132: row++;
! 133: }
! 134: chunk=row->link;
! 135: } while(chunk);
! 136: break2:
! 137: return result;
! 138: }
! 139:
! 140: bool String::operator == (String& src) {
! 141: return false;
! 142: }
E-mail: