VirtualBox

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

Last change on this file since 21617 was 21617, checked in by vboxsync, 16 years ago

ministring.cpp: properties, file header (short description, sepearate paragraph with elaborations - looks better in doxygen).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: ministring.cpp 21617 2009-07-15 15:36:09Z 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.215389.xyz. 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
36using namespace iprt;
37
38const size_t MiniString::npos = (size_t)-1;
39
40size_t MiniString::find(const char *pcszFind,
41 size_t pos /*= 0*/)
42 const
43{
44 const char *pszThis, *p;
45
46 if ( ((pszThis = c_str()))
47 && (pos < length())
48 && ((p = strstr(pszThis + pos, pcszFind)))
49 )
50 return p - pszThis;
51
52 return npos;
53}
54
55MiniString MiniString::substr(size_t pos /*= 0*/, size_t n /*= npos*/)
56 const
57{
58 MiniString ret;
59
60 if (n)
61 {
62 const char *psz;
63
64 if ((psz = c_str()))
65 {
66 RTUNICP cp;
67
68 // walk the UTF-8 characters until where the caller wants to start
69 size_t i = pos;
70 while (*psz && i--)
71 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
72 return ret; // return empty string on bad encoding
73
74 const char *pFirst = psz;
75
76 if (n == npos)
77 // all the rest:
78 ret = pFirst;
79 else
80 {
81 i = n;
82 while (*psz && i--)
83 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
84 return ret; // return empty string on bad encoding
85
86 size_t cbCopy = psz - pFirst;
87 ret.reserve(cbCopy + 1);
88 memcpy(ret.m_psz, pFirst, cbCopy);
89 ret.m_cbLength = cbCopy;
90 ret.m_psz[cbCopy] = '\0';
91 }
92 }
93 }
94
95 return ret;
96}
97
98bool MiniString::endsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
99{
100 size_t l1 = length();
101 if (l1 == 0)
102 return false;
103
104 size_t l2 = that.length();
105 if (l1 < l2)
106 return false;
107
108 size_t l = l1 - l2;
109 if (cs == CaseSensitive)
110 return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
111 else
112 return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
113}
114
115bool MiniString::startsWith(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
116{
117 size_t l1 = length();
118 size_t l2 = that.length();
119 if (l1 == 0 || l2 == 0)
120 return false;
121
122 if (l1 < l2)
123 return false;
124
125 if (cs == CaseSensitive)
126 return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
127 else
128 return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
129}
130
131bool MiniString::contains(const MiniString &that, CaseSensitivity cs /*= CaseSensitive*/) const
132{
133 if (cs == CaseSensitive)
134 return ::RTStrStr(m_psz, that.m_psz) != NULL;
135 else
136 return ::RTStrIStr(m_psz, that.m_psz) != NULL;
137}
138
139int MiniString::toInt(uint64_t &i) const
140{
141 if (!m_psz)
142 return VERR_NO_DIGITS;
143 return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
144}
145
146int MiniString::toInt(uint32_t &i) const
147{
148 if (!m_psz)
149 return VERR_NO_DIGITS;
150 return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
151}
152
Note: See TracBrowser for help on using the repository browser.

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