| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights reserv
ed. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 | |
| 27 #include "wtf/text/CString.h" | 26 #include "wtf/text/CString.h" |
| 28 | 27 |
| 29 #include "wtf/PartitionAlloc.h" | 28 #include "wtf/PartitionAlloc.h" |
| 30 #include "wtf/Partitions.h" | 29 #include "wtf/Partitions.h" |
| 31 #include <string.h> | 30 #include <string.h> |
| 32 | 31 |
| 33 using namespace std; | 32 using namespace std; |
| 34 | 33 |
| 35 namespace WTF { | 34 namespace WTF { |
| 36 | 35 |
| 37 PassRefPtr<CStringBuffer> CStringBuffer::createUninitialized(size_t length) | 36 PassRefPtr<CStringBuffer> CStringBuffer::createUninitialized(size_t length) { |
| 38 { | 37 RELEASE_ASSERT(length < |
| 39 RELEASE_ASSERT(length < (numeric_limits<unsigned>::max() - sizeof(CStringBuf
fer))); | 38 (numeric_limits<unsigned>::max() - sizeof(CStringBuffer))); |
| 40 | 39 |
| 41 // The +1 is for the terminating NUL character. | 40 // The +1 is for the terminating NUL character. |
| 42 size_t size = sizeof(CStringBuffer) + length + 1; | 41 size_t size = sizeof(CStringBuffer) + length + 1; |
| 43 CStringBuffer* stringBuffer = static_cast<CStringBuffer*>(Partitions::buffer
Malloc(size, WTF_HEAP_PROFILER_TYPE_NAME(CStringBuffer))); | 42 CStringBuffer* stringBuffer = |
| 44 return adoptRef(new (stringBuffer) CStringBuffer(length)); | 43 static_cast<CStringBuffer*>(Partitions::bufferMalloc( |
| 44 size, WTF_HEAP_PROFILER_TYPE_NAME(CStringBuffer))); |
| 45 return adoptRef(new (stringBuffer) CStringBuffer(length)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void CStringBuffer::operator delete(void* ptr) | 48 void CStringBuffer::operator delete(void* ptr) { |
| 48 { | 49 Partitions::bufferFree(ptr); |
| 49 Partitions::bufferFree(ptr); | |
| 50 } | 50 } |
| 51 | 51 |
| 52 CString::CString(const char* str) | 52 CString::CString(const char* str) { |
| 53 { | 53 if (!str) |
| 54 if (!str) | 54 return; |
| 55 return; | |
| 56 | 55 |
| 57 init(str, strlen(str)); | 56 init(str, strlen(str)); |
| 58 } | 57 } |
| 59 | 58 |
| 60 CString::CString(const char* str, size_t length) | 59 CString::CString(const char* str, size_t length) { |
| 61 { | 60 if (!str) { |
| 62 if (!str) { | 61 ASSERT(!length); |
| 63 ASSERT(!length); | 62 return; |
| 64 return; | 63 } |
| 65 } | |
| 66 | 64 |
| 67 init(str, length); | 65 init(str, length); |
| 68 } | 66 } |
| 69 | 67 |
| 70 void CString::init(const char* str, size_t length) | 68 void CString::init(const char* str, size_t length) { |
| 71 { | 69 ASSERT(str); |
| 72 ASSERT(str); | |
| 73 | 70 |
| 74 m_buffer = CStringBuffer::createUninitialized(length); | 71 m_buffer = CStringBuffer::createUninitialized(length); |
| 75 memcpy(m_buffer->mutableData(), str, length); | 72 memcpy(m_buffer->mutableData(), str, length); |
| 76 m_buffer->mutableData()[length] = '\0'; | 73 m_buffer->mutableData()[length] = '\0'; |
| 77 } | 74 } |
| 78 | 75 |
| 79 char* CString::mutableData() | 76 char* CString::mutableData() { |
| 80 { | 77 copyBufferIfNeeded(); |
| 81 copyBufferIfNeeded(); | 78 if (!m_buffer) |
| 82 if (!m_buffer) | 79 return 0; |
| 83 return 0; | 80 return m_buffer->mutableData(); |
| 84 return m_buffer->mutableData(); | |
| 85 } | 81 } |
| 86 | 82 |
| 87 CString CString::newUninitialized(size_t length, char*& characterBuffer) | 83 CString CString::newUninitialized(size_t length, char*& characterBuffer) { |
| 88 { | 84 CString result; |
| 89 CString result; | 85 result.m_buffer = CStringBuffer::createUninitialized(length); |
| 90 result.m_buffer = CStringBuffer::createUninitialized(length); | 86 char* bytes = result.m_buffer->mutableData(); |
| 91 char* bytes = result.m_buffer->mutableData(); | 87 bytes[length] = '\0'; |
| 92 bytes[length] = '\0'; | 88 characterBuffer = bytes; |
| 93 characterBuffer = bytes; | 89 return result; |
| 94 return result; | |
| 95 } | 90 } |
| 96 | 91 |
| 97 void CString::copyBufferIfNeeded() | 92 void CString::copyBufferIfNeeded() { |
| 98 { | 93 if (!m_buffer || m_buffer->hasOneRef()) |
| 99 if (!m_buffer || m_buffer->hasOneRef()) | 94 return; |
| 100 return; | |
| 101 | 95 |
| 102 RefPtr<CStringBuffer> buffer = m_buffer.release(); | 96 RefPtr<CStringBuffer> buffer = m_buffer.release(); |
| 103 size_t length = buffer->length(); | 97 size_t length = buffer->length(); |
| 104 m_buffer = CStringBuffer::createUninitialized(length); | 98 m_buffer = CStringBuffer::createUninitialized(length); |
| 105 memcpy(m_buffer->mutableData(), buffer->data(), length + 1); | 99 memcpy(m_buffer->mutableData(), buffer->data(), length + 1); |
| 106 } | 100 } |
| 107 | 101 |
| 108 bool CString::isSafeToSendToAnotherThread() const | 102 bool CString::isSafeToSendToAnotherThread() const { |
| 109 { | 103 return !m_buffer || m_buffer->hasOneRef(); |
| 110 return !m_buffer || m_buffer->hasOneRef(); | |
| 111 } | 104 } |
| 112 | 105 |
| 113 bool operator==(const CString& a, const CString& b) | 106 bool operator==(const CString& a, const CString& b) { |
| 114 { | 107 if (a.isNull() != b.isNull()) |
| 115 if (a.isNull() != b.isNull()) | 108 return false; |
| 116 return false; | 109 if (a.length() != b.length()) |
| 117 if (a.length() != b.length()) | 110 return false; |
| 118 return false; | 111 return !memcmp(a.data(), b.data(), a.length()); |
| 119 return !memcmp(a.data(), b.data(), a.length()); | |
| 120 } | 112 } |
| 121 | 113 |
| 122 bool operator==(const CString& a, const char* b) | 114 bool operator==(const CString& a, const char* b) { |
| 123 { | 115 if (a.isNull() != !b) |
| 124 if (a.isNull() != !b) | 116 return false; |
| 125 return false; | 117 if (!b) |
| 126 if (!b) | 118 return true; |
| 127 return true; | 119 return !strcmp(a.data(), b); |
| 128 return !strcmp(a.data(), b); | |
| 129 } | 120 } |
| 130 | 121 |
| 131 } // namespace WTF | 122 } // namespace WTF |
| OLD | NEW |