OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * reserved. | 3 // found in the LICENSE file. |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 */ | |
26 | 4 |
27 #ifndef CString_h | 5 #include "platform/wtf/text/CString.h" |
28 #define CString_h | |
29 | 6 |
30 #include "wtf/Allocator.h" | 7 // The contents of this header was moved to platform/wtf as part of |
31 #include "wtf/RefCounted.h" | 8 // WTF migration project. See the following post for details: |
32 #include "wtf/RefPtr.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
33 #include "wtf/WTFExport.h" | |
34 #include "wtf/allocator/PartitionAllocator.h" | |
35 #include <string.h> | |
36 | |
37 namespace WTF { | |
38 | |
39 // CStringImpl is an immutable ref-counted storage for the characters in a | |
40 // CString. It's analogous to a StringImpl but may contain any arbitrary | |
41 // sequence of bytes. The data is always allocated 1 longer than length() and is | |
42 // null terminated. | |
43 class WTF_EXPORT CStringImpl : public RefCounted<CStringImpl> { | |
44 WTF_MAKE_NONCOPYABLE(CStringImpl); | |
45 | |
46 public: | |
47 // CStringImpls are allocated out of the WTF buffer partition. | |
48 void* operator new(size_t, void* ptr) { return ptr; } | |
49 void operator delete(void*); | |
50 | |
51 static PassRefPtr<CStringImpl> createUninitialized(size_t length, | |
52 char*& data); | |
53 | |
54 const char* data() const { return reinterpret_cast<const char*>(this + 1); } | |
55 size_t length() const { return m_length; } | |
56 | |
57 private: | |
58 explicit CStringImpl(size_t length) : m_length(length) {} | |
59 | |
60 const unsigned m_length; | |
61 }; | |
62 | |
63 // 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 | |
65 // valid Latin1 or UTF-16. Instead a CString can contain any arbitrary bytes. | |
66 class WTF_EXPORT CString { | |
67 USING_FAST_MALLOC(CString); | |
68 | |
69 public: | |
70 // Construct a null string, distinguishable from an empty string. | |
71 CString() {} | |
72 | |
73 // Construct a string from arbitrary bytes. | |
74 CString(const char* chars) : CString(chars, chars ? strlen(chars) : 0) {} | |
75 CString(const char*, size_t length); | |
76 | |
77 // Construct a string referencing an existing buffer. | |
78 CString(CStringImpl* buffer) : m_buffer(buffer) {} | |
79 CString(PassRefPtr<CStringImpl> buffer) : m_buffer(std::move(buffer)) {} | |
80 | |
81 static CString createUninitialized(size_t length, char*& data) { | |
82 return CStringImpl::createUninitialized(length, data); | |
83 } | |
84 | |
85 // The bytes of the string, always NUL terminated. May be null. | |
86 const char* data() const { return m_buffer ? m_buffer->data() : 0; } | |
87 | |
88 // The length of the data(), *not* including the NUL terminator. | |
89 size_t length() const { return m_buffer ? m_buffer->length() : 0; } | |
90 | |
91 bool isNull() const { return !m_buffer; } | |
92 | |
93 bool isSafeToSendToAnotherThread() const; | |
94 | |
95 CStringImpl* impl() const { return m_buffer.get(); } | |
96 | |
97 private: | |
98 RefPtr<CStringImpl> m_buffer; | |
99 }; | |
100 | |
101 WTF_EXPORT bool operator==(const CString& a, const CString& b); | |
102 inline bool operator!=(const CString& a, const CString& b) { | |
103 return !(a == b); | |
104 } | |
105 WTF_EXPORT bool operator==(const CString& a, const char* b); | |
106 inline bool operator!=(const CString& a, const char* b) { | |
107 return !(a == b); | |
108 } | |
109 | |
110 // Pretty printer for gtest and base/logging.*. It prepends and appends | |
111 // double-quotes, and escapes characters other than ASCII printables. | |
112 WTF_EXPORT std::ostream& operator<<(std::ostream&, const CString&); | |
113 | |
114 } // namespace WTF | |
115 | |
116 using WTF::CString; | |
117 | |
118 #endif // CString_h | |
OLD | NEW |