VirtualBox

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

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

string.cpp: enable optimization

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* $Id: string.cpp 16384 2009-01-29 18:16:07Z vboxsync $ */
2
3/** @file
4 *
5 * MS COM / XPCOM Abstraction Layer:
6 * Smart string classes definition
7 */
8
9/*
10 * Copyright (C) 2006-2007 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
21 * Clara, CA 95054 USA or visit http://www.sun.com if you need
22 * additional information or have any questions.
23 */
24
25#include "VBox/com/string.h"
26
27#include <iprt/err.h>
28
29namespace com
30{
31
32/* static */
33const Bstr Bstr::Null; /* default ctor is OK */
34
35/* static */
36const Utf8Str Utf8Str::Null; /* default ctor is OK */
37
38const size_t Utf8Str::npos = (size_t)-1;
39
40Utf8Str Utf8Str::substr(size_t pos /*= 0*/, size_t n /*= npos*/) const
41{
42 Utf8Str ret;
43
44 if (n)
45 {
46 const char *psz;
47
48 if ((psz = c_str()))
49 {
50 RTUNICP cp;
51
52 // walk the UTF-8 characters until where the caller wants to start
53 size_t i = pos;
54 while (*psz && i--)
55 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
56 return ret; // return empty string on bad encoding
57
58 const char *pFirst = psz;
59
60 if (n == npos)
61 // all the rest:
62 ret = pFirst;
63 else
64 {
65 i = n;
66 while (*psz && i--)
67 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
68 return ret; // return empty string on bad encoding
69
70 size_t cbCopy = psz - pFirst;
71 ret.alloc(cbCopy + 1);
72 memcpy(ret.str, pFirst, cbCopy);
73 ret.str[cbCopy] = '\0';
74 }
75 }
76 }
77
78 return ret;
79}
80
81struct FormatData
82{
83 static const size_t CacheIncrement = 256;
84 size_t size;
85 size_t pos;
86 char *cache;
87};
88
89void Utf8StrFmt::init (const char *format, va_list args)
90{
91 if (!format)
92 return;
93
94 // assume an extra byte for a terminating zero
95 size_t fmtlen = strlen (format) + 1;
96
97 FormatData data;
98 data.size = FormatData::CacheIncrement;
99 if (fmtlen >= FormatData::CacheIncrement)
100 data.size += fmtlen;
101 data.pos = 0;
102 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
103
104 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
105
106 AssertMsg (n == data.pos,
107 ("The number of bytes formatted doesn't match: %d and %d!",
108 n, data.pos));
109 NOREF (n);
110
111 // finalize formatting
112 data.cache [data.pos] = 0;
113 (*static_cast <Utf8Str *> (this)) = data.cache;
114 ::RTMemTmpFree (data.cache);
115}
116
117// static
118DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
119 size_t cbChars)
120{
121 Assert (pvArg);
122 FormatData &data = *(FormatData *) pvArg;
123
124 if (!(pachChars == NULL && cbChars == 0))
125 {
126 Assert (pachChars);
127
128 // append to cache (always assume an extra byte for a terminating zero)
129 size_t needed = cbChars + 1;
130 if (data.pos + needed > data.size)
131 {
132 data.size += FormatData::CacheIncrement;
133 if (needed >= FormatData::CacheIncrement)
134 data.size += needed;
135 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
136 }
137 strncpy (data.cache + data.pos, pachChars, cbChars);
138 data.pos += cbChars;
139 }
140
141 return cbChars;
142}
143
144
145} /* namespace com */
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