//================================================================================================== // S t a n d a r d Interface // B a s i c _ s t r i n g // By Bruno Bachelet //================================================================================================== // Copyright (c) 1999-2016 // Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net // // This file is part of the B++ Library. This library is free software; you can redistribute it // and/or modify it under the terms of the GNU Library General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your option) any later // version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU Library General Public License for more details (http://www.gnu.org).
/*DESCRIPTION*/ /* This module provides facilities to manipulate strings of characters. The existence of this module is due to the need of manipulating strings very early in the module hierarchy. */
// File Name //------------------------------------------------------------------------------------- #line __LINE__ "standard/basic_string.hpp"
// Guardian //-------------------------------------------------------------------------------------- #ifndef guStandardBasicString #define guStandardBasicString
// Headers //--------------------------------------------------------------------------------------- #include <bpp/standard/type.hpp> /*INCLUDE*/
namespace bpp {
// Importation/Exportation //----------------------------------------------------------------------- #ifdef STANDARD_DLL #define dll_export DLL_EXPORT #else #define dll_export DLL_IMPORT #endif
// Namespaces //------------------------------------------------------------------------------------ #define public_area standardBasicString #define private_area standardBasicString_private
namespace public_area { /*NAMESPACE*/ using namespace standardType; } namespace private_area { using namespace public_area; }
// Initialization //--------------------------------------------------------------------------------
// Macrocommands //---------------------------------------------------------------------------------
// Types & Classes //------------------------------------------------------------------------------- namespace public_area { //------------------------------------------------------------------------------------------Classes class clString; //-----------------------------------------------------------------------------------Constant Types typedef const clString ctString; }
namespace private_area { typedef std::istream clInStream; typedef std::ostream clOutStream; }
// Functions Interface //--------------------------------------------------------------------------- namespace public_area { function tyInteger compare(tcString,tcString); function void copy(tyString,tcString); function void lowerCase(tyString); function tyBoolean matched(tcString,tcString); function tyCardinal size(tcString); function void upperCase(tyString);
inline tyBoolean operator == (const clString &,const clString &); inline tyBoolean operator == (const clString &,tcString); inline tyBoolean operator == (tcString,const clString &);
inline tyBoolean operator != (const clString &,const clString &); inline tyBoolean operator != (const clString &,tcString); inline tyBoolean operator != (tcString,const clString &);
inline tyBoolean operator < (const clString &,const clString &); inline tyBoolean operator < (const clString &,tcString); inline tyBoolean operator < (tcString,const clString &);
inline tyBoolean operator > (const clString &,const clString &); inline tyBoolean operator > (const clString &,tcString); inline tyBoolean operator > (tcString,const clString &);
inline tyBoolean operator >= (const clString &,const clString &); inline tyBoolean operator >= (const clString &,tcString); inline tyBoolean operator >= (tcString,const clString &);
inline tyBoolean operator <= (const clString &,const clString &); inline tyBoolean operator <= (const clString &,tcString); inline tyBoolean operator <= (tcString,const clString &);
function clString operator + (const clString &,const clString &); function clString operator + (tcString,const clString &); function clString operator + (const clString &,tcString);
function private_area::clInStream & operator >> (private_area::clInStream &,clString &); function private_area::clOutStream & operator << (private_area::clOutStream &,ctString &);
function void getLine(private_area::clInStream &,clString &); function tyBoolean getString(private_area::clInStream &,clString &); }
namespace private_area {}
// Errors //---------------------------------------------------------------------------------------- namespace public_area {}
// Constants & Variables //------------------------------------------------------------------------- namespace public_area {} namespace private_area {}
// S t r i n g Interface //------------------------------------------------------------------------ namespace public_area { /*CLASS clString */ /* This class represents a string of characters. */ class clString { //-----------------------------------------------------------------------------------------Friends friend tyBoolean operator == (const clString &,const clString &); friend tyBoolean operator == (const clString &,tcString); friend tyBoolean operator == (tcString,const clString &);
friend tyBoolean operator != (const clString &,const clString &); friend tyBoolean operator != (const clString &,tcString); friend tyBoolean operator != (tcString,const clString &);
friend tyBoolean operator < (const clString &,const clString &); friend tyBoolean operator < (const clString &,tcString); friend tyBoolean operator < (tcString,const clString &);
friend tyBoolean operator > (const clString &,const clString &); friend tyBoolean operator > (const clString &,tcString); friend tyBoolean operator > (tcString,const clString &);
friend tyBoolean operator >= (const clString &,const clString &); friend tyBoolean operator >= (const clString &,tcString); friend tyBoolean operator >= (tcString,const clString &);
friend tyBoolean operator <= (const clString &,const clString &); friend tyBoolean operator <= (const clString &,tcString); friend tyBoolean operator <= (tcString,const clString &);
friend clString operator + (const clString &,const clString &); friend clString operator + (tcString,const clString &); friend clString operator + (const clString &,tcString);
friend private_area::clInStream & operator >> (private_area::clInStream &,clString &); friend private_area::clOutStream & operator << (private_area::clOutStream &,ctString &); //-----------------------------------------------------------------------------------------Private private_property void allocate(tyCardinal);
private_property tyCardinal atSize; //------------------------------------------------------------------------------------------Public /*ATTRIBUTE clString */ /* Array of characters that composes the string (ended by the null character). */ read_only_attribute(tyString,atData,data);
/*ATTRIBUTE clString */ /* Length of the string (without the null character). */ read_only_attribute(tyCardinal,atLength,size);
public_property explicit constructor clString(tyCardinal=0); public_property explicit constructor clString(tcString); public_property constructor clString(const clString &); public_property destructor clString(void);
public_property tyCharacter & operator [] (tyCardinal); public_property tyCharacter operator [] (tyCardinal) const;
public_property tyCharacter & at(tyCardinal); public_property tyCharacter at(tyCardinal) const;
public_property clString & operator = (tcString); public_property clString & operator = (const clString &);
public_property clString & operator += (tyCharacter); public_property clString & operator += (tcString); public_property clString & operator += (const clString &);
public_property void fill(tyCardinal,tyCardinal,tyCharacter); public_property tyBoolean find(tyCharacter,tyCardinal &); public_property clString part(tyCardinal,tyCardinal) const; public_property void reserve(tyCardinal); public_property void resize(tyCardinal,tyCharacter=' '); public_property void updateSize(void); }; }
// Functions Inline //------------------------------------------------------------------------------ namespace public_area { //--------------------------------------------------------------------------------------Operator == /*FUNCTION*/ /* Tests if two strings are the same. */ inline tyBoolean operator == (const clString & agString1,const clString & agString2) { return (compare(agString1.atData,agString2.atData)==0); } //--------------------------------------------------------------------------------------Operator == /*FUNCTION*/ /* Tests if two strings are the same. */ inline tyBoolean operator == (const clString & agString1,tcString agString2) { return (compare(agString1.atData,agString2)==0); } //--------------------------------------------------------------------------------------Operator == /*FUNCTION*/ /* Tests if two strings are the same. */ inline tyBoolean operator == (tcString agString1,const clString & agString2) { return (compare(agString1,agString2.atData)==0); } //--------------------------------------------------------------------------------------Operator != /*FUNCTION*/ /* Tests if two strings are different. */ inline tyBoolean operator != (const clString & agString1,const clString & agString2) { return (compare(agString1.atData,agString2.atData)!=0); } //--------------------------------------------------------------------------------------Operator != /*FUNCTION*/ /* Tests if two strings are different. */ inline tyBoolean operator != (const clString & agString1,tcString agString2) { return (compare(agString1.atData,agString2)!=0); } //--------------------------------------------------------------------------------------Operator != /*FUNCTION*/ /* Tests if two strings are different. */ inline tyBoolean operator != (tcString agString1,const clString & agString2) { return (compare(agString1,agString2.atData)!=0); } //---------------------------------------------------------------------------------------Operator < /*FUNCTION*/ /* Tests if a string is before another one according to the lexicographical order. */ inline tyBoolean operator < (const clString & agString1,const clString & agString2) { return (compare(agString1.atData,agString2.atData)==-1); } //---------------------------------------------------------------------------------------Operator < /*FUNCTION*/ /* Tests if a string is before another one according to the lexicographical order. */ inline tyBoolean operator < (const clString & agString1,tcString agString2) { return (compare(agString1.atData,agString2)==-1); } //---------------------------------------------------------------------------------------Operator < /*FUNCTION*/ /* Tests if a string is before another one according to the lexicographical order. */ inline tyBoolean operator < (tcString agString1,const clString & agString2) { return (compare(agString1,agString2.atData)==-1); } //---------------------------------------------------------------------------------------Operator > /*FUNCTION*/ /* Tests if a string is after another one according to the lexicographical order. */ inline tyBoolean operator > (const clString & agString1,const clString & agString2) { return (compare(agString1.atData,agString2.atData)==1); } //---------------------------------------------------------------------------------------Operator > /*FUNCTION*/ /* Tests if a string is after another one according to the lexicographical order. */ inline tyBoolean operator > (const clString & agString1,tcString agString2) { return (compare(agString1.atData,agString2)==1); } //---------------------------------------------------------------------------------------Operator > /*FUNCTION*/ /* Tests if a string is after another one according to the lexicographical order. */ inline tyBoolean operator > (tcString agString1,const clString & agString2) { return (compare(agString1,agString2.atData)==1); } //--------------------------------------------------------------------------------------Operator >= /*FUNCTION*/ /* Tests if a string is after or the same as another one according to the lexicographical order. */ inline tyBoolean operator >= (const clString & agString1,const clString & agString2) { return (compare(agString1.atData,agString2.atData)>=0); } //--------------------------------------------------------------------------------------Operator >= /*FUNCTION*/ /* Tests if a string is after or the same as another one according to the lexicographical order. */ inline tyBoolean operator >= (const clString & agString1,tcString agString2) { return (compare(agString1.atData,agString2)>=0); } //--------------------------------------------------------------------------------------Operator >= /*FUNCTION*/ /* Tests if a string is after or the same as another one according to the lexicographical order. */ inline tyBoolean operator >= (tcString agString1,const clString & agString2) { return (compare(agString1,agString2.atData)>=0); } //--------------------------------------------------------------------------------------Operator <= /*FUNCTION*/ /* Tests if a string is before or the same as another one according to the lexicographical order. */ inline tyBoolean operator <= (const clString & agString1,const clString & agString2) { return (compare(agString1.atData,agString2.atData)<=0); } //--------------------------------------------------------------------------------------Operator <= /*FUNCTION*/ /* Tests if a string is before or the same as another one according to the lexicographical order. */ inline tyBoolean operator <= (const clString & agString1,tcString agString2) { return (compare(agString1.atData,agString2)<=0); } //--------------------------------------------------------------------------------------Operator <= /*FUNCTION*/ /* Tests if a string is before or the same as another one according to the lexicographical order. */ inline tyBoolean operator <= (tcString agString1,const clString & agString2) { return (compare(agString1,agString2.atData)<=0); } }
namespace private_area {}
// S t r i n g Inline //--------------------------------------------------------------------------- namespace public_area { //--------------------------------------------------------------------------------------Constructor /*METHOD clString */ /* Builds an empty string with a given number of characters reserved. */ inline clString::clString(tyCardinal agSize) : atSize(0),atData(nil),atLength(0) { allocate(agSize); } //--------------------------------------------------------------------------------------Constructor /*METHOD clString */ /* Builds a copy of a string. */ inline clString::clString(tcString agString) : atSize(0),atData(nil),atLength(0) { tyCardinal lcLength = public_area::size(agString); allocate(lcLength); copy(atData,agString); atLength=lcLength; } //--------------------------------------------------------------------------------------Constructor /*METHOD clString */ /* Builds a copy of a string. */ inline clString::clString(const clString & agString) : atSize(0),atData(nil),atLength(0) { allocate(agString.atLength); copy(atData,agString.atData); atLength=agString.atLength; } //---------------------------------------------------------------------------------------Destructor /*METHOD clString */ /* Destructs a string. */ inline clString::~clString(void) { if (atData!=nil) delete [] atData; } //--------------------------------------------------------------------------------------Operator [] /*METHOD clString */ /* Returns the character at a given position. The position 0 is the first character. */ inline tyCharacter & clString::operator [] (tyCardinal agPosition) { return (atData[agPosition]); } //--------------------------------------------------------------------------------------Operator [] /*METHOD clString */ /* Returns the character at a given position. The position 0 is the first character. */ inline tyCharacter clString::operator [] (tyCardinal agPosition) const { return (atData[agPosition]); } //-----------------------------------------------------------------------------------------------At /*METHOD clString */ /* Returns the character at a given position. The position 0 is the first character. The validity of the position is verified. */ inline tyCharacter & clString::at(tyCardinal agPosition) { if (agPosition>=atLength) throw("Basic String - Invalid character position."); return (atData[agPosition]); } //-----------------------------------------------------------------------------------------------At /*METHOD clString */ /* Returns the character at a given position. The position 0 is the first character. The validity of the position is verified. */ inline tyCharacter clString::at(tyCardinal agPosition) const { if (agPosition>=atLength) throw("Basic String - Invalid character position."); return (atData[agPosition]); } //---------------------------------------------------------------------------------------Operator = /*METHOD clString */ /* Copies a string. */ inline clString & clString::operator = (tcString agString) { tyCardinal lcLength = public_area::size(agString);
allocate(lcLength); copy(atData,agString); atLength=lcLength; return (*this); } //---------------------------------------------------------------------------------------Operator = /*METHOD clString */ /* Copies a string. */ inline clString & clString::operator = (const clString & agString) { allocate(agString.atLength); copy(atData,agString.atData); atLength=agString.atLength; return (*this); } //--------------------------------------------------------------------------------------Operator += /*METHOD clString */ /* Appends a character at the end of the string. */ inline clString & clString::operator += (tyCharacter agCharacter) { reserve(atLength+1); atData[atLength++]=agCharacter; atData[atLength]=end_string; return (*this); } //--------------------------------------------------------------------------------------Operator += /*METHOD clString */ /* Appends a string at the end of the string. */ inline clString & clString::operator += (tcString agString) { tyCardinal lcLength = public_area::size(agString);
reserve(atLength+lcLength); copy(atData+atLength,agString); atLength+=lcLength; return (*this); } //--------------------------------------------------------------------------------------Operator += /*METHOD clString */ /* Appends a string at the end of the string. */ inline clString & clString::operator += (const clString & agString) { reserve(atLength+agString.atLength); copy(atData+atLength,agString.atData); atLength+=agString.atLength; return (*this); } //---------------------------------------------------------------------------------------UpdateSize /*METHOD clString */ /* Updates the length of the string by finding the first null character. */ inline void clString::updateSize(void) { atLength=public_area::size(atData); } }
// End //------------------------------------------------------------------------------------------- } #undef dll_export #undef public_area #undef private_area #endif |
//================================================================================================== // S t a n d a r d Implementation // B a s i c _ s t r i n g // By Bruno Bachelet //================================================================================================== // Copyright (c) 1999-2016 // Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net // // This file is part of the B++ Library. This library is free software; you can redistribute it // and/or modify it under the terms of the GNU Library General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your option) any later // version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU Library General Public License for more details (http://www.gnu.org).
// File Name //------------------------------------------------------------------------------------- #line __LINE__ "standard/basic_string.cpp"
// DLL Belonging //--------------------------------------------------------------------------------- #define STANDARD_DLL
// Headers //--------------------------------------------------------------------------------------- #include <cctype> /*NEED*/ #include <bpp/standard/basic_string.hpp> /*INTERFACE*/
namespace bpp {
// Namespaces //------------------------------------------------------------------------------------ #define public_area standardBasicString #define private_area standardBasicString_private #define dll_export DLL_EXPORT
namespace public_area {} namespace private_area {}
// Initialization //--------------------------------------------------------------------------------
// Errors //---------------------------------------------------------------------------------------- namespace public_area {}
// Constants & Variables //------------------------------------------------------------------------- namespace public_area {} namespace private_area {}
// Static Members //-------------------------------------------------------------------------------- namespace public_area {} namespace private_area {}
// Functions Implementation //---------------------------------------------------------------------- namespace public_area { //------------------------------------------------------------------------------------------Compare /*FUNCTION*/ /* Compares two strings. */ function tyInteger compare(tcString agString1,tcString agString2) { if (agString1==nil or agString1[0]==end_string) { if (agString2==nil or agString2[0]==end_string) return (0); else return (-1); } else if (agString2==nil or agString2[0]==end_string) return (1);
while (agString1[0]!=end_string and agString2[0]!=end_string and agString1[0]==agString2[0]) { agString1++; agString2++; }
if (agString1[0]==agString2[0]) return (0); else if (agString1[0]<agString2[0]) return (-1); return (1); } //---------------------------------------------------------------------------------------------Copy /*FUNCTION*/ /* Copies a string. */ function void copy(tyString agString1,tcString agString2) { if (agString2==nil) agString1[0]=end_string; else { while (agString2[0]!=end_string) *(agString1++)=*(agString2++); agString1[0]=end_string; } } //----------------------------------------------------------------------------------------LowerCase /*FUNCTION*/ /* Converts a string into lower cases. */ function void lowerCase(tyString agString) { if (agString==nil) return;
while (agString[0]!=end_string) { if (agString[0]>='A' and agString[0]<='Z') agString[0]=tyCharacter(tyCharacter(agString[0]-'A')+'a');
agString++; } } //------------------------------------------------------------------------------------------Matched /*FUNCTION*/ /* Tests if a string matches a format. */ function tyBoolean matched(tcString agString,tcString agFormat) { tyCardinal lcLength = size(agFormat)+1;
tcString * lcFormatS = new tcString [lcLength]; tyCardinal * lcJockerS = new tyCardinal [lcLength]; tcString * lcStringS = new tcString [lcLength];
tyBoolean lcAgain = true; tyBoolean lcMatched = true; tyCardinal lcTop = 0;
tyCharacter lcCharacter;
if (agFormat==nil) throw("Standard String - The format string is 'nil'."); if (agString==nil) agString="";
while (lcAgain and lcMatched) { lcCharacter=*agFormat;
if (lcCharacter==*agString) { if (lcCharacter!=end_string) { agFormat++; agString++; } else lcAgain=false; } else if (lcCharacter=='?') { lcJockerS[lcTop]=1; while (*(++agFormat)=='?') lcJockerS[lcTop]++; lcStringS[lcTop]=agString; lcFormatS[lcTop]=agFormat; lcTop++; } else if (lcCharacter=='*' or lcCharacter=='%') { while (*(++agFormat)=='*' or *agFormat=='%'); lcJockerS[lcTop]=cardinalMax(); lcStringS[lcTop]=agString; lcFormatS[lcTop]=agFormat; lcTop++; } else if (lcTop==0) lcMatched=false; else if (lcStringS[lcTop-1][0]!=end_string and lcJockerS[lcTop-1]>0) { agFormat=lcFormatS[lcTop-1]; agString=++lcStringS[lcTop-1]; lcJockerS[lcTop-1]--; } else { lcTop--; if (lcTop==0) lcMatched=false; else { agFormat=lcFormatS[lcTop-1]; agString=++lcStringS[lcTop-1]; if (lcJockerS[lcTop-1]>0) lcJockerS[lcTop-1]--; } } }
delete [] lcFormatS; delete [] lcJockerS; delete [] lcStringS;
return (lcMatched); } //---------------------------------------------------------------------------------------------Size /*FUNCTION*/ /* Returns the size of a string (without the null character). */ function tyCardinal size(tcString agString) { tcString lcBegin = agString;
if (agString==nil) return (0); while (agString[0]!=end_string) agString++; return (agString-lcBegin); } //----------------------------------------------------------------------------------------UpperCase /*FUNCTION*/ /* Converts a string into upper cases. */ function void upperCase(tyString agString) { if (agString==nil) return;
while (agString[0]!=end_string) { if (agString[0]>='a' and agString[0]<='z') agString[0]=tyCharacter(tyCharacter(agString[0]-'a')+'A');
agString++; } } //---------------------------------------------------------------------------------------Operator + /*FUNCTION*/ /* Concatenates two strings. */ function clString operator + (const clString & agString1,const clString & agString2) { clString lcString(agString1.atLength+agString2.atLength);
copy(lcString.atData,agString1.atData); copy(lcString.atData+agString1.atLength,agString2.atData); lcString.atLength=lcString.atSize; return (lcString); } //---------------------------------------------------------------------------------------Operator + /*FUNCTION*/ /* Concatenates two strings. */ function clString operator + (const clString & agString1,tcString agString2) { tyCardinal lcLength = size(agString2);
clString lcString(agString1.atLength+lcLength);
copy(lcString.atData,agString1.atData); copy(lcString.atData+agString1.atLength,agString2); lcString.atLength=lcString.atSize; return (lcString); } //---------------------------------------------------------------------------------------Operator + /*FUNCTION*/ /* Concatenates two strings. */ function clString operator + (tcString agString1,const clString & agString2) { tyCardinal lcLength = size(agString1);
clString lcString(lcLength+agString2.atLength);
copy(lcString.atData,agString1); copy(lcString.atData+lcLength,agString2.atData); lcString.atLength=lcString.atSize; return (lcString); } //--------------------------------------------------------------------------------------Operator << /*FUNCTION*/ /* Writes a string into a stream. */ function private_area::clOutStream & operator << (private_area::clOutStream & agStream,ctString & agString) { agStream << agString.atData; return (agStream); } //--------------------------------------------------------------------------------------Operator >> /*FUNCTION*/ /* Reads a string from a stream. */ function private_area::clInStream & operator >> (private_area::clInStream & agStream,clString & agString) { tyCardinal lcCounter1 = 0; tyCardinal lcCounter2 = 0;
tyCharacter lcCharacter; tyCharacter lcString[257];
lcString[256]=end_string; agString="";
if (not agStream.eof() and agStream.peek()!=-1) agStream.get(lcCharacter);
while (not agStream.eof() and agStream.peek()!=-1 and isspace(lcCharacter)) agStream.get(lcCharacter);
while (not agStream.eof() and agStream.peek()!=-1 and lcCounter1<cardinalMax() and not isspace(lcCharacter)) { lcString[lcCounter2++]=lcCharacter; agStream.get(lcCharacter);
if (lcCounter2==256) { agString+=lcString; lcCounter2=0; }
lcCounter1++; }
if (lcCounter2!=0) { lcString[lcCounter2]=end_string; agString+=lcString; }
return (agStream); } //------------------------------------------------------------------------------------------GetLine /*FUNCTION*/ /* Reads a line from a stream. */ function void getLine(private_area::clInStream & agStream,clString & agString) { tyBoolean lcStop; clString lcString;
lcStop=getString(agStream,agString);
while (not lcStop) { lcStop=getString(agStream,lcString); if (lcString!="") agString+=" "+lcString; } } //----------------------------------------------------------------------------------------GetString /*FUNCTION*/ /* Reads a string from a stream and returns <CODE>true</CODE> if the end of the line is reached. */ function tyBoolean getString(private_area::clInStream & agStream,clString & agString) { tyBoolean lcAgain = true; tyCardinal lcCounter1 = 0; tyCardinal lcCounter2 = 0;
tyCharacter lcCharacter; tyCharacter lcString[257];
lcString[256]=end_string; agString="";
if (agStream.peek()!=-1) agStream.get(lcCharacter); else lcAgain=false;
while (lcAgain and isspace(lcCharacter)) { if (agStream.peek()!=-1) agStream.get(lcCharacter); else lcAgain=false; }
while (lcAgain and lcCounter1<cardinalMax() and not isspace(lcCharacter)) { lcString[lcCounter2++]=lcCharacter;
if (agStream.peek()!=-1) agStream.get(lcCharacter); else lcAgain=false;
if (lcCounter2==256) { agString+=lcString; lcCounter2=0; }
lcCounter1++; }
if (lcCounter2!=0) { lcString[lcCounter2]=end_string; agString+=lcString; }
if (agStream.peek()==-1 or agStream.peek()==10 or agStream.peek()==13 or lcCharacter==10 or lcCharacter==13) return (true); else return (false); } }
namespace private_area {}
// S t r i n g Implementation //------------------------------------------------------------------- namespace public_area { //-----------------------------------------------------------------------------------------Allocate property void clString::allocate(tyCardinal agSize) { if (atData!=nil) delete [] atData; atData=new tyCharacter [agSize+1]; if (atData==nil) throw("Basic String - Out of memory."); atSize=agSize; atLength=0; atData[0]=end_string; } //---------------------------------------------------------------------------------------------Fill /*METHOD clString */ /* Fills the string with a given character from a given position to another one. */ property void clString::fill(tyCardinal agBegin,tyCardinal agEnd,tyCharacter agCharacter) { while (agBegin<=agEnd and agBegin<atLength) atData[agBegin++]=agCharacter; } //---------------------------------------------------------------------------------------------Find /*METHOD clString */ /* Finds a given character in the string. <CODE>false</CODE> is returned if no character is found, else the position of the first character found is stored in the last parameter of the method. */ property tyBoolean clString::find(tyCharacter agCharacter,tyCardinal & agPosition) { agPosition=0; if (atData==nil) return (false); while (atData[agPosition]!=agCharacter and atData[agPosition]!=end_string) agPosition++; return (atData[agPosition]==agCharacter); } //---------------------------------------------------------------------------------------------Part /*METHOD clString */ /* Returns a part of the string, starting at a given position and having a given length. */ property clString clString::part(tyCardinal agBegin,tyCardinal agLength) const { tyCardinal lcEnd = agBegin+agLength; tyCharacter lcCharacter = atData[lcEnd]; clString lcString;
atData[lcEnd]=end_string; lcString=atData+agBegin; atData[lcEnd]=lcCharacter; return (lcString); } //------------------------------------------------------------------------------------------Reserve /*METHOD clString */ /* Reserves characters for the string (the null character is not counted). */ property void clString::reserve(tyCardinal agSize) { tyString lcString=atData;
if (agSize>atSize) { atSize=agSize; atData=new tyCharacter [agSize+1]; if (atData==nil) throw("Basic String - Out of memory.");
if (lcString==nil) atData[0]=end_string; else { copy(atData,lcString); delete [] lcString; } } } //-------------------------------------------------------------------------------------------Resize /*METHOD clString */ /* Adjusts the size of the string. If the given length is less than the current one, the string is truncated. If the given length is more than the current one, new characters are added to fit the given length. */ property void clString::resize(tyCardinal agLength,tyCharacter agCharacter) { if (agLength>atLength) { reserve(agLength); fill(atLength,agLength,agCharacter); }
atLength=agLength; atData[atLength]=end_string; } }
// End //------------------------------------------------------------------------------------------- } |
|