2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29 #include "DeprecatedCString.h"
33 CString::CString(const char* str)
35 init(str, strlen(str));
38 CString::CString(const char* str, unsigned length)
43 CString::CString(const DeprecatedCString& str)
45 init(str.data(), str.length());
48 void CString::init(const char* str, unsigned length)
53 m_buffer = new CStringBuffer(length + 1);
54 memcpy(m_buffer->data(), str, length);
55 m_buffer->data()[length] = '\0';
58 char* CString::mutableData()
63 return m_buffer->data();
66 unsigned CString::length() const
68 return m_buffer ? m_buffer->length() - 1 : 0;
71 DeprecatedCString CString::deprecatedCString() const
73 return DeprecatedCString(data(), length() + 1);
76 CString CString::newUninitialized(size_t length, char*& characterBuffer)
79 result.m_buffer = new CStringBuffer(length + 1);
80 char* bytes = result.m_buffer->data();
82 characterBuffer = bytes;
86 void CString::copyBufferIfNeeded()
88 if (!m_buffer || m_buffer->hasOneRef())
91 int len = m_buffer->length();
92 RefPtr<CStringBuffer> m_temp = m_buffer;
93 m_buffer = new CStringBuffer(len);
94 memcpy(m_buffer->data(), m_temp->data(), len);
97 int CString::find(const char* substring, int index) const
99 const char* str = data();
101 if(str && str[0] && substring && index >=0) { // don't search empty strings
102 // advance until we get to index
106 return -1; // index is beyond end of str
108 // now search from index onward
109 while(str[index] != 0) {
112 // compare until we reach the end or a mismatch
115 while((a = substring[pos]) && (b = str[index]) && a == b)
118 // reached the end of our compare string without a mismatch?
119 if(substring[pos] == 0)