VirtualBox

source: vbox/trunk/src/VBox/Main/glue/string.cpp@ 33563

最後變更 在這個檔案從33563是 33563,由 vboxsync 提交於 14 年 前

iprt:ministring: Added the java-style equals() and equalsIgnoreCase() as equals() can optimize the comparison by first checking if the length is the same (compare() cannot as it needs to determin the ordering). Added appendCodePoint() for UTF-8. Fixed the incorrect assumption in toUpper and toLower that the string length remained unchanged - the string might shrink as the folded code points may have a shorter encoding. Added testcase that verifies that a code point will not grow during folding and that have a stable encoding length after it has been changed in a folding.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 3.9 KB
 
1/* $Id: string.cpp 33563 2010-10-28 14:46:26Z vboxsync $ */
2
3/** @file
4 *
5 * MS COM / XPCOM Abstraction Layer:
6 * UTF-8 and UTF-16 string classes
7 */
8
9/*
10 * Copyright (C) 2006-2007 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.alldomusa.eu.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
21#include "VBox/com/string.h"
22
23#include <iprt/err.h>
24#include <iprt/path.h>
25
26namespace com
27{
28
29// BSTR representing a null wide char with 32 bits of length prefix (0);
30// this will work on Windows as well as other platforms where BSTR does
31// not use length prefixes
32const OLECHAR g_achEmptyBstr[3] = { 0, 0, 0 };
33const BSTR g_bstrEmpty = (BSTR)(&g_achEmptyBstr[2]);
34
35/* static */
36const Bstr Bstr::Empty; /* default ctor is OK */
37
38/* static */
39const Utf8Str Utf8Str::Empty; /* default ctor is OK */
40
41#if defined (VBOX_WITH_XPCOM)
42void Utf8Str::cloneTo(char **pstr) const
43{
44 size_t cb = length() + 1;
45 *pstr = (char*)nsMemory::Alloc(cb);
46 if (!*pstr)
47 throw std::bad_alloc();
48 memcpy(*pstr, c_str(), cb);
49}
50#endif
51
52Utf8Str& Utf8Str::stripTrailingSlash()
53{
54 if (length())
55 {
56 ::RTPathStripTrailingSlash(m_psz);
57 jolt();
58 }
59 return *this;
60}
61
62Utf8Str& Utf8Str::stripFilename()
63{
64 if (length())
65 {
66 RTPathStripFilename(m_psz);
67 jolt();
68 }
69 return *this;
70}
71
72Utf8Str& Utf8Str::stripPath()
73{
74 if (length())
75 {
76 char *pcszFilename = ::RTStrDup(::RTPathFilename(m_psz));
77 cleanup();
78 MiniString::copyFrom(pcszFilename);
79 RTStrFree(pcszFilename);
80 }
81 return *this;
82}
83
84Utf8Str& Utf8Str::stripExt()
85{
86 if (length())
87 {
88 RTPathStripExt(m_psz);
89 jolt();
90 }
91 return *this;
92}
93
94/**
95 * Internal function used in Utf8Str copy constructors and assignment when
96 * copying from a UTF-16 string.
97 *
98 * As with the iprt::ministring::copyFrom() variants, this unconditionally
99 * sets the members to a copy of the given other strings and makes
100 * no assumptions about previous contents. This can therefore be used
101 * both in copy constructors, when member variables have no defined
102 * value, and in assignments after having called cleanup().
103 *
104 * This variant converts from a UTF-16 string, most probably from
105 * a Bstr assignment.
106 *
107 * @param s
108 */
109void Utf8Str::copyFrom(CBSTR s)
110{
111 if (s && *s)
112 {
113 int vrc = RTUtf16ToUtf8Ex((PRTUTF16)s, // PCRTUTF16 pwszString
114 RTSTR_MAX, // size_t cwcString: translate entire string
115 &m_psz, // char **ppsz: output buffer
116 0, // size_t cch: if 0, func allocates buffer in *ppsz
117 &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
118 if (RT_FAILURE(vrc))
119 {
120 if ( vrc == VERR_NO_STR_MEMORY
121 || vrc == VERR_NO_MEMORY
122 )
123 throw std::bad_alloc();
124
125 // @todo what do we do with bad input strings? throw also? for now just keep an empty string
126 m_cch = 0;
127 m_cbAllocated = 0;
128 m_psz = NULL;
129 }
130 else
131 m_cbAllocated = m_cch + 1;
132 }
133 else
134 {
135 m_cch = 0;
136 m_cbAllocated = 0;
137 m_psz = NULL;
138 }
139}
140
141void Utf8StrFmt::init(const char *format, va_list args)
142{
143 if (!format || !*format)
144 {
145 m_cch = 0;
146 m_cbAllocated = 0;
147 m_psz = NULL;
148 }
149 else
150 {
151 m_cch = RTStrAPrintfV(&m_psz, format, args);
152 m_cbAllocated = m_cch + 1;
153 }
154}
155
156} /* namespace com */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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