| 1 |
|
| 2 |
// Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without modification, |
| 6 |
// are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions, and the following disclaimer. |
| 10 |
// |
| 11 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
// this list of conditions, and the following disclaimer in the documentation |
| 13 |
// and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 |
// may be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
// THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 |
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 |
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 |
// SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 |
// OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 |
// ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 |
// |
| 32 |
|
| 33 |
#include "xsstring.h" |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> // memcpy |
| 36 |
#include <ctype.h> |
| 37 |
|
| 38 |
#if defined(WIN32) |
| 39 |
#include <Windows.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
/*! \struct XsString |
| 43 |
\brief A 0-terminated managed string of characters |
| 44 |
\details This structure uses XsArray to manage its internal data. |
| 45 |
The C++ interface reports the size of the string excluding the terminating 0, but since the C version |
| 46 |
uses XsArray directly, its m_size member includes the terminating 0. |
| 47 |
When using the C version, make sure that the 0 character is always preserved when manipulating the data. |
| 48 |
\sa XsArray |
| 49 |
*/ |
| 50 |
|
| 51 |
/*! \copydoc XsArrayDescriptor::itemSwap |
| 52 |
\note Specialization for char*/ |
| 53 |
void swapChar(char* a, char* b) |
| 54 |
{ |
| 55 |
char tmp = *a; |
| 56 |
*a = *b; |
| 57 |
*b = tmp; |
| 58 |
} |
| 59 |
|
| 60 |
/*! \copydoc XsArrayDescriptor::itemCopy |
| 61 |
\note Specialization for char*/ |
| 62 |
void copyChar(char* to, char const* from) |
| 63 |
{ |
| 64 |
*to = *from; |
| 65 |
} |
| 66 |
|
| 67 |
/*! \copydoc XsArrayDescriptor::itemCompare |
| 68 |
\note Specialization for char*/ |
| 69 |
int compareChar(void const* a, void const* b) |
| 70 |
{ |
| 71 |
if (*(char*)a < *(char*)b) |
| 72 |
return -1; |
| 73 |
if (*(char*)a > *(char*)b) |
| 74 |
return 1; |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
//! \brief Descriptor for XsInt64Array |
| 79 |
XsArrayDescriptor const g_xsStringDescriptor = { |
| 80 |
sizeof(char), // const size_t itemSize; //!< \protected Size of a single data element |
| 81 |
XSEXPCASTITEMSWAP swapChar, // void (*itemSwap)(void* a, void* b); |
| 82 |
0, // void (*itemConstruct)(void* e); |
| 83 |
XSEXPCASTITEMCOPY copyChar, // void (*itemCopyConstruct)(void* e, void const* s); |
| 84 |
0, // void (*itemDestruct)(void* e); |
| 85 |
XSEXPCASTITEMCOPY copyChar, // void (*itemCopy)(void const* from, void* to); |
| 86 |
XSEXPCASTITEMCOMP compareChar, // int (*itemCompare)(void const* a, void const* b); |
| 87 |
XSEXPCASTRAWCOPY XsArray_rawCopy // void (*rawCopy)(void* to, void const* from, XsSize count, XsSize iSize) |
| 88 |
}; |
| 89 |
|
| 90 |
/*! \brief Initializes the XsString object as an empty string |
| 91 |
\details This function initializes the object as an empty string. |
| 92 |
*/ |
| 93 |
void XsString_construct(XsString* thisPtr) |
| 94 |
{ |
| 95 |
XsArray_construct(thisPtr, &g_xsStringDescriptor, 0, 0); |
| 96 |
} |
| 97 |
|
| 98 |
/*! \copydoc XsArray_destruct |
| 99 |
\note Specialization for XsString |
| 100 |
*/ |
| 101 |
void XsString_destruct(XsString* thisPtr) |
| 102 |
{ |
| 103 |
XsArray_destruct(thisPtr); |
| 104 |
} |
| 105 |
|
| 106 |
/*! \copydoc XsArray_assign |
| 107 |
\note Specialization for XsString |
| 108 |
*/ |
| 109 |
void XsString_assign(XsString* thisPtr, XsSize count, const char* src) |
| 110 |
{ |
| 111 |
if (!count && src) |
| 112 |
count = (XsSize)strlen(src)+1; |
| 113 |
|
| 114 |
if (src) |
| 115 |
{ |
| 116 |
if (src[count-1]) |
| 117 |
{ |
| 118 |
XsArray_assign(thisPtr, count+1, 0); |
| 119 |
memcpy(thisPtr->m_data, src, count); |
| 120 |
thisPtr->m_data[count] = 0; |
| 121 |
} |
| 122 |
else |
| 123 |
XsArray_assign(thisPtr, count, src); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
if (count) |
| 128 |
{ |
| 129 |
XsArray_assign(thisPtr, count+1, 0); |
| 130 |
memset(thisPtr->m_data, ' ', count); |
| 131 |
thisPtr->m_data[count] = 0; |
| 132 |
} |
| 133 |
else |
| 134 |
XsArray_assign(thisPtr, 0, 0); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/*! \brief This function determines the size of \a src and copies the contents to the object */ |
| 139 |
void XsString_assignCharArray(XsString* thisPtr, const char* src) |
| 140 |
{ |
| 141 |
XsString_assign(thisPtr, 0, src); |
| 142 |
} |
| 143 |
|
| 144 |
#ifndef XSENS_NO_WCHAR |
| 145 |
/*! \brief This function determines the size of \a src and copies the contents to the object after |
| 146 |
converting it from a unicode string to a multibyte character string. |
| 147 |
*/ |
| 148 |
void XsString_assignWCharArray(XsString* thisPtr, const wchar_t* src) |
| 149 |
{ |
| 150 |
if (src) |
| 151 |
{ |
| 152 |
#ifdef WIN32 |
| 153 |
int unicodeLength = lstrlenW( src ); // Convert all UNICODE characters |
| 154 |
int required = WideCharToMultiByte(CP_UTF8, 0, src, unicodeLength, NULL, 0, NULL, NULL); |
| 155 |
if (required != -1 && required > 0) |
| 156 |
{ |
| 157 |
XsSize reqv = (XsSize)(ptrdiff_t)required+1; |
| 158 |
if (reqv > thisPtr->m_reserved) |
| 159 |
XsArray_reserve(thisPtr, reqv); |
| 160 |
WideCharToMultiByte(CP_UTF8, 0, src, unicodeLength, thisPtr->m_data, required+1, NULL, NULL); |
| 161 |
thisPtr->m_data[required] = '\0'; |
| 162 |
*((XsSize*) &thisPtr->m_size) = reqv; |
| 163 |
return; |
| 164 |
} |
| 165 |
#else |
| 166 |
size_t required = wcstombs(0, src, 0); |
| 167 |
if (required != (size_t) -1 && required > 0) |
| 168 |
{ |
| 169 |
if ((XsSize)required+1 > thisPtr->m_reserved) |
| 170 |
XsArray_reserve(thisPtr, required+1); |
| 171 |
wcstombs(thisPtr->m_data, src, required+1); |
| 172 |
*((XsSize*) &thisPtr->m_size) = required+1; |
| 173 |
return; |
| 174 |
} |
| 175 |
#endif |
| 176 |
} |
| 177 |
XsArray_assign(thisPtr, 0, 0); |
| 178 |
} |
| 179 |
|
| 180 |
/*! \brief This function copies the contents of the object to a unicode wchar_t array |
| 181 |
*/ |
| 182 |
XsSize XsString_copyToWCharArray(const XsString* thisPtr, wchar_t* dest, XsSize size) |
| 183 |
{ |
| 184 |
#ifdef WIN32 |
| 185 |
return (XsSize)(ptrdiff_t) MultiByteToWideChar(CP_UTF8, 0, thisPtr->m_data, (int)(ptrdiff_t) thisPtr->m_size, dest, (int)(ptrdiff_t) size); |
| 186 |
#else |
| 187 |
return mbstowcs(dest, thisPtr->m_data, size) + (dest?0:1); |
| 188 |
#endif |
| 189 |
} |
| 190 |
|
| 191 |
/*! \brief Append unicode character \a c to the string |
| 192 |
\param c The character to append |
| 193 |
*/ |
| 194 |
void XsString_push_backWChar(XsString* thisPtr, wchar_t c) |
| 195 |
{ |
| 196 |
wchar_t buf[2] = { c, 0 }; |
| 197 |
XsString tmp; |
| 198 |
|
| 199 |
XsString_construct(&tmp); |
| 200 |
XsString_assignWCharArray(&tmp, buf); |
| 201 |
XsString_append(thisPtr, &tmp); |
| 202 |
XsString_destruct(&tmp); |
| 203 |
} |
| 204 |
#endif // XSENS_NO_WCHAR |
| 205 |
|
| 206 |
/*! \brief This function resizes the contained string to the desired size, while retaining its contents |
| 207 |
\param count The desired size of the string. This excludes the terminating 0 character. |
| 208 |
\sa XsArray_resize |
| 209 |
*/ |
| 210 |
void XsString_resize(XsString* thisPtr, XsSize count) |
| 211 |
{ |
| 212 |
XsSize sz = thisPtr->m_size; |
| 213 |
XsArray_resize(thisPtr, count?count+1:0); |
| 214 |
if (count) |
| 215 |
{ |
| 216 |
for (;sz < count; ++sz) |
| 217 |
thisPtr->m_data[sz] = ' '; |
| 218 |
thisPtr->m_data[count] = 0; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/*! \brief This function concatenates the \a other to this |
| 223 |
*/ |
| 224 |
void XsString_append(XsString* thisPtr, XsString const* other) |
| 225 |
{ |
| 226 |
if (other && other->m_size > 1) |
| 227 |
{ |
| 228 |
// remove terminating null from this and append arrays |
| 229 |
XsArray_erase(thisPtr, thisPtr->m_size-1, 1); |
| 230 |
XsArray_append(thisPtr, other); |
| 231 |
if (thisPtr == other) |
| 232 |
{ |
| 233 |
// add terminating null again |
| 234 |
static const char nullChar = 0; |
| 235 |
XsArray_insert(thisPtr, (XsSize) -1, 1, &nullChar); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/*! \copydoc XsArray_erase |
| 241 |
\note The function maintains the terminating 0 character |
| 242 |
*/ |
| 243 |
void XsString_erase(XsString* thisPtr, XsSize index, XsSize count) |
| 244 |
{ |
| 245 |
if (index + count >= thisPtr->m_size) |
| 246 |
{ |
| 247 |
if (index) |
| 248 |
XsArray_erase(thisPtr, index, (thisPtr->m_size-1)-index); |
| 249 |
else |
| 250 |
XsArray_erase(thisPtr, 0, thisPtr->m_size); |
| 251 |
} |
| 252 |
else |
| 253 |
XsArray_erase(thisPtr, index, count); |
| 254 |
} |
| 255 |
|
| 256 |
/*! \brief Append character \a c to the string |
| 257 |
\param c The character to append |
| 258 |
*/ |
| 259 |
void XsString_push_back(XsString* thisPtr, char c) |
| 260 |
{ |
| 261 |
XsSize sz = thisPtr->m_size; |
| 262 |
if (!sz) |
| 263 |
sz = 1; |
| 264 |
XsString_resize(thisPtr, sz); |
| 265 |
thisPtr->m_data[sz-1] = c; |
| 266 |
} |
| 267 |
|
| 268 |
uint8_t const * advanceUtf8(const uint8_t* p) |
| 269 |
{ |
| 270 |
if ((*p & 0xC0) != 0xC0) |
| 271 |
++p; |
| 272 |
else |
| 273 |
if (*p & 0x20) |
| 274 |
if (*p & 0x10) |
| 275 |
if (*p & 0x08) |
| 276 |
if (*p & 0x04) |
| 277 |
p += 6; |
| 278 |
else |
| 279 |
p += 5; |
| 280 |
else |
| 281 |
p += 4; |
| 282 |
else |
| 283 |
p += 3; |
| 284 |
else |
| 285 |
p += 2; |
| 286 |
return p; |
| 287 |
} |
| 288 |
|
| 289 |
/*! \brief Returns the number of characters in a UTF-8 encoded string |
| 290 |
\details http://en.wikipedia.org/wiki/Utf-8#Description |
| 291 |
\returns the number of characters in a UTF-8 encoded string |
| 292 |
*/ |
| 293 |
XsSize XsString_utf8Len(XsString const * thisPtr) |
| 294 |
{ |
| 295 |
XsSize count = 0; |
| 296 |
uint8_t const * p = (uint8_t const*) thisPtr->m_data; |
| 297 |
|
| 298 |
if (!thisPtr || !thisPtr->m_data) |
| 299 |
return 0; |
| 300 |
|
| 301 |
while (*p != 0) |
| 302 |
{ |
| 303 |
++count; |
| 304 |
p = advanceUtf8(p); |
| 305 |
} |
| 306 |
return count; |
| 307 |
} |
| 308 |
|
| 309 |
#ifndef XSENS_NO_WCHAR |
| 310 |
int32_t shiftUtf8(int32_t t, uint8_t const* p, int bytes) |
| 311 |
{ |
| 312 |
int i; |
| 313 |
for (i = 0; i < bytes; ++i) |
| 314 |
t = (t << 6) | (p[i] & 0x3F); |
| 315 |
return t; |
| 316 |
} |
| 317 |
|
| 318 |
/*! \brief The decoded UTF-8 character at index \a index in the UTF-8 encoded string |
| 319 |
\details http://en.wikipedia.org/wiki/Utf-8#Description |
| 320 |
\param index The index of the character to return. |
| 321 |
\returns the decoded UTF-8 character at index \a index in the UTF-8 encoded string |
| 322 |
*/ |
| 323 |
wchar_t XsString_utf8At(XsString const* thisPtr, XsSize index) |
| 324 |
{ |
| 325 |
int32_t t = 0; |
| 326 |
uint8_t const * p = (uint8_t const*) thisPtr->m_data; |
| 327 |
|
| 328 |
if (!thisPtr || !thisPtr->m_data) |
| 329 |
return 0; |
| 330 |
|
| 331 |
while (*p != 0 && index) |
| 332 |
{ |
| 333 |
--index; |
| 334 |
p = advanceUtf8(p); |
| 335 |
} |
| 336 |
|
| 337 |
if (*p == 0) |
| 338 |
return 0; |
| 339 |
|
| 340 |
// translate! |
| 341 |
|
| 342 |
if ((*p & 0xC0) != 0xC0) |
| 343 |
t = (*p & 0x7F); |
| 344 |
else |
| 345 |
if (*p & 0x20) |
| 346 |
if (*p & 0x10) |
| 347 |
if (*p & 0x08) |
| 348 |
if (*p & 0x04) |
| 349 |
t = shiftUtf8(p[0] & 0x01, p+1, 5); |
| 350 |
else |
| 351 |
t = shiftUtf8(p[0] & 0x03, p+1, 4); |
| 352 |
else |
| 353 |
t = shiftUtf8(p[0] & 0x07, p+1, 3); |
| 354 |
else |
| 355 |
t = shiftUtf8(p[0] & 0x0F, p+1, 2); |
| 356 |
else |
| 357 |
t = shiftUtf8(p[0] & 0x1F, p+1, 1); |
| 358 |
return (wchar_t) t; |
| 359 |
} |
| 360 |
#endif |
| 361 |
|
| 362 |
/*! \brief Returns whether this string ends with \a other |
| 363 |
\param other The string to match with the end of this string |
| 364 |
\param caseSensitive Whether to compare case sensitive or not |
| 365 |
\return true when the string ends with the given string |
| 366 |
*/ |
| 367 |
int XsString_endsWith(XsString const * thisPtr, XsString const* other, int caseSensitive) |
| 368 |
{ |
| 369 |
const char* left; |
| 370 |
const char* right; |
| 371 |
|
| 372 |
// we can never find a bigger string than our own string |
| 373 |
if (thisPtr->m_size < other->m_size) |
| 374 |
return 0; |
| 375 |
|
| 376 |
// we always match an empty string |
| 377 |
if (other->m_size <= 1) |
| 378 |
return 1; |
| 379 |
|
| 380 |
left = thisPtr->m_data + thisPtr->m_size - other->m_size; |
| 381 |
right = other->m_data; |
| 382 |
|
| 383 |
if (caseSensitive) |
| 384 |
for (; *left == *right && *right; ++left, ++right); |
| 385 |
else |
| 386 |
for (; tolower((unsigned char)*left) == tolower((unsigned char)*right) && *right; ++left, ++right); |
| 387 |
|
| 388 |
if (!*right) |
| 389 |
return 1; |
| 390 |
|
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
/*! \brief Returns whether this string starts with \a other |
| 395 |
\param other The string to match with the start of this string |
| 396 |
\param caseSensitive Whether to compare case sensitive or not |
| 397 |
\return true when the string starts with the given string |
| 398 |
*/ |
| 399 |
int XsString_startsWith(XsString const * thisPtr, XsString const* other, int caseSensitive) |
| 400 |
{ |
| 401 |
const char* left = thisPtr->m_data; |
| 402 |
const char* right = other->m_data; |
| 403 |
|
| 404 |
// we can never find a bigger string than our own string |
| 405 |
if (thisPtr->m_size < other->m_size) |
| 406 |
return 0; |
| 407 |
|
| 408 |
// we always match an empty string |
| 409 |
if (other->m_size <= 1) |
| 410 |
return 1; |
| 411 |
|
| 412 |
if (caseSensitive) |
| 413 |
for (; *left == *right && *right; ++left, ++right); |
| 414 |
else |
| 415 |
for (; tolower((unsigned char)*left) == tolower((unsigned char)*right) && *right; ++left, ++right); |
| 416 |
|
| 417 |
if (!*right) |
| 418 |
return 1; |
| 419 |
|
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
/*! \brief Returns whether this string contains \a other |
| 424 |
\param other The string to match with this string |
| 425 |
\param caseSensitive Whether to compare case sensitive or not (case insensitive is the default) |
| 426 |
\param offset when not null, this will be filled with the offset at which \a other was found |
| 427 |
\return true when the string contains the given string |
| 428 |
*/ |
| 429 |
int XsString_contains(XsString const * thisPtr, XsString const* other, int caseSensitive, XsSize* offset) |
| 430 |
{ |
| 431 |
XsSize offsetI = 0; |
| 432 |
if (!offset) |
| 433 |
offset = &offsetI; |
| 434 |
*offset = 0; |
| 435 |
|
| 436 |
// we always match an empty string |
| 437 |
if (other->m_size <= 1) |
| 438 |
return 1; |
| 439 |
|
| 440 |
// we can never find a bigger string than our own string |
| 441 |
while (thisPtr->m_size-*offset >= other->m_size) |
| 442 |
{ |
| 443 |
const char* left = thisPtr->m_data+*offset; |
| 444 |
const char* right = other->m_data; |
| 445 |
if (caseSensitive) |
| 446 |
for (; *left == *right && *right; ++left, ++right); |
| 447 |
else |
| 448 |
for (; tolower((unsigned char)*left) == tolower((unsigned char)*right) && *right; ++left, ++right); |
| 449 |
|
| 450 |
if (!*right) |
| 451 |
return 1; |
| 452 |
|
| 453 |
++*offset; |
| 454 |
} |
| 455 |
*offset = (XsSize)-1; |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
/*! \brief Returns true when the supplied string is empty |
| 460 |
\return true when the string is empty |
| 461 |
*/ |
| 462 |
int XsString_empty(XsString const * thisPtr) |
| 463 |
{ |
| 464 |
if (!thisPtr) |
| 465 |
return 1; |
| 466 |
if (!thisPtr->m_size || (thisPtr->m_flags & XSDF_Empty)) |
| 467 |
return 1; |
| 468 |
return !(thisPtr->m_size-1); |
| 469 |
} |
| 470 |
|
| 471 |
/*! \brief Sorts the string |
| 472 |
\details This function sorts using qsort |
| 473 |
*/ |
| 474 |
void XsString_sort(XsString* thisPtr) |
| 475 |
{ |
| 476 |
if (thisPtr->m_size > 2) |
| 477 |
qsort(thisPtr->m_data, thisPtr->m_size-1, sizeof(char), compareChar); |
| 478 |
} |
| 479 |
|
| 480 |
/*! \brief Reverses the contents of the string |
| 481 |
\details This reverses the contents in-place |
| 482 |
\note This does not take into account utf-8 encoded characters |
| 483 |
*/ |
| 484 |
void XsString_reverse(XsString* thisPtr) |
| 485 |
{ |
| 486 |
int i, half; |
| 487 |
char* data, tmp, *right; |
| 488 |
if (thisPtr->m_size > 2) |
| 489 |
{ |
| 490 |
half = (int) ((thisPtr->m_size-1) >> 1); |
| 491 |
data = (char*) thisPtr->m_data; |
| 492 |
right = data + thisPtr->m_size-2; |
| 493 |
for (i = 0; i < half; ++i) |
| 494 |
{ |
| 495 |
tmp = data[i]; |
| 496 |
data[i] = right[-i]; |
| 497 |
right[-i] = tmp; |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
/*! \brief Find the first occurrence of \a needle in the string |
| 503 |
\param needle The string to find |
| 504 |
\return The offset of \a needle or -1 if it was not found |
| 505 |
*/ |
| 506 |
int XsString_findSubStr(XsString const* thisPtr, XsString const* needle) |
| 507 |
{ |
| 508 |
XsSize offset, i, end, endN; |
| 509 |
if (!thisPtr) // no string to search in |
| 510 |
return -1; |
| 511 |
if (!needle || needle->m_size <= 1) // empty string matches start of string, even if searchee is empty |
| 512 |
return 0; |
| 513 |
if (thisPtr->m_size <= 1 || thisPtr->m_size < needle->m_size) |
| 514 |
return -1; |
| 515 |
|
| 516 |
end = thisPtr->m_size - needle->m_size; |
| 517 |
endN = needle->m_size-1; |
| 518 |
|
| 519 |
for (offset = 0; offset <= end; ++offset) |
| 520 |
{ |
| 521 |
for (i = 0; i < endN; ++i) |
| 522 |
if (thisPtr->m_data[offset + i] != needle->m_data[i]) |
| 523 |
break; |
| 524 |
if (i == endN) |
| 525 |
return (int) offset; // found! |
| 526 |
} |
| 527 |
// not found |
| 528 |
return -1; |
| 529 |
} |
| 530 |
|
| 531 |
/*! \brief Copy a substring of the \a source string |
| 532 |
\details The function copies up to \a count characters from \a source to the string, starting at offset \a start |
| 533 |
\param source The source to copy from |
| 534 |
\param start The offset of the first character to copy |
| 535 |
\param count The maximum number of characters to copy |
| 536 |
*/ |
| 537 |
void XsString_mid(XsString* thisPtr, XsString const* source, XsSize start, XsSize count) |
| 538 |
{ |
| 539 |
if (!thisPtr || !source) |
| 540 |
return; |
| 541 |
if (start >= source->m_size) |
| 542 |
{ |
| 543 |
XsString_assign(thisPtr, 0, 0); |
| 544 |
return; |
| 545 |
} |
| 546 |
if (start + count >= source->m_size) |
| 547 |
count = (source->m_size - 1) - start; |
| 548 |
XsString_assign(thisPtr, count, count ? source->m_data + start : 0); |
| 549 |
} |