michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkString.h" michael@0: #include "SkFixed.h" michael@0: #include "SkThread.h" michael@0: #include "SkUtils.h" michael@0: #include michael@0: #include michael@0: michael@0: // number of bytes (on the stack) to receive the printf result michael@0: static const size_t kBufferSize = 1024; michael@0: michael@0: #ifdef SK_BUILD_FOR_WIN michael@0: #define VSNPRINTF(buffer, size, format, args) \ michael@0: _vsnprintf_s(buffer, size, _TRUNCATE, format, args) michael@0: #define SNPRINTF _snprintf michael@0: #else michael@0: #define VSNPRINTF vsnprintf michael@0: #define SNPRINTF snprintf michael@0: #endif michael@0: michael@0: #define ARGS_TO_BUFFER(format, buffer, size) \ michael@0: do { \ michael@0: va_list args; \ michael@0: va_start(args, format); \ michael@0: VSNPRINTF(buffer, size, format, args); \ michael@0: va_end(args); \ michael@0: } while (0) michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkStrEndsWith(const char string[], const char suffixStr[]) { michael@0: SkASSERT(string); michael@0: SkASSERT(suffixStr); michael@0: size_t strLen = strlen(string); michael@0: size_t suffixLen = strlen(suffixStr); michael@0: return strLen >= suffixLen && michael@0: !strncmp(string + strLen - suffixLen, suffixStr, suffixLen); michael@0: } michael@0: michael@0: bool SkStrEndsWith(const char string[], const char suffixChar) { michael@0: SkASSERT(string); michael@0: size_t strLen = strlen(string); michael@0: if (0 == strLen) { michael@0: return false; michael@0: } else { michael@0: return (suffixChar == string[strLen-1]); michael@0: } michael@0: } michael@0: michael@0: int SkStrStartsWithOneOf(const char string[], const char prefixes[]) { michael@0: int index = 0; michael@0: do { michael@0: const char* limit = strchr(prefixes, '\0'); michael@0: if (!strncmp(string, prefixes, limit - prefixes)) { michael@0: return index; michael@0: } michael@0: prefixes = limit + 1; michael@0: index++; michael@0: } while (prefixes[0]); michael@0: return -1; michael@0: } michael@0: michael@0: char* SkStrAppendU32(char string[], uint32_t dec) { michael@0: SkDEBUGCODE(char* start = string;) michael@0: michael@0: char buffer[SkStrAppendU32_MaxSize]; michael@0: char* p = buffer + sizeof(buffer); michael@0: michael@0: do { michael@0: *--p = SkToU8('0' + dec % 10); michael@0: dec /= 10; michael@0: } while (dec != 0); michael@0: michael@0: SkASSERT(p >= buffer); michael@0: char* stop = buffer + sizeof(buffer); michael@0: while (p < stop) { michael@0: *string++ = *p++; michael@0: } michael@0: SkASSERT(string - start <= SkStrAppendU32_MaxSize); michael@0: return string; michael@0: } michael@0: michael@0: char* SkStrAppendS32(char string[], int32_t dec) { michael@0: if (dec < 0) { michael@0: *string++ = '-'; michael@0: dec = -dec; michael@0: } michael@0: return SkStrAppendU32(string, static_cast(dec)); michael@0: } michael@0: michael@0: char* SkStrAppendU64(char string[], uint64_t dec, int minDigits) { michael@0: SkDEBUGCODE(char* start = string;) michael@0: michael@0: char buffer[SkStrAppendU64_MaxSize]; michael@0: char* p = buffer + sizeof(buffer); michael@0: michael@0: do { michael@0: *--p = SkToU8('0' + (int32_t) (dec % 10)); michael@0: dec /= 10; michael@0: minDigits--; michael@0: } while (dec != 0); michael@0: michael@0: while (minDigits > 0) { michael@0: *--p = '0'; michael@0: minDigits--; michael@0: } michael@0: michael@0: SkASSERT(p >= buffer); michael@0: size_t cp_len = buffer + sizeof(buffer) - p; michael@0: memcpy(string, p, cp_len); michael@0: string += cp_len; michael@0: michael@0: SkASSERT(string - start <= SkStrAppendU64_MaxSize); michael@0: return string; michael@0: } michael@0: michael@0: char* SkStrAppendS64(char string[], int64_t dec, int minDigits) { michael@0: if (dec < 0) { michael@0: *string++ = '-'; michael@0: dec = -dec; michael@0: } michael@0: return SkStrAppendU64(string, static_cast(dec), minDigits); michael@0: } michael@0: michael@0: char* SkStrAppendFloat(char string[], float value) { michael@0: // since floats have at most 8 significant digits, we limit our %g to that. michael@0: static const char gFormat[] = "%.8g"; michael@0: // make it 1 larger for the terminating 0 michael@0: char buffer[SkStrAppendScalar_MaxSize + 1]; michael@0: int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value); michael@0: memcpy(string, buffer, len); michael@0: SkASSERT(len <= SkStrAppendScalar_MaxSize); michael@0: return string + len; michael@0: } michael@0: michael@0: char* SkStrAppendFixed(char string[], SkFixed x) { michael@0: SkDEBUGCODE(char* start = string;) michael@0: if (x < 0) { michael@0: *string++ = '-'; michael@0: x = -x; michael@0: } michael@0: michael@0: unsigned frac = x & 0xFFFF; michael@0: x >>= 16; michael@0: if (frac == 0xFFFF) { michael@0: // need to do this to "round up", since 65535/65536 is closer to 1 than to .9999 michael@0: x += 1; michael@0: frac = 0; michael@0: } michael@0: string = SkStrAppendS32(string, x); michael@0: michael@0: // now handle the fractional part (if any) michael@0: if (frac) { michael@0: static const uint16_t gTens[] = { 1000, 100, 10, 1 }; michael@0: const uint16_t* tens = gTens; michael@0: michael@0: x = SkFixedRoundToInt(frac * 10000); michael@0: SkASSERT(x <= 10000); michael@0: if (x == 10000) { michael@0: x -= 1; michael@0: } michael@0: *string++ = '.'; michael@0: do { michael@0: unsigned powerOfTen = *tens++; michael@0: *string++ = SkToU8('0' + x / powerOfTen); michael@0: x %= powerOfTen; michael@0: } while (x != 0); michael@0: } michael@0: michael@0: SkASSERT(string - start <= SkStrAppendScalar_MaxSize); michael@0: return string; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // the 3 values are [length] [refcnt] [terminating zero data] michael@0: const SkString::Rec SkString::gEmptyRec = { 0, 0, 0 }; michael@0: michael@0: #define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec) michael@0: michael@0: static uint32_t trim_size_t_to_u32(size_t value) { michael@0: if (sizeof(size_t) > sizeof(uint32_t)) { michael@0: if (value > SK_MaxU32) { michael@0: value = SK_MaxU32; michael@0: } michael@0: } michael@0: return (uint32_t)value; michael@0: } michael@0: michael@0: static size_t check_add32(size_t base, size_t extra) { michael@0: SkASSERT(base <= SK_MaxU32); michael@0: if (sizeof(size_t) > sizeof(uint32_t)) { michael@0: if (base + extra > SK_MaxU32) { michael@0: extra = SK_MaxU32 - base; michael@0: } michael@0: } michael@0: return extra; michael@0: } michael@0: michael@0: SkString::Rec* SkString::AllocRec(const char text[], size_t len) { michael@0: Rec* rec; michael@0: michael@0: if (0 == len) { michael@0: rec = const_cast(&gEmptyRec); michael@0: } else { michael@0: len = trim_size_t_to_u32(len); michael@0: michael@0: // add 1 for terminating 0, then align4 so we can have some slop when growing the string michael@0: rec = (Rec*)sk_malloc_throw(SizeOfRec() + SkAlign4(len + 1)); michael@0: rec->fLength = SkToU32(len); michael@0: rec->fRefCnt = 1; michael@0: if (text) { michael@0: memcpy(rec->data(), text, len); michael@0: } michael@0: rec->data()[len] = 0; michael@0: } michael@0: return rec; michael@0: } michael@0: michael@0: SkString::Rec* SkString::RefRec(Rec* src) { michael@0: if (src != &gEmptyRec) { michael@0: sk_atomic_inc(&src->fRefCnt); michael@0: } michael@0: return src; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkString::validate() const { michael@0: // make sure know one has written over our global michael@0: SkASSERT(0 == gEmptyRec.fLength); michael@0: SkASSERT(0 == gEmptyRec.fRefCnt); michael@0: SkASSERT(0 == gEmptyRec.data()[0]); michael@0: michael@0: if (fRec != &gEmptyRec) { michael@0: SkASSERT(fRec->fLength > 0); michael@0: SkASSERT(fRec->fRefCnt > 0); michael@0: SkASSERT(0 == fRec->data()[fRec->fLength]); michael@0: } michael@0: SkASSERT(fStr == c_str()); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkString::SkString() : fRec(const_cast(&gEmptyRec)) { michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: michael@0: SkString::SkString(size_t len) { michael@0: fRec = AllocRec(NULL, len); michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: michael@0: SkString::SkString(const char text[]) { michael@0: size_t len = text ? strlen(text) : 0; michael@0: michael@0: fRec = AllocRec(text, len); michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: michael@0: SkString::SkString(const char text[], size_t len) { michael@0: fRec = AllocRec(text, len); michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: michael@0: SkString::SkString(const SkString& src) { michael@0: src.validate(); michael@0: michael@0: fRec = RefRec(src.fRec); michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: michael@0: SkString::~SkString() { michael@0: this->validate(); michael@0: michael@0: if (fRec->fLength) { michael@0: SkASSERT(fRec->fRefCnt > 0); michael@0: if (sk_atomic_dec(&fRec->fRefCnt) == 1) { michael@0: sk_free(fRec); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkString::equals(const SkString& src) const { michael@0: return fRec == src.fRec || this->equals(src.c_str(), src.size()); michael@0: } michael@0: michael@0: bool SkString::equals(const char text[]) const { michael@0: return this->equals(text, text ? strlen(text) : 0); michael@0: } michael@0: michael@0: bool SkString::equals(const char text[], size_t len) const { michael@0: SkASSERT(len == 0 || text != NULL); michael@0: michael@0: return fRec->fLength == len && !memcmp(fRec->data(), text, len); michael@0: } michael@0: michael@0: SkString& SkString::operator=(const SkString& src) { michael@0: this->validate(); michael@0: michael@0: if (fRec != src.fRec) { michael@0: SkString tmp(src); michael@0: this->swap(tmp); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: SkString& SkString::operator=(const char text[]) { michael@0: this->validate(); michael@0: michael@0: SkString tmp(text); michael@0: this->swap(tmp); michael@0: michael@0: return *this; michael@0: } michael@0: michael@0: void SkString::reset() { michael@0: this->validate(); michael@0: michael@0: if (fRec->fLength) { michael@0: SkASSERT(fRec->fRefCnt > 0); michael@0: if (sk_atomic_dec(&fRec->fRefCnt) == 1) { michael@0: sk_free(fRec); michael@0: } michael@0: } michael@0: michael@0: fRec = const_cast(&gEmptyRec); michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: michael@0: char* SkString::writable_str() { michael@0: this->validate(); michael@0: michael@0: if (fRec->fLength) { michael@0: if (fRec->fRefCnt > 1) { michael@0: Rec* rec = AllocRec(fRec->data(), fRec->fLength); michael@0: if (sk_atomic_dec(&fRec->fRefCnt) == 1) { michael@0: // In this case after our check of fRecCnt > 1, we suddenly michael@0: // did become the only owner, so now we have two copies of the michael@0: // data (fRec and rec), so we need to delete one of them. michael@0: sk_free(fRec); michael@0: } michael@0: fRec = rec; michael@0: #ifdef SK_DEBUG michael@0: fStr = fRec->data(); michael@0: #endif michael@0: } michael@0: } michael@0: return fRec->data(); michael@0: } michael@0: michael@0: void SkString::set(const char text[]) { michael@0: this->set(text, text ? strlen(text) : 0); michael@0: } michael@0: michael@0: void SkString::set(const char text[], size_t len) { michael@0: len = trim_size_t_to_u32(len); michael@0: michael@0: if (0 == len) { michael@0: this->reset(); michael@0: } else if (1 == fRec->fRefCnt && len <= fRec->fLength) { michael@0: // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))? michael@0: // just use less of the buffer without allocating a smaller one michael@0: char* p = this->writable_str(); michael@0: if (text) { michael@0: memcpy(p, text, len); michael@0: } michael@0: p[len] = 0; michael@0: fRec->fLength = SkToU32(len); michael@0: } else if (1 == fRec->fRefCnt && (fRec->fLength >> 2) == (len >> 2)) { michael@0: // we have spare room in the current allocation, so don't alloc a larger one michael@0: char* p = this->writable_str(); michael@0: if (text) { michael@0: memcpy(p, text, len); michael@0: } michael@0: p[len] = 0; michael@0: fRec->fLength = SkToU32(len); michael@0: } else { michael@0: SkString tmp(text, len); michael@0: this->swap(tmp); michael@0: } michael@0: } michael@0: michael@0: void SkString::setUTF16(const uint16_t src[]) { michael@0: int count = 0; michael@0: michael@0: while (src[count]) { michael@0: count += 1; michael@0: } michael@0: this->setUTF16(src, count); michael@0: } michael@0: michael@0: void SkString::setUTF16(const uint16_t src[], size_t count) { michael@0: count = trim_size_t_to_u32(count); michael@0: michael@0: if (0 == count) { michael@0: this->reset(); michael@0: } else if (count <= fRec->fLength) { michael@0: // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1)) michael@0: if (count < fRec->fLength) { michael@0: this->resize(count); michael@0: } michael@0: char* p = this->writable_str(); michael@0: for (size_t i = 0; i < count; i++) { michael@0: p[i] = SkToU8(src[i]); michael@0: } michael@0: p[count] = 0; michael@0: } else { michael@0: SkString tmp(count); // puts a null terminator at the end of the string michael@0: char* p = tmp.writable_str(); michael@0: michael@0: for (size_t i = 0; i < count; i++) { michael@0: p[i] = SkToU8(src[i]); michael@0: } michael@0: this->swap(tmp); michael@0: } michael@0: } michael@0: michael@0: void SkString::insert(size_t offset, const char text[]) { michael@0: this->insert(offset, text, text ? strlen(text) : 0); michael@0: } michael@0: michael@0: void SkString::insert(size_t offset, const char text[], size_t len) { michael@0: if (len) { michael@0: size_t length = fRec->fLength; michael@0: if (offset > length) { michael@0: offset = length; michael@0: } michael@0: michael@0: // Check if length + len exceeds 32bits, we trim len michael@0: len = check_add32(length, len); michael@0: if (0 == len) { michael@0: return; michael@0: } michael@0: michael@0: /* If we're the only owner, and we have room in our allocation for the insert, michael@0: do it in place, rather than allocating a new buffer. michael@0: michael@0: To know we have room, compare the allocated sizes michael@0: beforeAlloc = SkAlign4(length + 1) michael@0: afterAlloc = SkAligh4(length + 1 + len) michael@0: but SkAlign4(x) is (x + 3) >> 2 << 2 michael@0: which is equivalent for testing to (length + 1 + 3) >> 2 == (length + 1 + 3 + len) >> 2 michael@0: and we can then eliminate the +1+3 since that doesn't affec the answer michael@0: */ michael@0: if (1 == fRec->fRefCnt && (length >> 2) == ((length + len) >> 2)) { michael@0: char* dst = this->writable_str(); michael@0: michael@0: if (offset < length) { michael@0: memmove(dst + offset + len, dst + offset, length - offset); michael@0: } michael@0: memcpy(dst + offset, text, len); michael@0: michael@0: dst[length + len] = 0; michael@0: fRec->fLength = SkToU32(length + len); michael@0: } else { michael@0: /* Seems we should use realloc here, since that is safe if it fails michael@0: (we have the original data), and might be faster than alloc/copy/free. michael@0: */ michael@0: SkString tmp(fRec->fLength + len); michael@0: char* dst = tmp.writable_str(); michael@0: michael@0: if (offset > 0) { michael@0: memcpy(dst, fRec->data(), offset); michael@0: } michael@0: memcpy(dst + offset, text, len); michael@0: if (offset < fRec->fLength) { michael@0: memcpy(dst + offset + len, fRec->data() + offset, michael@0: fRec->fLength - offset); michael@0: } michael@0: michael@0: this->swap(tmp); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkString::insertUnichar(size_t offset, SkUnichar uni) { michael@0: char buffer[kMaxBytesInUTF8Sequence]; michael@0: size_t len = SkUTF8_FromUnichar(uni, buffer); michael@0: michael@0: if (len) { michael@0: this->insert(offset, buffer, len); michael@0: } michael@0: } michael@0: michael@0: void SkString::insertS32(size_t offset, int32_t dec) { michael@0: char buffer[SkStrAppendS32_MaxSize]; michael@0: char* stop = SkStrAppendS32(buffer, dec); michael@0: this->insert(offset, buffer, stop - buffer); michael@0: } michael@0: michael@0: void SkString::insertS64(size_t offset, int64_t dec, int minDigits) { michael@0: char buffer[SkStrAppendS64_MaxSize]; michael@0: char* stop = SkStrAppendS64(buffer, dec, minDigits); michael@0: this->insert(offset, buffer, stop - buffer); michael@0: } michael@0: michael@0: void SkString::insertU32(size_t offset, uint32_t dec) { michael@0: char buffer[SkStrAppendU32_MaxSize]; michael@0: char* stop = SkStrAppendU32(buffer, dec); michael@0: this->insert(offset, buffer, stop - buffer); michael@0: } michael@0: michael@0: void SkString::insertU64(size_t offset, uint64_t dec, int minDigits) { michael@0: char buffer[SkStrAppendU64_MaxSize]; michael@0: char* stop = SkStrAppendU64(buffer, dec, minDigits); michael@0: this->insert(offset, buffer, stop - buffer); michael@0: } michael@0: michael@0: void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) { michael@0: minDigits = SkPin32(minDigits, 0, 8); michael@0: michael@0: static const char gHex[] = "0123456789ABCDEF"; michael@0: michael@0: char buffer[8]; michael@0: char* p = buffer + sizeof(buffer); michael@0: michael@0: do { michael@0: *--p = gHex[hex & 0xF]; michael@0: hex >>= 4; michael@0: minDigits -= 1; michael@0: } while (hex != 0); michael@0: michael@0: while (--minDigits >= 0) { michael@0: *--p = '0'; michael@0: } michael@0: michael@0: SkASSERT(p >= buffer); michael@0: this->insert(offset, p, buffer + sizeof(buffer) - p); michael@0: } michael@0: michael@0: void SkString::insertScalar(size_t offset, SkScalar value) { michael@0: char buffer[SkStrAppendScalar_MaxSize]; michael@0: char* stop = SkStrAppendScalar(buffer, value); michael@0: this->insert(offset, buffer, stop - buffer); michael@0: } michael@0: michael@0: void SkString::printf(const char format[], ...) { michael@0: char buffer[kBufferSize]; michael@0: ARGS_TO_BUFFER(format, buffer, kBufferSize); michael@0: michael@0: this->set(buffer, strlen(buffer)); michael@0: } michael@0: michael@0: void SkString::appendf(const char format[], ...) { michael@0: char buffer[kBufferSize]; michael@0: ARGS_TO_BUFFER(format, buffer, kBufferSize); michael@0: michael@0: this->append(buffer, strlen(buffer)); michael@0: } michael@0: michael@0: void SkString::appendVAList(const char format[], va_list args) { michael@0: char buffer[kBufferSize]; michael@0: VSNPRINTF(buffer, kBufferSize, format, args); michael@0: michael@0: this->append(buffer, strlen(buffer)); michael@0: } michael@0: michael@0: void SkString::prependf(const char format[], ...) { michael@0: char buffer[kBufferSize]; michael@0: ARGS_TO_BUFFER(format, buffer, kBufferSize); michael@0: michael@0: this->prepend(buffer, strlen(buffer)); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkString::remove(size_t offset, size_t length) { michael@0: size_t size = this->size(); michael@0: michael@0: if (offset < size) { michael@0: if (offset + length > size) { michael@0: length = size - offset; michael@0: } michael@0: if (length > 0) { michael@0: SkASSERT(size > length); michael@0: SkString tmp(size - length); michael@0: char* dst = tmp.writable_str(); michael@0: const char* src = this->c_str(); michael@0: michael@0: if (offset) { michael@0: SkASSERT(offset <= tmp.size()); michael@0: memcpy(dst, src, offset); michael@0: } michael@0: size_t tail = size - offset - length; michael@0: SkASSERT((int32_t)tail >= 0); michael@0: if (tail) { michael@0: // SkASSERT(offset + length <= tmp.size()); michael@0: memcpy(dst + offset, src + offset + length, tail); michael@0: } michael@0: SkASSERT(dst[tmp.size()] == 0); michael@0: this->swap(tmp); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkString::swap(SkString& other) { michael@0: this->validate(); michael@0: other.validate(); michael@0: michael@0: SkTSwap(fRec, other.fRec); michael@0: #ifdef SK_DEBUG michael@0: SkTSwap(fStr, other.fStr); michael@0: #endif michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkString SkStringPrintf(const char* format, ...) { michael@0: SkString formattedOutput; michael@0: char buffer[kBufferSize]; michael@0: ARGS_TO_BUFFER(format, buffer, kBufferSize); michael@0: formattedOutput.set(buffer); michael@0: return formattedOutput; michael@0: } michael@0: michael@0: void SkStrSplit(const char* str, const char* delimiters, SkTArray* out) { michael@0: const char* end = str + strlen(str); michael@0: while (str != end) { michael@0: // Find a token. michael@0: const size_t len = strcspn(str, delimiters); michael@0: out->push_back().set(str, len); michael@0: str += len; michael@0: // Skip any delimiters. michael@0: str += strspn(str, delimiters); michael@0: } michael@0: } michael@0: michael@0: #undef VSNPRINTF michael@0: #undef SNPRINTF