1 | /* $Id: ministring.cpp 25349 2009-12-13 17:23:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Mini C++ string class.
|
---|
4 | *
|
---|
5 | * This is a base for both Utf8Str and other places where IPRT may want to use
|
---|
6 | * a lean C++ string class.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2007-2009 Sun Microsystems, Inc.
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | * available from http://www.virtualbox.org. This file is free software;
|
---|
14 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | * General Public License (GPL) as published by the Free Software
|
---|
16 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | *
|
---|
20 | * The contents of this file may alternatively be used under the terms
|
---|
21 | * of the Common Development and Distribution License Version 1.0
|
---|
22 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | * CDDL are applicable instead of those of the GPL.
|
---|
25 | *
|
---|
26 | * You may elect to license modified versions of this file under the
|
---|
27 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | *
|
---|
29 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
30 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
31 | * additional information or have any questions.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <iprt/cpp/ministring.h>
|
---|
35 |
|
---|
36 | using namespace iprt;
|
---|
37 |
|
---|
38 | const size_t MiniString::npos = ~(size_t)0;
|
---|
39 |
|
---|
40 | MiniString &MiniString::append(const MiniString &that)
|
---|
41 | {
|
---|
42 | size_t lenThat = that.length();
|
---|
43 | if (lenThat)
|
---|
44 | {
|
---|
45 | size_t lenThis = length();
|
---|
46 | size_t cbBoth = lenThis + lenThat + 1;
|
---|
47 |
|
---|
48 | reserve(cbBoth);
|
---|
49 | // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
|
---|
50 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
51 | AssertRelease(capacity() >= cbBoth);
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | memcpy(m_psz + lenThis, that.m_psz, lenThat);
|
---|
55 | m_psz[lenThis + lenThat] = '\0';
|
---|
56 | m_cbLength = cbBoth - 1;
|
---|
57 | }
|
---|
58 | return *this;
|
---|
59 | }
|
---|
60 |
|
---|
61 | MiniString& MiniString::append(char c)
|
---|
62 | {
|
---|
63 | if (c)
|
---|
64 | {
|
---|
65 | // allocate in chunks of 20 in case this gets called several times
|
---|
66 | if (m_cbLength + 1 >= m_cbAllocated)
|
---|
67 | {
|
---|
68 | reserve(m_cbLength + 10);
|
---|
69 | // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
|
---|
70 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
71 | AssertRelease(capacity() >= m_cbLength + 1);
|
---|
72 | #endif
|
---|
73 | }
|
---|
74 |
|
---|
75 | m_psz[m_cbLength] = c;
|
---|
76 | m_psz[m_cbLength + 1] = '\0';
|
---|
77 | ++m_cbLength;
|
---|
78 | }
|
---|
79 | return *this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/)
|
---|
83 | const
|
---|
84 | {
|
---|
85 | const char *pszThis, *p;
|
---|
86 |
|
---|
87 | if ( ((pszThis = c_str()))
|
---|
88 | && (pos < length())
|
---|
89 | && ((p = strstr(pszThis + pos, pcszFind)))
|
---|
90 | )
|
---|
91 | return p - pszThis;
|
---|
92 |
|
---|
93 | return npos;
|
---|
94 | }
|
---|
95 |
|
---|
96 | MiniString MiniString::substr(size_t pos /*= 0*/, size_t n /*= npos*/)
|
---|
97 | const
|
---|
98 | {
|
---|
99 | MiniString ret;
|
---|
100 |
|
---|
101 | if (n)
|
---|
102 | {
|
---|
103 | const char *psz;
|
---|
104 |
|
---|
105 | if ((psz = c_str()))
|
---|
106 | {
|
---|
107 | RTUNICP cp;
|
---|
108 |
|
---|
109 | // walk the UTF-8 characters until where the caller wants to start
|
---|
110 | size_t i = pos;
|
---|
111 | while (*psz && i--)
|
---|
112 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
113 | return ret; // return empty string on bad encoding
|
---|
114 |
|
---|
115 | const char *pFirst = psz;
|
---|
116 |
|
---|
117 | if (n == npos)
|
---|
118 | // all the rest:
|
---|
119 | ret = pFirst;
|
---|
120 | else
|
---|
121 | {
|
---|
122 | i = n;
|
---|
123 | while (*psz && i--)
|
---|
124 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
125 | return ret; // return empty string on bad encoding
|
---|
126 |
|
---|
127 | size_t cbCopy = psz - pFirst;
|
---|
128 | ret.reserve(cbCopy + 1); // may throw bad_alloc
|
---|
129 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
130 | AssertRelease(capacity() >= cbCopy + 1);
|
---|
131 | #endif
|
---|
132 | memcpy(ret.m_psz, pFirst, cbCopy);
|
---|
133 | ret.m_cbLength = cbCopy;
|
---|
134 | ret.m_psz[cbCopy] = '\0';
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | return ret;
|
---|
140 | }
|
---|
141 |
|
---|
142 | bool MiniString::endsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
143 | {
|
---|
144 | size_t l1 = length();
|
---|
145 | if (l1 == 0)
|
---|
146 | return false;
|
---|
147 |
|
---|
148 | size_t l2 = that.length();
|
---|
149 | if (l1 < l2)
|
---|
150 | return false;
|
---|
151 | /** @todo r=bird: If l2 is 0, then m_psz can be NULL and we will crash. See
|
---|
152 | * also handling of l2 == in startsWith. */
|
---|
153 |
|
---|
154 | size_t l = l1 - l2;
|
---|
155 | if (cs == CaseSensitive)
|
---|
156 | return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
|
---|
157 | else
|
---|
158 | return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 | bool MiniString::startsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
162 | {
|
---|
163 | size_t l1 = length();
|
---|
164 | size_t l2 = that.length();
|
---|
165 | if (l1 == 0 || l2 == 0) /** @todo r=bird: this differs from endsWith, and I think other IPRT code. If l2 == 0, it matches anything. */
|
---|
166 | return false;
|
---|
167 |
|
---|
168 | if (l1 < l2)
|
---|
169 | return false;
|
---|
170 |
|
---|
171 | if (cs == CaseSensitive)
|
---|
172 | return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
|
---|
173 | else
|
---|
174 | return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool MiniString::contains(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
178 | {
|
---|
179 | /** @todo r-bird: Not checking for NULL strings like startsWith does (and
|
---|
180 | * endsWith only does half way). */
|
---|
181 | if (cs == CaseSensitive)
|
---|
182 | return ::RTStrStr(m_psz, that.m_psz) != NULL;
|
---|
183 | else
|
---|
184 | return ::RTStrIStr(m_psz, that.m_psz) != NULL;
|
---|
185 | }
|
---|
186 |
|
---|
187 | int MiniString::toInt(uint64_t &i) const
|
---|
188 | {
|
---|
189 | if (!m_psz)
|
---|
190 | return VERR_NO_DIGITS;
|
---|
191 | return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
|
---|
192 | }
|
---|
193 |
|
---|
194 | int MiniString::toInt(uint32_t &i) const
|
---|
195 | {
|
---|
196 | if (!m_psz)
|
---|
197 | return VERR_NO_DIGITS;
|
---|
198 | return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
|
---|
199 | }
|
---|
200 |
|
---|