#include "CLucene/StdHeader.h" #include "StringBuffer.h" namespace lucene{ namespace util { void StringBuffer::growBuffer(int minLength){ bufferLength += LUCENE_STREAM_BUFFER_SIZE; if ( bufferLengthlen is LUCENE_STREAM_BUFFER_SIZE (meaning that this->buffer's ** size is sizeof(char_t * LUCENE_STREAM_BUFFER_SIZE)), the old stringNCopy ** call below read one char off the end of this->buffer. Instead, it needs ** to stringNCopy only this->len (not this->len+1) chars, then set ** tmp[len+1] = '\0'. ** Old impl: ** stringNCopy(tmp,buffer,len+1); */ stringNCopy(tmp,buffer,len); tmp[len+1] = '\0'; delete[] buffer; buffer = tmp; } StringBuffer::StringBuffer(const char_t* value): len(stringLength(value)) { bufferLength = len + 1; //minimum half buffer size if ( bufferLength < (LUCENE_STREAM_BUFFER_SIZE/2) ) bufferLength = LUCENE_STREAM_BUFFER_SIZE/2; buffer = new char_t[bufferLength]; stringNCopy(buffer,value,len+1); } StringBuffer::StringBuffer(int_t initSize): buffer (new char_t[initSize]), bufferLength (initSize+1), len(0) { } StringBuffer::StringBuffer(): //buffer (new char_t[LUCENE_STREAM_BUFFER_SIZE]), bufferLength (LUCENE_STREAM_BUFFER_SIZE), len(0) { buffer = new char_t[LUCENE_STREAM_BUFFER_SIZE]; } StringBuffer::~StringBuffer(){ delete[] buffer; } void StringBuffer::append(const uchar_t character){ #ifdef UTF8 if ( len + 7 > bufferLength ) growBuffer(len + 7); int buflen = g_unichar_to_utf8 (character, buffer+len); len+=buflen; #else if ( len + 1 > bufferLength ) growBuffer(len + 1); buffer[len] = character; len++; #endif } void StringBuffer::append( const char_t* value ){ int_t sl = stringLength(value); append( value, sl ); } void StringBuffer::append( const char_t* value, int_t chars ){ if ( len+chars+1 > bufferLength ) growBuffer ( len+chars+1 ); for ( int_t i=0;i bufferLength ) growBuffer ( len+sl+1 ); //move buffer up for ( int_t i=len+1;i>=0;i-- ) buffer[i+sl] = buffer[i]; len += sl; for ( int_t i=0;i