2007-03-24 14:59:28 +00:00
|
|
|
|
/*
|
|
|
|
|
** zstring.cpp
|
|
|
|
|
** A dynamically-allocated string class.
|
|
|
|
|
**
|
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
|
** Copyright 2005-2007 Randy Heit
|
|
|
|
|
** All rights reserved.
|
|
|
|
|
**
|
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
|
** are met:
|
|
|
|
|
**
|
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
|
**
|
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <string.h>
|
2006-05-16 02:50:18 +00:00
|
|
|
|
#include <new> // for bad_alloc
|
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
|
#include "zstring.h"
|
|
|
|
|
|
2006-05-16 02:50:18 +00:00
|
|
|
|
FNullStringData FString::NullString =
|
|
|
|
|
{
|
|
|
|
|
0, // Length of string
|
|
|
|
|
2, // Size of character buffer
|
|
|
|
|
2, // RefCount; it must never be modified, so keep it above 1 user at all times
|
|
|
|
|
"\0"
|
|
|
|
|
};
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
2006-05-16 02:50:18 +00:00
|
|
|
|
void FString::AttachToOther (const FString &other)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
assert (other.Chars != NULL);
|
|
|
|
|
|
|
|
|
|
if (other.Data()->RefCount < 0)
|
|
|
|
|
{
|
|
|
|
|
AllocBuffer (other.Data()->Len);
|
|
|
|
|
StrCopy (Chars, other.Chars, other.Data()->Len);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Chars = const_cast<FString &>(other).Data()->AddRef();
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const char *copyStr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (copyStr == NULL || *copyStr == '\0')
|
|
|
|
|
{
|
|
|
|
|
NullString.RefCount++;
|
|
|
|
|
Chars = &NullString.Nothing[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
size_t len = strlen (copyStr);
|
|
|
|
|
AllocBuffer (len);
|
|
|
|
|
StrCopy (Chars, copyStr, len);
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const char *copyStr, size_t len)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (len);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars, copyStr, len);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (char oneChar)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (oneChar == '\0')
|
|
|
|
|
{
|
|
|
|
|
NullString.RefCount++;
|
|
|
|
|
Chars = &NullString.Nothing[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AllocBuffer (1);
|
|
|
|
|
Chars[0] = oneChar;
|
|
|
|
|
Chars[1] = '\0';
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const FString &head, const FString &tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = head.Len();
|
|
|
|
|
size_t len2 = tail.Len();
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (len1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars, head);
|
|
|
|
|
StrCopy (Chars + len1, tail);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const FString &head, const char *tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = head.Len();
|
|
|
|
|
size_t len2 = strlen (tail);
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (len1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars, head);
|
|
|
|
|
StrCopy (Chars + len1, tail, len2);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const FString &head, char tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = head.Len();
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (len1 + 1);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars, head);
|
|
|
|
|
Chars[len1] = tail;
|
|
|
|
|
Chars[len1+1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const char *head, const FString &tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = strlen (head);
|
|
|
|
|
size_t len2 = tail.Len();
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (len1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars, head, len1);
|
|
|
|
|
StrCopy (Chars + len1, tail);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (const char *head, const char *tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = strlen (head);
|
|
|
|
|
size_t len2 = strlen (tail);
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (len1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars, head, len1);
|
|
|
|
|
StrCopy (Chars + len1, tail, len2);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::FString (char head, const FString &tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len2 = tail.Len();
|
2006-05-16 02:50:18 +00:00
|
|
|
|
AllocBuffer (1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
Chars[0] = head;
|
|
|
|
|
StrCopy (Chars + 1, tail);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString::~FString ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
Data()->Release();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-12-06 19:15:07 +00:00
|
|
|
|
char *FString::LockNewBuffer(size_t len)
|
|
|
|
|
{
|
|
|
|
|
Data()->Release();
|
|
|
|
|
AllocBuffer(len);
|
|
|
|
|
assert(Data()->RefCount == 1);
|
|
|
|
|
Data()->RefCount = -1;
|
|
|
|
|
return Chars;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 02:50:18 +00:00
|
|
|
|
char *FString::LockBuffer()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount == 1)
|
|
|
|
|
{ // We're the only user, so we can lock it straight away
|
|
|
|
|
Data()->RefCount = -1;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
else if (Data()->RefCount < -1)
|
|
|
|
|
{ // Already locked; just add to the lock count
|
|
|
|
|
Data()->RefCount--;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2006-05-16 02:50:18 +00:00
|
|
|
|
{ // Somebody else is also using this character buffer, so create a copy
|
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (old->Len);
|
|
|
|
|
StrCopy (Chars, old->Chars(), old->Len);
|
|
|
|
|
old->Release();
|
2006-10-05 03:30:44 +00:00
|
|
|
|
Data()->RefCount = -1;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
return Chars;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FString::UnlockBuffer()
|
|
|
|
|
{
|
|
|
|
|
assert (Data()->RefCount < 0);
|
|
|
|
|
|
|
|
|
|
if (++Data()->RefCount == 0)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
Data()->RefCount = 1;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 02:50:18 +00:00
|
|
|
|
FString &FString::operator = (const FString &other)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
assert (Chars != NULL);
|
|
|
|
|
|
|
|
|
|
if (&other != this)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
int oldrefcount = Data()->RefCount < 0;
|
|
|
|
|
Data()->Release();
|
|
|
|
|
AttachToOther(other);
|
|
|
|
|
if (oldrefcount < 0)
|
|
|
|
|
{
|
|
|
|
|
LockBuffer();
|
|
|
|
|
Data()->RefCount = oldrefcount;
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
FString &FString::operator = (const char *copyStr)
|
|
|
|
|
{
|
2007-01-25 04:02:06 +00:00
|
|
|
|
if (copyStr != Chars)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2007-01-25 04:02:06 +00:00
|
|
|
|
if (copyStr == NULL || *copyStr == '\0')
|
|
|
|
|
{
|
|
|
|
|
NullString.RefCount++;
|
|
|
|
|
Chars = &NullString.Nothing[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// In case copyStr is inside us, we can't release it until
|
|
|
|
|
// we've finished the copy.
|
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
|
|
|
|
|
if (copyStr < Chars || copyStr >= Chars + old->Len)
|
|
|
|
|
{
|
|
|
|
|
// We know the string isn't in our buffer, so release it now
|
|
|
|
|
// to reduce the potential for needless memory fragmentation.
|
|
|
|
|
old->Release();
|
|
|
|
|
old = NULL;
|
|
|
|
|
}
|
|
|
|
|
size_t len = strlen (copyStr);
|
|
|
|
|
AllocBuffer (len);
|
|
|
|
|
StrCopy (Chars, copyStr, len);
|
|
|
|
|
if (old != NULL)
|
|
|
|
|
{
|
|
|
|
|
old->Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Format (const char *fmt, ...)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
va_list arglist;
|
|
|
|
|
va_start (arglist, fmt);
|
|
|
|
|
VFormat (fmt, arglist);
|
|
|
|
|
va_end (arglist);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-26 04:38:22 +00:00
|
|
|
|
void FString::AppendFormat (const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arglist;
|
|
|
|
|
va_start (arglist, fmt);
|
|
|
|
|
StringFormat::VWorker (FormatHelper, this, fmt, arglist);
|
|
|
|
|
va_end (arglist);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::VFormat (const char *fmt, va_list arglist)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
Data()->Release();
|
|
|
|
|
Chars = (char *)(FStringData::Alloc(128) + 1);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StringFormat::VWorker (FormatHelper, this, fmt, arglist);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-26 04:38:22 +00:00
|
|
|
|
void FString::VAppendFormat (const char *fmt, va_list arglist)
|
|
|
|
|
{
|
|
|
|
|
StringFormat::VWorker (FormatHelper, this, fmt, arglist);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
int FString::FormatHelper (void *data, const char *cstr, int len)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString *str = (FString *)data;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
size_t len1 = str->Len();
|
2006-05-27 10:27:51 +00:00
|
|
|
|
if (len1 + len > str->Data()->AllocLen || str->Chars == &NullString.Nothing[0])
|
2006-05-16 02:50:18 +00:00
|
|
|
|
{
|
2006-05-27 10:27:51 +00:00
|
|
|
|
str->ReallocBuffer((len1 + len + 127) & ~127);
|
2006-05-16 02:50:18 +00:00
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (str->Chars + len1, cstr, len);
|
2006-05-16 02:50:18 +00:00
|
|
|
|
str->Data()->Len = (unsigned int)(len1 + len);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString FString::operator + (const FString &tail) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (*this, tail);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString FString::operator + (const char *tail) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (*this, tail);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString operator + (const char *head, const FString &tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (head, tail);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString FString::operator + (char tail) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (*this, tail);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString operator + (char head, const FString &tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (head, tail);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString &FString::operator += (const FString &tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = Len();
|
|
|
|
|
size_t len2 = tail.Len();
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (len1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars + len1, tail);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString &FString::operator += (const char *tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = Len();
|
|
|
|
|
size_t len2 = strlen(tail);
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (len1 + len2);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
StrCopy (Chars + len1, tail, len2);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString &FString::operator += (char tail)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len1 = Len();
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (len1 + 1);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
Chars[len1] = tail;
|
|
|
|
|
Chars[len1+1] = '\0';
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-31 00:16:12 +00:00
|
|
|
|
FString &FString::AppendCStrPart (const char *tail, size_t tailLen)
|
|
|
|
|
{
|
|
|
|
|
size_t len1 = Len();
|
|
|
|
|
ReallocBuffer (len1 + tailLen);
|
|
|
|
|
StrCopy (Chars + len1, tail, tailLen);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-16 02:50:18 +00:00
|
|
|
|
void FString::Truncate (long newlen)
|
2006-05-03 22:45:01 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (newlen >= 0 && newlen < (long)Len())
|
2006-05-03 22:45:01 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (newlen);
|
2006-05-03 22:45:01 +00:00
|
|
|
|
Chars[newlen] = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FString FString::Left (size_t numChars) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len = Len();
|
|
|
|
|
if (len < numChars)
|
|
|
|
|
{
|
|
|
|
|
numChars = len;
|
|
|
|
|
}
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (Chars, numChars);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString FString::Right (size_t numChars) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len = Len();
|
|
|
|
|
if (len < numChars)
|
|
|
|
|
{
|
|
|
|
|
numChars = len;
|
|
|
|
|
}
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (Chars + len - numChars, numChars);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
FString FString::Mid (size_t pos, size_t numChars) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t len = Len();
|
|
|
|
|
if (pos >= len)
|
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
return FString();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
if (pos + numChars > len)
|
|
|
|
|
{
|
|
|
|
|
numChars = len - pos;
|
|
|
|
|
}
|
2006-05-03 22:45:01 +00:00
|
|
|
|
return FString (Chars + pos, numChars);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::IndexOf (const FString &substr, long startIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return IndexOf (substr.Chars, startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::IndexOf (const char *substr, long startIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (startIndex > 0 && Len() <= (size_t)startIndex)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
char *str = strstr (Chars + startIndex, substr);
|
|
|
|
|
if (str == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return long(str - Chars);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::IndexOf (char subchar, long startIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (startIndex > 0 && Len() <= (size_t)startIndex)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
char *str = strchr (Chars + startIndex, subchar);
|
|
|
|
|
if (str == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return long(str - Chars);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::IndexOfAny (const FString &charset, long startIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return IndexOfAny (charset.Chars, startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::IndexOfAny (const char *charset, long startIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
if (startIndex > 0 && Len() <= (size_t)startIndex)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
char *brk = strpbrk (Chars + startIndex, charset);
|
|
|
|
|
if (brk == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return long(brk - Chars);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (const FString &substr) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOf (substr.Chars, long(Len()), substr.Len());
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (const char *substr) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOf (substr, long(Len()), strlen(substr));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (char subchar) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOf (subchar, long(Len()));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (const FString &substr, long endIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOf (substr.Chars, endIndex, substr.Len());
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (const char *substr, long endIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOf (substr, endIndex, strlen(substr));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (char subchar, long endIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
if ((size_t)endIndex > Len())
|
|
|
|
|
{
|
|
|
|
|
endIndex = long(Len());
|
|
|
|
|
}
|
|
|
|
|
while (--endIndex >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (Chars[endIndex] == subchar)
|
|
|
|
|
{
|
|
|
|
|
return endIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOf (const char *substr, long endIndex, size_t substrlen) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
if ((size_t)endIndex > Len())
|
|
|
|
|
{
|
|
|
|
|
endIndex = long(Len());
|
|
|
|
|
}
|
|
|
|
|
substrlen--;
|
|
|
|
|
while (--endIndex >= long(substrlen))
|
|
|
|
|
{
|
|
|
|
|
if (strncmp (substr, Chars + endIndex - substrlen, substrlen + 1) == 0)
|
|
|
|
|
{
|
|
|
|
|
return endIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOfAny (const FString &charset) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOfAny (charset.Chars, long(Len()));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOfAny (const char *charset) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOfAny (charset, long(Len()));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOfAny (const FString &charset, long endIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return LastIndexOfAny (charset.Chars, endIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::LastIndexOfAny (const char *charset, long endIndex) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
if ((size_t)endIndex > Len())
|
|
|
|
|
{
|
|
|
|
|
endIndex = long(Len());
|
|
|
|
|
}
|
|
|
|
|
while (--endIndex >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (strchr (charset, Chars[endIndex]) != NULL)
|
|
|
|
|
{
|
|
|
|
|
return endIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::ToUpper ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
size_t max = Len();
|
|
|
|
|
for (size_t i = 0; i < max; ++i)
|
|
|
|
|
{
|
2006-12-21 04:34:43 +00:00
|
|
|
|
Chars[i] = (char)toupper(Chars[i]);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::ToLower ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
size_t max = Len();
|
|
|
|
|
for (size_t i = 0; i < max; ++i)
|
|
|
|
|
{
|
2006-12-21 04:34:43 +00:00
|
|
|
|
Chars[i] = (char)tolower(Chars[i]);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::SwapCase ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
size_t max = Len();
|
|
|
|
|
for (size_t i = 0; i < max; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (isupper(Chars[i]))
|
|
|
|
|
{
|
2006-12-21 04:34:43 +00:00
|
|
|
|
Chars[i] = (char)tolower(Chars[i]);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-12-21 04:34:43 +00:00
|
|
|
|
Chars[i] = (char)toupper(Chars[i]);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripLeft ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t max = Len(), i, j;
|
|
|
|
|
for (i = 0; i < max; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!isspace(Chars[i]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
for (j = 0; i <= max; ++j, ++i)
|
|
|
|
|
{
|
|
|
|
|
Chars[j] = Chars[i];
|
|
|
|
|
}
|
|
|
|
|
ReallocBuffer (j-1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (max - i);
|
|
|
|
|
StrCopy (Chars, old->Chars() + i, max - i);
|
|
|
|
|
old->Release();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripLeft (const FString &charset)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return StripLeft (charset.Chars);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripLeft (const char *charset)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t max = Len(), i, j;
|
|
|
|
|
for (i = 0; i < max; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!strchr (charset, Chars[i]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; i <= max; ++j, ++i)
|
|
|
|
|
{
|
|
|
|
|
Chars[j] = Chars[i];
|
|
|
|
|
}
|
|
|
|
|
ReallocBuffer (j-1);
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (max - i);
|
|
|
|
|
StrCopy (Chars, old->Chars() + i, max - i);
|
|
|
|
|
old->Release();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripRight ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t max = Len(), i;
|
|
|
|
|
for (i = max - 1; i-- > 0; )
|
|
|
|
|
{
|
|
|
|
|
if (!isspace(Chars[i]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
|
|
|
|
{
|
|
|
|
|
Chars[i+1] = '\0';
|
|
|
|
|
ReallocBuffer (i+1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (i+1);
|
|
|
|
|
StrCopy (Chars, old->Chars(), i+1);
|
|
|
|
|
old->Release();
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripRight (const FString &charset)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return StripRight (charset.Chars);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripRight (const char *charset)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t max = Len(), i;
|
|
|
|
|
for (i = max - 1; i-- > 0; )
|
|
|
|
|
{
|
|
|
|
|
if (!strchr (charset, Chars[i]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
|
|
|
|
{
|
|
|
|
|
Chars[i+1] = '\0';
|
|
|
|
|
ReallocBuffer (i+1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (i+1);
|
|
|
|
|
StrCopy (Chars, old->Chars(), i+1);
|
|
|
|
|
old->Release();
|
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripLeftRight ()
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t max = Len(), i, j, k;
|
|
|
|
|
for (i = 0; i < max; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!isspace(Chars[i]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (j = max - 1; j >= i; --j)
|
|
|
|
|
{
|
|
|
|
|
if (!isspace(Chars[j]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
|
|
|
|
{
|
|
|
|
|
for (k = 0; i <= j; ++i, ++k)
|
|
|
|
|
{
|
|
|
|
|
Chars[k] = Chars[i];
|
|
|
|
|
}
|
|
|
|
|
Chars[k] = '\0';
|
|
|
|
|
ReallocBuffer (k);
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (j - i);
|
|
|
|
|
StrCopy (Chars, old->Chars(), j - i);
|
|
|
|
|
old->Release();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripLeftRight (const FString &charset)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
return StripLeftRight (charset.Chars);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripLeftRight (const char *charset)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t max = Len(), i, j, k;
|
|
|
|
|
for (i = 0; i < max; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!strchr (charset, Chars[i]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (j = max - 1; j >= i; --j)
|
|
|
|
|
{
|
|
|
|
|
if (!strchr (charset, Chars[j]))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
|
|
|
|
{
|
|
|
|
|
for (k = 0; i <= j; ++i, ++k)
|
|
|
|
|
{
|
|
|
|
|
Chars[k] = Chars[i];
|
|
|
|
|
}
|
|
|
|
|
Chars[k] = '\0';
|
|
|
|
|
ReallocBuffer (k);
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (j - i);
|
|
|
|
|
StrCopy (Chars, old->Chars(), j - i);
|
|
|
|
|
old->Release();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Insert (size_t index, const FString &instr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
Insert (index, instr.Chars, instr.Len());
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Insert (size_t index, const char *instr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
Insert (index, instr, strlen(instr));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Insert (size_t index, const char *instr, size_t instrlen)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t mylen = Len();
|
|
|
|
|
if (index > mylen)
|
|
|
|
|
{
|
|
|
|
|
index = mylen;
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
if (Data()->RefCount <= 1)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (mylen + instrlen);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
memmove (Chars + index + instrlen, Chars + index, (mylen - index + 1)*sizeof(char));
|
2006-05-16 02:50:18 +00:00
|
|
|
|
memcpy (Chars + index, instr, instrlen*sizeof(char));
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
else
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (mylen + instrlen);
|
|
|
|
|
StrCopy (Chars, old->Chars(), index);
|
|
|
|
|
StrCopy (Chars + index, instr, instrlen);
|
|
|
|
|
StrCopy (Chars + index + instrlen, Chars + index, mylen - index + 1);
|
|
|
|
|
old->Release();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::ReplaceChars (char oldchar, char newchar)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t i, j;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (i = 0, j = Len(); i < j; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (Chars[i] == oldchar)
|
|
|
|
|
{
|
|
|
|
|
Chars[i] = newchar;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::ReplaceChars (const char *oldcharset, char newchar)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t i, j;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (i = 0, j = Len(); i < j; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (strchr (oldcharset, Chars[i]) != NULL)
|
|
|
|
|
{
|
|
|
|
|
Chars[i] = newchar;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripChars (char killchar)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t read, write, mylen;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (read = write = 0, mylen = Len(); read < mylen; ++read)
|
|
|
|
|
{
|
|
|
|
|
if (Chars[read] != killchar)
|
|
|
|
|
{
|
|
|
|
|
Chars[write++] = Chars[read];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Chars[write] = '\0';
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (write);
|
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StripChars (const char *killchars)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t read, write, mylen;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (read = write = 0, mylen = Len(); read < mylen; ++read)
|
|
|
|
|
{
|
|
|
|
|
if (strchr (killchars, Chars[read]) == NULL)
|
|
|
|
|
{
|
|
|
|
|
Chars[write++] = Chars[read];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Chars[write] = '\0';
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (write);
|
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::MergeChars (char merger)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
MergeChars (merger, merger);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::MergeChars (char merger, char newchar)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t read, write, mylen;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (read = write = 0, mylen = Len(); read < mylen; )
|
|
|
|
|
{
|
|
|
|
|
if (Chars[read] == merger)
|
|
|
|
|
{
|
|
|
|
|
while (Chars[++read] == merger)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
Chars[write++] = newchar;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Chars[write++] = Chars[read++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Chars[write] = '\0';
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (write);
|
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::MergeChars (const char *charset, char newchar)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
size_t read, write, mylen;
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (read = write = 0, mylen = Len(); read < mylen; )
|
|
|
|
|
{
|
|
|
|
|
if (strchr (charset, Chars[read]) != NULL)
|
|
|
|
|
{
|
|
|
|
|
while (strchr (charset, Chars[++read]) != NULL)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
Chars[write++] = newchar;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Chars[write++] = Chars[read++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Chars[write] = '\0';
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (write);
|
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Substitute (const FString &oldstr, const FString &newstr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return Substitute (oldstr.Chars, newstr.Chars, oldstr.Len(), newstr.Len());
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Substitute (const char *oldstr, const FString &newstr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return Substitute (oldstr, newstr.Chars, strlen(oldstr), newstr.Len());
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Substitute (const FString &oldstr, const char *newstr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return Substitute (oldstr.Chars, newstr, oldstr.Len(), strlen(newstr));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Substitute (const char *oldstr, const char *newstr)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return Substitute (oldstr, newstr, strlen(oldstr), strlen(newstr));
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::Substitute (const char *oldstr, const char *newstr, size_t oldstrlen, size_t newstrlen)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
LockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
for (size_t checkpt = 0; checkpt < Len(); )
|
|
|
|
|
{
|
|
|
|
|
char *match = strstr (Chars + checkpt, oldstr);
|
|
|
|
|
size_t len = Len();
|
|
|
|
|
if (match != NULL)
|
|
|
|
|
{
|
|
|
|
|
size_t matchpt = match - Chars;
|
|
|
|
|
if (oldstrlen != newstrlen)
|
|
|
|
|
{
|
2006-05-16 02:50:18 +00:00
|
|
|
|
ReallocBuffer (len + newstrlen - oldstrlen);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
memmove (Chars + matchpt + newstrlen, Chars + matchpt + oldstrlen, (len + 1 - matchpt - oldstrlen)*sizeof(char));
|
|
|
|
|
}
|
|
|
|
|
memcpy (Chars + matchpt, newstr, newstrlen);
|
|
|
|
|
checkpt = matchpt + newstrlen;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
UnlockBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
bool FString::IsInt () const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
// String must match: [whitespace] [{+ | <20>}] [0 [{ x | X }]] [digits] [whitespace]
|
|
|
|
|
|
|
|
|
|
/* This state machine is based on a simplification of re2c's output for this input:
|
|
|
|
|
digits = [0-9];
|
|
|
|
|
hexdigits = [0-9a-fA-F];
|
|
|
|
|
octdigits = [0-7];
|
|
|
|
|
|
|
|
|
|
("0" octdigits+ | "0" [xX] hexdigits+ | (digits \ '0') digits*) { return true; }
|
|
|
|
|
[\000-\377] { return false; }*/
|
|
|
|
|
const char *YYCURSOR = Chars;
|
|
|
|
|
char yych;
|
|
|
|
|
|
|
|
|
|
yych = *YYCURSOR;
|
|
|
|
|
|
|
|
|
|
// Skip preceding whitespace
|
|
|
|
|
while (yych != '\0' && isspace(yych)) { yych = *++YYCURSOR; }
|
|
|
|
|
|
|
|
|
|
// Check for sign
|
|
|
|
|
if (yych == '+' || yych == '-') { yych = *++YYCURSOR; }
|
|
|
|
|
|
|
|
|
|
if (yych == '0')
|
|
|
|
|
{
|
|
|
|
|
yych = *++YYCURSOR;
|
|
|
|
|
if (yych >= '0' && yych <= '7')
|
|
|
|
|
{
|
|
|
|
|
do { yych = *++YYCURSOR; } while (yych >= '0' && yych <= '7');
|
|
|
|
|
}
|
|
|
|
|
else if (yych == 'X' || yych == 'x')
|
|
|
|
|
{
|
|
|
|
|
bool gothex = false;
|
|
|
|
|
yych = *++YYCURSOR;
|
|
|
|
|
while ((yych >= '0' && yych <= '9') || (yych >= 'A' && yych <= 'F') || (yych >= 'a' && yych <= 'f'))
|
|
|
|
|
{
|
|
|
|
|
gothex = true;
|
|
|
|
|
yych = *++YYCURSOR;
|
|
|
|
|
}
|
|
|
|
|
if (!gothex) return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (yych >= '1' && yych <= '9')
|
|
|
|
|
{
|
|
|
|
|
do { yych = *++YYCURSOR; } while (yych >= '0' && yych <= '9');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The rest should all be whitespace
|
|
|
|
|
while (yych != '\0' && isspace(yych)) { yych = *++YYCURSOR; }
|
|
|
|
|
return yych == '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
bool FString::IsFloat () const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
// String must match: [whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits] [whitespace]
|
|
|
|
|
/* This state machine is based on a simplification of re2c's output for this input:
|
|
|
|
|
digits = [0-9];
|
|
|
|
|
|
|
|
|
|
(digits+ | digits* "." digits+) ([dDeE] [+-]? digits+)? { return true; }
|
|
|
|
|
[\000-\377] { return false; }
|
|
|
|
|
*/
|
|
|
|
|
const char *YYCURSOR = Chars;
|
|
|
|
|
char yych;
|
|
|
|
|
bool gotdig = false;
|
|
|
|
|
|
|
|
|
|
yych = *YYCURSOR;
|
|
|
|
|
|
|
|
|
|
// Skip preceding whitespace
|
|
|
|
|
while (yych != '\0' && isspace(yych)) { yych = *++YYCURSOR; }
|
|
|
|
|
|
|
|
|
|
// Check for sign
|
|
|
|
|
if (yych == '+' || yych == '-') { yych = *++YYCURSOR; }
|
|
|
|
|
|
|
|
|
|
while (yych >= '0' && yych <= '9')
|
|
|
|
|
{
|
|
|
|
|
gotdig = true;
|
|
|
|
|
yych = *++YYCURSOR;
|
|
|
|
|
}
|
|
|
|
|
if (yych == '.')
|
|
|
|
|
{
|
|
|
|
|
yych = *++YYCURSOR;
|
|
|
|
|
if (yych >= '0' && yych <= '9')
|
|
|
|
|
{
|
|
|
|
|
gotdig = true;
|
|
|
|
|
do { yych = *++YYCURSOR; } while (yych >= '0' && yych <= '9');
|
|
|
|
|
}
|
|
|
|
|
else return false;
|
|
|
|
|
}
|
|
|
|
|
if (gotdig)
|
|
|
|
|
{
|
|
|
|
|
if (yych == 'D' || yych == 'd' || yych == 'E' || yych == 'e')
|
|
|
|
|
{
|
|
|
|
|
yych = *++YYCURSOR;
|
|
|
|
|
if (yych == '+' || yych == '-') yych = *++YYCURSOR;
|
|
|
|
|
while (yych >= '0' && yych <= '9') { yych = *++YYCURSOR; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The rest should all be whitespace
|
|
|
|
|
while (yych != '\0' && isspace(yych)) { yych = *++YYCURSOR; }
|
|
|
|
|
return yych == '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
long FString::ToLong (int base) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return strtol (Chars, NULL, base);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
unsigned long FString::ToULong (int base) const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return strtoul (Chars, NULL, base);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
double FString::ToDouble () const
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
return strtod (Chars, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StrCopy (char *to, const char *from, size_t len)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
memcpy (to, from, len*sizeof(char));
|
|
|
|
|
to[len] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-03 22:45:01 +00:00
|
|
|
|
void FString::StrCopy (char *to, const FString &from)
|
2006-02-24 04:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
StrCopy (to, from.Chars, from.Len());
|
|
|
|
|
}
|
2006-05-16 02:50:18 +00:00
|
|
|
|
|
|
|
|
|
void FString::AllocBuffer (size_t len)
|
|
|
|
|
{
|
|
|
|
|
Chars = (char *)(FStringData::Alloc(len) + 1);
|
|
|
|
|
Data()->Len = (unsigned int)len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FString::ReallocBuffer (size_t newlen)
|
|
|
|
|
{
|
|
|
|
|
if (Data()->RefCount > 1)
|
|
|
|
|
{ // If more than one reference, we must use a new copy
|
|
|
|
|
FStringData *old = Data();
|
|
|
|
|
AllocBuffer (newlen);
|
|
|
|
|
StrCopy (Chars, old->Chars(), old->Len);
|
|
|
|
|
old->Release();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (newlen > Data()->AllocLen)
|
|
|
|
|
{
|
|
|
|
|
Chars = (char *)(Data()->Realloc(newlen) + 1);
|
|
|
|
|
}
|
|
|
|
|
Data()->Len = (unsigned int)newlen;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Under Windows, use the system heap functions for managing string memory.
|
|
|
|
|
// Under other OSs, use ordinary memory management instead.
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
static HANDLE StringHeap;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
FStringData *FStringData::Alloc (size_t strlen)
|
|
|
|
|
{
|
|
|
|
|
strlen += 1 + sizeof(FStringData); // Add space for header and terminating null
|
|
|
|
|
strlen = (strlen + 7) & ~7; // Pad length up
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (StringHeap == NULL)
|
|
|
|
|
{
|
|
|
|
|
StringHeap = HeapCreate (0, 64*1024, 0);
|
|
|
|
|
if (StringHeap == NULL)
|
|
|
|
|
{
|
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FStringData *block = (FStringData *)HeapAlloc (StringHeap, 0, strlen);
|
|
|
|
|
#else
|
|
|
|
|
FStringData *block = (FStringData *)malloc (strlen);
|
|
|
|
|
#endif
|
|
|
|
|
if (block == NULL)
|
|
|
|
|
{
|
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
}
|
|
|
|
|
block->Len = 0;
|
|
|
|
|
block->AllocLen = (unsigned int)strlen - sizeof(FStringData) - 1;
|
|
|
|
|
block->RefCount = 1;
|
|
|
|
|
return block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FStringData *FStringData::Realloc (size_t newstrlen)
|
|
|
|
|
{
|
|
|
|
|
assert (RefCount <= 1);
|
|
|
|
|
|
|
|
|
|
newstrlen += 1 + sizeof(FStringData); // Add space for header and terminating null
|
|
|
|
|
newstrlen = (newstrlen + 7) & ~7; // Pad length up
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
FStringData *block = (FStringData *)HeapReAlloc (StringHeap, 0, this, newstrlen);
|
|
|
|
|
#else
|
|
|
|
|
FStringData *block = (FStringData *)realloc (this, newstrlen);
|
|
|
|
|
#endif
|
|
|
|
|
if (block == NULL)
|
|
|
|
|
{
|
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
}
|
|
|
|
|
block->AllocLen = (unsigned int)newstrlen - sizeof(FStringData) - 1;
|
|
|
|
|
return block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FStringData::Dealloc ()
|
|
|
|
|
{
|
|
|
|
|
assert (RefCount <= 0);
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
HeapFree (StringHeap, 0, this);
|
|
|
|
|
#else
|
|
|
|
|
free (this);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FStringData *FStringData::MakeCopy ()
|
|
|
|
|
{
|
|
|
|
|
FStringData *copy = Alloc (Len);
|
|
|
|
|
copy->Len = Len;
|
|
|
|
|
FString::StrCopy (copy->Chars(), Chars(), Len);
|
|
|
|
|
return copy;
|
|
|
|
|
}
|