January 26, 2023, 09:17:19 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


CString Class

Started by Parker, December 17, 2006, 01:57:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Parker

December 17, 2006, 01:57:10 PM Last Edit: December 20, 2006, 01:08:17 PM by Parker
This is a simple string class that automatically resizes itself when it needs more memory. It can act as a replacement for Aurora's STRING and DSTRING types. Instead of =, use set(). Instead of +=, use append(). When you need the simple string, use get(). Instead of len(), use the length() method. The alloc() method is provided for when you need to call append() many times but don't have that much memory in the string. It allocates however many bytes you choose (that many more than the length of the string it stores) and speeds up multiple calls to append(). By default, it allocates 64 more bytes.

It shouldn't be very hard to add any other functions you need (i.e. mid, trim) by extending the class.

To Paul: how about a renew operator that uses GlobalReAlloc? renew( mem, byte, 20 );

class CString
{
private:
string *str;
unsigned int allocspace;
unsigned int s_length;

public:
declare CString( );
declare _CString( );

declare set( string *s );

declare append( string *s );

declare get( ),string;

declare length( ),unsigned int;

declare alloc( opt unsigned int bytes = 64 );
}

CString::CString( )
{
str = new( byte, 1 );
allocspace = 1;
s_length = 0;
}

CString::_CString( )
{
delete str;
}

CString::set( string *s )
{
if( s == null ) return;
unsigned int strlen = len( *s );
if( allocspace > strlen )
{
*str = *s;
}
else
{
delete str;
str = new( byte, strlen + 1 );
allocspace = strlen + 1;
*str = *s;
}

s_length = strlen;
}

CString::append( string *s )
{
if( s == null ) return;
unsigned int strlen = len( *s );
if( allocspace > s_length + strlen )
{
*str += *s;
}
else
{
string *temp = new( byte, s_length + 1 );
*temp = *str;
delete str;
str = new( byte, s_length + strlen + 1 );
*str = *temp + *s;
allocspace = s_length + strlen + 1;
delete temp;
}

s_length += strlen;
}

CString::get( )
{
return *str;
}

CString::length( )
{
return s_length;
}

CString::alloc( unsigned int bytes )
{
string *temp = new( byte, s_length + 1 );
*temp = *str;
delete str;
str = new( byte, s_length + bytes + 1 );
*str = *temp;
allocspace = s_length + bytes + 1;
}

sub main( )
{
CString str;

str.set( "Hello world" );
str.append( "!" );
print( str.get( ) );
str.set( "Aurora" );
str.append( " CString" );
print( str.get( ) );
str.set( "A" );
str.append( "u" );
str.append( "r" );
str.append( "o" );
str.append( "r" );
str.append( "a" );
print( str.get( ) );
str.set( str.get( ) );
print( str.get( ) );
do{}until getkey( ) != "";
}

Zen

Yeah I've got something similar to this for CCL, but it has all the standard string functions in there too, aswell as a merge method to combine two CString classes together. I didn't add it yet though because I wanted to wait for operator overloading, so you assign a string directly ;)

Lewis

Mike Stefanik

Hopefully when operator overloads are added to the language, some standard types will be made part of the base language so that 'string' is actually an object, as well as 'variant' (being a VARIANTARG type). As it is now, the native string type is a bit retro. :)
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation