DeprecatedCString.cpp [plain text]
#include "config.h"
#include "DeprecatedCString.h"
#include <wtf/ASCIICType.h>
#include <wtf/Assertions.h>
using namespace WTF;
namespace WebCore {
DeprecatedCString::DeprecatedCString()
{
}
DeprecatedCString::DeprecatedCString(int size) : DeprecatedByteArray(size)
{
if( size>0 && data() )
{
data()[0] = 0; data()[size-1] = 0; }
}
DeprecatedCString::DeprecatedCString(const char *str)
{
size_t len;
if( str && (len=strlen(str)+1) && resize(len) ) strcpy( data(), str );
}
DeprecatedCString::DeprecatedCString(const char *str, unsigned max)
{
if( str && max )
{
const char* p = str;
unsigned len = 1; while( *p++ && len<max )
len ++;
if( resize(len) )
{
char *dest = data();
strncpy( dest, str, len );
dest[len-1] = 0; }
}
}
bool DeprecatedCString::isEmpty() const
{ return length()==0; }
unsigned DeprecatedCString::length() const
{
const char *d = data();
return d ? strlen(d) : 0;
}
bool DeprecatedCString::resize(unsigned len)
{
bool success = DeprecatedByteArray::resize(len);
if( success && len>0 )
data()[len-1] = 0;
return success;
}
bool DeprecatedCString::truncate(unsigned pos)
{
return resize(pos+1);
}
DeprecatedCString DeprecatedCString::lower() const
{
DeprecatedCString tmp = *this; char* str = tmp.data();
if( str )
{
while( *str != 0 )
{
*str = toASCIILower(*str);
str++;
}
}
return tmp;
}
DeprecatedCString DeprecatedCString::upper() const
{
DeprecatedCString tmp = *this; char* str = tmp.data();
if( str )
{
while( *str != 0 )
{
*str = toASCIIUpper(*str);
str++;
}
}
return tmp;
}
inline DeprecatedCString DeprecatedCString::left(unsigned len) const
{ return mid(0, len); }
inline DeprecatedCString DeprecatedCString::right(unsigned len) const
{ return mid(length() - len, len); }
DeprecatedCString DeprecatedCString::mid(unsigned index, unsigned len) const
{
unsigned size = length();
if( data() && index<size ) {
if( len > size - index )
len = size - index;
return DeprecatedCString( &(data()[index]), len+1); }
return DeprecatedCString();
}
int DeprecatedCString::find(const char *sub, int index, bool cs) const
{
const char* str = data();
if( str && str[0] && sub && index>=0 ) {
int pos = 0;
while( pos < index )
if( str[pos++] == 0 )
return -1;
while( str[index] != 0 )
{
char a, b;
pos = 0;
if( cs )
while( (a=sub[pos]) && (b=str[index]) && a==b )
pos++, index++;
else
while( (a=sub[pos]) && (b=str[index]) && toASCIILower(a)==toASCIILower(b) )
pos++, index++;
if( sub[pos] == 0 )
return index - pos;
index ++;
}
}
return -1;
}
int DeprecatedCString::contains(char c, bool cs) const
{
unsigned found = 0;
unsigned len = length();
if (len) {
const char *str = data();
if (cs) {
for (unsigned i = 0; i != len; ++i) {
found += str[i] == c;
}
} else {
c = toASCIILower(c);
for (unsigned i = 0; i != len; ++i) {
char chr = str[i];
chr = toASCIILower(chr);
found += chr == c;
}
}
}
return found;
}
DeprecatedCString &DeprecatedCString::operator=(const char *assignFrom)
{
duplicate(assignFrom, (assignFrom ? strlen(assignFrom) : 0) + 1);
return *this;
}
DeprecatedCString& DeprecatedCString::append(const char *s)
{
if (s) {
unsigned len2 = strlen(s);
if (len2) {
detach();
unsigned len1 = length();
if (DeprecatedByteArray::resize(len1 + len2 + 1))
memcpy(data() + len1, s, len2 + 1);
}
}
return *this;
}
DeprecatedCString &DeprecatedCString::append(char c)
{
detach();
unsigned len = length();
if (DeprecatedByteArray::resize(len + 2)) {
*(data() + len) = c;
*(data() + len + 1) = '\0';
}
return *this;
}
DeprecatedCString &DeprecatedCString::replace(char c1, char c2)
{
unsigned len = length();
if (len) {
unsigned i;
{
const char *s = data();
for (i = 0; i != len; ++i) {
if (s[i] == c1) {
break;
}
}
}
if (i != len) {
detach();
char *s = data();
for (; i != len; ++i) {
if (s[i] == c1) {
s[i] = c2;
}
}
}
}
return *this;
}
bool operator==(const DeprecatedCString &s1, const char *s2)
{
if (s1.size() == 0 && !s2)
return true;
if (s1.size() == 0 && s2)
return false;
return strcmp(s1, s2) == 0;
}
}