VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/ministring.cpp@ 28800

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1/* $Id: ministring.cpp 28800 2010-04-27 08:22: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 Oracle Corporation
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
30#include <iprt/cpp/ministring.h>
31
32using namespace iprt;
33
34const size_t MiniString::npos = ~(size_t)0;
35
36MiniString &MiniString::append(const MiniString &that)
37{
38 size_t lenThat = that.length();
39 if (lenThat)
40 {
41 size_t lenThis = length();
42 size_t cbBoth = lenThis + lenThat + 1;
43
44 reserve(cbBoth);
45 // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
46#ifndef RT_EXCEPTIONS_ENABLED
47 AssertRelease(capacity() >= cbBoth);
48#endif
49
50 memcpy(m_psz + lenThis, that.m_psz, lenThat);
51 m_psz[lenThis + lenThat] = '\0';
52 m_cbLength = cbBoth - 1;
53 }
54 return *this;
55}
56
57MiniString& MiniString::append(char c)
58{
59 if (c)
60 {
61 // allocate in chunks of 20 in case this gets called several times
62 if (m_cbLength + 1 >= m_cbAllocated)
63 {
64 reserve(m_cbLength + 10);
65 // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
66#ifndef RT_EXCEPTIONS_ENABLED
67 AssertRelease(capacity() >= m_cbLength + 1);
68#endif
69 }
70
71 m_psz[m_cbLength] = c;
72 m_psz[m_cbLength + 1] = '\0';
73 ++m_cbLength;
74 }
75 return *this;
76}
77
78size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/)
79 const
80{
81 const char *pszThis, *p;
82
83 if ( ((pszThis = c_str()))
84 && (pos < length())
85 && ((p = strstr(pszThis + pos, pcszFind)))
86 )
87 return p - pszThis;
88
89 return npos;
90}
91
92MiniString MiniString::substr(size_t pos /*= 0*/, size_t n /*= npos*/)
93 const
94{
95 MiniString ret;
96
97 if (n)
98 {
99 const char *psz;
100
101 if ((psz = c_str()))
102 {
103 RTUNICP cp;
104
105 // walk the UTF-8 characters until where the caller wants to start
106 size_t i = pos;
107 while (*psz && i--)
108 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
109 return ret; // return empty string on bad encoding
110
111 const char *pFirst = psz;
112
113 if (n == npos)
114 // all the rest:
115 ret = pFirst;
116 else
117 {
118 i = n;
119 while (*psz && i--)
120 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
121 return ret; // return empty string on bad encoding
122
123 size_t cbCopy = psz - pFirst;
124 ret.reserve(cbCopy + 1); // may throw bad_alloc
125#ifndef RT_EXCEPTIONS_ENABLED
126 AssertRelease(capacity() >= cbCopy + 1);
127#endif
128 memcpy(ret.m_psz, pFirst, cbCopy);
129 ret.m_cbLength = cbCopy;
130 ret.m_psz[cbCopy] = '\0';
131 }
132 }
133 }
134
135 return ret;
136}
137
138bool MiniString::endsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
139{
140 size_t l1 = length();
141 if (l1 == 0)
142 return false;
143
144 size_t l2 = that.length();
145 if (l1 < l2)
146 return false;
147 /** @todo r=bird: If l2 is 0, then m_psz can be NULL and we will crash. See
148 * also handling of l2 == in startsWith. */
149
150 size_t l = l1 - l2;
151 if (cs == CaseSensitive)
152 return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
153 else
154 return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
155}
156
157bool MiniString::startsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
158{
159 size_t l1 = length();
160 size_t l2 = that.length();
161 if (l1 == 0 || l2 == 0) /** @todo r=bird: this differs from endsWith, and I think other IPRT code. If l2 == 0, it matches anything. */
162 return false;
163
164 if (l1 < l2)
165 return false;
166
167 if (cs == CaseSensitive)
168 return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
169 else
170 return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
171}
172
173bool MiniString::contains(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
174{
175 /** @todo r-bird: Not checking for NULL strings like startsWith does (and
176 * endsWith only does half way). */
177 if (cs == CaseSensitive)
178 return ::RTStrStr(m_psz, that.m_psz) != NULL;
179 else
180 return ::RTStrIStr(m_psz, that.m_psz) != NULL;
181}
182
183int MiniString::toInt(uint64_t &i) const
184{
185 if (!m_psz)
186 return VERR_NO_DIGITS;
187 return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
188}
189
190int MiniString::toInt(uint32_t &i) const
191{
192 if (!m_psz)
193 return VERR_NO_DIGITS;
194 return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
195}
196
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette