Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(616)

Side by Side Diff: third_party/WebKit/Source/wtf/text/CString.h

Issue 2345893002: Make CString more similar to WTF::String. (Closed)
Patch Set: asserts. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/text/CString.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 12 matching lines...) Expand all
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 #ifndef CString_h 26 #ifndef CString_h
27 #define CString_h 27 #define CString_h
28 28
29 #include "wtf/Allocator.h" 29 #include "wtf/Allocator.h"
30 #include "wtf/RefCounted.h" 30 #include "wtf/RefCounted.h"
31 #include "wtf/RefPtr.h" 31 #include "wtf/RefPtr.h"
32 #include "wtf/WTFExport.h" 32 #include "wtf/WTFExport.h"
33 #include <string.h>
33 34
34 namespace WTF { 35 namespace WTF {
35 36
36 // CStringBuffer is the ref-counted storage class for the characters in a CStrin g. 37 // CStringBuffer is an immutable ref-counted storage for the characters in a
37 // The data is implicitly allocated 1 character longer than length(), as it is z ero-terminated. 38 // CString. It's analogous to a StringImpl but may contain any arbitrary
39 // sequence of bytes. The data is always allocated 1 longer than length() and is
40 // null terminated.
41 // TODO(esprehn): Rename to CStringImpl.
38 class WTF_EXPORT CStringBuffer : public RefCounted<CStringBuffer> { 42 class WTF_EXPORT CStringBuffer : public RefCounted<CStringBuffer> {
43 WTF_MAKE_NONCOPYABLE(CStringBuffer);
39 public: 44 public:
40 const char* data() { return mutableData(); }
41 size_t length() const { return m_length; }
42
43 private:
44 friend class CString;
45 friend class RefCounted<CStringBuffer>;
46 // CStringBuffers are allocated out of the WTF buffer partition. 45 // CStringBuffers are allocated out of the WTF buffer partition.
47 void* operator new(size_t, void* ptr) { return ptr; } 46 void* operator new(size_t, void* ptr) { return ptr; }
48 void operator delete(void*); 47 void operator delete(void*);
49 48
50 static PassRefPtr<CStringBuffer> createUninitialized(size_t length); 49 static PassRefPtr<CStringBuffer> createUninitialized(size_t length, char*& d ata);
51 50
52 CStringBuffer(size_t length) : m_length(length) { } 51 const char* data() const { return reinterpret_cast<const char*>(this + 1); }
53 char* mutableData() { return reinterpret_cast<char*>(this + 1); } 52 size_t length() const { return m_length; }
53
54 private:
55 explicit CStringBuffer(size_t length)
56 : m_length(length) {}
54 57
55 const unsigned m_length; 58 const unsigned m_length;
56 }; 59 };
57 60
58 // A container for a null-terminated char array supporting copy-on-write 61 // A container for an immutable ref-counted null-terminated char array. This is
59 // assignment. The contained char array may be null. 62 // analogous to a WTF::String but does not require the contained bytes to be
63 // valid Latin1 or UTF-16. Instead a CString can contain any arbitrary bytes.
60 class WTF_EXPORT CString { 64 class WTF_EXPORT CString {
61 USING_FAST_MALLOC(CString); 65 USING_FAST_MALLOC(CString);
62 public: 66 public:
63 CString() { } 67 // Construct a null string, distinguishable from an empty string.
64 CString(const char*); 68 CString() {}
69
70 // Construct a string from arbitrary bytes.
71 CString(const char* chars)
72 : CString(chars, chars ? strlen(chars) : 0) {}
65 CString(const char*, size_t length); 73 CString(const char*, size_t length);
66 CString(CStringBuffer* buffer) : m_buffer(buffer) { }
67 static CString newUninitialized(size_t length, char*& characterBuffer);
68 74
69 const char* data() const 75 // Construct a string referencing an existing buffer.
76 CString(CStringBuffer* buffer)
77 : m_buffer(buffer) {}
78 CString(PassRefPtr<CStringBuffer> buffer)
79 : m_buffer(buffer) {}
80
81 // TODO(esprehn): Rename to createUninitialized.
82 static CString newUninitialized(size_t length, char*& data)
70 { 83 {
71 return m_buffer ? m_buffer->data() : 0; 84 return CStringBuffer::createUninitialized(length, data);
72 }
73 char* mutableData();
74 size_t length() const
75 {
76 return m_buffer ? m_buffer->length() : 0;
77 } 85 }
78 86
87 // The bytes of the string, always NUL terminated. May be null.
88 const char* data() const { return m_buffer ? m_buffer->data() : 0; }
89
90 // The length of the data(), *not* including the NUL terminator.
91 size_t length() const { return m_buffer ? m_buffer->length() : 0; }
92
79 bool isNull() const { return !m_buffer; } 93 bool isNull() const { return !m_buffer; }
94
80 bool isSafeToSendToAnotherThread() const; 95 bool isSafeToSendToAnotherThread() const;
81 96
97 // TODO(esprehn): Rename to impl() when CStringBuffer is renamed.
82 CStringBuffer* buffer() const { return m_buffer.get(); } 98 CStringBuffer* buffer() const { return m_buffer.get(); }
83 99
84 private: 100 private:
85 void copyBufferIfNeeded();
86 void init(const char*, size_t length);
87 RefPtr<CStringBuffer> m_buffer; 101 RefPtr<CStringBuffer> m_buffer;
88 }; 102 };
89 103
90 WTF_EXPORT bool operator==(const CString& a, const CString& b); 104 WTF_EXPORT bool operator==(const CString& a, const CString& b);
91 inline bool operator!=(const CString& a, const CString& b) { return !(a == b); } 105 inline bool operator!=(const CString& a, const CString& b) { return !(a == b); }
92 WTF_EXPORT bool operator==(const CString& a, const char* b); 106 WTF_EXPORT bool operator==(const CString& a, const char* b);
93 inline bool operator!=(const CString& a, const char* b) { return !(a == b); } 107 inline bool operator!=(const CString& a, const char* b) { return !(a == b); }
108
94 // Pretty printer for gtest and base/logging.*. It prepends and appends 109 // Pretty printer for gtest and base/logging.*. It prepends and appends
95 // double-quotes, and escapes characters other than ASCII printables. 110 // double-quotes, and escapes characters other than ASCII printables.
96 WTF_EXPORT std::ostream& operator<<(std::ostream&, const CString&); 111 WTF_EXPORT std::ostream& operator<<(std::ostream&, const CString&);
97 112
98 } // namespace WTF 113 } // namespace WTF
99 114
100 using WTF::CString; 115 using WTF::CString;
101 116
102 #endif // CString_h 117 #endif // CString_h
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/text/CString.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698