AString C++ Ansi String Library: AString Main Page

AString C++ Ansi String Library 0.4
  • Main Page
  • Related Pages
  • Data Structures
AString Main Page
Author
Laurent NAVARRO ln@al.nosp@m.tide.nosp@m.v.com
Version
0.4 2014-09-03
Copyright
MIT X11 : See .h file header Copyright (C) 2013 Laurent NAVARRO Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL LAURENT NAVARRO BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the Laurent NAVARRO shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from the Laurent NAVARRO.

Description

AString is a library providing a C++ Ansi (8 bits char set) String class

I've create it because, I work with several compiler and it's often difficult to decide which string class I should use, because some compiler have their own framework string class (Visual Studio CString, QT QString,C++ Builder AnsiString) and all of them support std::string and C char*. I could always use std:string but framework's strings are generally a bit more richer in features. I'm also doing multi-threading but on pre-c++ 11 there's no guarantee std::string works fine, you have to investigate, sometimes it's ok, sometimes it's not OK.

AString provide the following features :

  • an interface similar (not identical) to std::string
  • Can be implemented with std:string or not (#define based). If not, it's a simple buffer based implementation (easy to debug)
  • include interoperability with frameworks string class (C++ Builder,CString )
  • include constructor from numbers (int, float, double, ... ) and also String To Number methods (ToInt, ... )
  • include sprintf/format like feature
  • include a secured strncpy char* copier
  • include implode and explode (split/merge of string array)
  • Buffer based implementation can be directly used on printf with s on some compiler
  • Can easily be modified

Documentation

Documentation is generated using Doxygen, available online here http://www.altidev.com/ln/AString/ or at chm format here AString.chm and on the repository.

Configuration details

Some configuration can be done by settings some define on the — Write your settings Here — section of the Header file
Here a summary See source for details :

  • ASTRING_IMPLEMENT_WITH_STD_STRING make AString inherits from std::string (probably better if you fully trust it) instead of the buffer based implementation
  • ASTRING_INTEROP_STD_STRING for buffer base implementation, allow transparent interoperability with std::string (constructor & cast operator)
  • ASTRING_NOSTL remove all feature using STL like vector et stream.
  • ASTRING_INTEROP_BCB Enable interoperability with C++ Builder AnsiString and UnicodeString (constructor & cast operator)
  • ASTRING_INTEROP_VSS Enable interoperability with Visual Studio CString (constructor & cast operator)

Implementation details

if ASTRING_IMPLEMENT_WITH_STD_STRING is set to 0 the string is implemented by a pointer on a buffer.
However a specific tips was used to allow string to be used on s printf like function on some compiler inspired from Chris OldWood blog A Not-So-Clever-Now String Implementation
The tips consists to have a pointer pointing on the full buffer itself but on the char* section of the buffer and to compute with offset to access to stored size and capacity attributes as described here.

+--------------+
+ Size + <-- StrBuffer - 2*sizeof(unsigned) or GetSize() or GetRawBuffer()
+--------------+
+ Capacity + <-- StrBuffer - 1*sizeof(unsigned) or GetCapacity()
+--------------+
+ char* Buffer + <-- StrBuffer Attribute, conatains zero terminated characters string.
+--------------+
Todo:
  • Add missing std::string reimplementation (rfind)
  • Add some features ToUpper, ToLower
  • Add some useful string simple algorithms
    • ToIntFromHex "0x20" or "20" to 32 , ToIntFromBin "0101" to 5
    • assignAsHex 32 to "0x20" or "20" optional 0x
    • assignAsBin 5 to "0101" parameter to set width and space on result (0101 0011 if param=4)
  • Finalize/improve documentation and tests, test on Clang
  • Add interaction with some others frameworks (QT, WxWidget, ... )
  • Improve implementation of CString interoperability (make constructor param const by avoiding get buffer

Test details

I'm using the TDD logic for this library, so it include mainly project to run CPP Unit Project include test project on

  • CodeBlock with Mingw GCC in Windows 32 bits
  • CodeBlock with GCC on Linux Fedora 17 32bits
  • CodeBlock with GCC on Linux Debian 7 64bits
  • Visual Sutdio 2010 32 & 64 bits
  • C++ Builder XE2 32 bits

I've done memory check using visual leak detector on Visual Studio http://vld.codeplex.com/
And DrMemory on Windows
And valgring on Linux, here command to test :

  • valgrind –tool=memcheck bin/Debug/AStringUT
  • valgrind –leak-check=full –tool=memcheck bin/Debug/AStringUT 've done coverage test on GCC using GCov 've used CppCheck also.

Release History

  • version 0.1 2013-08-22 Initial
  • version 0.2 2013-12-29
    • Adding Replace of string by a string
    • Adding trim, ltrim, rtrim
  • version 0.3 2014-05-02
    • Bugfix on comparison operator
    • Added Char* on left of comparison operator
  • version 0.4 2014-09-03
    • Added test for 100% coverage on Gcc
    • Minor Bugfix in case of reserve(0)
Generated on Wed Sep 3 2014 15:37:11 for AString C++ Ansi String Library by doxygen 1.8.4


AString C++ Ansi String Library: Todo List

  • Data Structures
Todo List
page AString Main Page
  • Add missing std::string reimplementation (rfind)
  • Add some features ToUpper, ToLower
  • Add some useful string simple algorithms
    • ToIntFromHex "0x20" or "20" to 32 , ToIntFromBin "0101" to 5
    • assignAsHex 32 to "0x20" or "20" optional 0x
    • assignAsBin 5 to "0101" parameter to set width and space on result (0101 0011 if param=4)
  • Finalize/improve documentation and tests, test on Clang
  • Add interaction with some others frameworks (QT, WxWidget, ... )
  • Improve implementation of CString interoperability (make constructor param const by avoiding get buffer


AString C++ Ansi String Library: Data Structures

  • Class Hierarchy
  • Data Fields
Data Structures
Here are the data structures with brief descriptions:
o C AString |
\ C AStringException | AString Exception class, inherit from std::exception


AString C++ Ansi String Library: AString Class Reference

  • Data Fields
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends
AString Class Reference

#include <AString.h>

Public Types

typedef char * iterator
STL like iterator. More...

Public Member Functions

AString ()
Default constructor. More...
AString (const char *str, int count=-1)
Char *constructor. More...
AString (const AString &Src)
Copy constructor. More...
AString (const std::string &Src)
std::string constructor. More...
operator std::string () const
std::string cast operator. More...
AString & | operator= (const AString &Src)
Assignement operator from AString. More...
AString (AString &&Src) ASTRING_NOEXCEPT
C++ 11 Move construtor. More...
AString & | operator= (AString &&Src) ASTRING_NOEXCEPT
C++ 11 Assignement move operator . More...
AString (int value)
int constructor. More...
AString (unsigned value)
unsigned constructor. More...
AString (double value)
double constructor. More...
~AString () ASTRING_NOEXCEPT
destructor. More...
iterator begin ()
STL like container begin. More...
iterator end ()
STL like container end. More...
char * | c_str ()
get pointer on the string. More...
const char * | c_str () const
get a const pointer on the string. More...
size_t | size () const
get the number of character contained in the string. More...
size_t | capacity () const
get the maximum number of character that can be contained in the string whithout reallocatig memory. More...
void | reserve (size_t count)
Force the string to allocate supplementary space. More...
void | clear ()
Clear the string. More...
char & | operator[] (size_t idx)
char | operator[] (size_t idx) const
char & | at (size_t idx)
char | at (size_t idx) const
AString & | assign (const char *str, int count=-1)
AString | operator+ (const AString &RightStr) const
AString | operator+ (const char *RightStr) const
AString & | operator+= (const AString &RightStr)
Append a string to the current string. More...
bool | operator== (const AString &RightStr) const
bool | operator== (const char *RightStr) const
bool | operator!= (const AString &RightStr) const
bool | operator!= (const char *RightStr) const
bool | operator< (const AString &RightStr) const
bool | operator< (const char *RightStr) const
bool | operator> (const AString &RightStr) const
bool | operator> (const char *RightStr) const
AString | substr (size_t pos=0, size_t count=npos) const
size_t | find (const char *Searched, size_t StartPos=0, size_t count=0) const
size_t | find (char Searched, size_t StartPos=0) const
AString & | replace (size_t pos, size_t count, const AString &str)
Replace a set or character by a string on the current string. More...
AString & | replace (const AString &src, const AString &str)
Replace a substring by a new string on the current string. More...
AString & | rtrim (const char *CharsToRemove=" \r\n\t")
AString & | ltrim (const char *CharsToRemove=" \r\n\t")
AString & | trim (const char *CharsToRemove=" \r\n\t")
void | strncpy (char *dest, size_t destsize, unsigned pos=0) const
Securely copy the current string on a C buffer. More...
AString & | Format (const char *Fmt,...)
generate a string on a similar manner than sprintf. More...
void | RecomputeSize ()
Recompute Size internal attribute based on Content. More...
int | ToInt ()
Convert the content of the string containg a textual representation of a number to an int. More...
int | ToIntDef (int defval)
Convert the content of the string containg a textual representation of a number to an int an return defval. More...
template<class T >
AString & | operator<< (const T &Streamable)
operate similary with ostringstream. More...
template<class ResultContainerType >
unsigned | explode (AString Separator, ResultContainerType &Result)
explode a string in a collection of string (reverse of implode). More...
template<class T >
AString | implode (AString Separator, const T &Ctnr)

Static Public Member Functions

template<class T >
static AString implode (AString Separator, const T &Ctnr)
implode strings to a string whit a separator. More...

Static Public Attributes

static const size_t npos =(size_t)-1

Protected Member Functions

char * | GetRawBuffer ()
the real address of the buffer More...
unsigned & | GetSize ()
R/W access to the offset position of size on the buffer. More...
unsigned & | GetCapacity ()
R/W access to the offset position of capacity on the buffer. More...
unsigned | GetSize () const
Read Only access to the offset position of size on the buffer. More...
unsigned | GetCapacity () const
Read Only access to the offset position of capacity on the buffer. More...
void | AllocBuffer (unsigned count)
Allocate an internal buffer of a capacity of count character. More...

Protected Attributes

char * StrBuffer
offset buffer More...

Static Protected Attributes

static const char EmptyBuffer [4] ={0,0,0,0}
Static const shared buffer for empty string. More...

Friends

AString | operator+ (const char *LeftStr, const AString &RightStr)
bool | operator== (const char *LeftStr, const AString &RightStr)
bool | operator!= (const char *LeftStr, const AString &RightStr)
bool | operator< (const char *LeftStr, const AString &RightStr)
bool | operator> (const char *LeftStr, const AString &RightStr)
AString | Asprintf (const char *Fmt,...)

Detailed Description

Definition at line 195 of file AString.h.

Member Typedef Documentation

typedef char* iterator

STL like iterator.

Definition at line 289 of file AString.h.

Constructor & Destructor Documentation

AString ( )

Default constructor.

Construct an empty string

Definition at line 25 of file AString.cpp.

AString | ( | const char * | str,
 |  | int | count = -1
 | ) |  |

Char *constructor.

Construct an string by copying a C String.

Parameters
str | source string
count | Number of used caracters, if <0 means the full stream.

Definition at line 43 of file AString.cpp.

AString ( const AString & Src)

Copy constructor.

Parameters
Src source string

Definition at line 129 of file AString.cpp.

AString ( const std::string & Src)

std::string constructor.

Parameters
Src source string

Definition at line 244 of file AString.cpp.

AString ( AString && Src)

C++ 11 Move construtor.

Parameters
Src source string

Definition at line 900 of file AString.cpp.

AString ( int value)

int constructor.

Construct a text representation of Value

Parameters
value value to convert as text

Definition at line 65 of file AString.cpp.

AString ( unsigned value)

unsigned constructor.

Construct a text representation of Value

Parameters
value value to convert as text

Definition at line 80 of file AString.cpp.

AString ( double value)

double constructor.

Construct a text representation of Value

Parameters
value value to convert as text

Definition at line 104 of file AString.cpp.

~AString ( )

destructor.

Definition at line 34 of file AString.cpp.

Member Function Documentation

void AllocBuffer ( unsigned count)
protected

Allocate an internal buffer of a capacity of count character.

  • final zero + Control area or point to EmptyBuffer
    Parameters
    count requested capacity
    Returns
    void

Definition at line 383 of file AString.cpp.

AString & assign | ( | const char * | str,
 |  | int | count = -1
 | ) |  |

Definition at line 296 of file AString.cpp.

char & at ( size_t idx)

Definition at line 414 of file AString.cpp.

char at ( size_t idx) const

Definition at line 421 of file AString.cpp.

AString::iterator begin ( )

STL like container begin.

return an iterator on the begining of the string

Definition at line 701 of file AString.cpp.

char * c_str ( )

get pointer on the string.

return a read/write pointer on the string
if you add or remove a '\0' char throught provided pointer you have to call RecomputeSize().

Returns
pointer on string

Definition at line 317 of file AString.cpp.

const char * c_str ( ) const

get a const pointer on the string.

return a read only pointer on the string

Returns
const pointer on string

Definition at line 322 of file AString.cpp.

size_t capacity ( ) const

get the maximum number of character that can be contained in the string whithout reallocatig memory.

Returns
string capacity

Definition at line 335 of file AString.cpp.

void clear ( )

Clear the string.

After this method call string is empty and Size=Capacity=0

Definition at line 351 of file AString.cpp.

AString::iterator end ( )

STL like container end.

return an iterator on the character following the end of the string

Definition at line 706 of file AString.cpp.

unsigned explode | ( | AString | Separator,
 |  | ResultContainerType & | Result
 | ) |  |

explode a string in a collection of string (reverse of implode).

Split a string in a collection of substring each time Separator is met. Result must be a STL like container, deque is suggested.

Parameters
Separator | the searched separator
Result | the target collection a deque<AString>
Returns
the number if string returned = size of the result container.
AString S3("Hello dear friends");
std::deque<AString> Ds;
unsigned res=S3.explode(" ",Ds);
// Result : res=3, Ds contains 3 values : "Hello" ,"dear", "friends"

Definition at line 563 of file AString.h.

size_t find | ( | const char * | Searched,
 |  | size_t | StartPos = 0,
 |  | size_t | count = 0
 | ) |  | const

Definition at line 671 of file AString.cpp.

size_t find | ( | char | Searched,
 |  | size_t | StartPos = 0
 | ) |  | const

Definition at line 696 of file AString.cpp.

AString & Format | ( | const char * | Fmt,
 |  |  | ...
 | ) |  |

generate a string on a similar manner than sprintf.

And store the result on the current string

S3.Format("Y %d %.02f %s",10,2.52,"DEF");
// S3 ="Y 10 2.52 DEF"
Parameters
Fmt format string silmilar to sprintf
Returns
Formated string

Definition at line 966 of file AString.cpp.

unsigned & GetCapacity ( )
protected

R/W access to the offset position of capacity on the buffer.

Definition at line 434 of file AString.cpp.

unsigned GetCapacity ( ) const
protected

Read Only access to the offset position of capacity on the buffer.

Definition at line 444 of file AString.cpp.

char * GetRawBuffer ( )
protected

the real address of the buffer

Definition at line 343 of file AString.cpp.

unsigned & GetSize ( )
protected

R/W access to the offset position of size on the buffer.

Definition at line 429 of file AString.cpp.

unsigned GetSize ( ) const
protected

Read Only access to the offset position of size on the buffer.

Definition at line 439 of file AString.cpp.

static AString implode | ( | AString | Separator,
 |  | const T & | Ctnr
 | ) |  |
static

implode strings to a string whit a separator.

Merge/join/agregate a collection of string contained on a STL like container to an unique string whith separator added between each string.

Parameters
Separator | the separator added between each string.
Ctnr | an STL like container of AString : vector<AString>
AString implode | ( | AString | Separator,
 |  | const T & | Ctnr
 | ) |  |

Definition at line 543 of file AString.h.

AString & ltrim ( const char * CharsToRemove = " \r\n\t")

Definition at line 804 of file AString.cpp.

operator std::string ( ) const

std::string cast operator.

To easyly convert an AString to a std::string

Definition at line 261 of file AString.cpp.

bool operator!= ( const AString & RightStr) const

Definition at line 648 of file AString.cpp.

bool operator!= ( const char * RightStr) const

Definition at line 653 of file AString.cpp.

AString operator+ ( const AString & RightStr) const

Definition at line 449 of file AString.cpp.

AString operator+ ( const char * RightStr) const

Definition at line 464 of file AString.cpp.

AString & operator+= ( const AString & RightStr)

Append a string to the current string.

If the current string has available space, it will be done by extending the current string. It's more efficient than S1=S1+S2;
To be really efficient, its better to anticipage space requirement by using reserve()

S1.reserve(20);
S1+="My String";
Parameters
RightStr the appended string
Returns
the current string

Definition at line 494 of file AString.cpp.

bool operator< ( const AString & RightStr) const

Definition at line 545 of file AString.cpp.

bool operator< ( const char * RightStr) const

Definition at line 579 of file AString.cpp.

AString & operator<< ( const T & Streamable)

operate similary with ostringstream.

Append to the Left string with the textual representation ofthe right parameter and return the current string.

AString S2("A");
S2<<15<<" XXX "<<2.5<<" XXX "<<S1;
Result : S2 = "A 15 XXX 2.5 XXX 0123456789ABCDEF"), S2 );
Parameters
Streamable A variable that can be outputed on a stream as text

Definition at line 535 of file AString.h.

AString & operator= ( const AString & Src)

Assignement operator from AString.

Parameters
Src source string

Definition at line 273 of file AString.cpp.

AString & operator= ( AString && Src)

C++ 11 Assignement move operator .

Parameters
Src source string

Definition at line 910 of file AString.cpp.

bool operator== ( const AString & RightStr) const

Definition at line 507 of file AString.cpp.

bool operator== ( const char * RightStr) const

Definition at line 524 of file AString.cpp.

bool operator> ( const AString & RightStr) const

Definition at line 563 of file AString.cpp.

bool operator> ( const char * RightStr) const

Definition at line 597 of file AString.cpp.

char & operator[] ( size_t idx)

Definition at line 400 of file AString.cpp.

char operator[] ( size_t idx) const

Definition at line 407 of file AString.cpp.

void RecomputeSize ( )

Recompute Size internal attribute based on Content.

Required when using [] operator or direct buffer access you change the size by moving the terminal zero character.

Returns
void

Definition at line 862 of file AString.cpp.

AString & replace | ( | size_t | pos,
 |  | size_t | count,
 |  | const AString & | str
 | ) |  |

Replace a set or character by a string on the current string.

Parameters
pos | position where the substitition start
count | number of character to substitute
str | string to insert on the specified location
Returns
the current string

Definition at line 721 of file AString.cpp.

AString & replace | ( | const AString & | src,
 |  | const AString & | str
 | ) |  |

Replace a substring by a new string on the current string.

Parameters
src | the substring to replace
str | the string used to replace
Returns
the current string

Definition at line 758 of file AString.cpp.

void reserve ( size_t count)

Force the string to allocate supplementary space.

Parameters
count new string capacity

Definition at line 361 of file AString.cpp.

AString & rtrim ( const char * CharsToRemove = " \r\n\t")

Definition at line 771 of file AString.cpp.

size_t size ( ) const

get the number of character contained in the string.

Returns
character count

Definition at line 327 of file AString.cpp.

void strncpy | ( | char * | dest,
 |  | size_t | destsize,
 |  | unsigned | pos = 0
 | ) |  | const

Securely copy the current string on a C buffer.

If the string is longer than what can be hold on the destination buffer, string will be truncated.
There's always an ending zero added on the destination buffer

char buf[16];
AString S1("0123456789ABCDEF");
S1.strncpy(buf,4,5);
// buf == "567" +ending zero, then 4 characters
S1.strncpy(buf,5);
// buf=="0123"
S1.strncpy(buf,sizeof(buf),10);
// buf=="ABCDEF"
Parameters
dest | destination C Buffer
destsize | size of the buffer
pos | position where the copy will occur

Definition at line 848 of file AString.cpp.

AString substr | ( | size_t | pos = 0,
 |  | size_t | count = npos
 | ) |  | const

Definition at line 662 of file AString.cpp.

int ToInt ( )

Convert the content of the string containg a textual representation of a number to an int.

Returns
int
Exceptions
if the text doesn't represent an integer value.

Definition at line 874 of file AString.cpp.

int ToIntDef ( int defval)

Convert the content of the string containg a textual representation of a number to an int an return defval.

Parameters
defval default value if text can't be converted
Returns
int
Exceptions
none

Definition at line 886 of file AString.cpp.

AString & trim ( const char * CharsToRemove = " \r\n\t")

Definition at line 841 of file AString.cpp.

Friends And Related Function Documentation

AString Asprintf | ( | const char * | Fmt,
 |  |  | ...
 | ) |  |
friend

Definition at line 926 of file AString.cpp.

bool operator!= | ( | const char * | LeftStr,
 |  | const AString & | RightStr
 | ) |  |
friend

Definition at line 657 of file AString.cpp.

AString operator+ | ( | const char * | LeftStr,
 |  | const AString & | RightStr
 | ) |  |
friend

Definition at line 479 of file AString.cpp.

bool operator< | ( | const char * | LeftStr,
 |  | const AString & | RightStr
 | ) |  |
friend

Definition at line 613 of file AString.cpp.

bool operator== | ( | const char * | LeftStr,
 |  | const AString & | RightStr
 | ) |  |
friend

Definition at line 540 of file AString.cpp.

bool operator> | ( | const char * | LeftStr,
 |  | const AString & | RightStr
 | ) |  |
friend

Definition at line 631 of file AString.cpp.

Field Documentation

const char EmptyBuffer ={0,0,0,0}
staticprotected

Static const shared buffer for empty string.

Definition at line 494 of file AString.h.

const size_t npos =(size_t)-1
static

Definition at line 485 of file AString.h.

char* StrBuffer
protected

offset buffer

Definition at line 488 of file AString.h.


AString C++ Ansi String Library: AStringException Class Reference

Public Member Functions | Data Fields
AStringException Class Reference

AString Exception class, inherit from std::exception. More...

#include <AString.h>

Inherits exception.

Public Member Functions

AStringException (AString _Msg)
Contruct the exception with a AString message. More...
virtual const char * what () const ASTRING_NOEXCEPT
Return a char* on the message like std::exception does. More...

Data Fields

AString Msg

Detailed Description

AString Exception class, inherit from std::exception.

Can be inherited for your own exception with a AString contructor.

Definition at line 511 of file AString.h.

Constructor & Destructor Documentation

AStringException ( AString _Msg)
inline

Contruct the exception with a AString message.

Parameters
_Msg Message of the exception

Definition at line 521 of file AString.h.

Member Function Documentation

virtual const char* what ( ) const
inlinevirtual

Return a char* on the message like std::exception does.

Returns
the message

Definition at line 525 of file AString.h.

Field Documentation

AString Msg

Definition at line 527 of file AString.h.


AString C++ Ansi String Library: Class Hierarchy

Class Hierarchy
This inheritance list is sorted roughly, but not completely, alphabetically:
[detail level 12]
o C AString |
\ C exception |
\ C AStringException | AString Exception class, inherit from std::exception


AString C++ Ansi String Library: Data Fields

  • All
  • Functions
  • Variables
  • Typedefs
  • Related Functions
  • a
  • b
  • c
  • e
  • f
  • g
  • i
  • l
  • m
  • n
  • o
  • r
  • s
  • t
  • w
  • ~
Here is a list of all struct and union fields with links to the structures/unions they belong to:

- a -

  • AllocBuffer() : AString
  • Asprintf : AString
  • assign() : AString
  • AString() : AString
  • AStringException() : AStringException
  • at() : AString

- b -

  • begin() : AString

- c -

  • c_str() : AString
  • capacity() : AString
  • clear() : AString

- e -

  • EmptyBuffer : AString
  • end() : AString
  • explode() : AString

- f -

  • find() : AString
  • Format() : AString

- g -

  • GetCapacity() : AString
  • GetRawBuffer() : AString
  • GetSize() : AString

- i -

  • implode() : AString
  • iterator : AString

- l -

  • ltrim() : AString

- m -

  • Msg : AStringException

- n -

  • npos : AString

- o -

  • operator std::string() : AString
  • operator!=() : AString
  • operator+() : AString
  • operator+=() : AString
  • operator<() : AString
  • operator<<() : AString
  • operator=() : AString
  • operator==() : AString
  • operator>() : AString
  • operator[]() : AString

- r -

  • RecomputeSize() : AString
  • replace() : AString
  • reserve() : AString
  • rtrim() : AString

- s -

  • size() : AString
  • StrBuffer : AString
  • strncpy() : AString
  • substr() : AString

- t -

  • ToInt() : AString
  • ToIntDef() : AString
  • trim() : AString

- w -

  • what() : AStringException

- ~ -

  • ~AString() : AString


AString C++ Ansi String Library: Data Fields - Functions

  • All
  • Functions
  • Variables
  • Typedefs
  • Related Functions
  • a
  • b
  • c
  • e
  • f
  • g
  • i
  • l
  • o
  • r
  • s
  • t
  • w
  • ~

- a -

  • AllocBuffer() : AString
  • assign() : AString
  • AString() : AString
  • AStringException() : AStringException
  • at() : AString

- b -

  • begin() : AString

- c -

  • c_str() : AString
  • capacity() : AString
  • clear() : AString

- e -

  • end() : AString
  • explode() : AString

- f -

  • find() : AString
  • Format() : AString

- g -

  • GetCapacity() : AString
  • GetRawBuffer() : AString
  • GetSize() : AString

- i -

  • implode() : AString

- l -

  • ltrim() : AString

- o -

  • operator std::string() : AString
  • operator!=() : AString
  • operator+() : AString
  • operator+=() : AString
  • operator<() : AString
  • operator<<() : AString
  • operator=() : AString
  • operator==() : AString
  • operator>() : AString
  • operator[]() : AString

- r -

  • RecomputeSize() : AString
  • replace() : AString
  • reserve() : AString
  • rtrim() : AString

- s -

  • size() : AString
  • strncpy() : AString
  • substr() : AString

- t -

  • ToInt() : AString
  • ToIntDef() : AString
  • trim() : AString

- w -

  • what() : AStringException

- ~ -

  • ~AString() : AString


AString C++ Ansi String Library: Data Fields - Variables

  • All
  • Functions
  • Variables
  • Typedefs
  • Related Functions
  • EmptyBuffer : AString
  • Msg : AStringException
  • npos : AString
  • StrBuffer : AString


AString C++ Ansi String Library: Data Fields - Typedefs

  • All
  • Functions
  • Variables
  • Typedefs
  • Related Functions
  • iterator : AString


AString C++ Ansi String Library: Data Fields - Related Functions

  • All
  • Functions
  • Variables
  • Typedefs
  • Related Functions
  • Asprintf : AString
  • operator!= : AString
  • operator+ : AString
  • operator< : AString
  • operator== : AString
  • operator> : AString


AString C++ Ansi String Library: D:/dev/_Altidev/cplusplus/AString/AString.cpp Source File

  • D:
  • dev
  • _Altidev
  • cplusplus
  • AString
AString.cpp
1 #include "AString.h"
2 #include <cstring>
3 #include <stdarg.h>
4 #include <stdio.h>
5
6 //#define ASTRING_DEBUG_TRACE_ENABLE
7 #ifdef ASTRING_DEBUG_TRACE_ENABLE
8 #define ASTRING_DEBUG_TRACE(a,b) std::cout<<"ASTRING DEBUG :"<<a<<b<<std::endl
9 #else
10 #define ASTRING_DEBUG_TRACE(a,b)
11 #endif
12 #if ASTRING_IMPLEMENT_WITH_STD_STRING
13 #define ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(x) x
14 #else
15 #define ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(x)
16 #endif
17
18
19 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
20 const char AString::EmptyBuffer[4]={0,0,0,0};
21 const size_t AString::npos=(size_t)-1;
22 #endif
23
24 //-------------------------------------------------------------
25 AString::AString()
26 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string())
27 {
28 ASTRING_DEBUG_TRACE("construct empty","");
29 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
30 StrBuffer=(char*)&EmptyBuffer;
31 #endif
32 }
33 //-------------------------------------------------------------
34 AString::~AString() ASTRING_NOEXCEPT
35 {
36 ASTRING_DEBUG_TRACE("destroy ",c_str());
37 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
38 if(StrBuffer!=EmptyBuffer)
39 delete [] GetRawBuffer();
40 #endif
41 }
42 //-------------------------------------------------------------
43 AString::AString(const char *str,int count/*=-1*/)
44 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(str,(count)<0?strlen(str):count))
45 {
46 ASTRING_DEBUG_TRACE("construct char* ",str);
47 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
48 size_t len;
49 if(count>=0)
50 len=count;
51 else
52 len=strlen(str);
53 if(len>0)
54 {
55 AllocBuffer((unsigned)len);
56 memcpy(StrBuffer,str,len);
57 StrBuffer[len]=0;
58 GetSize()=(unsigned)strlen(StrBuffer); // in case str contains a \0
59 }
60 else
61 StrBuffer=(char*)&EmptyBuffer;
62 #endif
63 }
64 //-------------------------------------------------------------
65 AString::AString(int value)
66 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string())
67 {
68 // Todo reimplement it without std::stringstream
69 // Todo support multiple base
70 ASTRING_DEBUG_TRACE("construct int ",value);
71 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
72 StrBuffer=(char*)&EmptyBuffer;
73 #endif
74 /*std::stringstream ss;
75 ss << value;
76 assign(ss.str().c_str());*/
77 Format("%d",value);
78 }
79 //-------------------------------------------------------------
80 AString::AString(unsigned value)
81 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string())
82 {
83 // Todo reimplement it without std::stringstream
84 // Todo support multiple base
85 ASTRING_DEBUG_TRACE("construct unsigned ",value);
86 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
87 StrBuffer=(char*)&EmptyBuffer;
88 #endif
89 /*std::stringstream ss;
90 ss << value;
91 assign(ss.str().c_str());*/
92 Format("%u",value);
93 }
94 /*AString::AString(float value)
95 {
96 // Todo reimplement it without std::stringstream
97 ASTRING_DEBUG_TRACE("construct float ",value);
98 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
99 StrBuffer=(char*)&EmptyBuffer;
100 #endif
101
102 Format("%f",value);
103 }*/
104 AString::AString(double value)
105 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string())
106 {
107 ASTRING_DEBUG_TRACE("construct double ",value);
108 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
109 StrBuffer=(char*)&EmptyBuffer;
110 #endif
111 /*std::stringstream ss;
112 ss << value;
113 assign(ss.str().c_str()); */
114 #if __MINGW32__ || _MSC_VER
115 Format("%f",value);
116 #else
117 Format("%lf",value);
118 #endif
119 // Trim right 0 and . but keep at least one char
120 for(unsigned i=(unsigned)size()-1;i>0;i--)
121 if((at(i)=='0')||(at(i)=='.'))
122 at(i)=0;
123 else
124 break;
125 RecomputeSize();
126
127 }
128 //-------------------------------------------------------------
129 AString::AString(const AString &Src)
130 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(Src))
131 {
132 ASTRING_DEBUG_TRACE("construct copy ",Src.c_str());
133 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
134 unsigned len=(unsigned)Src.size();
135 if(len>0)
136 {
137 AllocBuffer(len);
138 GetSize()=len;
139 memcpy(StrBuffer,Src.StrBuffer,len);
140 StrBuffer[len]=0;
141 }
142 else
143 StrBuffer=(char*)&EmptyBuffer;
144 #endif
145 }
146
147 #if ASTRING_INTEROP_BCB
148 AString::AString(const AnsiString &Src)
149 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(Src.c_str() ))
150 {
151 ASTRING_DEBUG_TRACE("construct AnsiString ",Src.c_str());
152 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
153 unsigned len=(unsigned)Src.Length();
154 if(len>0)
155 {
156 AllocBuffer(len);
157 GetSize()=len;
158 memcpy(StrBuffer,Src.c_str(),len);
159 StrBuffer[len]=0;
160 }
161 else
162 StrBuffer=(char*)&EmptyBuffer;
163 #endif
164 }
165 AString::AString(const UnicodeString &Src)
166 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(AnsiString(Src).c_str() ))
167 {
168 ASTRING_DEBUG_TRACE("construct AnsiString ",Src.c_str());
169 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
170 AnsiString AS(Src); // Convert to Ansi
171 unsigned len=(unsigned)AS.Length();
172 if(len>0)
173 {
174 AllocBuffer(len);
175 GetSize()=len;
176 memcpy(StrBuffer,AS.c_str(),len);
177 StrBuffer[len]=0;
178 }
179 else
180 StrBuffer=(char*)&EmptyBuffer;
181 #endif
182 }
183 AString::operator AnsiString ()
184 {
185 return AnsiString(c_str());
186 }
187 AString::operator UnicodeString ()
188 {
189 return UnicodeString(c_str());
190 }
191
192 #endif //ASTRING_INTEROP_BCB
193
194 #if ASTRING_INTEROP_VSS
195 AString::AString(CStringA &Src)
196 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(Src.GetBuffer(0) ))
197 {
198 // ASTRING_DEBUG_TRACE("construct CStringA ",Src.c_str());
199 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
200 unsigned len=(unsigned)Src.GetLength();
201 if(len>0)
202 {
203 AllocBuffer(len);
204 GetSize()=len;
205 memcpy(StrBuffer,Src.GetBuffer(0),len);
206 StrBuffer[len]=0;
207 }
208 else
209 StrBuffer=(char*)&EmptyBuffer;
210 #endif
211 }
212 AString::operator CStringA ()
213 {
214 return CStringA(c_str());
215 }
216
217 AString::AString(CStringW &Src)
218 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(CStringA(Src).GetBuffer(0) ))
219 {
220 // ASTRING_DEBUG_TRACE("construct CStringW ",Src.c_str());
221 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
222 CStringA Cs(Src);
223 unsigned len=(unsigned)Cs.GetLength();
224 if(len>0)
225 {
226 AllocBuffer(len);
227 GetSize()=len;
228 memcpy(StrBuffer,Cs.GetBuffer(0),len);
229 StrBuffer[len]=0;
230 }
231 else
232 StrBuffer=(char*)&EmptyBuffer;
233 #endif
234 }
235 AString::operator CStringW ()
236 {
237 return CStringW(CStringA(c_str()));
238 }
239 #endif // ASTRING_INTEROP_VSS
240
241
242 //-------------------------------------------------------------
243 #if ASTRING_INTEROP_STD_STRING
244 AString::AString(const std::string &Src)
245 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(Src))
246 {
247 ASTRING_DEBUG_TRACE("construct string copy ",Src.c_str());
248 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
249 unsigned len=(unsigned)Src.size();
250 if(len>0)
251 {
252 AllocBuffer(len);
253 GetSize()=len;
254 memcpy(StrBuffer,Src.c_str(),len);
255 StrBuffer[len]=0;
256 }
257 else
258 StrBuffer=(char*)&EmptyBuffer;
259 #endif
260 }
261 AString::operator std::string () const
262 {
263 #if ASTRING_IMPLEMENT_WITH_STD_STRING
264 return *this;
265 #else
266 std::string s(c_str());
267 return s;
268 #endif
269 }
270
271 #endif
272 //-------------------------------------------------------------
273 AString& AString::operator =(const AString& Src)
274 {
275 ASTRING_DEBUG_TRACE("operator = AString ",Src.c_str());
276 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
277 if(StrBuffer!=((char*)&EmptyBuffer))
278 delete [] GetRawBuffer();
279 unsigned len=(unsigned)Src.size();
280 if(len>0)
281 {
282 AllocBuffer(len);
283 GetSize()=len;
284 memcpy(StrBuffer,Src.StrBuffer,len);
285 StrBuffer[len]=0;
286 }
287 else
288 StrBuffer=(char*)&EmptyBuffer;
289 #else
290 assign(Src);
291 #endif
292 return *this;
293 }
294 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
295 //-------------------------------------------------------------
296 AString& AString::assign(const char *str,int count/*=-1*/)
297 {
298 ASTRING_DEBUG_TRACE("assign char* ",str);
299 unsigned len;
300 if(count>=0)
301 len=count;
302 else
303 len=(unsigned)strlen(str);
304 clear();
305 if(len>0)
306 {
307 AllocBuffer(len);
308 GetSize()=len;
309 memcpy(StrBuffer,str,len);
310 StrBuffer[len]=0;
311 }
312 else
313 StrBuffer=(char*)&EmptyBuffer;
314 return *this;
315 }
316 //-------------------------------------------------------------
317 char* AString::c_str()
318 {
319 return StrBuffer;
320 }
321 //-------------------------------------------------------------
322 const char* AString::c_str() const
323 {
324 return StrBuffer;
325 }
326 //-------------------------------------------------------------
327 size_t AString::size() const
328 {
329 if(StrBuffer==((char*)&EmptyBuffer))
330 return 0;
331 else
332 return GetSize();
333 }
334 //-------------------------------------------------------------
335 size_t AString::capacity() const
336 {
337 if(StrBuffer==((char*)&EmptyBuffer))
338 return 0;
339 else
340 return GetCapacity();
341 }
342 //-------------------------------------------------------------
343 char* AString::GetRawBuffer()
344 {
345 if(StrBuffer==((char*)&EmptyBuffer))
346 return NULL;
347 else
348 return StrBuffer-2*sizeof(unsigned);
349 }
350 //-------------------------------------------------------------
351 void AString::clear()
352 {
353 ASTRING_DEBUG_TRACE("clear ","");
354 if(StrBuffer!=EmptyBuffer)
355 {
356 delete [] GetRawBuffer();
357 StrBuffer=(char*)EmptyBuffer;
358 }
359 }
360 //-------------------------------------------------------------
361 void AString::reserve(size_t count)
362 {
363 ASTRING_DEBUG_TRACE("reserve ",count);
364 char *PrevStr=c_str();
365 unsigned PrevLen=(unsigned)size();
366 char *PrevBuffer=GetRawBuffer();
367 if(PrevLen>(unsigned)count)
368 count=(size_t)PrevLen; // Reserve can't be smaller than current size
369 AllocBuffer((unsigned)count);
370 if(count)
371 {
372 GetSize()=(unsigned)count;
373 memcpy(StrBuffer,PrevStr,PrevLen);
374 StrBuffer[PrevLen]=0;
375 GetSize()=PrevLen;
376 }
377 if((PrevBuffer)&&(PrevBuffer!=EmptyBuffer))
378 delete []PrevBuffer;
379
380
381 }
382 //-------------------------------------------------------------
383 void AString::AllocBuffer(unsigned count)
384 {
385 ASTRING_DEBUG_TRACE("Alloc buffer ",count);
386
387 if(count>0)
388 {
389 count|=7; // round up to 8 bytes (with the +1 of the next line)
390 StrBuffer=new char[count+1+2*sizeof(unsigned)];
391 StrBuffer+=2*sizeof(unsigned);
392 GetSize()=0;
393 GetCapacity()=count;
394 StrBuffer[0]=0;
395 }
396 else
397 StrBuffer=(char*)&EmptyBuffer;
398 }
399 //-------------------------------------------------------------
400 char& AString::operator[](size_t idx)
401 {
402 if(idx>size())
403 throw AStringException("Invalid index");
404 return StrBuffer[idx];
405 }
406 //-------------------------------------------------------------
407 char AString::operator[](size_t idx) const
408 {
409 if(idx>size())
410 throw AStringException("Invalid index");
411 return StrBuffer[idx];
412 }
413 //-------------------------------------------------------------
414 char& AString::at(size_t idx)
415 {
416 if(idx>size())
417 throw AStringException("Invalid index");
418 return StrBuffer[idx];
419 }
420 //-------------------------------------------------------------
421 char AString::at(size_t idx) const
422 {
423 if(idx>size())
424 throw AStringException("Invalid index");
425 return StrBuffer[idx];
426 }
427
428 //-------------------------------------------------------------
429 unsigned& AString::GetSize()
430 {
431 return *((unsigned*)(StrBuffer-2*sizeof(unsigned)));
432 }
433 //-------------------------------------------------------------
434 unsigned& AString::GetCapacity()
435 {
436 return *((unsigned*)(StrBuffer-sizeof(unsigned)));
437 }
438 //-------------------------------------------------------------
439 unsigned AString::GetSize() const
440 {
441 return *((unsigned*)(StrBuffer-2*sizeof(unsigned)));
442 }
443 //-------------------------------------------------------------
444 unsigned AString::GetCapacity() const
445 {
446 return *((unsigned*)(StrBuffer-sizeof(unsigned)));
447 }
448 //-------------------------------------------------------------
449 AString AString::operator +(const AString &RightStr) const
450 {
451 AString res;
452 unsigned LLen=(unsigned)size();
453 unsigned RLen=(unsigned)RightStr.size();
454 res.reserve(LLen+RLen );
455 res.GetSize()=LLen+ RLen;
456 if(LLen)
457 memcpy(res.StrBuffer,StrBuffer,LLen);
458 if(RLen)
459 memcpy(&res.StrBuffer[LLen],RightStr.StrBuffer,RLen);
460 res.StrBuffer[res.GetSize()]=0;
461 return res;
462 }
463 //-------------------------------------------------------------
464 AString AString::operator +(const char* RightStr) const
465 {
466 AString res;
467 unsigned LLen=(unsigned)size();
468 unsigned RLen=(unsigned)strlen(RightStr);
469 res.reserve(LLen+RLen );
470 res.GetSize()=LLen+ RLen;
471 if(LLen)
472 memcpy(res.StrBuffer,StrBuffer,LLen);
473 if(RLen)
474 memcpy(&res.StrBuffer[LLen],RightStr,RLen);
475 res.StrBuffer[res.GetSize()]=0;
476 return res;
477 }
478 //-------------------------------------------------------------
479 AString operator +(const char *LeftStr,const AString &RightStr)
480 {
481 AString res;
482 unsigned LLen=(unsigned)strlen(LeftStr);
483 unsigned RLen=(unsigned)RightStr.size();
484 res.reserve(LLen+RLen );
485 res.GetSize()=LLen+ RLen;
486 if(LLen)
487 memcpy(res.StrBuffer,LeftStr,LLen);
488 if(RLen)
489 memcpy(&res.StrBuffer[LLen],RightStr.StrBuffer,RLen);
490 res.StrBuffer[res.GetSize()]=0;
491 return res;
492 }
493 //-------------------------------------------------------------
494 AString& AString::operator +=(const AString &RightStr)
495 {
496 unsigned RLen=(unsigned)RightStr.size();
497 unsigned FinalLength=(unsigned)size()+RLen;
498 if(FinalLength>capacity())
499 reserve(FinalLength);
500 if(RLen)
501 memcpy(&StrBuffer[size()],RightStr.StrBuffer,RLen);
502 GetSize()=FinalLength;
503 StrBuffer[FinalLength]=0;
504 return *this;
505 }
506 //-------------------------------------------------------------
507 bool AString::operator ==(const AString &RightStr) const
508 {
509 unsigned RLen=(unsigned)RightStr.size();
510 if(RLen!=size())
511 return false;
512 char *sL=StrBuffer;
513 char *sR=RightStr.StrBuffer;
514 while(*sL)
515 {
516 if(*sL!=*sR)
517 return false;
518 ++sL; // Go to next char
519 ++sR;
520 }
521 return true;
522 }
523 //-------------------------------------------------------------
524 bool AString::operator ==(const char* RightStr) const
525 {
526 char *sL=StrBuffer;
527 const char *sR=RightStr;
528 while(*sL)
529 {
530 if(*sL!=*sR)
531 return false;
532 ++sL; // Go to next char
533 ++sR;
534 }
535 if(*sL!=*sR)
536 return false;
537 return true;
538 }
539 //-------------------------------------------------------------
540 bool operator ==(const char *LeftStr,const AString &RightStr)
541 {
542 return RightStr==LeftStr;
543 }
544 //-------------------------------------------------------------
545 bool AString::operator <(const AString &RightStr) const
546 {
547 char *sL=StrBuffer;
548 char *sR=RightStr.StrBuffer;
549 while(*sL)
550 {
551 if(*sL<*sR)
552 return true;
553 if(*sL>*sR)
554 return false;
555 ++sL; // Go to next char
556 ++sR;
557 }
558 if(*sR) // Right string have more char, if begining equal it's longer
559 return true;
560 return false;
561 }
562 //-------------------------------------------------------------
563 bool AString::operator >(const AString &RightStr) const
564 {
565 char *sL=StrBuffer;
566 char *sR=RightStr.StrBuffer;
567 while(*sL)
568 {
569 if(*sL>*sR)
570 return true;
571 if(*sL<*sR)
572 return false;
573 ++sL; // Go to next char
574 ++sR;
575 }
576 return false;
577 }
578 //-------------------------------------------------------------
579 bool AString::operator <(const char *RightStr) const
580 {
581 char *sL=StrBuffer;
582 const char *sR=RightStr;
583 while(*sL)
584 {
585 if(*sL<*sR)
586 return true;
587 if(*sL>*sR)
588 return false;
589 ++sL; // Go to next char
590 ++sR;
591 }
592 if(*sR) // Right string have more char, if begining equal it's longer
593 return true;
594 return false;
595 }
596 //-------------------------------------------------------------
597 bool AString::operator >(const char *RightStr) const
598 {
599 char *sL=StrBuffer;
600 const char *sR=RightStr;
601 while(*sL)
602 {
603 if(*sL>*sR)
604 return true;
605 if(*sL<*sR)
606 return false;
607 ++sL; // Go to next char
608 ++sR;
609 }
610 return false;
611 }
612 //-------------------------------------------------------------
613 bool operator <(const char *LeftStr,const AString &RightStr)
614 {
615 const char *sL=LeftStr;
616 char *sR=RightStr.StrBuffer;
617 while(*sL)
618 {
619 if(*sL<*sR)
620 return true;
621 if(*sL>*sR)
622 return false;
623 ++sL; // Go to next char
624 ++sR;
625 }
626 if(*sR) // Right string have more char, if begining equal it's longer
627 return true;
628 return false;
629 }
630 //-------------------------------------------------------------
631 bool operator >(const char *LeftStr,const AString &RightStr)
632 {
633 const char *sL=LeftStr;
634 char *sR=RightStr.StrBuffer;
635 while(*sL)
636 {
637 if(*sL>*sR)
638 return true;
639 if(*sL<*sR)
640 return false;
641 ++sL; // Go to next char
642 ++sR;
643 }
644 return false;
645 }
646
647 //-------------------------------------------------------------
648 bool AString::operator !=(const AString &RightStr) const
649 {
650 return !(*this==RightStr);
651 }
652 //-------------------------------------------------------------
653 bool AString::operator !=(const char* RightStr) const
654 {
655 return !(*this==RightStr);
656 }
657 bool operator !=(const char *LeftStr,const AString &RightStr)
658 {
659 return !(RightStr==LeftStr);
660 }
661 //-------------------------------------------------------------
662 AString AString::substr( size_t pos /*= 0*/,size_t count /*= npos*/ ) const
663 {
664 if(pos>size())
665 throw AStringException("substr:Invalid position");
666 if((count!=npos)&&((count>size())||((pos+count)>size())))
667 count=size()-pos;
668 return AString(&StrBuffer[pos],(int)count);
669 }
670 //-------------------------------------------------------------
671 size_t AString::find(const char* Searched,size_t StartPos/*=0*/,size_t count/*=0*/) const
672 {
673 if(count==0)
674 count=(unsigned)strlen(Searched);
675 if(StartPos>size())
676 return npos;
677 if(count>size())
678 return npos;
679 char *Source=StrBuffer+StartPos;
680 while(*Source)
681 {
682 unsigned c=0;
683 while(c<count)
684 {
685 if(Source[c]!=Searched[c])
686 break;
687 c++;
688 }
689 if(c==count)
690 return (unsigned)(Source-StrBuffer);
691 Source++;
692 }
693 return npos;
694 }
695 //-------------------------------------------------------------
696 size_t AString::find(char Searched,size_t StartPos/*=0*/) const
697 {
698 return find(&Searched,StartPos,1);
699 }
700 //-------------------------------------------------------------
701 AString::iterator AString::begin()
702 {
703 return c_str();
704 }
705 //-------------------------------------------------------------
706 AString::iterator AString::end()
707 {
708 return c_str()+size();
709 }
710 #endif
711 //-------------------------------------------------------------
712 #if !ASTRING_NOSTL
713 std::ostream& operator<<(std::ostream& os, const AString& str)
714 {
715 os << str.c_str();
716 return os;
717 }
718 #endif
719
720 //-------------------------------------------------------------
721 AString& AString::replace( size_t pos, size_t count,const AString& str )
722 {
723 #if ASTRING_IMPLEMENT_WITH_STD_STRING
724 std::string::replace(pos,count,str);
725 return *this;
726 #else
727 if((pos+count)>size())
728 throw AStringException("replace:pos+count > size");
729 size_t NewStringSize=str.size();
730 if(count==NewStringSize)
731 {// Replace by same size is simple and quick
732 for(size_t i=0;i<count;i++)
733 at(pos+i)=str[i];
734 }
735 else if(count>NewStringSize)
736 { // replace by smaller string
737 for(size_t i=0;i<NewStringSize;i++)
738 at(pos+i)=str[i];
739 size_t Remaining=size()-(pos+count);
740 for(size_t i=0;i<=Remaining;i++) // <= to copy also the zero
741 at(pos+NewStringSize+i)=at(pos+count+i);
742 GetSize()=static_cast<unsigned int>(pos+NewStringSize+Remaining);
743 }
744 else
745 { // replace by bigger string
746 // Save the end of the string
747 AString EndOfString=substr(pos+count);
748 GetSize()=static_cast<unsigned int>(pos);
749 if(capacity()<(GetSize()+str.size()+EndOfString.size()))
750 reserve(GetSize()+str.size()+EndOfString.size());
751 (*this)+=str;
752 (*this)+=EndOfString;
753 }
754 return *this;
755 #endif
756 }
757 //-------------------------------------------------------------
758 AString& AString::replace( const AString& src,const AString& str )
759 {
760 size_t startpos=0;
761 while(1)
762 {
763 size_t pos=find(src.c_str(),startpos,src.size());
764 if(pos==npos)
765 return *this;
766 replace(pos,src.size(),str);
767 startpos=pos+str.size();
768 }
769 }
770 //-------------------------------------------------------------
771 AString& AString::rtrim(const char *CharsToRemove)
772 {
773 unsigned CharToRemoveSize=static_cast<unsigned int>(strlen(CharsToRemove));
774 if(size()==0)
775 return *this;
776 size_t pos=size();
777 size_t count=0; // Count of char to remove
778 do{
779 pos--;
780 bool CharRemoved=false;
781 for(unsigned i=0;i<CharToRemoveSize;i++)
782 if((*this)[pos]==CharsToRemove[i])
783 {
784 CharRemoved=true;
785 count++;
786 break;
787 }
788 if(CharRemoved==false)
789 break;
790 }
791 while(pos);
792 if(count>0)
793 {
794 #if ASTRING_IMPLEMENT_WITH_STD_STRING
795 resize(size()-count);
796 #else
797 (*this)[size()-count]=0;
798 RecomputeSize();
799 #endif
800 }
801 return *this;
802 }
803 //-------------------------------------------------------------
804 AString& AString::ltrim(const char *CharsToRemove)
805 {
806 unsigned CharToRemoveSize=static_cast<unsigned int>(strlen(CharsToRemove));
807 if(size()==0)
808 return *this;
809 size_t pos=0;
810 size_t count=0; // Count of char to remove
811 do{
812 bool CharRemoved=false;
813 for(unsigned i=0;i<CharToRemoveSize;i++)
814 if((*this)[pos]==CharsToRemove[i])
815 {
816 CharRemoved=true;
817 count++;
818 break;
819 }
820 if(CharRemoved==false)
821 break;
822 pos++;
823 }
824 while(pos<size());
825 if(count>0)
826 {
827 #if ASTRING_IMPLEMENT_WITH_STD_STRING
828 assign(substr(count));
829 #else
830 char* destptr=StrBuffer;
831 do{
832 *destptr=destptr[pos];
833 destptr++;
834 }while(*(destptr-1));
835 RecomputeSize();
836 #endif
837 }
838 return *this;
839 }
840 //-------------------------------------------------------------
841 AString& AString::trim(const char *CharsToRemove)
842 {
843 rtrim(CharsToRemove);
844 ltrim(CharsToRemove);
845 return *this;
846 }
847 //-------------------------------------------------------------
848 void AString::strncpy( char* dest,size_t destsize,unsigned pos /*= 0*/) const
849 {
850 if(pos>size())
851 throw AStringException("Invalid position");
852 if(destsize<1)
853 throw AStringException("Invalid destination size should be >0");
854 destsize--; // zero based and/or final 0
855 if((destsize>size())||((pos+destsize)>size()))
856 destsize=size()-pos;
857 memcpy(dest,&c_str()[pos],destsize);
858 dest[destsize]=0;
859
860 }
861 //-------------------------------------------------------------
862 void AString::RecomputeSize()
863 {
864 unsigned NewSize=(unsigned)strlen(c_str());
865 if(NewSize==size())
866 return;
867 #if ASTRING_IMPLEMENT_WITH_STD_STRING
868 resize(NewSize);
869 #else
870 GetSize()=NewSize;
871 #endif
872 }
873 //-------------------------------------------------------------
874 int AString::ToInt()
875 {
876 int x;
877 #ifdef _MSC_VER
878 if(sscanf_s(c_str(),"%d",&x)<=0)
879 #else
880 if(sscanf(c_str(),"%d",&x)<=0)
881 #endif
882 throw AStringException(Asprintf("ToInt %s is not an integer ",c_str()));
883 return x;
884 }
885 //-------------------------------------------------------------
886 int AString::ToIntDef(int defval)
887 {
888 int x;
889 #ifdef _MSC_VER
890 if(sscanf_s(c_str(),"%d",&x)<=0)
891 #else
892 if(sscanf(c_str(),"%d",&x)<=0)
893 #endif
894 return defval;
895 return x;
896 }
897 //-------------------------------------------------------------
898 #if __cplusplus > 199711L // C++ 11
899 //-------------------------------------------------------------
900 AString::AString(AString &&Src) ASTRING_NOEXCEPT
901 ASTRING_IMPLEMENT_WITH_STD_STRING_CODE(:std::string(Src))
902 {
903 ASTRING_DEBUG_TRACE("Move constructor ",Src.c_str());
904 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
905 StrBuffer=Src.StrBuffer;
906 Src.StrBuffer=(char*)&EmptyBuffer;
907 #endif
908 }
909 //-------------------------------------------------------------
910 AString& AString::operator =(AString&& Src) ASTRING_NOEXCEPT
911 {
912 ASTRING_DEBUG_TRACE("Move operator = ",Src.c_str());
913 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
914 if(StrBuffer!=((char*)&EmptyBuffer))
915 delete [] GetRawBuffer();
916 StrBuffer=Src.StrBuffer;
917 Src.StrBuffer=(char*)&EmptyBuffer;
918 return *this;
919 #else
920 return this->operator =(Src);
921 #endif
922 }
923 #endif // C++ 11
924
925 //-------------------------------------------------------------
926 AString Asprintf(const char *Fmt,...)
927 {
928 int needed;
929 char* TempBuffer;
930 AString ret;
931 #ifdef _MSC_VER
932 va_list argptr;
933 va_start(argptr, Fmt);
934 needed=_vscprintf(Fmt, argptr);
935 #else
936 char Temp[4];
937 va_list argptr;
938 va_start(argptr, Fmt);
939 needed=vsnprintf(Temp,0, Fmt, argptr);
940 #endif
941 va_end(argptr);
942
943 #if ASTRING_IMPLEMENT_WITH_STD_STRING
944 TempBuffer =new char[needed+1];
945 #else
946 ret.reserve(needed);
947 TempBuffer=ret.c_str();
948 ret.GetSize()=needed;
949 #endif
950 va_start(argptr, Fmt);
951 #ifdef _MSC_VER
952 vsnprintf_s(TempBuffer,needed+1,_TRUNCATE, Fmt, argptr);
953 #else
954 vsnprintf(TempBuffer,needed+1, Fmt, argptr);
955 TempBuffer[needed]=0;
956 #endif
957 va_end(argptr);
958 #if ASTRING_IMPLEMENT_WITH_STD_STRING
959 ret=TempBuffer;
960 delete [] TempBuffer;
961 #endif
962 return ret;
963 }
964 //-------------------------------------------------------------
965 //AString& AString::Format(const AString &Fmt,...)
966 AString& AString::Format(const char *Fmt,...)
967 {
968 char* TempBuffer;
969 int needed;
970 va_list argptr;
971 va_start(argptr, Fmt);
972 #ifdef _MSC_VER
973 needed=_vscprintf(Fmt, argptr);
974 #else
975 char Temp[4];
976 needed=vsnprintf(Temp,0, Fmt, argptr);
977 #endif
978 va_end(argptr);
979 #if ASTRING_IMPLEMENT_WITH_STD_STRING
980 TempBuffer =new char[needed+1];
981 #else
982 reserve(needed);
983 TempBuffer=c_str();
984 GetSize()=needed;
985 #endif
986 va_start(argptr, Fmt);
987 #ifdef _MSC_VER
988 vsnprintf_s(TempBuffer,needed+1,_TRUNCATE, Fmt, argptr);
989 #else
990 vsnprintf(TempBuffer,needed+1, Fmt, argptr);
991 TempBuffer[needed]=0;
992 #endif
993 va_end(argptr);
994 #if ASTRING_IMPLEMENT_WITH_STD_STRING
995 assign(TempBuffer);
996 delete [] TempBuffer;
997 #endif
998 return *this;
999
1000 }
1001
1002


AString C++ Ansi String Library: D:/dev/_Altidev/cplusplus/AString/AString.h Source File

  • D:
  • dev
  • _Altidev
  • cplusplus
  • AString
AString.h
1 /**
2 \mainpage AString Main Page
3 \author Laurent NAVARRO ln@altidev.com
4 \version 0.4 2014-09-03
5 \copyright MIT X11 : See .h file header
6 Copyright (C) 2013 Laurent NAVARRO
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL LAURENT NAVARRO BE LIABLE FOR ANY
22 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 Except as contained in this notice, the name of the Laurent NAVARRO shall
27 not be used in advertising or otherwise to promote the sale, use or
28 other dealings in this Software without prior written authorization
29 from the Laurent NAVARRO.
30 \section desc Description
31 AString is a library providing a C++ Ansi (8 bits char set) String class
32
33 I've create it because, I work with several compiler and it's often difficult to decide which string class I should use,
34 because some compiler have their own framework string class (Visual Studio CString, QT QString,C++ Builder AnsiString) and all of them support std::string and C char*.
35 I could always use std:string but framework's strings are generally a bit more richer in features.
36 I'm also doing multi-threading but on pre-c++ 11 there's no guarantee std::string works fine, you have to investigate, sometimes it's ok, sometimes it's not OK.
37
38 AString provide the following features :
39 - an interface similar (not identical) to std::string
40 - Can be implemented with std:string or not (#define based). If not, it's a simple buffer based implementation (easy to debug)
41 - include interoperability with frameworks string class (C++ Builder,CString )
42 - include constructor from numbers (int, float, double, ... ) and also String To Number methods (ToInt, ... )
43 - include sprintf/format like feature
44 - include a secured strncpy char* copier
45 - include implode and explode (split/merge of string array)
46 - Buffer based implementation can be directly used on printf with %s on some compiler
47 - Can easily be modified
48
49 \subsection doc Documentation
50 Documentation is generated using Doxygen,
51 available online here <a href="http://www.altidev.com/ln/AString/">http://www.altidev.com/ln/AString/</a>
52 or at chm format here <a href="http://www.altidev.com/ln/AString/AString.chm">AString.chm</a> and on the repository.
53
54 \subsection config Configuration details
55 Some configuration can be done by settings some define on the --- Write your settings Here --- section of the Header file \n
56 Here a summary See source for details :
57 - ASTRING_IMPLEMENT_WITH_STD_STRING make AString inherits from std::string (probably better if you fully trust it) instead of the buffer based implementation
58 - ASTRING_INTEROP_STD_STRING for buffer base implementation, allow transparent interoperability with std::string (constructor & cast operator)
59 - ASTRING_NOSTL remove all feature using STL like vector et stream.
60 - ASTRING_INTEROP_BCB Enable interoperability with C++ Builder AnsiString and UnicodeString (constructor & cast operator)
61 - ASTRING_INTEROP_VSS Enable interoperability with Visual Studio CString (constructor & cast operator)
62
63 \subsection impl Implementation details
64 if \c ASTRING_IMPLEMENT_WITH_STD_STRING is set to 0 the string is implemented by a pointer on a buffer. \n
65 However a specific tips was used to allow string to be used on %s printf like function on some compiler inspired from Chris OldWood blog A Not-So-Clever-Now String Implementation \n
66 The tips consists to have a pointer pointing on the full buffer itself but on the char* section of the buffer
67 and to compute with offset to access to stored size and capacity attributes as described here.
68 \code
69 +--------------+
70 + Size + <-- StrBuffer - 2*sizeof(unsigned) or GetSize() or GetRawBuffer()
71 +--------------+
72 + Capacity + <-- StrBuffer - 1*sizeof(unsigned) or GetCapacity()
73 +--------------+
74 + char* Buffer + <-- StrBuffer Attribute, conatains zero terminated characters string.
75 +--------------+
76 \endcode
77 \todo
78 - Add missing std::string reimplementation (rfind)
79 - Add some features ToUpper, ToLower
80 - Add some useful string simple algorithms
81 - ToIntFromHex "0x20" or "20" to 32 , ToIntFromBin "0101" to 5
82 - assignAsHex 32 to "0x20" or "20" optional 0x
83 - assignAsBin 5 to "0101" parameter to set width and space on result (0101 0011 if param=4)
84 - Finalize/improve documentation and tests, test on Clang
85 - Add interaction with some others frameworks (QT, WxWidget, ... )
86 - Improve implementation of CString interoperability (make constructor param const by avoiding get buffer
87
88 \subsection test Test details
89 I'm using the TDD logic for this library, so it include mainly project to run CPP Unit
90 Project include test project on
91 - CodeBlock with Mingw GCC in Windows 32 bits
92 - CodeBlock with GCC on Linux Fedora 17 32bits
93 - CodeBlock with GCC on Linux Debian 7 64bits
94 - Visual Sutdio 2010 32 & 64 bits
95 - C++ Builder XE2 32 bits
96
97 I've done memory check using visual leak detector on Visual Studio http://vld.codeplex.com/ \n
98 And DrMemory on Windows \n
99 And valgring on Linux, here command to test : \n
100 - valgrind --tool=memcheck bin/Debug/AStringUT
101 - valgrind --leak-check=full --tool=memcheck bin/Debug/AStringUT
102 \nI've done coverage test on GCC using GCov
103 \nI've used CppCheck also.
104
105 \subsection release Release History
106 - version 0.1 2013-08-22 Initial
107 - version 0.2 2013-12-29
108 - Adding Replace of string by a string
109 - Adding trim, ltrim, rtrim
110 - version 0.3 2014-05-02
111 - Bugfix on comparison operator
112 - Added Char* on left of comparison operator
113 - version 0.4 2014-09-03
114 - Added test for 100% coverage on Gcc
115 - Minor Bugfix in case of reserve(0)
116
117 */
118 #ifndef ASTRING_H_INCLUDED
119 #define ASTRING_H_INCLUDED
120 //---------- Write your settings Here -------------------
121 //#define ASTRING_IMPLEMENT_WITH_STD_STRING 1
122 #define ASTRING_INTEROP_STD_STRING 1
123
124 #if _MSC_VER
125 #define ASTRING_INTEROP_VSS 1
126 #endif
127 //#define ASTRING_NOSTL 1
128 //---------- End of your settings -------------------
129
130 #ifndef ASTRING_IMPLEMENT_WITH_STD_STRING
131 /**
132 Define the implementation type.
133 Define if AString is implemented by inheritence of std::string or by a buffer based implementation
134 Default = 0 meaning implemented by buffer based implementation
135 *******************************************/
136 #define ASTRING_IMPLEMENT_WITH_STD_STRING 0
137 #else
138 // Implementation with std::string require to interop with std::string
139 #define ASTRING_INTEROP_STD_STRING 1
140 #endif
141 #ifndef ASTRING_NOSTL
142 /** Allow to remove all references and features relating the STL.
143 * Not compatible with ASTRING_IMPLEMENT_WITH_STD_STRING = 1
144 * Default value=0
145 */
146 #define ASTRING_NOSTL 0
147 #endif
148 #ifndef ASTRING_INTEROP_STD_STRING
149 /** Enable interoperability with std::string.
150 * Default value=0
151 * (forced when ASTRING_IMPLEMENT_WITH_STD_STRING = 1)\n
152 * Means constructor and cast operator for std::string
153 */
154 #define ASTRING_INTEROP_STD_STRING 0
155 #endif
156 // C++ Builder VCL interop
157 #ifndef ASTRING_INTEROP_BCB
158 #ifdef __BCPLUSPLUS__
159 /** Enable interoperability with C++ Builder AnsiString and UnicodeString
160 * Default value=1 on C++ Builder
161 * Means constructor and cast operator for theses classes
162 */
163 #define ASTRING_INTEROP_BCB 1
164 #else
165 #define ASTRING_INTEROP_BCB 0
166 #endif
167 #endif
168 // Visual Studio CString interop
169 #ifndef ASTRING_INTEROP_VSS
170 /** Enable interoperability with Visual Studio CString
171 * Default value=0
172 * Means constructor and cast operator for theses classes
173 */
174 #define ASTRING_INTEROP_VSS 0
175 #endif
176
177 #if ASTRING_INTEROP_VSS
178 #include <atlstr.h>
179 #endif
180 #if ASTRING_INTEROP_BCB
181 #include<vcl.h>
182 #endif
183
184 #if !ASTRING_NOSTL
185 #include <iostream>
186 #include <sstream>
187 #include <deque>
188 #include <exception>
189 #endif
190 #if ASTRING_IMPLEMENT_WITH_STD_STRING || ASTRING_INTEROP_STD_STRING
191 #include <string>
192 #endif
193
194
195 class AString
196 #if ASTRING_IMPLEMENT_WITH_STD_STRING
197 :public std::string
198 #endif
199 {
200 public:
201 /**
202 Default constructor.
203 Construct an empty string
204 ****************************************************/
205 AString();
206 /**
207 Char *constructor.
208 Construct an string by copying a C String.
209 \param str source string
210 \param count Number of used caracters, if <0 means the full stream.
211 ****************************************************/
212 AString(const char *str,int count=-1);
213 /**
214 Copy constructor.
215 \param Src source string
216 ****************************************************/
217 AString(const AString &Src);
218 #if ASTRING_INTEROP_STD_STRING
219 /**
220 std::string constructor.
221 \param Src source string
222 ****************************************************/
223 AString(const std::string &Src);
224 /**
225 std::string cast operator.
226 To easyly convert an AString to a std::string
227 ****************************************************/
228 operator std::string () const;
229 #endif
230 /**
231 Assignement operator from AString.
232 \param Src source string
233 ****************************************************/
234 AString& operator =(const AString& Src);
235 #if __cplusplus > 199711L // C++ 11
236 #define ASTRING_NOEXCEPT noexcept
237 /**
238 C++ 11 Move construtor.
239 \param Src source string
240 ****************************************************/
241 AString(AString &&Src) ASTRING_NOEXCEPT;
242 /**
243 C++ 11 Assignement move operator .
244 \param Src source string
245 ****************************************************/
246 AString& operator =(AString&& Src) ASTRING_NOEXCEPT;
247 #else // C++ 98
248 #define ASTRING_NOEXCEPT throw()
249 #endif
250 /**
251 int constructor.
252 Construct a text representation of Value
253 \param value value to convert as text
254 ****************************************************/
255 AString(int value);
256 /**
257 unsigned constructor.
258 Construct a text representation of Value
259 \param value value to convert as text
260 ****************************************************/
261 AString(unsigned value);
262 /**
263 double constructor.
264 Construct a text representation of Value
265 \param value value to convert as text
266 ****************************************************/
267 AString(double value);
268 /**
269 destructor.
270 ****************************************************/
271 ~AString() ASTRING_NOEXCEPT;
272 #if ASTRING_INTEROP_BCB
273 AString(const AnsiString &src);
274 AString(const UnicodeString &src);
275 operator AnsiString ();
276 operator UnicodeString ();
277 #endif
278 #if ASTRING_INTEROP_VSS
279 AString(CStringA &src);
280 operator CStringA ();
281 AString(CStringW &src);
282 operator CStringW ();
283 #endif
284
285 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
286 /**
287 STL like iterator.
288 ****************************************************/
289 typedef char* iterator;
290 /**
291 STL like container begin.
292 return an iterator on the begining of the string
293 ****************************************************/
294 iterator begin();
295 /**
296 STL like container end.
297 return an iterator on the character following the end of the string
298 ****************************************************/
299 iterator end();
300 /**
301 get pointer on the string.
302 return a read/write pointer on the string\n
303 if you add or remove a '\0' char throught provided pointer you have to call RecomputeSize().
304 @return pointer on string
305 ****************************************************/
306 char* c_str();
307 /**
308 get a const pointer on the string.
309 return a read only pointer on the string\n
310 @return const pointer on string
311 ****************************************************/
312 const char* c_str() const ;
313 /**
314 get the number of character contained in the string.
315 @return character count
316 ****************************************************/
317 size_t size() const;
318 /**
319 get the maximum number of character that can be contained in the string whithout reallocatig memory.
320 @return string capacity
321 ****************************************************/
322 size_t capacity() const;
323 /**
324 Force the string to allocate supplementary space.
325 \param count new string capacity
326 ****************************************************/
327 void reserve(size_t count);
328 /**
329 Clear the string.
330 After this method call string is empty and Size=Capacity=0
331 ****************************************************/
332 void clear();
333
334 char& operator[](size_t idx);
335 char operator[](size_t idx) const;
336 char& at(size_t idx);
337 char at(size_t idx) const;
338 AString& assign(const char *str,int count=-1);
339
340 AString operator +(const AString &RightStr) const;
341 AString operator +(const char *RightStr) const;
342 friend AString operator +(const char *LeftStr,const AString &RightStr);
343 /**
344 Append a string to the current string.
345 If the current string has available space, it will be done by extending the current string.
346 It's more efficient than \c S1=S1+S2; \n
347 To be really efficient, its better to anticipage space requirement by using reserve()
348 \code
349 S1.reserve(20);
350 S1+="My String";
351 \endcode
352 \param RightStr the appended string
353 \return the current string
354 ****************************************************/
355 AString& operator +=(const AString &RightStr) ;
356 bool operator ==(const AString &RightStr) const;
357 friend bool operator ==(const char *LeftStr,const AString &RightStr);
358 bool operator ==(const char* RightStr) const;
359 bool operator !=(const AString &RightStr) const;
360 bool operator !=(const char *RightStr) const;
361 friend bool operator !=(const char *LeftStr,const AString &RightStr);
362 bool operator <(const AString &RightStr) const;
363 bool operator <(const char *RightStr) const;
364 bool operator >(const AString &RightStr) const;
365 bool operator >(const char *RightStr) const;
366 friend bool operator <(const char *LeftStr,const AString &RightStr);
367 friend bool operator >(const char *LeftStr,const AString &RightStr);
368 AString substr( size_t pos = 0,size_t count = npos ) const;
369 size_t find(const char* Searched,size_t StartPos=0,size_t count=0) const;
370 size_t find(char Searched,size_t StartPos=0) const;
371 #endif // ! ASTRING_IMPLEMENT_WITH_STD_STRING
372 /**
373 Replace a set or character by a string on the current string.
374 \param pos position where the substitition start
375 \param count number of character to substitute
376 \param str string to insert on the specified location
377 \return the current string
378 */
379 AString& replace( size_t pos, size_t count,const AString& str );
380 /**
381 Replace a substring by a new string on the current string.
382 \param src the substring to replace
383 \param str the string used to replace
384 \return the current string
385 */
386 AString& replace( const AString& src,const AString& str );
387 AString& rtrim(const char *CharsToRemove=" \r\n\t");
388 AString& ltrim(const char *CharsToRemove=" \r\n\t");
389 AString& trim(const char *CharsToRemove=" \r\n\t");
390 /**
391 Securely copy the current string on a C buffer.
392 If the string is longer than what can be hold on the destination buffer, string will
393 be truncated.\n
394 There's always an ending zero added on the destination buffer
395 \code
396 char buf[16];
397 AString S1("0123456789ABCDEF");
398 S1.strncpy(buf,4,5);
399 // buf == "567" +ending zero, then 4 characters
400 S1.strncpy(buf,5);
401 // buf=="0123"
402 S1.strncpy(buf,sizeof(buf),10);
403 // buf=="ABCDEF"
404 \endcode
405 \param dest destination C Buffer
406 \param destsize size of the buffer
407 \param pos position where the copy will occur
408 ****************************************************/
409 void strncpy( char* dest,size_t destsize,unsigned pos = 0) const;
410
411 /**
412 generate a string on a similar manner than sprintf.
413 And store the result on the current string
414 \code
415 S3.Format("Y %d %.02f %s",10,2.52,"DEF");
416 // S3 ="Y 10 2.52 DEF"
417 \endcode
418 \param Fmt format string silmilar to sprintf
419 \return Formated string
420 ****************************************************/
421 AString& Format(const char *Fmt,...);
422 friend AString Asprintf(const char *Fmt,...);
423
424 /** Recompute Size internal attribute based on Content.
425 * Required when using [] operator or direct buffer access you change the size by moving the terminal zero character.
426 * \return void
427 */
428 void RecomputeSize();
429
430 /** Convert the content of the string containg a textual representation of a number to an int.
431 * \return int
432 * \exception if the text doesn't represent an integer value.
433 */
434 int ToInt();
435 /** Convert the content of the string containg a textual representation of a number to an int an return defval
436 * \param defval default value if text can't be converted
437 * \return int
438 * \exception none
439 */
440 int ToIntDef(int defval);
441
442 #if !ASTRING_NOSTL
443 /**
444 operate similary with ostringstream.
445 Append to the Left string with the textual representation ofthe right parameter and return the current string.
446 \code
447 AString S2("A");
448 S2<<15<<" XXX "<<2.5<<" XXX "<<S1;
449 Result : S2 = "A 15 XXX 2.5 XXX 0123456789ABCDEF"), S2 );
450 \endcode
451 \param Streamable A variable that can be outputed on a stream as text
452 ****************************************************/
453 template <class T>
454 AString& operator<<(const T &Streamable);
455
456 /**
457 implode strings to a string whit a separator.
458 Merge/join/agregate a collection of string contained on a STL like container to an unique string
459 whith separator added between each string.
460 \param Separator the separator added between each string.
461 \param Ctnr an STL like container of AString : vector<AString>
462 ****************************************************/
463 template <class T>
464 static AString implode(AString Separator,const T &Ctnr);
465
466 /** explode a string in a collection of string (reverse of implode).
467 * Split a string in a collection of substring each time Separator is met.
468 Result must be a STL like container, deque is suggested.
469 * \param Separator the searched separator
470 * \param Result the target collection a deque<AString>
471 * \return the number if string returned = size of the result container.
472 \code
473 AString S3("Hello dear friends");
474 std::deque<AString> Ds;
475 unsigned res=S3.explode(" ",Ds);
476 // Result : res=3, Ds contains 3 values : "Hello" ,"dear", "friends"
477 \endcode
478 */
479 template <class ResultContainerType>
480 unsigned explode(AString Separator,ResultContainerType &Result);
481
482 #endif
483
484 #if ! ASTRING_IMPLEMENT_WITH_STD_STRING
485 static const size_t npos;
486
487 protected:
488 char* StrBuffer; /**< offset buffer */
489 char* GetRawBuffer();/**< the real address of the buffer */
490 unsigned& GetSize(); /**< R/W access to the offset position of size on the buffer */
491 unsigned& GetCapacity(); /**< R/W access to the offset position of capacity on the buffer */
492 unsigned GetSize() const; /**< Read Only access to the offset position of size on the buffer */
493 unsigned GetCapacity() const; /**< Read Only access to the offset position of capacity on the buffer */
494 static const char EmptyBuffer[4]; /**< Static const shared buffer for empty string*/
495 /** Allocate an internal buffer of a capacity of count character.
496 * + final zero + Control area or point to EmptyBuffer
497 * \param count requested capacity
498 * \return void
499 */
500 void AllocBuffer(unsigned count);
501 #endif
502 }; // class AString
503 #if !ASTRING_NOSTL
504 std::ostream& operator<<(std::ostream& os, const AString& str);
505 #endif
506 AString Asprintf(const char *Fmt,...);
507
508 /** AString Exception class, inherit from std::exception.
509 Can be inherited for your own exception with a AString contructor.
510 */
511 class AStringException
512 #if !ASTRING_NOSTL
513 : public std::exception
514 #endif
515 {
516 public:
517
518 /** Contruct the exception with a AString message
519 * \param _Msg Message of the exception
520 */
521 AStringException(AString _Msg):Msg(_Msg) {}
522 /** Return a char* on the message like std::exception does.
523 * \return the message
524 */
525 virtual const char* what() const ASTRING_NOEXCEPT
526 {return Msg.c_str();}
527 AString Msg;
528 };
529
530 //-------------------------------------------------------------------------------------
531 // Implementation on inline & template function
532 //-------------------------------------------------------------------------------------
533 #if !ASTRING_NOSTL
534 template <class T>
535 AString& AString::operator<<(const T &Streamable)
536 {
537 std::ostringstream ss;
538 ss<<Streamable;
539 *this+=ss.str().c_str();
540 return *this;
541 }
542 template <class T>
543 AString AString::implode(AString Separator,const T &Ctnr)
544 {
545 typename T::const_iterator it;
546 size_t StrLen=0;
547 AString res;
548 if(Ctnr.empty())
549 return res;
550 for(it=Ctnr.begin();it!=Ctnr.end();++it)
551 StrLen+=it->size()+Separator.size();
552 res.reserve(StrLen);
553 res+=*Ctnr.begin();
554 for(it=++Ctnr.begin();it!=Ctnr.end();++it)
555 {
556 res+=Separator;
557 res+=*it;
558 }
559 return res;
560 }
561
562 template <class ResultContainerType>
563 unsigned AString::explode(AString Separator,ResultContainerType &Result)
564 {
565 unsigned start=0;
566 Result.clear();
567 while(1)
568 {
569 size_t p=find(Separator.c_str(),start);
570 if(p==npos)
571 {
572 Result.push_back(substr(start));
573 return (unsigned int)Result.size();
574 }
575 else
576 {
577 Result.push_back(substr(start,(unsigned)p - start));
578 start=(unsigned)p+(unsigned)Separator.size();
579 }
580 }
581 }
582 #endif // !ASTRING_NOSTL
583
584 #endif // ASTRING_H_INCLUDED


AString C++ Ansi String Library: Data Structure Index

Data Structure Index
A
A
AStringException
AString
A


AString C++ Ansi String Library: D:/dev/_Altidev/cplusplus Directory Reference

  • D:
  • dev
  • _Altidev
  • cplusplus
cplusplus Directory Reference

Directories

directory AString


AString C++ Ansi String Library: D: Directory Reference

  • D:
D: Directory Reference

Directories

directory dev


AString C++ Ansi String Library: D:/dev/_Altidev/cplusplus/AString Directory Reference

  • D:
  • dev
  • _Altidev
  • cplusplus
  • AString
AString Directory Reference

Files

file | AString.cpp [code]
file | AString.h [code]


AString C++ Ansi String Library: D:/dev/_Altidev Directory Reference

  • D:
  • dev
  • _Altidev
_Altidev Directory Reference

Directories

directory cplusplus


AString C++ Ansi String Library: D:/dev Directory Reference

  • D:
  • dev
dev Directory Reference

Directories

directory _Altidev


AString C++ Ansi String Library: Related Pages

Related Pages
Here is a list of all related documentation pages:
\ Todo List