1 | /* $Id: ministring.cpp 23223 2009-09-22 15:50:03Z 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/ministring_cpp.h>
|
---|
35 |
|
---|
36 | using namespace iprt;
|
---|
37 |
|
---|
38 | const size_t MiniString::npos = (size_t)-1;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Appends a copy of @a that to "this".
|
---|
42 | * @param that
|
---|
43 | */
|
---|
44 | MiniString& MiniString::append(const MiniString &that)
|
---|
45 | {
|
---|
46 | size_t lenThat = that.length();
|
---|
47 | if (lenThat)
|
---|
48 | {
|
---|
49 | size_t lenThis = length();
|
---|
50 | size_t cbBoth = lenThis + lenThat + 1;
|
---|
51 |
|
---|
52 | reserve(cbBoth);
|
---|
53 | // calls realloc(cbBoth) and sets m_cbAllocated
|
---|
54 |
|
---|
55 | memcpy(m_psz + lenThis, that.m_psz, lenThat);
|
---|
56 | m_psz[lenThis + lenThat] = '\0';
|
---|
57 | m_cbLength = cbBoth - 1;
|
---|
58 | }
|
---|
59 | return *this;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Appends the given character to "this".
|
---|
64 | * @param c
|
---|
65 | * @return
|
---|
66 | */
|
---|
67 | MiniString& MiniString::append(char c)
|
---|
68 | {
|
---|
69 | if (c)
|
---|
70 | {
|
---|
71 | // allocate in chunks of 20 in case this gets called several times
|
---|
72 | if (m_cbLength + 1 >= m_cbAllocated)
|
---|
73 | reserve(m_cbLength + 10);
|
---|
74 | // calls realloc() and sets m_cbAllocated
|
---|
75 |
|
---|
76 | m_psz[m_cbLength] = c;
|
---|
77 | m_psz[m_cbLength + 1] = '\0';
|
---|
78 | ++m_cbLength;
|
---|
79 | }
|
---|
80 | return *this;
|
---|
81 | }
|
---|
82 |
|
---|
83 | size_t MiniString::find(const char *pcszFind,
|
---|
84 | size_t pos /*= 0*/)
|
---|
85 | const
|
---|
86 | {
|
---|
87 | const char *pszThis, *p;
|
---|
88 |
|
---|
89 | if ( ((pszThis = c_str()))
|
---|
90 | && (pos < length())
|
---|
91 | && ((p = strstr(pszThis + pos, pcszFind)))
|
---|
92 | )
|
---|
93 | return p - pszThis;
|
---|
94 |
|
---|
95 | return npos;
|
---|
96 | }
|
---|
97 |
|
---|
98 | MiniString MiniString::substr(size_t pos /*= 0*/, size_t n /*= npos*/)
|
---|
99 | const
|
---|
100 | {
|
---|
101 | MiniString ret;
|
---|
102 |
|
---|
103 | if (n)
|
---|
104 | {
|
---|
105 | const char *psz;
|
---|
106 |
|
---|
107 | if ((psz = c_str()))
|
---|
108 | {
|
---|
109 | RTUNICP cp;
|
---|
110 |
|
---|
111 | // walk the UTF-8 characters until where the caller wants to start
|
---|
112 | size_t i = pos;
|
---|
113 | while (*psz && i--)
|
---|
114 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
115 | return ret; // return empty string on bad encoding
|
---|
116 |
|
---|
117 | const char *pFirst = psz;
|
---|
118 |
|
---|
119 | if (n == npos)
|
---|
120 | // all the rest:
|
---|
121 | ret = pFirst;
|
---|
122 | else
|
---|
123 | {
|
---|
124 | i = n;
|
---|
125 | while (*psz && i--)
|
---|
126 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
127 | return ret; // return empty string on bad encoding
|
---|
128 |
|
---|
129 | size_t cbCopy = psz - pFirst;
|
---|
130 | ret.reserve(cbCopy + 1);
|
---|
131 | memcpy(ret.m_psz, pFirst, cbCopy);
|
---|
132 | ret.m_cbLength = cbCopy;
|
---|
133 | ret.m_psz[cbCopy] = '\0';
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | return ret;
|
---|
139 | }
|
---|
140 |
|
---|
141 | bool MiniString::endsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
142 | {
|
---|
143 | size_t l1 = length();
|
---|
144 | if (l1 == 0)
|
---|
145 | return false;
|
---|
146 |
|
---|
147 | size_t l2 = that.length();
|
---|
148 | if (l1 < l2)
|
---|
149 | return false;
|
---|
150 |
|
---|
151 | size_t l = l1 - l2;
|
---|
152 | if (cs == CaseSensitive)
|
---|
153 | return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
|
---|
154 | else
|
---|
155 | return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
|
---|
156 | }
|
---|
157 |
|
---|
158 | bool MiniString::startsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
159 | {
|
---|
160 | size_t l1 = length();
|
---|
161 | size_t l2 = that.length();
|
---|
162 | if (l1 == 0 || l2 == 0)
|
---|
163 | return false;
|
---|
164 |
|
---|
165 | if (l1 < l2)
|
---|
166 | return false;
|
---|
167 |
|
---|
168 | if (cs == CaseSensitive)
|
---|
169 | return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
|
---|
170 | else
|
---|
171 | return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | bool MiniString::contains(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
175 | {
|
---|
176 | if (cs == CaseSensitive)
|
---|
177 | return ::RTStrStr(m_psz, that.m_psz) != NULL;
|
---|
178 | else
|
---|
179 | return ::RTStrIStr(m_psz, that.m_psz) != NULL;
|
---|
180 | }
|
---|
181 |
|
---|
182 | int MiniString::toInt(uint64_t &i) const
|
---|
183 | {
|
---|
184 | if (!m_psz)
|
---|
185 | return VERR_NO_DIGITS;
|
---|
186 | return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
|
---|
187 | }
|
---|
188 |
|
---|
189 | int MiniString::toInt(uint32_t &i) const
|
---|
190 | {
|
---|
191 | if (!m_psz)
|
---|
192 | return VERR_NO_DIGITS;
|
---|
193 | return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
|
---|
194 | }
|
---|
195 |
|
---|