| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335 |
- /*
- ** Command & Conquer Generals(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program 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 General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /******************************************************************************
- *
- * FILE
- * $Archive: $
- *
- * DESCRIPTION
- * String management class (Unicode)
- *
- * PROGRAMMER
- * Denzil E. Long, Jr.
- * $Author: $
- *
- * VERSION INFO
- * $Modtime: $
- * $Revision: $
- *
- ******************************************************************************/
- #include "VisualC.h"
- #include "UString.h"
- #include "StringConvert.h"
- #include <string.h>
- #include <stdlib.h>
- #include <assert.h>
- // Convert character to lowercase
- template<typename T> T CharToLower(const T ch)
- {
- if ((ch >= (T)'A') && (ch <= (T)'Z'))
- {
- return (ch + 32);
- }
- return ch;
- }
- // Convert character to uppercase
- template<typename T> T CharToUpper(const T ch)
- {
- if ((ch >= (T)'a') && (ch <= (T)'z'))
- {
- return (ch - 32);
- }
- return ch;
- }
- // Check if character is one of the specified characters
- template<typename T>bool IsCharacter(WChar ch, const T* oneOf)
- {
- assert(oneOf != NULL);
- int length = 0;
- while (oneOf[length] != 0)
- {
- length++;
- }
- for (int index = 0; index < length; index++)
- {
- if (ch == (WChar)oneOf[index])
- {
- return true;
- }
- }
- return false;
- }
- // Strip all left side characters that are trim chars
- template<typename T> bool StripLeft(WChar* string, const T* trimChars)
- {
- // Strip leading trim characters from the string.
- WChar* start = string;
- while ((*start != 0) && IsCharacter<T>(*start, trimChars))
- {
- start++;
- }
- if (start != string)
- {
- wcscpy(string, start);
- }
- return true;
- }
- // Strip all right side characters that are trim chars
- template<typename T> bool StripRight(WChar* string, const T* trimChars)
- {
- int length = wcslen(string) - 1;
- int index = length;
- while (index >= 0)
- {
- if (!IsCharacter<T>(string[index], trimChars))
- {
- break;
- }
- string[index] = 0;
- index--;
- }
- return (index != length);
- }
- /******************************************************************************
- *
- * NAME
- * UString::UString - Default constructor
- *
- * DESCRIPTION
- * Create a new empty UString.
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- UString::UString()
- : mData(NULL),
- mCapacity(0)
- {
- }
- /******************************************************************************
- *
- * NAME
- * UString::UString - Capacity contructor
- *
- * DESCRIPTION
- * Create a new empty UString with the specified capacity.
- *
- * INPUTS
- * Capacity - Number of characters to allocated to string.
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- UString::UString(UInt capacity)
- : mData(NULL),
- mCapacity(0)
- {
- AllocString(capacity);
- }
- /******************************************************************************
- *
- * NAME
- * UString::UString - ANSI string literal constructor
- *
- * DESCRIPTION
- * Create a new UString from an ANSI string literal
- *
- * INPUTS
- * String - Pointer to a NULL terminated ANSI string
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- UString::UString(const Char* s)
- : mData(NULL),
- mCapacity(0)
- {
- Copy(s);
- }
- /******************************************************************************
- *
- * NAME
- * UString::UString - UNICODE string literal constructor
- *
- * DESCRIPTION
- * Create a new UString from a UNICODE string literal
- *
- * INPUTS
- * String - Pointer to a NULL terminated UNICODE string
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- UString::UString(const WChar* ws)
- : mData(NULL),
- mCapacity(0)
- {
- Copy(ws);
- }
- /******************************************************************************
- *
- * NAME
- * UString::UString - Copy constructor
- *
- * DESCRIPTION
- * Create a new UString from another UString
- *
- * INPUTS
- * String - Reference to UString to copy
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- UString::UString(const UString& s)
- : mData(NULL),
- mCapacity(0)
- {
- Copy(s);
- }
- /******************************************************************************
- *
- * NAME
- * UString::~UString - Destructor
- *
- * DESCRIPTION
- * Destroy the object
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- UString::~UString()
- {
- if (mData != NULL)
- {
- delete mData;
- }
- }
- /******************************************************************************
- *
- * NAME
- * UString::Length
- *
- * DESCRIPTION
- * Retrieve the length of the string in characters.
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * Length - Length of string
- *
- ******************************************************************************/
- UInt UString::Length(void) const
- {
- if (mData == NULL)
- {
- return 0;
- }
- return wcslen(mData);
- }
- /******************************************************************************
- *
- * NAME
- * UString::Copy - ANSI string literal
- *
- * DESCRIPTION
- * Copy ANSI string literal.
- *
- * INPUTS
- * String - Pointer to ANSI string literal
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Copy(const Char* s)
- {
- assert(s != NULL);
- UInt length = strlen(s);
- if (length == 0)
- {
- return;
- }
- if (Capacity() < length)
- {
- AllocString(length);
- }
- // Copy and convert ansi string to unicode
- assert(Capacity() >= length);
- WChar* wsPtr = mData;
- const Char* sPtr = s;
- while (length-- > 0)
- {
- *wsPtr++ = (WChar)*sPtr++;
- }
- *wsPtr = 0;
- }
- /******************************************************************************
- *
- * NAME
- * UString::Copy - Unicode string literal
- *
- * DESCRIPTION
- * Copy a Unicode string literal
- *
- * INPUTS
- * String - Pointer to UNICODE string literal
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Copy(const WChar* ws)
- {
- assert(ws != NULL);
- UInt length = wcslen(ws);
- if (length == 0)
- {
- return;
- }
- if (Capacity() < length)
- {
- AllocString(length);
- }
- assert(Capacity() >= length);
- wcscpy(mData, ws);
- }
- /******************************************************************************
- *
- * NAME
- * UString::Copy
- *
- * DESCRIPTION
- * Copy string
- *
- * INPUTS
- * String - Reference to UString
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Copy(const UString& s)
- {
- Copy(s.Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::Concat - ANSI string literal
- *
- * DESCRIPTION
- * Concatenate an ANSI string literal to this string
- *
- * INPUTS
- * String - Pointer to ANSI string literal
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Concat(const Char* s)
- {
- // Parameter check
- assert(s != NULL);
- UInt length = Length();
- UInt additional = strlen(s);
- UInt totalLength = (length + additional);
- // Resize the string if the combined size is to small
- if (Capacity() < totalLength)
- {
- Resize(totalLength);
- }
- // Concatenate and convert ansi string to unicode
- WChar* wsPtr = &mData[length];
- const Char* sPtr = s;
- while (additional-- > 0)
- {
- *wsPtr++ = (WChar)*sPtr++;
- }
- *wsPtr = 0;
- }
- /******************************************************************************
- *
- * NAME
- * UString::Concat - Unicode string literal
- *
- * DESCRIPTION
- * Concatenate a Unicode string literal to this string
- *
- * INPUTS
- * String - Pointer to UNICODE string literal
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Concat(const WChar* ws)
- {
- assert(ws != NULL);
- UInt length = (Length() + wcslen(ws));
- if (Capacity() < length)
- {
- Resize(length);
- }
- wcscat(mData, ws);
- }
- /******************************************************************************
- *
- * NAME
- * UString::Concat
- *
- * DESCRIPTION
- * Concatenate string
- *
- * INPUTS
- * String - Reference to UString
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Concat(const UString& s)
- {
- Concat(s.Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::Compare - ANSI string literal
- *
- * DESCRIPTION
- * Compare strings, returning a value representing their relationship.
- *
- * INPUTS
- * String - String to compare against
- *
- * RESULT
- * Relationship - < 0 String less than this.
- * = 0 Strings identical
- * > 0 String greater than this.
- *
- ******************************************************************************/
- Int UString::Compare(const Char* s) const
- {
- // If comparing string is NULL and this string is NULL then strings are equal,
- // otherwise comparing string is less than this string.
- if (s == NULL)
- {
- if (Get() == NULL)
- {
- return 0;
- }
- return -1;
- }
- // If this string is NULL then comparing string is greater
- if (Get() == NULL)
- {
- return 1;
- }
- // Compare each character
- const WChar* ws = Get();
- Int index = 0;
- for (;;)
- {
- // Difference between characters
- Int diff = ((WChar)s[index] - ws[index]);
- // If the difference is not zero then the characters differ
- if (diff != 0)
- {
- return diff;
- }
- // If the end of either string has been reached then terminate loop
- if ((ws[index] == L'\0') || (s[index] == '\0'))
- {
- break;
- }
- // Advance to next character
- index++;
- }
- return 0;
- }
- /******************************************************************************
- *
- * NAME
- * UString::Compare - Unicode string literal
- *
- * DESCRIPTION
- * Compare strings, returning a value representing their relationship.
- *
- * INPUTS
- * String - String to compare against
- *
- * RESULT
- * Relationship - < 0 String less than this.
- * = 0 Strings identical
- * > 0 String greater than this.
- *
- ******************************************************************************/
- Int UString::Compare(const WChar* ws) const
- {
- return wcscmp(ws, Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::Compare
- *
- * DESCRIPTION
- * Compare strings, returning a value representing their relationship.
- *
- * INPUTS
- * String - String to compare against
- *
- * RESULT
- * Relationship - < 0 String less than this.
- * = 0 Strings identical
- * > 0 String greater than this.
- *
- ******************************************************************************/
- Int UString::Compare(const UString& s) const
- {
- return Compare(s.Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::CompareNoCase - ANSI string literal
- *
- * DESCRIPTION
- * Compare strings (case insensitive), returning a value representing their
- * relationship.
- *
- * INPUTS
- * String - String to compare against
- *
- * RESULT
- * Relationship - < 0 String less than this.
- * = 0 Strings identical
- * > 0 String greater than this.
- *
- ******************************************************************************/
- Int UString::CompareNoCase(const Char* s) const
- {
- // If comparing string is NULL and this string is NULL then strings are
- // equal, otherwise comparing string is less than this string.
- if (s == NULL)
- {
- if (Get() == NULL)
- {
- return 0;
- }
- return -1;
- }
- // If this string is NULL then comparing string is greater.
- if (Get() == NULL)
- {
- return 1;
- }
- // Compare each character
- const WChar* ws = Get();
- Int index = 0;
- for (;;)
- {
- // Convert to lowercase for compare
- WChar sc = (WChar)CharToLower<Char>(s[index]);
- WChar wc = CharToLower<WChar>(ws[index]);
-
- // Difference between characters.
- Int diff = (sc - wc);
- // If the difference is not zero then the characters differ.
- if (diff != 0)
- {
- return diff;
- }
- // If the end of either string has been reached then terminate loop.
- if ((ws[index] == L'\0') || (s[index] == '\0'))
- {
- break;
- }
- // Advance to next character
- index++;
- }
- return 0;
- }
- /******************************************************************************
- *
- * NAME
- * UString::CompareNoCase - Unicode string literal
- *
- * DESCRIPTION
- * Compare strings (case insensitive), returning a value representing their
- * relationship.
- *
- * INPUTS
- * String - String to compare against
- *
- * RESULT
- * Relationship - < 0 String less than this.
- * = 0 Strings identical
- * > 0 String greater than this.
- *
- ******************************************************************************/
- Int UString::CompareNoCase(const WChar* ws) const
- {
- return wcsicmp(ws, Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::CompareNoCase
- *
- * DESCRIPTION
- * Compare strings (case insensitive), returning a value representing their
- * relationship.
- *
- * INPUTS
- * String - String to compare against
- *
- * RESULT
- * Relationship - < 0 String less than this.
- * = 0 Strings identical
- * > 0 String greater than this.
- *
- ******************************************************************************/
- Int UString::CompareNoCase(const UString& s) const
- {
- return CompareNoCase(s.Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::Find - ANSI character
- *
- * DESCRIPTION
- * Find the first occurance of character
- *
- * INPUTS
- * Char - ANSI character to search for
- *
- * RESULT
- * Position - Position of character (-1 if not found)
- *
- ******************************************************************************/
- Int UString::Find(Char c) const
- {
- return Find((WChar)c);
- }
- /******************************************************************************
- *
- * NAME
- * UString::Find - Unicode character
- *
- * DESCRIPTION
- * Find the first occurance of character
- *
- * INPUTS
- * Char - Unicode character to search for.
- *
- * RESULT
- * Position - Position of character (-1 if not found)
- *
- ******************************************************************************/
- Int UString::Find(WChar c) const
- {
- WChar* ptr = wcschr(Get(), c);
- // Not found?
- if (ptr == NULL)
- {
- return -1;
- }
- return ((ptr - mData) / sizeof(WChar));
- }
- /******************************************************************************
- *
- * NAME
- * UString::FindLast - ANSI character
- *
- * DESCRIPTION
- * Find the last occurance of a character
- *
- * INPUTS
- * Char - ANSI character
- *
- * RESULT
- * Position - Position of character (-1 if not found)
- *
- ******************************************************************************/
- Int UString::FindLast(Char c) const
- {
- return FindLast((WChar)c);
- }
- /******************************************************************************
- *
- * NAME
- * UString::FindLast - Unicode character
- *
- * DESCRIPTION
- * Find the last occurance of a character
- *
- * INPUTS
- * Char - Unicode character
- *
- * RESULT
- * Position - Position of character (-1 if not found)
- *
- ******************************************************************************/
- Int UString::FindLast(WChar c) const
- {
- assert(mData != NULL);
- WChar* ptr = wcsrchr(mData, (WChar)c);
- // Not found?
- if (ptr == NULL)
- {
- return -1;
- }
- return ((ptr - mData) / sizeof(WChar));
- }
- /******************************************************************************
- *
- * NAME
- * UString::SubString - ANSI string literal
- *
- * DESCRIPTION
- * Find a substring
- *
- * INPUTS
- * SubString - Substring to search for.
- *
- * RESULT
- *
- *
- ******************************************************************************/
- UString UString::SubString(const Char* s)
- {
- assert(false);
- assert(s != NULL);
- return UString("");
- }
- UString UString::SubString(const WChar* ws)
- {
- assert(false);
- assert(ws != NULL);
- return UString("");
- }
- UString UString::SubString(const UString& s)
- {
- assert(false);
- return SubString(s.mData);
- }
- /******************************************************************************
- *
- * NAME
- * UString::Left
- *
- * DESCRIPTION
- * Extract left part of the string.
- *
- * INPUTS
- * Count - Number of characters to extract
- *
- * RESULT
- * Left - Extracted left part of the string
- *
- ******************************************************************************/
- UString UString::Left(UInt count)
- {
- assert(false);
- // If the count is zero then return an empty string.
- if ((Length() == 0) || (count == 0))
- {
- return UString("");
- }
- return UString("");
- }
- /******************************************************************************
- *
- * NAME
- * UString::Middle
- *
- * DESCRIPTION
- * Extract middle part of the string.
- *
- * INPUTS
- * First - Position of first character
- * Count - Number of characters to extract
- *
- * RESULT
- * Middle - Extracted middle part of the string
- *
- ******************************************************************************/
- UString UString::Middle(UInt first, UInt count)
- {
- assert(false);
- // If the first character position is greater than the length of the string
- // or the count is zero then return an empty string.
- if ((Length() < first) || (count == 0))
- {
- return UString("");
- }
- return UString("");
- }
- /******************************************************************************
- *
- * NAME
- * UString::Right
- *
- * DESCRIPTION
- * Extract right part of the string.
- *
- * INPUTS
- * Count - Number of characters to extract
- *
- * RESULT
- * Right - Extracted right part of the string
- *
- ******************************************************************************/
- UString UString::Right(UInt count)
- {
- UInt length = Length();
- // If the count is zero then return an empty string.
- if ((length == 0) || (count == 0))
- {
- return UString("");
- }
- const WChar* ptr = Get();
- UInt pos = (length - count);
- return UString(ptr[pos]);
- }
- /******************************************************************************
- *
- * NAME
- * UString::ToUpper
- *
- * DESCRIPTION
- * Convert string to uppercase
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::ToUpper(void)
- {
- if (mData != NULL)
- {
- wcsupr(mData);
- }
- }
- /******************************************************************************
- *
- * NAME
- * UString::ToLower
- *
- * DESCRIPTION
- * Convert string to lowercase
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::ToLower(void)
- {
- if (mData != NULL)
- {
- wcslwr(mData);
- }
- }
- /******************************************************************************
- *
- * NAME
- * UString::Reverse
- *
- * DESCRIPTION
- * Reverse characters of string
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::Reverse(void)
- {
- if (mData != NULL)
- {
- wcsrev(mData);
- }
- }
- /******************************************************************************
- *
- * NAME
- * UString::Trim
- *
- * DESCRIPTION
- * Remove leading and trailing characters from string.
- *
- * INPUTS
- * TrimChars - Characters to trim
- *
- * RESULT
- * Removed - True if any characters removed
- *
- ******************************************************************************/
- bool UString::Trim(const Char* trimChars)
- {
- bool leftRemoved = TrimLeft(trimChars);
- bool rightRemoved = TrimRight(trimChars);
- return (leftRemoved || rightRemoved);
- }
- bool UString::Trim(const WChar* trimChars)
- {
- bool leftRemoved = TrimLeft(trimChars);
- bool rightRemoved = TrimRight(trimChars);
- return (leftRemoved || rightRemoved);
- }
- bool UString::Trim(const UString& trimChars)
- {
- bool leftRemoved = TrimLeft(trimChars);
- bool rightRemoved = TrimRight(trimChars);
- return (leftRemoved || rightRemoved);
- }
- /******************************************************************************
- *
- * NAME
- * UString::TrimLeft
- *
- * DESCRIPTION
- * Remove characters from left side of string
- *
- * INPUTS
- * TrimChars - Characters to trim
- *
- * RESULT
- * Removed - True if any characters removed
- *
- ******************************************************************************/
- bool UString::TrimLeft(const Char* trimChars)
- {
- if ((trimChars == NULL) || (strlen(trimChars) == 0))
- {
- return false;
- }
- return StripLeft<Char>(mData, trimChars);
- }
- bool UString::TrimLeft(const WChar* trimChars)
- {
- if ((trimChars == NULL) || (wcslen(trimChars) == 0))
- {
- return false;
- }
- return StripLeft<WChar>(mData, trimChars);
- }
- bool UString::TrimLeft(const UString& trimChars)
- {
- return TrimLeft(trimChars.Get());
- }
- /******************************************************************************
- *
- * NAME
- * UString::TrimRight
- *
- * DESCRIPTION
- * Remove characters from right side of string
- *
- * INPUTS
- * TrimChars - Characters to trim
- *
- * RESULT
- * Removed - True if any characters removed
- *
- ******************************************************************************/
- bool UString::TrimRight(const Char* trimChars)
- {
- if ((trimChars == NULL) || (strlen(trimChars) == 0))
- {
- return false;
- }
- return StripRight<Char>(mData, trimChars);
- }
- bool UString::TrimRight(const WChar* trimChars)
- {
- if ((trimChars == NULL) || (wcslen(trimChars) == 0))
- {
- return false;
- }
- return StripRight<WChar>(mData, trimChars);
- }
- bool UString::TrimRight(const UString& trimChars)
- {
- return TrimRight(trimChars.mData);
- }
- /******************************************************************************
- *
- * NAME
- * UString::ConvertToANSI
- *
- * DESCRIPTION
- * Convert the string to ANSI string
- *
- * INPUTS
- * Buffer - Buffer to convert into
- * Length - Maximum length of buffer
- *
- * RESULT
- * NONE
- *
- ******************************************************************************/
- void UString::ConvertToANSI(Char* buffer, UInt bufferLength) const
- {
- UStringToANSI(*this, buffer, bufferLength);
- }
- /******************************************************************************
- *
- * NAME
- * UString::Size
- *
- * DESCRIPTION
- * Retrieve the size of the string in bytes
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * Size - Size of string in bytes
- *
- ******************************************************************************/
- UInt UString::Size(void) const
- {
- if (mData == NULL)
- {
- return 0;
- }
- return ((Length() + 1) * sizeof(WChar));
- }
- /******************************************************************************
- *
- * NAME
- * UString::Capacity
- *
- * DESCRIPTION
- * Retrieve the capacity (maximum number of characters) of the string.
- *
- * INPUTS
- * NONE
- *
- * RESULT
- * Capacity - Maximum number of characters that string can hold.
- *
- ******************************************************************************/
- UInt UString::Capacity(void) const
- {
- return mCapacity;
- }
- /******************************************************************************
- *
- * NAME
- * UString::Resize
- *
- * DESCRIPTION
- * Resize the string capacity.
- *
- * INPUTS
- * NewSize - Size to resize string to.
- *
- * RESULT
- * Success - True if successful; otherwise false
- *
- ******************************************************************************/
- bool UString::Resize(UInt size)
- {
- // Allocate new storage
- assert(size > 0);
- WChar* data = new WChar[size + 1];
- assert(data != NULL);
- if (data == NULL)
- {
- return false;
- }
- // Copy existing string into new storage buffer
- if (mData != NULL)
- {
- UInt minSize = __min(Capacity(), size);
- wcsncpy(data, mData, minSize);
- data[minSize] = 0;
- delete mData;
- }
- // Set new storage
- mData = data;
- mCapacity = size;
- return true;
- }
- /******************************************************************************
- *
- * NAME
- * UString::AllocString
- *
- * DESCRIPTION
- * Allocate string storage
- *
- * INPUTS
- * Size - Number of characters
- *
- * RESULT
- * Success - True if allocate successful; otherwise false
- *
- ******************************************************************************/
- bool UString::AllocString(UInt size)
- {
- WChar* data = new WChar[size + 1];
- assert(data != NULL);
- if (data == NULL)
- {
- return false;
- }
- data[0] = 0;
- if (mData != NULL)
- {
- delete mData;
- }
- mData = data;
- mCapacity = size;
- return true;
- }
|