diff options
author | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2014-10-25 15:36:53 -0700 |
---|---|---|
committer | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2014-10-25 15:36:53 -0700 |
commit | 22caaf22182c2ece9a368bace9e31efa52ca567a (patch) | |
tree | e8970adc63cad779c77d79918bed661680f716dd /src/core | |
parent | cd37682217b0dafacfe02a3a3d64e2c80a329cce (diff) | |
parent | 8caeae6e162faed40eb3ae2dce1a654789eff01c (diff) |
Merge branch 'master' into removing-icu-dependency
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/basetypes/MCData.cc | 36 | ||||
-rw-r--r-- | src/core/basetypes/MCData.h | 4 | ||||
-rw-r--r-- | src/core/basetypes/MCString.cc | 34 | ||||
-rw-r--r-- | src/core/basetypes/MCString.h | 2 |
4 files changed, 51 insertions, 25 deletions
diff --git a/src/core/basetypes/MCData.cc b/src/core/basetypes/MCData.cc index 5f2282d9..9845d77a 100644 --- a/src/core/basetypes/MCData.cc +++ b/src/core/basetypes/MCData.cc @@ -27,17 +27,29 @@ using namespace mailcore; -void Data::allocate(unsigned int length) +static int isPowerOfTwo (unsigned int x) { - length ++; - if (length < mAllocated) + return ((x != 0) && !(x & (x - 1))); +} + +void Data::allocate(unsigned int length, bool force) +{ + if (length <= mAllocated) return; - - if (mAllocated == 0) { - mAllocated = 4; + + if (force) { + mAllocated = length; } - while (length > mAllocated) { - mAllocated *= 2; + else { + if (!isPowerOfTwo(mAllocated)) { + mAllocated = 0; + } + if (mAllocated == 0) { + mAllocated = 4; + } + while (length > mAllocated) { + mAllocated *= 2; + } } mBytes = (char *) realloc(mBytes, mAllocated); @@ -68,7 +80,7 @@ Data::Data(const char * bytes, unsigned int length) { mBytes = NULL; reset(); - allocate(length); + allocate(length, true); appendBytes(bytes, length); } @@ -76,7 +88,7 @@ Data::Data(int capacity) { mBytes = NULL; reset(); - allocate(capacity); + allocate(capacity, true); } Data::~Data() @@ -483,7 +495,7 @@ String * Data::charsetWithFilteredHTML(bool filterHTML, String * hintCharset) #endif } -void Data::replaceWithAllocatedBytes(char * bytes, unsigned int length) +void Data::takeBytesOwnership(char * bytes, unsigned int length) { free(mBytes); mBytes = (char *) bytes; @@ -520,7 +532,7 @@ Data * Data::dataWithContentsOfFile(String * filename) } data = Data::data(); - data->replaceWithAllocatedBytes(buf, (unsigned int) stat_buf.st_size); + data->takeBytesOwnership(buf, (unsigned int) stat_buf.st_size); fclose(f); diff --git a/src/core/basetypes/MCData.h b/src/core/basetypes/MCData.h index 131cb722..43ffb30f 100644 --- a/src/core/basetypes/MCData.h +++ b/src/core/basetypes/MCData.h @@ -62,10 +62,10 @@ namespace mailcore { char * mBytes; unsigned int mLength; unsigned int mAllocated; - void allocate(unsigned int length); + void allocate(unsigned int length, bool force = false); void reset(); String * charsetWithFilteredHTMLWithoutHint(bool filterHTML); - void replaceWithAllocatedBytes(char * bytes, unsigned int length); + void takeBytesOwnership(char * bytes, unsigned int length); }; diff --git a/src/core/basetypes/MCString.cc b/src/core/basetypes/MCString.cc index a122c1e6..88c61a99 100644 --- a/src/core/basetypes/MCString.cc +++ b/src/core/basetypes/MCString.cc @@ -754,7 +754,7 @@ String::String(const UChar * unicodeChars) mUnicodeChars = NULL; reset(); if (unicodeChars != NULL) { - allocate(u_strlen(unicodeChars)); + allocate(u_strlen(unicodeChars), true); } appendCharacters(unicodeChars); } @@ -763,7 +763,7 @@ String::String(const UChar * unicodeChars, unsigned int length) { mUnicodeChars = NULL; reset(); - allocate(length); + allocate(length, true); appendCharactersLength(unicodeChars, length); } @@ -771,6 +771,7 @@ String::String(const char * UTF8Characters) { mUnicodeChars = NULL; reset(); + allocate((unsigned int) strlen(UTF8Characters), true); appendUTF8Characters(UTF8Characters); } @@ -792,7 +793,7 @@ String::String(const char * bytes, unsigned int length, const char * charset) { mUnicodeChars = NULL; reset(); - allocate(length); + allocate(length, true); if (charset == NULL) { appendUTF8CharactersLength(bytes, length); } @@ -806,17 +807,30 @@ String::~String() reset(); } -void String::allocate(unsigned int length) +static int isPowerOfTwo (unsigned int x) +{ + return ((x != 0) && !(x & (x - 1))); +} + +void String::allocate(unsigned int length, bool force) { length ++; - if (length < mAllocated) + if (length <= mAllocated) return; - - if (mAllocated == 0) { - mAllocated = 4; + + if (force) { + mAllocated = length; } - while (length > mAllocated) { - mAllocated *= 2; + else { + if (!isPowerOfTwo(mAllocated)) { + mAllocated = 0; + } + if (mAllocated == 0) { + mAllocated = 4; + } + while (length > mAllocated) { + mAllocated *= 2; + } } mUnicodeChars = (UChar *) realloc(mUnicodeChars, mAllocated * sizeof(* mUnicodeChars)); diff --git a/src/core/basetypes/MCString.h b/src/core/basetypes/MCString.h index 25439cf2..cad79c21 100644 --- a/src/core/basetypes/MCString.h +++ b/src/core/basetypes/MCString.h @@ -130,7 +130,7 @@ namespace mailcore { UChar * mUnicodeChars; unsigned int mLength; unsigned int mAllocated; - void allocate(unsigned int length); + void allocate(unsigned int length, bool force = false); void reset(); int compareWithCaseSensitive(String * otherString, bool caseSensitive); void appendBytes(const char * bytes, unsigned int length, const char * charset); |