Article 125 - StringBuffer Implementation

StringBuffer Structure is a String container with efficient concatentation operations; to append strings together with minimum effort.

Typically, a normal String structure is either immutable (Java) or fixed size (C char arrays), and operations to create arbitrary length Strings is desired. In Java, appending a String to an existing String produces a third String and memory content is copied over. This is inefficient and wasteful.

Interface

  • New StringBuffer
  • Append String
  • Get String

Implementation

In this C implementation, we will discuss the idea of using a linked list to piece together a String and only produce a char array when the entire String is requested.

The implementation of StringBuffer uses the String defintion. The reader is encouraged to review Article 118 - String Implementation for the String definition.

StringBuffer and Node structures.

typedef struct StringNode{
    struct StringNode * next;
    String * string;
};

typedef struct StringBuffer{
    StringNode * headStringNode;
    StringNode * tailStringNode;
} StringBuffer;

New StringBuffer

New StringBuffer Function.

StringBuffer * newStringBuffer(){
    StringBuffer * stringBuffer = 
        (StringBuffer *)malloc( sizeof( StringBuffer ) );

    if( stringBuffer ){
        stringBuffer->headStringNode = 0;
        stringBuffer->tailStringNode = 0;
    }

    return stringBuffer;
}

Append String

Add a new node to the StringBuffer's list of String nodes.

Append String to StringBuffer Function.

int append( StringBuffer * stringBuffer, char * string,
    unsigned int count ){

    StringNode * newStringNode;
    String * newString;
    
    if( !stringBuffer ){
        return -1;
    }

    if( !string ){
        return 0;
    }

    if( count == 0 ){
        return 0;
    }

    newStringNode = (StringNode *)malloc( sizeof( StringNode ) );

    if( !newStringNode ){
        return -2;
    }

    newString = newString();

    if( !newString ){
        free( newStringNode );
        return -2;
    }

    if( !append( newString, string, count ) ){
        free( newStringNode );
        deleteString( newString );
        return -3;
    }

    newStringNode->string = newString;
    newStringNode->next = 0;
    
    if( stringBuffer->tailStringNode ){
        stringBuffer->tailStringNode->next = newStringNode;        
    }
    else{
        stringBuffer->headStringNode = newStringNode;
    }

    stringBuffer->tailStringNode = newStringNode;

    return 0;
}

Get String Function.

char * getString( StringBuffer * stringBuffer ){
    char * stringCharArray;
    unsigned int stringLength = 0;
    String * stringNodePointer;

    if( !stringBuffer ){
        return 0;
    }

    //computer total size required to allocate char array
    for( stringNodePointer = stringBuffer->headStringNode; 
        stringNodePointer; 
        stringNodePointer = stringNodePointer->next ){

        stringLength += stringNodePointer->string->length;
    }

    stringCharArray = (char *)malloc( sizeof( char ) * stringLength );

    if( stringCharArray ){

        memset( stringCharArray, 0, stringLength );

        for( stringNodePointer = stringBuffer->headStringNode; 
            stringNodePointer; 
            stringNodePointer = stringNodePointer->next ){

            stringLength += stringNodePointer->string->length;

            memcpy( stringCharArray, 
                stringNodePointer->string->content, 
                0, 
                stringNodePointer->string->length );

            stringCharArray += stringNodePointer->string->length;
        }    
    }

    return stringCharArray;
}

Comments (1)

Posted by anonymous - Programmer at Friday, November 1, 2013 12:55 AM

Why is strong buffer better?
It is better because it doesn't need to copy the existing strong and create a new copy.
It only creates a copy once the string has been completely created.

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.