1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.
Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.
File: UtilString.cpp
Responsibility: LarryW
Last reviewed: 27Sep99
Code for string utilities.
----------------------------------------------------------------------------------------------*/
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
#ifndef UTILSTRING_I_CPP
#define UTILSTRING_I_CPP
#include "UtilString.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
//#undef THIS_FILE
//DEFINE_THIS_FILE
namespace gr
{
/*----------------------------------------------------------------------------------------------
Create a new internal buffer of size cch and return a pointer to the characters.
This preserves the characters currently in the string, up the the min of the old and
new sizes. It is expected that the caller will fill in any newly allocated characters.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::SetSize(int cchNew, XChar ** pprgch)
{
AssertObj(this);
AssertPtr(pprgch);
if (!cchNew)
{
_SetBuf(&s_bufEmpty);
*pprgch = NULL;
return;
}
int cchCur = m_pbuf->Cch();
if (cchNew != cchCur || m_pbuf->m_crefMinusOne)
{
StrBuffer * pbuf = StrBuffer::Create(cchNew);
if (cchCur > 0)
std::copy(m_pbuf->m_rgch, m_pbuf->m_rgch + (cchCur < cchNew ? cchCur : cchNew), pbuf->m_rgch);
// CopyItems(m_pbuf->m_rgch, pbuf->m_rgch, cchCur < cchNew ? cchCur : cchNew);
_AttachBuf(pbuf);
}
*pprgch = m_pbuf->m_rgch;
AssertObj(this);
}
/*----------------------------------------------------------------------------------------------
Set the character at index ich to ch.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::SetAt(int ich, XChar ch)
{
AssertObj(this);
Assert(ich >= 0);
Assert((unsigned int)ich < (unsigned int)m_pbuf->Cch());
// If ch is the same as the character already at ich, return.
if (ch == m_pbuf->m_rgch[ich])
return;
// If this string object is sharing a buffer, then you must not change the other string
// sharing the buffer. Rather, copy it.
if (m_pbuf->m_crefMinusOne > 0)
_Copy();
AssertObj(m_pbuf);
Assert(m_pbuf->m_crefMinusOne == 0);
m_pbuf->m_rgch[ich] = ch;
AssertObj(this);
}
#ifdef GR_FW
/*----------------------------------------------------------------------------------------------
Convert characters to lower case. If this string object is sharing a buffer, then the
existing characters are copied into a new buffer solely owned by this StrBase<>.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::ToLower(void)
{
AssertObj(this);
if (!m_pbuf->Cch())
return;
// If this string object is sharing a buffer, then you must not change the other string
// sharing the buffer. Rather, copy it.
if (m_pbuf->m_crefMinusOne > 0)
_Copy();
AssertObj(m_pbuf);
Assert(m_pbuf->m_crefMinusOne == 0);
gr::ToLower(m_pbuf->m_rgch, m_pbuf->Cch());
AssertObj(this);
}
/*----------------------------------------------------------------------------------------------
Convert characters to upper case. If this string object is sharing a buffer, then the
existing characters are copied into a new buffer solely owned by this StrBase<>.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::ToUpper(void)
{
AssertObj(this);
if (!m_pbuf->Cch())
return;
// If this string object is sharing a buffer, then you must not change the other string
// sharing the buffer. Rather, copy it.
if (m_pbuf->m_crefMinusOne > 0)
_Copy();
AssertObj(m_pbuf);
Assert(m_pbuf->m_crefMinusOne == 0);
::ToUpper(m_pbuf->m_rgch, m_pbuf->Cch());
AssertObj(this);
}
#endif
/*----------------------------------------------------------------------------------------------
Copy the existing characters into a new buffer solely owned by this StrBase<>. This is
so we can modify characters in place.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::_Copy(void)
{
AssertObj(this);
Assert(m_pbuf->m_crefMinusOne > 0);
Assert(m_pbuf->Cch() > 0);
// Allocate the new buffer.
int cch = m_pbuf->Cch();
StrBuffer * pbuf = StrBuffer::Create(cch);
std::copy(m_pbuf->m_rgch, m_pbuf->m_rgch + cch, pbuf->m_rgch);
// CopyItems(m_pbuf->m_rgch, pbuf->m_rgch, cch);
// Set our buffer to the new one.
_AttachBuf(pbuf);
AssertObj(this);
}
/*----------------------------------------------------------------------------------------------
Replace the range [ichMin, ichLim) with the given characters of the same type.
WARNING: We need to take care not to free the existing buffer until the operation succeeds.
This is in case the input uses the existing buffer.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::_Replace(int ichMin, int ichLim,
const XChar * prgchIns, XChar chIns, size_t cchIns)
{
AssertObj(this);
Assert(cchIns >= 0);
AssertArrayN(prgchIns, cchIns);
Assert(!chIns || !prgchIns);
auto cchCur = m_pbuf->Cch();
Assert((unsigned int)ichMin <= (unsigned int)ichLim && (unsigned int)ichLim <= (unsigned int)cchCur);
if (!cchIns)
{
// Nothing's being inserted.
if (ichMin == ichLim)
{
// Nothing's being deleted either so just return.
return;
}
if (!ichMin && ichLim == cchCur)
{
// Everything is being deleted so clear the string.
_SetBuf(&s_bufEmpty);
return;
}
}
StrBuffer * pbuf;
int cchNew = cchCur + cchIns - ichLim + ichMin;
if (cchNew == cchCur && !m_pbuf->m_crefMinusOne)
{
// The buffer size is staying the same and we own the characters.
pbuf = m_pbuf;
}
else
{
// Allocate the new buffer.
pbuf = StrBuffer::Create(cchNew);
}
// Copy the text.
if (ichMin > 0 && pbuf != m_pbuf)
std::copy(m_pbuf->m_rgch, m_pbuf->m_rgch + ichMin, pbuf->m_rgch);
// CopyItems(m_pbuf->m_rgch, pbuf->m_rgch, ichMin);
if (cchIns > 0)
{
if (prgchIns)
std::copy(prgchIns, prgchIns + cchIns, pbuf->m_rgch + ichMin);
// CopyItems(prgchIns, pbuf->m_rgch + ichMin, cchIns);
else
std::fill_n(pbuf->m_rgch + ichMin, cchIns, chIns);
}
if (pbuf != m_pbuf)
{
if (ichLim < cchCur)
std::copy(m_pbuf->m_rgch + ichLim, m_pbuf->m_rgch + cchCur, pbuf->m_rgch + ichMin + cchIns);
// CopyItems(m_pbuf->m_rgch + ichLim, pbuf->m_rgch + ichMin + cchIns, cchCur - ichLim);
// Set our buffer to the new one.
_AttachBuf(pbuf);
}
AssertObj(this);
}
/*----------------------------------------------------------------------------------------------
Replace the range [ichMin, ichLim) with the given characters of the other type. Use the given
codepage to convert between Unicode and 8-bit data.
WARNING: We need to take care not to free the existing buffer until the operation succeeds.
This is in case the input uses the existing buffer.
----------------------------------------------------------------------------------------------*/
template<typename XChar>
void StrBase<XChar>::_Replace(int ichMin, int ichLim,
const YChar * prgchIns, YChar chIns, size_t cchIns)
{
AssertObj(this);
Assert(cchIns >= 0);
AssertArray(prgchIns, cchIns);
// These are used ony when prgchIns is NULL.
const size_t kcchMaxChar = 8;
XChar rgchChar[kcchMaxChar];
int cchChar;
int cchCur = m_pbuf->Cch();
Assert((unsigned int)ichMin <= (unsigned int)ichLim && (unsigned int)ichLim <= (unsigned int)cchCur);
// Determine the number of characters we're inserting.
int cchDst;
if (cchIns)
{
if (prgchIns)
{
cchDst = ConvertText(prgchIns, cchIns, nullptr, 0);
if (!cchDst)
ThrowHr(WarnHr(E_FAIL));
Assert(cchCur + cchDst > cchCur);
}
else
{
cchChar = ConvertText(&chIns, 1, rgchChar, kcchMaxChar);
if (!cchChar)
ThrowHr(WarnHr(E_FAIL));
Assert(cchChar <= kcchMaxChar);
cchDst = cchChar * cchIns;
Assert(cchCur + cchDst > cchCur);
}
}
else
cchDst = 0;
// Allocate the new buffer.
StrBuffer * pbuf;
auto cchNew = cchCur + cchDst - ichLim + ichMin;
if (cchNew == cchCur && !m_pbuf->m_crefMinusOne)
{
// The buffer size is staying the same and we own the characters.
pbuf = m_pbuf;
}
else
{
// Allocate the new buffer.
pbuf = StrBuffer::Create(cchNew);
}
// Copy and convert the text.
if (ichMin > 0 && pbuf != m_pbuf)
std::copy(m_pbuf->m_rgch, m_pbuf->m_rgch + ichMin, pbuf->m_rgch);
// CopyItems(m_pbuf->m_rgch, pbuf->m_rgch, ichMin);
if (cchDst > 0)
{
if (prgchIns)
ConvertText(prgchIns, cchIns, pbuf->m_rgch + ichMin, cchDst);
else
{
XChar * pch = pbuf->m_rgch + ichMin;
if (cchChar == 1)
std::fill_n(pch, cchIns, rgchChar[0]);
// FillChars(pch, rgchChar[0], cchIns);
else
{
int cchT;
for (cchT = cchIns; --cchT >= 0; )
{
std::copy(rgchChar, rgchChar + cchChar, pch);
// CopyItems(rgchChar, pch, cchChar);
pch += cchChar;
}
}
}
}
if (pbuf != m_pbuf)
{
if (ichLim < cchCur)
std::copy(m_pbuf->m_rgch + ichLim, m_pbuf->m_rgch + cchCur, pbuf->m_rgch + ichMin + cchDst);
// CopyItems(m_pbuf->m_rgch + ichLim, pbuf->m_rgch + ichMin + cchDst, cchCur - ichLim);
// Set our buffer to the new one.
_AttachBuf(pbuf);
}
AssertObj(this);
}
} //namespace gr
#if !defined(GR_NAMESPACE)
using namespace gr;
#endif
#endif /*UTILSTRING_I_CPP*/
|