Commit 338cd0d7 authored by darin's avatar darin

Reviewed by Richard.

	- clean room rewrite of QCString

        * kwq/KWQCString.mm: Replaced functions that needed rewrite.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@3371 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 7ce491ff
2003-01-21 Darin Adler <darin@apple.com>
Reviewed by Richard.
- clean room rewrite of QCString
* kwq/KWQCString.mm: Replaced functions that needed rewrite.
2003-01-21 Richard Williamson <rjw@apple.com>
Added support to pause and resume javascript timers. This allows pages
......
2003-01-21 Darin Adler <darin@apple.com>
Reviewed by Richard.
- clean room rewrite of QCString
* kwq/KWQCString.mm: Replaced functions that needed rewrite.
2003-01-21 Richard Williamson <rjw@apple.com>
Added support to pause and resume javascript timers. This allows pages
......
/*
* Copyright (C) 2001, 2002, 2003 Apple Computer, Inc. All rights reserved.
* Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -25,236 +25,202 @@
#import "KWQCString.h"
#import "KWQAssertions.h"
#import <ctype.h>
using std::ostream;
QCString::QCString() : QByteArray(0)
QCString::QCString()
{
}
QCString::QCString(int size) : QByteArray(size)
{
if (size > 0) {
*data() = '\0';
*(data() + (size - 1)) = '\0';
if( size>0 && data() )
{
data()[0] = 0; // first null
data()[size-1] = 0; // last byte
}
// else null
}
QCString::QCString(const char *s)
{
duplicate(s, strlen(s) + 1);
}
QCString::QCString(const char *s, uint max)
QCString::QCString(const char *str)
{
if (s == 0) {
return;
}
uint len; // index of first '\0'
for (len = 0; len < max - 1; len++) {
if (s[len] == '\0') {
break;
}
}
QByteArray::resize(len + 1);
memcpy(data(), s, len);
data()[len] = 0;
size_t len;
if( str && (len=strlen(str)+1) && resize(len) ) // include null
strcpy( data(), str );
// else null
}
bool QCString::isEmpty() const
{
const char *s = data();
return !(s && *s);
}
int QCString::find(const char *s, int index, bool cs) const
QCString::QCString(const char *str, uint max)
{
int result;
const char *tmp;
result = -1;
if (!s || index >= (int)size()) {
result = -1;
}
else if (!*s) {
result = index;
}
else {
if (cs) {
tmp = strstr(data() + index, s);
}
else {
tmp = data() + index;
int len = strlen(s);
while (*tmp) {
if (strncasecmp(tmp, s, len) == 0) {
break;
}
tmp++;
}
if (!*tmp) {
tmp = 0;
}
}
if (tmp) {
result = (int)(tmp - data());
if( str && max )
{
// perform a truncated strlen on str
const char* p = str;
uint len = 1; // for the null
while( *p++ && len<max )
len ++;
if( resize(len) )
{
char *dest = data();
strncpy( dest, str, len );
dest[len-1] = 0; // re-terminate
}
}
return result;
// else null
}
int QCString::contains(char c, bool cs) const
{
int result;
const char *tmp;
result = 0;
tmp = data();
if (tmp) {
if (cs) {
while (*tmp) {
if (*tmp == c) {
result++;
}
tmp++;
}
}
else {
c = tolower((uchar)c);
while (*tmp) {
if (tolower((uchar)*tmp) == c) {
result++;
}
tmp++;
}
}
}
bool QCString::isEmpty() const
{ return length()==0; }
return result;
}
uint QCString::length() const
{
const char *s = data();
return s == NULL ? 0 : strlen(s);
const char *d = data();
return d ? strlen(d) : 0;
}
bool QCString::resize(uint len)
{
detach();
uint oldlen;
oldlen = length();
bool success = QByteArray::resize(len);
if( success && len>0 )
data()[len-1] = 0; // always terminate last byte
if (!QByteArray::resize(len)) {
return false;
}
if (len) {
*(data() + len-1) = '\0';
}
if (len > 0 && oldlen == 0) {
*(data()) = '\0';
}
return true;
return success;
}
bool QCString::truncate(uint pos)
{
return resize(pos + 1);
return resize(pos+1);
}
QCString QCString::lower() const
{
QCString result(data());
char *p = result.data();
if (p) {
while(*p) {
*p = tolower((uchar)*p);
p++;
// convert
QCString tmp = *this; // copy
char* str = tmp.data();
if( str )
{
while( *str != 0 )
{
*str = tolower(*str);
str++;
}
}
return result;
return tmp;
}
QCString QCString::upper() const
{
QCString result(data());
char *p = result.data();
if (p) {
while(*p) {
*p = toupper((uchar)*p);
p++;
QCString tmp = *this; // copy
char* str = tmp.data();
if( str )
{
while( *str != 0 )
{
*str = toupper(*str);
str++;
}
}
return result;
return tmp;
}
QCString QCString::left(uint len) const
inline QCString QCString::left(uint len) const
{ return mid(0, len); }
inline QCString QCString::right(uint len) const
{ return mid(length() - len, len); }
QCString QCString::mid(uint index, uint len) const
{
if (isEmpty()) {
QCString empty;
return empty;
}
else if (len >= size()) {
QCString same(data());
return same;
}
else {
QCString s(len + 1);
strncpy(s.data(), data(), len);
*(s.data() + len) = '\0';
return s;
uint size = length();
if( data() && index<size ) // return null if index out-of-range
{
// clip length
if( len > size - index )
len = size - index;
// copy and return
return QCString( &(data()[index]), len+1); // include nul
}
// degenerate case
return QCString();
}
QCString QCString::right(uint len) const
int QCString::find(const char *sub, int index, bool cs) const
{
if (isEmpty()) {
QCString empty;
return empty;
}
else {
uint l = length();
if (len > l) {
len = l;
const char* str = data();
if( str && str[0] && sub && index>=0 ) // don't search empty strings
{
// advance until we get to index
int pos = 0;
while( pos < index )
if( str[pos++] == 0 )
return -1; // index is beyond end of str
// now search from index onward
while( str[index] != 0 )
{
char a, b;
// compare until we reach the end or a mismatch
pos = 0;
if( cs )
while( (a=sub[pos]) && (b=str[index]) && a==b )
pos++, index++;
else
while( (a=sub[pos]) && (b=str[index]) && tolower(a)==tolower(b) )
pos++, index++;
// reached the end of our compare string without a mismatch?
if( sub[pos] == 0 )
return index - pos;
index ++;
}
const char *p = data() + (l - len);
return QCString(p);
}
return -1;
}
QCString QCString::mid(uint index, uint len) const
int QCString::contains(char c, bool cs) const
{
uint slen;
slen = strlen(data());
if (len == 0xffffffff) {
len = length() - index;
const char* str = data();
uint found = 0;
uint len = length();
if( str && len )
{
// drop char if we're insensitive
if( !cs )
c = tolower(c);
for( unsigned i=0; i<len; i++ )
{
char chr = str[i];
if( !cs )
chr = tolower(chr);
if( chr == c )
found ++;
}
}
if (isEmpty() || index >= slen) {
QCString result;
return result;
}
else {
const char *p = data() + index;
QCString result(len + 1);
strncpy(result.data(), p, len);
*(result.data() + len) = '\0';
return result;
}
return found;
}
QCString &QCString::operator=(const char *assignFrom)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment