September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Utility Libraries for BREW - A String Class

  • February 27, 2003
  • By Radu Braniste
  • Send Email »
  • More Articles »

...and the BrewString...

The library is hosted in BrewString.h. It assumes that templates are available (true with ARM compiler) and it doesn't use exceptions (as they are not supported by ARM). The interface replicates the Java String class, as this emphasizes the fact that this is not an STL compatible implementation, having no support for iterators, and also has the additional merit of being a well-proved and popular API. A typedef takes the similarity one step further:

typedef BrewString<char> String;

On the other hand we'll see immediately that the similarity is merely syntactic — BrewString is not an immutable class. Immutable classes have distinct advantages (see Bloch — Effective Java) but are notorious bad-performers.

A PlatformSpecific header takes care of the differences between platforms as well as the global overload of operators new, new[], delete and delete[], as required by BREW. The use of new[] and delete[] is not a specific requirement for this library but is a good practice, insuring code portability. The same time this overload might provide a good starting point for a custom allocator.

To achieve the desired flexibility BrewString uses templates, with minimal code bloat. An example:

template <class U, class V>   const BrewString<T>&   replace( const U& match, const V& replacement );

This deals with all the possible char/string combinations in parameters and is instantiated as needed. You pay only for what you use.

As one can easily see BrewString does almost everything in place, avoiding temporaries. There is even an in-place substring() version:

const BrewString<T>& partialString( UINT right ) ;      const BrewString<T>& partialString( UINT left, UINT right ) ;

If we have to write code like:

String s = "something";s = s.substr(0,4);

then

s.partialString(4);

is at least 2 times faster than the STL implementation.

There are only 2 functions returning a BrewString by value: substring and operator+, and even this two use the so-called "return value optimization". For example:

const BrewString<T>   substring( UINT left, UINT right ) const   {      if ( ( left >= right ) || ( right > length_ ) )      {         return BrewString();         }        return BrewString ( buffer_ + left, right - left );   }String s = str.substr(0, 2);

will be optimized in the sense that there will be no temporary created to pass the value from the function. (Actually the rules for this optimization have been relaxed somehow but there might be still problems when using templates[7]).

There is a notable improvement in implementing operator+, making it 33% faster than the fastest STL implementation of this operator. There is a non-standard constructor:

template < class U, class V>   BrewString (const U& lhs, UINT lnl, const V& rhs, UINT lnr);

This can be used like:

String sc ="outdoor";String h("indoor", 2, sc, 3);String h will contain "inout".

Using the following operator+:

template <  class U, class V>   friend BrewString<T> operator+ ( const U& lhs,                                      const V& rhs )   {      String t;      UINT lnl = t.getLen(lhs);      UINT lnr = t.getLen(rhs);      return String(lhs, lnl,  rhs, lnr);   }

results into the return value optimization (String t is void, so that it adds nothing to the final tab)

BrewString — public interface:

template <class U>      BrewString( const U& rhs , UINT sz = 0);   BrewString( const BrewString<T> &rhs );   BrewString( ) ;   const BrewString<T> &   operator=( const BrewString &rhs );      const BrewString<T> &   operator=( const T* rhs );   template <class U>      const BrewString<T> &   operator+=( const U& rhs );      template <class U>   bool equals( const U& rhs ) ;      template <class U>   bool startsWith( const U &rhs, UINT offset = 0 ) ;   template <class U>   bool endsWith( const U& rhs ) ;   template <class U>   int indexOf( const U &rhs, UINT fromIndex = 0 ) ;      template <class U>      int   lastIndexOf( const U &rhs ) ;   template <class U>   int lastIndexOf( const U &rhs, UINT fromIndex ) ;      template <class U, class V>   const BrewString<T>&   replace( const U& match, const V& replacement );      const BrewString<T>&   replace( T findChar, T replaceChar );      const BrewString<T>   substring( UINT left ) ;      const BrewString<T>   substring( UINT left, UINT right ) ;      const BrewString<T>& partialString( UINT right ) ;      const BrewString<T>& partialString( UINT left, UINT right ) ;      const BrewString<T>& trim() ;      const BrewString<T>& toLowerCase( ) ;         const BrewString<T>& toUpperCase( ) ;      const T* toCharArray() ;   const UINT length( ) ;   const UINT capacity( ) ;   const T charAt( int index ) ;      void ensureCapacity(UINT sz);      void setLength(UINT sz);   




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel