code style

git-svn-id: http://skia.googlecode.com/svn/trunk@1095 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
mike@reedtribe.org 2011-04-10 01:04:37 +00:00
parent d5e05d4286
commit 4e1d3acc16
4 changed files with 248 additions and 324 deletions

View File

@ -78,25 +78,21 @@ public:
const char* c_str() const { return fRec->data(); }
char operator[](size_t n) const { return this->c_str()[n]; }
bool equals(const SkString&) const;
bool equals(const char text[]) const;
bool equals(const char text[], size_t len) const;
bool equals(const SkString&) const;
bool equals(const char text[]) const;
bool equals(const char text[], size_t len) const;
bool startsWith(const char prefix[]) const
{
bool startsWith(const char prefix[]) const {
return SkStrStartsWith(fRec->data(), prefix);
}
bool endsWith(const char suffix[]) const
{
bool endsWith(const char suffix[]) const {
return SkStrEndsWith(fRec->data(), suffix);
}
friend int operator==(const SkString& a, const SkString& b)
{
friend int operator==(const SkString& a, const SkString& b) {
return a.equals(b);
}
friend int operator!=(const SkString& a, const SkString& b)
{
friend int operator!=(const SkString& a, const SkString& b) {
return !a.equals(b);
}
@ -107,51 +103,52 @@ public:
char* writable_str();
char& operator[](size_t n) { return this->writable_str()[n]; }
void reset();
void resize(size_t len) { this->set(NULL, len); }
void set(const SkString& src) { *this = src; }
void set(const char text[]);
void set(const char text[], size_t len);
void setUTF16(const uint16_t[]);
void setUTF16(const uint16_t[], size_t len);
void reset();
void resize(size_t len) { this->set(NULL, len); }
void set(const SkString& src) { *this = src; }
void set(const char text[]);
void set(const char text[], size_t len);
void setUTF16(const uint16_t[]);
void setUTF16(const uint16_t[], size_t len);
void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); }
void insert(size_t offset, const char text[]);
void insert(size_t offset, const char text[], size_t len);
void insertUnichar(size_t offset, SkUnichar);
void insertS32(size_t offset, int32_t value);
void insertS64(size_t offset, int64_t value, int minDigits = 0);
void insertHex(size_t offset, uint32_t value, int minDigits = 0);
void insertScalar(size_t offset, SkScalar);
void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); }
void insert(size_t offset, const char text[]);
void insert(size_t offset, const char text[], size_t len);
void insertUnichar(size_t offset, SkUnichar);
void insertS32(size_t offset, int32_t value);
void insertS64(size_t offset, int64_t value, int minDigits = 0);
void insertHex(size_t offset, uint32_t value, int minDigits = 0);
void insertScalar(size_t offset, SkScalar);
void append(const SkString& str) { this->insert((size_t)-1, str); }
void append(const char text[]) { this->insert((size_t)-1, text); }
void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); }
void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
void appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
void appendS64(int64_t value, int minDigits = 0) { this->insertS64((size_t)-1, value, minDigits); }
void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
void append(const SkString& str) { this->insert((size_t)-1, str); }
void append(const char text[]) { this->insert((size_t)-1, text); }
void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); }
void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
void appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
void appendS64(int64_t value, int minDigits = 0) { this->insertS64((size_t)-1, value, minDigits); }
void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
void prepend(const SkString& str) { this->insert(0, str); }
void prepend(const char text[]) { this->insert(0, text); }
void prepend(const char text[], size_t len) { this->insert(0, text, len); }
void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); }
void prependS32(int32_t value) { this->insertS32(0, value); }
void prependS64(int32_t value, int minDigits = 0) { this->insertS64(0, value, minDigits); }
void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); }
void prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
void prepend(const SkString& str) { this->insert(0, str); }
void prepend(const char text[]) { this->insert(0, text); }
void prepend(const char text[], size_t len) { this->insert(0, text, len); }
void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); }
void prependS32(int32_t value) { this->insertS32(0, value); }
void prependS64(int32_t value, int minDigits = 0) { this->insertS64(0, value, minDigits); }
void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); }
void prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
void printf(const char format[], ...);
void appendf(const char format[], ...);
void prependf(const char format[], ...);
void printf(const char format[], ...);
void appendf(const char format[], ...);
void prependf(const char format[], ...);
void remove(size_t offset, size_t length);
void remove(size_t offset, size_t length);
/** Swap contents between this and other. This function is guaranteed
to never fail or throw.
*/
void swap(SkString& other);
/**
* Swap contents between this and other. This function is guaranteed
* to never fail or throw.
*/
void swap(SkString& other);
private:
struct Rec {
@ -185,6 +182,7 @@ public:
/** This returns the number of ucs2 characters
*/
int count() const { return fCount; }
/** This returns a null terminated ucs2 string
*/
const uint16_t* getUCS2() const { return fUCS2; }

View File

@ -19,7 +19,7 @@
#include "SkTypes.h"
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/** Similar to memset(), but it assigns a 16bit value into the buffer.
@param buffer The memory to have value copied into it
@ -54,7 +54,7 @@ extern SkMemset16Proc sk_memset16;
extern SkMemset32Proc sk_memset32;
#endif
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#define kMaxBytesInUTF8Sequence 4
@ -64,8 +64,7 @@ extern SkMemset32Proc sk_memset32;
#define SkUTF8_LeadByteToCount(c) ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
#endif
inline int SkUTF8_CountUTF8Bytes(const char utf8[])
{
inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
SkASSERT(utf8);
return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
}

View File

@ -42,15 +42,13 @@ static const size_t kBufferSize = 256;
///////////////////////////////////////////////////////////////////////////////
bool SkStrStartsWith(const char string[], const char prefix[])
{
bool SkStrStartsWith(const char string[], const char prefix[]) {
SkASSERT(string);
SkASSERT(prefix);
return !strncmp(string, prefix, strlen(prefix));
}
bool SkStrEndsWith(const char string[], const char suffix[])
{
bool SkStrEndsWith(const char string[], const char suffix[]) {
SkASSERT(string);
SkASSERT(suffix);
size_t strLen = strlen(string);
@ -59,50 +57,50 @@ bool SkStrEndsWith(const char string[], const char suffix[])
!strncmp(string + strLen - suffixLen, suffix, suffixLen);
}
int SkStrStartsWithOneOf(const char string[], const char prefixes[])
{
int SkStrStartsWithOneOf(const char string[], const char prefixes[]) {
int index = 0;
do {
const char* limit = strchr(prefixes, '\0');
if (!strncmp(string, prefixes, limit - prefixes))
if (!strncmp(string, prefixes, limit - prefixes)) {
return index;
}
prefixes = limit + 1;
index++;
} while (prefixes[0]);
return -1;
}
char* SkStrAppendS32(char string[], int32_t dec)
{
char* SkStrAppendS32(char string[], int32_t dec) {
SkDEBUGCODE(char* start = string;)
char buffer[SkStrAppendS32_MaxSize];
char* p = buffer + sizeof(buffer);
bool neg = false;
if (dec < 0)
{
if (dec < 0) {
neg = true;
dec = -dec;
}
do {
*--p = SkToU8('0' + dec % 10);
dec /= 10;
} while (dec != 0);
if (neg)
if (neg) {
*--p = '-';
}
SkASSERT(p >= buffer);
char* stop = buffer + sizeof(buffer);
while (p < stop)
while (p < stop) {
*string++ = *p++;
}
SkASSERT(string - start <= SkStrAppendS32_MaxSize);
return string;
}
char* SkStrAppendS64(char string[], int64_t dec, int minDigits)
{
char* SkStrAppendS64(char string[], int64_t dec, int minDigits) {
SkDEBUGCODE(char* start = string;)
char buffer[SkStrAppendS64_MaxSize];
@ -113,18 +111,21 @@ char* SkStrAppendS64(char string[], int64_t dec, int minDigits)
neg = true;
dec = -dec;
}
do {
*--p = SkToU8('0' + dec % 10);
dec /= 10;
minDigits--;
} while (dec != 0);
while (minDigits > 0) {
*--p = '0';
minDigits--;
}
if (neg)
*--p = '-';
if (neg) {
*--p = '-';
}
SkASSERT(p >= buffer);
size_t cp_len = buffer + sizeof(buffer) - p;
memcpy(string, p, cp_len);
@ -135,8 +136,7 @@ char* SkStrAppendS64(char string[], int64_t dec, int minDigits)
}
#ifdef SK_CAN_USE_FLOAT
char* SkStrAppendFloat(char string[], float value)
{
char* SkStrAppendFloat(char string[], float value) {
// since floats have at most 8 significant digits, we limit our %g to that.
static const char gFormat[] = "%.8g";
// make it 1 larger for the terminating 0
@ -148,27 +148,24 @@ char* SkStrAppendFloat(char string[], float value)
}
#endif
char* SkStrAppendFixed(char string[], SkFixed x)
{
char* SkStrAppendFixed(char string[], SkFixed x) {
SkDEBUGCODE(char* start = string;)
if (x < 0)
{
if (x < 0) {
*string++ = '-';
x = -x;
}
unsigned frac = x & 0xFFFF;
x >>= 16;
if (frac == 0xFFFF) // need to do this to "round up", since 65535/65536 is closer to 1 than to .9999
{
if (frac == 0xFFFF) {
// need to do this to "round up", since 65535/65536 is closer to 1 than to .9999
x += 1;
frac = 0;
}
string = SkStrAppendS32(string, x);
// now handle the fractional part (if any)
if (frac)
{
if (frac) {
static const uint16_t gTens[] = { 1000, 100, 10, 1 };
const uint16_t* tens = gTens;
@ -189,7 +186,7 @@ char* SkStrAppendFixed(char string[], SkFixed x)
return string;
}
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#define kMaxRefCnt_SkString SK_MaxU16
@ -198,47 +195,43 @@ const SkString::Rec SkString::gEmptyRec = { 0, 0, 0 };
#define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec)
SkString::Rec* SkString::AllocRec(const char text[], U16CPU len)
{
SkString::Rec* SkString::AllocRec(const char text[], U16CPU len) {
Rec* rec;
if (len == 0)
if (len == 0) {
rec = const_cast<Rec*>(&gEmptyRec);
else
{
} else {
// add 1 for terminating 0, then align4 so we can have some slop when growing the string
rec = (Rec*)sk_malloc_throw(SizeOfRec() + SkAlign4(len + 1));
rec->fLength = SkToU16(len);
rec->fRefCnt = 1;
if (text)
if (text) {
memcpy(rec->data(), text, len);
}
rec->data()[len] = 0;
}
return rec;
}
SkString::Rec* SkString::RefRec(Rec* src)
{
if (src != &gEmptyRec)
{
SkString::Rec* SkString::RefRec(Rec* src) {
if (src != &gEmptyRec) {
if (src->fRefCnt == kMaxRefCnt_SkString) {
src = AllocRec(src->data(), src->fLength);
} else
} else {
src->fRefCnt += 1;
}
}
return src;
}
#ifdef SK_DEBUG
void SkString::validate() const
{
void SkString::validate() const {
// make sure know one has written over our global
SkASSERT(gEmptyRec.fLength == 0);
SkASSERT(gEmptyRec.fRefCnt == 0);
SkASSERT(gEmptyRec.data()[0] == 0);
if (fRec != &gEmptyRec)
{
if (fRec != &gEmptyRec) {
SkASSERT(fRec->fLength > 0);
SkASSERT(fRec->fRefCnt > 0);
SkASSERT(fRec->data()[fRec->fLength] == 0);
@ -247,7 +240,7 @@ void SkString::validate() const
}
#endif
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SkString::SkString() : fRec(const_cast<Rec*>(&gEmptyRec)) {
#ifdef SK_DEBUG
@ -255,8 +248,7 @@ SkString::SkString() : fRec(const_cast<Rec*>(&gEmptyRec)) {
#endif
}
SkString::SkString(size_t len)
{
SkString::SkString(size_t len) {
SkASSERT(SkToU16(len) == len); // can't handle larger than 64K
fRec = AllocRec(NULL, (U16CPU)len);
@ -265,8 +257,7 @@ SkString::SkString(size_t len)
#endif
}
SkString::SkString(const char text[])
{
SkString::SkString(const char text[]) {
size_t len = text ? strlen(text) : 0;
fRec = AllocRec(text, (U16CPU)len);
@ -275,16 +266,14 @@ SkString::SkString(const char text[])
#endif
}
SkString::SkString(const char text[], size_t len)
{
SkString::SkString(const char text[], size_t len) {
fRec = AllocRec(text, (U16CPU)len);
#ifdef SK_DEBUG
fStr = fRec->data();
#endif
}
SkString::SkString(const SkString& src)
{
SkString::SkString(const SkString& src) {
src.validate();
fRec = RefRec(src.fRec);
@ -293,56 +282,49 @@ SkString::SkString(const SkString& src)
#endif
}
SkString::~SkString()
{
SkString::~SkString() {
this->validate();
if (fRec->fLength)
{
if (fRec->fLength) {
SkASSERT(fRec->fRefCnt > 0);
if (--fRec->fRefCnt == 0)
if (--fRec->fRefCnt == 0) {
sk_free(fRec);
}
}
}
bool SkString::equals(const SkString& src) const
{
bool SkString::equals(const SkString& src) const {
return fRec == src.fRec || this->equals(src.c_str(), src.size());
}
bool SkString::equals(const char text[]) const
{
bool SkString::equals(const char text[]) const {
return this->equals(text, text ? strlen(text) : 0);
}
bool SkString::equals(const char text[], size_t len) const
{
bool SkString::equals(const char text[], size_t len) const {
SkASSERT(len == 0 || text != NULL);
return fRec->fLength == len && !memcmp(fRec->data(), text, len);
}
SkString& SkString::operator=(const SkString& src)
{
SkString& SkString::operator=(const SkString& src) {
this->validate();
if (fRec != src.fRec)
{
if (fRec != src.fRec) {
SkString tmp(src);
this->swap(tmp);
}
return *this;
}
void SkString::reset()
{
void SkString::reset() {
this->validate();
if (fRec->fLength)
{
if (fRec->fLength) {
SkASSERT(fRec->fRefCnt > 0);
if (--fRec->fRefCnt == 0)
if (--fRec->fRefCnt == 0) {
sk_free(fRec);
}
}
fRec = const_cast<Rec*>(&gEmptyRec);
@ -351,14 +333,11 @@ void SkString::reset()
#endif
}
char* SkString::writable_str()
{
char* SkString::writable_str() {
this->validate();
if (fRec->fLength)
{
if (fRec->fRefCnt > 1)
{
if (fRec->fLength) {
if (fRec->fRefCnt > 1) {
fRec->fRefCnt -= 1;
fRec = AllocRec(fRec->data(), fRec->fLength);
#ifdef SK_DEBUG
@ -369,86 +348,79 @@ char* SkString::writable_str()
return fRec->data();
}
void SkString::set(const char text[])
{
void SkString::set(const char text[]) {
this->set(text, text ? strlen(text) : 0);
}
void SkString::set(const char text[], size_t len)
{
if (len == 0)
void SkString::set(const char text[], size_t len) {
if (len == 0) {
this->reset();
else if (fRec->fRefCnt == 1 && len <= fRec->fLength) // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))
{
} else if (fRec->fRefCnt == 1 && len <= fRec->fLength) {
// should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))?
// just use less of the buffer without allocating a smaller one
char* p = this->writable_str();
if (text)
if (text) {
memcpy(p, text, len);
}
p[len] = 0;
fRec->fLength = SkToU16(len);
}
else if (fRec->fRefCnt == 1 && ((unsigned)fRec->fLength >> 2) == (len >> 2))
{
} else if (fRec->fRefCnt == 1 && ((unsigned)fRec->fLength >> 2) == (len >> 2)) {
// we have spare room in the current allocation, so don't alloc a larger one
char* p = this->writable_str();
if (text)
if (text) {
memcpy(p, text, len);
}
p[len] = 0;
fRec->fLength = SkToU16(len);
}
else
{
} else {
SkString tmp(text, len);
this->swap(tmp);
}
}
void SkString::setUTF16(const uint16_t src[])
{
void SkString::setUTF16(const uint16_t src[]) {
int count = 0;
while (src[count])
while (src[count]) {
count += 1;
}
setUTF16(src, count);
}
void SkString::setUTF16(const uint16_t src[], size_t count)
{
if (count == 0)
void SkString::setUTF16(const uint16_t src[], size_t count) {
if (count == 0) {
this->reset();
else if (count <= fRec->fLength) // should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))
{
if (count < fRec->fLength)
} else if (count <= fRec->fLength) {
// should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))
if (count < fRec->fLength) {
this->resize(count);
}
char* p = this->writable_str();
for (size_t i = 0; i < count; i++)
for (size_t i = 0; i < count; i++) {
p[i] = SkToU8(src[i]);
}
p[count] = 0;
}
else
{
SkString tmp(count); // puts a null terminator at the end of the string
char* p = tmp.writable_str();
} else {
SkString tmp(count); // puts a null terminator at the end of the string
char* p = tmp.writable_str();
for (size_t i = 0; i < count; i++)
for (size_t i = 0; i < count; i++) {
p[i] = SkToU8(src[i]);
}
this->swap(tmp);
}
}
void SkString::insert(size_t offset, const char text[])
{
void SkString::insert(size_t offset, const char text[]) {
this->insert(offset, text, text ? strlen(text) : 0);
}
void SkString::insert(size_t offset, const char text[], size_t len)
{
if (len)
{
void SkString::insert(size_t offset, const char text[], size_t len) {
if (len) {
size_t length = fRec->fLength;
if (offset > length)
if (offset > length) {
offset = length;
}
/* If we're the only owner, and we have room in our allocation for the insert,
do it in place, rather than allocating a new buffer.
@ -460,61 +432,59 @@ void SkString::insert(size_t offset, const char text[], size_t len)
which is equivalent for testing to (length + 1 + 3) >> 2 == (length + 1 + 3 + len) >> 2
and we can then eliminate the +1+3 since that doesn't affec the answer
*/
if (fRec->fRefCnt == 1 && (length >> 2) == ((length + len) >> 2))
{
if (fRec->fRefCnt == 1 && (length >> 2) == ((length + len) >> 2)) {
char* dst = this->writable_str();
if (offset < length)
if (offset < length) {
memmove(dst + offset + len, dst + offset, length - offset);
}
memcpy(dst + offset, text, len);
dst[length + len] = 0;
fRec->fLength = SkToU16(length + len);
}
else
{
} else {
/* Seems we should use realloc here, since that is safe if it fails
(we have the original data), and might be faster than alloc/copy/free.
*/
SkString tmp(fRec->fLength + len);
char* dst = tmp.writable_str();
if (offset > 0)
if (offset > 0) {
memcpy(dst, fRec->data(), offset);
}
memcpy(dst + offset, text, len);
if (offset < fRec->fLength)
memcpy(dst + offset + len, fRec->data() + offset, fRec->fLength - offset);
if (offset < fRec->fLength) {
memcpy(dst + offset + len, fRec->data() + offset,
fRec->fLength - offset);
}
this->swap(tmp);
}
}
}
void SkString::insertUnichar(size_t offset, SkUnichar uni)
{
void SkString::insertUnichar(size_t offset, SkUnichar uni) {
char buffer[kMaxBytesInUTF8Sequence];
size_t len = SkUTF8_FromUnichar(uni, buffer);
if (len)
if (len) {
this->insert(offset, buffer, len);
}
}
void SkString::insertS32(size_t offset, int32_t dec)
{
void SkString::insertS32(size_t offset, int32_t dec) {
char buffer[SkStrAppendS32_MaxSize];
char* stop = SkStrAppendS32(buffer, dec);
this->insert(offset, buffer, stop - buffer);
}
void SkString::insertS64(size_t offset, int64_t dec, int minDigits)
{
void SkString::insertS64(size_t offset, int64_t dec, int minDigits) {
char buffer[SkStrAppendS64_MaxSize];
char* stop = SkStrAppendS64(buffer, dec, minDigits);
this->insert(offset, buffer, stop - buffer);
}
void SkString::insertHex(size_t offset, uint32_t hex, int minDigits)
{
void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) {
minDigits = SkPin32(minDigits, 0, 8);
static const char gHex[] = "0123456789ABCDEF";
@ -527,15 +497,16 @@ void SkString::insertHex(size_t offset, uint32_t hex, int minDigits)
hex >>= 4;
minDigits -= 1;
} while (hex != 0);
while (--minDigits >= 0)
while (--minDigits >= 0) {
*--p = '0';
}
SkASSERT(p >= buffer);
this->insert(offset, p, buffer + sizeof(buffer) - p);
}
void SkString::insertScalar(size_t offset, SkScalar value)
{
void SkString::insertScalar(size_t offset, SkScalar value) {
char buffer[SkStrAppendScalar_MaxSize];
char* stop = SkStrAppendScalar(buffer, value);
this->insert(offset, buffer, stop - buffer);
@ -566,30 +537,26 @@ void SkString::prependf(const char format[], ...) {
///////////////////////////////////////////////////////////////////////////////
void SkString::remove(size_t offset, size_t length)
{
void SkString::remove(size_t offset, size_t length) {
size_t size = this->size();
if (offset < size)
{
if (offset + length > size)
if (offset < size) {
if (offset + length > size) {
length = size - offset;
if (length > 0)
{
}
if (length > 0) {
SkASSERT(size > length);
SkString tmp(size - length);
char* dst = tmp.writable_str();
const char* src = this->c_str();
if (offset)
{
if (offset) {
SkASSERT(offset <= tmp.size());
memcpy(dst, src, offset);
}
size_t tail = size - offset - length;
SkASSERT((int32_t)tail >= 0);
if (tail)
{
if (tail) {
// SkASSERT(offset + length <= tmp.size());
memcpy(dst + offset, src + offset + length, tail);
}
@ -599,8 +566,7 @@ void SkString::remove(size_t offset, size_t length)
}
}
void SkString::swap(SkString& other)
{
void SkString::swap(SkString& other) {
this->validate();
other.validate();
@ -610,7 +576,7 @@ void SkString::swap(SkString& other)
#endif
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SkAutoUCS2::SkAutoUCS2(const char utf8[]) {
size_t len = strlen(utf8);

View File

@ -43,18 +43,17 @@
} while (0)
#endif
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void sk_memset16_portable(uint16_t dst[], uint16_t value, int count)
{
void sk_memset16_portable(uint16_t dst[], uint16_t value, int count) {
SkASSERT(dst != NULL && count >= 0);
if (count <= 0)
if (count <= 0) {
return;
}
// not sure if this helps to short-circuit on small values of count
if (count < 8)
{
if (count < 8) {
do {
*dst++ = (uint16_t)value;
} while (--count != 0);
@ -62,8 +61,7 @@ void sk_memset16_portable(uint16_t dst[], uint16_t value, int count)
}
// ensure we're on a long boundary
if ((size_t)dst & 2)
{
if ((size_t)dst & 2) {
*dst++ = (uint16_t)value;
count -= 1;
}
@ -73,8 +71,7 @@ void sk_memset16_portable(uint16_t dst[], uint16_t value, int count)
// handle the bulk with our unrolled macro
{
int sixteenlongs = count >> 5;
if (sixteenlongs)
{
if (sixteenlongs) {
uint32_t* dst32 = (uint32_t*)dst;
do {
assign_16_longs(dst32, value32);
@ -87,8 +84,7 @@ void sk_memset16_portable(uint16_t dst[], uint16_t value, int count)
// handle (most) of the rest
{
int longs = count >> 1;
if (longs)
{
if (longs) {
do {
*(uint32_t*)dst = value32;
dst += 2;
@ -97,27 +93,23 @@ void sk_memset16_portable(uint16_t dst[], uint16_t value, int count)
}
// cleanup a possible trailing short
if (count & 1)
if (count & 1) {
*dst = (uint16_t)value;
}
}
void sk_memset32_portable(uint32_t dst[], uint32_t value, int count)
{
void sk_memset32_portable(uint32_t dst[], uint32_t value, int count) {
SkASSERT(dst != NULL && count >= 0);
{
int sixteenlongs = count >> 4;
if (sixteenlongs)
{
do {
assign_16_longs(dst, value);
} while (--sixteenlongs != 0);
count &= 15;
}
int sixteenlongs = count >> 4;
if (sixteenlongs) {
do {
assign_16_longs(dst, value);
} while (--sixteenlongs != 0);
count &= 15;
}
if (count)
{
if (count) {
do {
*dst++ = value;
} while (--count != 0);
@ -125,8 +117,7 @@ void sk_memset32_portable(uint32_t dst[], uint32_t value, int count)
}
#if !defined(ANDROID) || defined(SK_BUILD_FOR_ANDROID_NDK)
static void sk_memset16_stub(uint16_t dst[], uint16_t value, int count)
{
static void sk_memset16_stub(uint16_t dst[], uint16_t value, int count) {
SkMemset16Proc proc = SkMemset16GetPlatformProc();
sk_memset16 = proc ? proc : sk_memset16_portable;
sk_memset16(dst, value, count);
@ -134,8 +125,7 @@ static void sk_memset16_stub(uint16_t dst[], uint16_t value, int count)
SkMemset16Proc sk_memset16 = sk_memset16_stub;
static void sk_memset32_stub(uint32_t dst[], uint32_t value, int count)
{
static void sk_memset32_stub(uint32_t dst[], uint32_t value, int count) {
SkMemset32Proc proc = SkMemset32GetPlatformProc();
sk_memset32 = proc ? proc : sk_memset32_portable;
sk_memset32(dst, value, count);
@ -145,7 +135,7 @@ SkMemset32Proc sk_memset32 = sk_memset32_stub;
#endif
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* 0xxxxxxx 1 total
10xxxxxx // never a leading byte
@ -159,14 +149,12 @@ SkMemset32Proc sk_memset32 = sk_memset32_stub;
*/
#ifdef SK_DEBUG
static void assert_utf8_leadingbyte(unsigned c)
{
static void assert_utf8_leadingbyte(unsigned c) {
SkASSERT(c <= 0xF7); // otherwise leading byte is too big (more than 4 bytes)
SkASSERT((c & 0xC0) != 0x80); // can't begin with a middle char
}
int SkUTF8_LeadByteToCount(unsigned c)
{
int SkUTF8_LeadByteToCount(unsigned c) {
assert_utf8_leadingbyte(c);
return (((0xE5 << 24) >> (c >> 4 << 1)) & 3) + 1;
}
@ -174,41 +162,36 @@ SkMemset32Proc sk_memset32 = sk_memset32_stub;
#define assert_utf8_leadingbyte(c)
#endif
int SkUTF8_CountUnichars(const char utf8[])
{
int SkUTF8_CountUnichars(const char utf8[]) {
SkASSERT(utf8);
int count = 0;
for (;;)
{
for (;;) {
int c = *(const uint8_t*)utf8;
if (c == 0)
if (c == 0) {
break;
}
utf8 += SkUTF8_LeadByteToCount(c);
count += 1;
}
return count;
}
int SkUTF8_CountUnichars(const char utf8[], size_t byteLength)
{
int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) {
SkASSERT(NULL != utf8 || 0 == byteLength);
int count = 0;
const char* stop = utf8 + byteLength;
while (utf8 < stop)
{
while (utf8 < stop) {
utf8 += SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
count += 1;
}
return count;
}
SkUnichar SkUTF8_ToUnichar(const char utf8[])
{
SkUnichar SkUTF8_ToUnichar(const char utf8[]) {
SkASSERT(NULL != utf8);
const uint8_t* p = (const uint8_t*)utf8;
@ -217,8 +200,7 @@ SkUnichar SkUTF8_ToUnichar(const char utf8[])
assert_utf8_leadingbyte(c);
if (hic < 0)
{
if (hic < 0) {
uint32_t mask = (uint32_t)~0x3F;
hic <<= 1;
do {
@ -230,8 +212,7 @@ SkUnichar SkUTF8_ToUnichar(const char utf8[])
return c;
}
SkUnichar SkUTF8_NextUnichar(const char** ptr)
{
SkUnichar SkUTF8_NextUnichar(const char** ptr) {
SkASSERT(NULL != ptr && NULL != *ptr);
const uint8_t* p = (const uint8_t*)*ptr;
@ -240,8 +221,7 @@ SkUnichar SkUTF8_NextUnichar(const char** ptr)
assert_utf8_leadingbyte(c);
if (hic < 0)
{
if (hic < 0) {
uint32_t mask = (uint32_t)~0x3F;
hic <<= 1;
do {
@ -254,32 +234,31 @@ SkUnichar SkUTF8_NextUnichar(const char** ptr)
return c;
}
SkUnichar SkUTF8_PrevUnichar(const char** ptr)
{
SkUnichar SkUTF8_PrevUnichar(const char** ptr) {
SkASSERT(NULL != ptr && NULL != *ptr);
const char* p = *ptr;
if (*--p & 0x80)
while (*--p & 0x40)
if (*--p & 0x80) {
while (*--p & 0x40) {
;
}
}
*ptr = (char*)p;
return SkUTF8_NextUnichar(&p);
}
size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[])
{
if ((uint32_t)uni > 0x10FFFF)
{
size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[]) {
if ((uint32_t)uni > 0x10FFFF) {
SkASSERT(!"bad unichar");
return 0;
}
if (uni <= 127)
{
if (utf8)
if (uni <= 127) {
if (utf8) {
*utf8 = (char)uni;
}
return 1;
}
@ -289,19 +268,18 @@ size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[])
SkDEBUGCODE(SkUnichar orig = uni;)
while (uni > 0x7F >> count)
{
while (uni > 0x7F >> count) {
*p++ = (char)(0x80 | (uni & 0x3F));
uni >>= 6;
count += 1;
}
if (utf8)
{
if (utf8) {
p = tmp;
utf8 += count;
while (p < tmp + count - 1)
while (p < tmp + count - 1) {
*--utf8 = *p++;
}
*--utf8 = (char)(~(0xFF >> count) | uni);
}
@ -309,19 +287,16 @@ size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[])
return count;
}
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int SkUTF16_CountUnichars(const uint16_t src[])
{
int SkUTF16_CountUnichars(const uint16_t src[]) {
SkASSERT(src);
int count = 0;
unsigned c;
while ((c = *src++) != 0)
{
while ((c = *src++) != 0) {
SkASSERT(!SkUTF16_IsLowSurrogate(c));
if (SkUTF16_IsHighSurrogate(c))
{
if (SkUTF16_IsHighSurrogate(c)) {
c = *src++;
SkASSERT(SkUTF16_IsLowSurrogate(c));
}
@ -330,18 +305,15 @@ int SkUTF16_CountUnichars(const uint16_t src[])
return count;
}
int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues)
{
int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues) {
SkASSERT(src);
const uint16_t* stop = src + numberOf16BitValues;
int count = 0;
while (src < stop)
{
while (src < stop) {
unsigned c = *src++;
SkASSERT(!SkUTF16_IsLowSurrogate(c));
if (SkUTF16_IsHighSurrogate(c))
{
if (SkUTF16_IsHighSurrogate(c)) {
SkASSERT(src < stop);
c = *src++;
SkASSERT(SkUTF16_IsLowSurrogate(c));
@ -351,16 +323,14 @@ int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues)
return count;
}
SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr)
{
SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr) {
SkASSERT(srcPtr && *srcPtr);
const uint16_t* src = *srcPtr;
SkUnichar c = *src++;
SkASSERT(!SkUTF16_IsLowSurrogate(c));
if (SkUTF16_IsHighSurrogate(c))
{
if (SkUTF16_IsHighSurrogate(c)) {
unsigned c2 = *src++;
SkASSERT(SkUTF16_IsLowSurrogate(c2));
@ -372,16 +342,14 @@ SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr)
return c;
}
SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr)
{
SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr) {
SkASSERT(srcPtr && *srcPtr);
const uint16_t* src = *srcPtr;
SkUnichar c = *--src;
SkASSERT(!SkUTF16_IsHighSurrogate(c));
if (SkUTF16_IsLowSurrogate(c))
{
if (SkUTF16_IsLowSurrogate(c)) {
unsigned c2 = *--src;
SkASSERT(SkUTF16_IsHighSurrogate(c2));
c = (c2 << 10) + c + (0x10000 - (0xD800 << 10) - 0xDC00);
@ -390,16 +358,13 @@ SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr)
return c;
}
size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[])
{
size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[]) {
SkASSERT((unsigned)uni <= 0x10FFFF);
int extra = (uni > 0xFFFF);
if (dst)
{
if (extra)
{
if (dst) {
if (extra) {
// dst[0] = SkToU16(0xD800 | ((uni - 0x10000) >> 10));
// dst[0] = SkToU16(0xD800 | ((uni >> 10) - 64));
dst[0] = SkToU16((0xD800 - 64) + (uni >> 10));
@ -407,9 +372,7 @@ size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[])
SkASSERT(SkUTF16_IsHighSurrogate(dst[0]));
SkASSERT(SkUTF16_IsLowSurrogate(dst[1]));
}
else
{
} else {
dst[0] = SkToU16(uni);
SkASSERT(!SkUTF16_IsHighSurrogate(dst[0]));
SkASSERT(!SkUTF16_IsLowSurrogate(dst[0]));
@ -418,46 +381,44 @@ size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[])
return 1 + extra;
}
size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues, char utf8[])
{
size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
char utf8[]) {
SkASSERT(numberOf16BitValues >= 0);
if (numberOf16BitValues <= 0)
if (numberOf16BitValues <= 0) {
return 0;
}
SkASSERT(utf16 != NULL);
const uint16_t* stop = utf16 + numberOf16BitValues;
size_t size = 0;
if (utf8 == NULL) // just count
{
while (utf16 < stop)
if (utf8 == NULL) { // just count
while (utf16 < stop) {
size += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), NULL);
}
else
{
}
} else {
char* start = utf8;
while (utf16 < stop)
while (utf16 < stop) {
utf8 += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), utf8);
}
size = utf8 - start;
}
return size;
}
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#if 0
static int round_to_K(size_t bytes)
{
static int round_to_K(size_t bytes) {
return (bytes + 512) >> 10;
}
#endif
SkAutoMemoryUsageProbe::SkAutoMemoryUsageProbe(const char label[])
: fLabel(label)
{
: fLabel(label) {
#if 0
struct mallinfo mi = mallinfo();
@ -465,14 +426,14 @@ SkAutoMemoryUsageProbe::SkAutoMemoryUsageProbe(const char label[])
#endif
}
SkAutoMemoryUsageProbe::~SkAutoMemoryUsageProbe()
{
SkAutoMemoryUsageProbe::~SkAutoMemoryUsageProbe() {
#if 0
struct mallinfo mi = mallinfo();
printf("SkAutoMemoryUsageProbe ");
if (fLabel)
if (fLabel) {
printf("<%s> ", fLabel);
}
printf("delta %dK, current total allocated %dK\n",
round_to_K(mi.uordblks - fBytesAllocated),
round_to_K(mi.uordblks));