| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights | 2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights |
| 3 * reserved. | 3 * reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #define CString_h | 28 #define CString_h |
| 29 | 29 |
| 30 #include "wtf/Allocator.h" | 30 #include "wtf/Allocator.h" |
| 31 #include "wtf/RefCounted.h" | 31 #include "wtf/RefCounted.h" |
| 32 #include "wtf/RefPtr.h" | 32 #include "wtf/RefPtr.h" |
| 33 #include "wtf/WTFExport.h" | 33 #include "wtf/WTFExport.h" |
| 34 #include <string.h> | 34 #include <string.h> |
| 35 | 35 |
| 36 namespace WTF { | 36 namespace WTF { |
| 37 | 37 |
| 38 // CStringBuffer is an immutable ref-counted storage for the characters in a | 38 // CStringImpl is an immutable ref-counted storage for the characters in a |
| 39 // CString. It's analogous to a StringImpl but may contain any arbitrary | 39 // CString. It's analogous to a StringImpl but may contain any arbitrary |
| 40 // sequence of bytes. The data is always allocated 1 longer than length() and is | 40 // sequence of bytes. The data is always allocated 1 longer than length() and is |
| 41 // null terminated. | 41 // null terminated. |
| 42 // TODO(esprehn): Rename to CStringImpl. | 42 class WTF_EXPORT CStringImpl : public RefCounted<CStringImpl> { |
| 43 class WTF_EXPORT CStringBuffer : public RefCounted<CStringBuffer> { | 43 WTF_MAKE_NONCOPYABLE(CStringImpl); |
| 44 WTF_MAKE_NONCOPYABLE(CStringBuffer); | |
| 45 | 44 |
| 46 public: | 45 public: |
| 47 // CStringBuffers are allocated out of the WTF buffer partition. | 46 // CStringImpls are allocated out of the WTF buffer partition. |
| 48 void* operator new(size_t, void* ptr) { return ptr; } | 47 void* operator new(size_t, void* ptr) { return ptr; } |
| 49 void operator delete(void*); | 48 void operator delete(void*); |
| 50 | 49 |
| 51 static PassRefPtr<CStringBuffer> createUninitialized(size_t length, | 50 static PassRefPtr<CStringImpl> createUninitialized(size_t length, |
| 52 char*& data); | 51 char*& data); |
| 53 | 52 |
| 54 const char* data() const { return reinterpret_cast<const char*>(this + 1); } | 53 const char* data() const { return reinterpret_cast<const char*>(this + 1); } |
| 55 size_t length() const { return m_length; } | 54 size_t length() const { return m_length; } |
| 56 | 55 |
| 57 private: | 56 private: |
| 58 explicit CStringBuffer(size_t length) : m_length(length) {} | 57 explicit CStringImpl(size_t length) : m_length(length) {} |
| 59 | 58 |
| 60 const unsigned m_length; | 59 const unsigned m_length; |
| 61 }; | 60 }; |
| 62 | 61 |
| 63 // A container for an immutable ref-counted null-terminated char array. This is | 62 // A container for an immutable ref-counted null-terminated char array. This is |
| 64 // analogous to a WTF::String but does not require the contained bytes to be | 63 // analogous to a WTF::String but does not require the contained bytes to be |
| 65 // valid Latin1 or UTF-16. Instead a CString can contain any arbitrary bytes. | 64 // valid Latin1 or UTF-16. Instead a CString can contain any arbitrary bytes. |
| 66 class WTF_EXPORT CString { | 65 class WTF_EXPORT CString { |
| 67 USING_FAST_MALLOC(CString); | 66 USING_FAST_MALLOC(CString); |
| 68 | 67 |
| 69 public: | 68 public: |
| 70 // Construct a null string, distinguishable from an empty string. | 69 // Construct a null string, distinguishable from an empty string. |
| 71 CString() {} | 70 CString() {} |
| 72 | 71 |
| 73 // Construct a string from arbitrary bytes. | 72 // Construct a string from arbitrary bytes. |
| 74 CString(const char* chars) : CString(chars, chars ? strlen(chars) : 0) {} | 73 CString(const char* chars) : CString(chars, chars ? strlen(chars) : 0) {} |
| 75 CString(const char*, size_t length); | 74 CString(const char*, size_t length); |
| 76 | 75 |
| 77 // Construct a string referencing an existing buffer. | 76 // Construct a string referencing an existing buffer. |
| 78 CString(CStringBuffer* buffer) : m_buffer(buffer) {} | 77 CString(CStringImpl* buffer) : m_buffer(buffer) {} |
| 79 CString(PassRefPtr<CStringBuffer> buffer) : m_buffer(buffer) {} | 78 CString(PassRefPtr<CStringImpl> buffer) : m_buffer(buffer) {} |
| 80 | 79 |
| 81 // TODO(esprehn): Rename to createUninitialized. | 80 static CString createUninitialized(size_t length, char*& data) { |
| 82 static CString newUninitialized(size_t length, char*& data) { | 81 return CStringImpl::createUninitialized(length, data); |
| 83 return CStringBuffer::createUninitialized(length, data); | |
| 84 } | 82 } |
| 85 | 83 |
| 86 // The bytes of the string, always NUL terminated. May be null. | 84 // The bytes of the string, always NUL terminated. May be null. |
| 87 const char* data() const { return m_buffer ? m_buffer->data() : 0; } | 85 const char* data() const { return m_buffer ? m_buffer->data() : 0; } |
| 88 | 86 |
| 89 // The length of the data(), *not* including the NUL terminator. | 87 // The length of the data(), *not* including the NUL terminator. |
| 90 size_t length() const { return m_buffer ? m_buffer->length() : 0; } | 88 size_t length() const { return m_buffer ? m_buffer->length() : 0; } |
| 91 | 89 |
| 92 bool isNull() const { return !m_buffer; } | 90 bool isNull() const { return !m_buffer; } |
| 93 | 91 |
| 94 bool isSafeToSendToAnotherThread() const; | 92 bool isSafeToSendToAnotherThread() const; |
| 95 | 93 |
| 96 // TODO(esprehn): Rename to impl() when CStringBuffer is renamed. | 94 CStringImpl* impl() const { return m_buffer.get(); } |
| 97 CStringBuffer* buffer() const { return m_buffer.get(); } | |
| 98 | 95 |
| 99 private: | 96 private: |
| 100 RefPtr<CStringBuffer> m_buffer; | 97 RefPtr<CStringImpl> m_buffer; |
| 101 }; | 98 }; |
| 102 | 99 |
| 103 WTF_EXPORT bool operator==(const CString& a, const CString& b); | 100 WTF_EXPORT bool operator==(const CString& a, const CString& b); |
| 104 inline bool operator!=(const CString& a, const CString& b) { | 101 inline bool operator!=(const CString& a, const CString& b) { |
| 105 return !(a == b); | 102 return !(a == b); |
| 106 } | 103 } |
| 107 WTF_EXPORT bool operator==(const CString& a, const char* b); | 104 WTF_EXPORT bool operator==(const CString& a, const char* b); |
| 108 inline bool operator!=(const CString& a, const char* b) { | 105 inline bool operator!=(const CString& a, const char* b) { |
| 109 return !(a == b); | 106 return !(a == b); |
| 110 } | 107 } |
| 111 | 108 |
| 112 // Pretty printer for gtest and base/logging.*. It prepends and appends | 109 // Pretty printer for gtest and base/logging.*. It prepends and appends |
| 113 // double-quotes, and escapes characters other than ASCII printables. | 110 // double-quotes, and escapes characters other than ASCII printables. |
| 114 WTF_EXPORT std::ostream& operator<<(std::ostream&, const CString&); | 111 WTF_EXPORT std::ostream& operator<<(std::ostream&, const CString&); |
| 115 | 112 |
| 116 } // namespace WTF | 113 } // namespace WTF |
| 117 | 114 |
| 118 using WTF::CString; | 115 using WTF::CString; |
| 119 | 116 |
| 120 #endif // CString_h | 117 #endif // CString_h |
| OLD | NEW |