OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights | |
3 * reserved. | |
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 | |
27 #include "wtf/text/CString.h" | |
28 | |
29 #include "wtf/ASCIICType.h" | |
30 #include "wtf/allocator/Partitions.h" | |
31 #include <string.h> | |
32 | |
33 using namespace std; | |
34 | |
35 namespace WTF { | |
36 | |
37 PassRefPtr<CStringImpl> CStringImpl::createUninitialized(size_t length, | |
38 char*& data) { | |
39 // TODO(esprehn): This doesn't account for the NUL. | |
40 RELEASE_ASSERT(length < | |
41 (numeric_limits<unsigned>::max() - sizeof(CStringImpl))); | |
42 | |
43 // The +1 is for the terminating NUL character. | |
44 size_t size = sizeof(CStringImpl) + length + 1; | |
45 CStringImpl* buffer = static_cast<CStringImpl*>( | |
46 Partitions::bufferMalloc(size, WTF_HEAP_PROFILER_TYPE_NAME(CStringImpl))); | |
47 data = reinterpret_cast<char*>(buffer + 1); | |
48 data[length] = '\0'; | |
49 return adoptRef(new (buffer) CStringImpl(length)); | |
50 } | |
51 | |
52 void CStringImpl::operator delete(void* ptr) { | |
53 Partitions::bufferFree(ptr); | |
54 } | |
55 | |
56 CString::CString(const char* chars, size_t length) { | |
57 if (!chars) { | |
58 DCHECK_EQ(length, 0u); | |
59 return; | |
60 } | |
61 char* data; | |
62 m_buffer = CStringImpl::createUninitialized(length, data); | |
63 memcpy(data, chars, length); | |
64 } | |
65 | |
66 bool CString::isSafeToSendToAnotherThread() const { | |
67 return !m_buffer || m_buffer->hasOneRef(); | |
68 } | |
69 | |
70 bool operator==(const CString& a, const CString& b) { | |
71 if (a.isNull() != b.isNull()) | |
72 return false; | |
73 if (a.length() != b.length()) | |
74 return false; | |
75 return !memcmp(a.data(), b.data(), a.length()); | |
76 } | |
77 | |
78 bool operator==(const CString& a, const char* b) { | |
79 if (a.isNull() != !b) | |
80 return false; | |
81 if (!b) | |
82 return true; | |
83 return !strcmp(a.data(), b); | |
84 } | |
85 | |
86 std::ostream& operator<<(std::ostream& ostream, const CString& string) { | |
87 if (string.isNull()) | |
88 return ostream << "<null>"; | |
89 | |
90 ostream << '"'; | |
91 for (size_t index = 0; index < string.length(); ++index) { | |
92 // Print shorthands for select cases. | |
93 char character = string.data()[index]; | |
94 switch (character) { | |
95 case '\t': | |
96 ostream << "\\t"; | |
97 break; | |
98 case '\n': | |
99 ostream << "\\n"; | |
100 break; | |
101 case '\r': | |
102 ostream << "\\r"; | |
103 break; | |
104 case '"': | |
105 ostream << "\\\""; | |
106 break; | |
107 case '\\': | |
108 ostream << "\\\\"; | |
109 break; | |
110 default: | |
111 if (isASCIIPrintable(character)) { | |
112 ostream << character; | |
113 } else { | |
114 // Print "\xHH" for control or non-ASCII characters. | |
115 ostream << "\\x"; | |
116 if (character >= 0 && character < 0x10) | |
117 ostream << "0"; | |
118 ostream.setf(std::ios_base::hex, std::ios_base::basefield); | |
119 ostream.setf(std::ios::uppercase); | |
120 ostream << (character & 0xff); | |
121 } | |
122 break; | |
123 } | |
124 } | |
125 return ostream << '"'; | |
126 } | |
127 | |
128 } // namespace WTF | |
OLD | NEW |