| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 30 авг. 2017 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_RUNTIME_LSPSTRING_H_ |
| 23 |
#define LSP_PLUG_IN_RUNTIME_LSPSTRING_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/stdlib/string.h> |
| 28 |
#include <lsp-plug.in/lltl/types.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <stdarg.h> |
| 31 |
|
| 32 |
namespace lsp |
| 33 |
{ |
| 34 |
/** |
| 35 |
* String class |
| 36 |
*/ |
| 37 |
class LSPString |
| 38 |
{ |
| 39 |
protected: |
| 40 |
typedef struct buffer_t |
| 41 |
{ |
| 42 |
size_t nOffset; |
| 43 |
size_t nLength; |
| 44 |
char *pData; |
| 45 |
} buffer_t; |
| 46 |
|
| 47 |
protected: |
| 48 |
size_t nLength; |
| 49 |
size_t nCapacity; |
| 50 |
lsp_wchar_t *pData; |
| 51 |
mutable size_t nHash; |
| 52 |
mutable buffer_t *pTemp; |
| 53 |
|
| 54 |
protected: |
| 55 |
bool size_reserve(size_t size); |
| 56 |
inline bool cap_reserve(size_t size); |
| 57 |
inline bool cap_grow(size_t delta); |
| 58 |
void drop_temp(); |
| 59 |
bool append_temp(const char *p, size_t n) const; |
| 60 |
bool resize_temp(size_t n) const; |
| 61 |
bool grow_temp(size_t n) const; |
| 62 |
|
| 63 |
static inline lsp_wchar_t *xmalloc(size_t size) { return reinterpret_cast<lsp_wchar_t *>(::malloc(size * sizeof(lsp_wchar_t))); } |
| 64 |
static inline lsp_wchar_t *xrealloc(lsp_wchar_t * ptr, size_t size) { return reinterpret_cast<lsp_wchar_t *>(::realloc(ptr, size * sizeof(lsp_wchar_t))); }; |
| 65 |
static inline void xfree(lsp_wchar_t *ptr) { ::free(ptr); } |
| 66 |
static inline void xmove(lsp_wchar_t *dst, const lsp_wchar_t *src, size_t n) { ::memmove(dst, src, n * sizeof(lsp_wchar_t)); } |
| 67 |
static inline size_t xlen(const lsp_wchar_t *s); |
| 68 |
static inline size_t u16len(const lsp_utf16_t *s); |
| 69 |
|
| 70 |
#ifdef ARCH_LE |
| 71 |
static inline int xcmp(const lsp_wchar_t *a, const lsp_wchar_t *b, size_t n) { return ::memcmp(a, b, n * sizeof(lsp_wchar_t)); } |
| 72 |
#else |
| 73 |
static int xcmp(const lsp_wchar_t *a, const lsp_wchar_t *b, size_t n); |
| 74 |
#endif /* ARCH_LE */ |
| 75 |
static int xcasecmp(const lsp_wchar_t *a, const lsp_wchar_t *b, size_t n); |
| 76 |
static inline void acopy(lsp_wchar_t *dst, const char *src, size_t n); |
| 77 |
|
| 78 |
public: |
| 79 |
explicit LSPString(); |
| 80 |
~LSPString(); |
| 81 |
|
| 82 |
public: |
| 83 |
/** Get the length of the string |
| 84 |
* |
| 85 |
* @return the length of the string |
| 86 |
*/ |
| 87 |
inline size_t length() const { return nLength; } |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the length of the range |
| 91 |
* @param first the index of first character |
| 92 |
* @param last the index of last character |
| 93 |
* @return final length of the range |
| 94 |
*/ |
| 95 |
size_t range_length(ssize_t first, ssize_t last) const; |
| 96 |
|
| 97 |
/** |
| 98 |
* Set the length of the string, allows only to cut data from tail |
| 99 |
* @param length the length of the string |
| 100 |
* @return the length of the string after applied operarion |
| 101 |
*/ |
| 102 |
size_t set_length(size_t length); |
| 103 |
|
| 104 |
/** Get the capacity of the string |
| 105 |
* |
| 106 |
* @return capacity of the string |
| 107 |
*/ |
| 108 |
inline size_t capacity() const { return nCapacity; } |
| 109 |
|
| 110 |
/** Check whether the string is emtpy |
| 111 |
* |
| 112 |
* @return true if string is empty |
| 113 |
*/ |
| 114 |
inline bool is_empty() const { return nLength <= 0; } |
| 115 |
|
| 116 |
/** Get pointer to internal non-zero-terminated characters array |
| 117 |
* This method is unsafe and is provided only for low-level optimizations |
| 118 |
* You should know what you're doing when calling this method |
| 119 |
* |
| 120 |
* @return pointer to internal to internal non-null-terminated characters array |
| 121 |
*/ |
| 122 |
inline const lsp_wchar_t *characters() const { return pData; } |
| 123 |
|
| 124 |
/** Reserve additional capacity at the tail for further modifications |
| 125 |
* |
| 126 |
* @param size number of characters to use as capacity |
| 127 |
* @return true on success |
| 128 |
*/ |
| 129 |
inline bool reserve(size_t size) { return (size > nCapacity) ? size_reserve(size) : true; }; |
| 130 |
|
| 131 |
/** |
| 132 |
* Clear the string without deallocating internal buffer |
| 133 |
*/ |
| 134 |
void clear(); |
| 135 |
|
| 136 |
/** Truncate the string (and remove extra capacity) |
| 137 |
* |
| 138 |
*/ |
| 139 |
void truncate(); |
| 140 |
bool truncate(size_t size); |
| 141 |
|
| 142 |
/** Try to reduce memory allocated by the string (remove extra capacity) |
| 143 |
* |
| 144 |
*/ |
| 145 |
void reduce(); |
| 146 |
|
| 147 |
/** |
| 148 |
* Trim spaces at the start of line and at the end of line |
| 149 |
*/ |
| 150 |
void trim(); |
| 151 |
|
| 152 |
/** Swap internal contents with other string |
| 153 |
* |
| 154 |
* @param src string to perform the swap |
| 155 |
*/ |
| 156 |
void swap(LSPString *src); |
| 157 |
inline void swap(LSPString &src) { swap(&src); } |
| 158 |
|
| 159 |
/** Swap two characters |
| 160 |
* |
| 161 |
* @param idx1 character at first position |
| 162 |
* @param idx2 character at second position |
| 163 |
* @return true on success |
| 164 |
*/ |
| 165 |
bool swap(ssize_t idx1, ssize_t idx2); |
| 166 |
|
| 167 |
/** Take data from another string and empty that string |
| 168 |
* |
| 169 |
* @param src source string |
| 170 |
*/ |
| 171 |
void take(LSPString *src); |
| 172 |
void take(LSPString &src); |
| 173 |
|
| 174 |
/** Give data to another string and empty self |
| 175 |
* |
| 176 |
* @param dst target string |
| 177 |
*/ |
| 178 |
inline void give(LSPString *dst) { dst->take(this); } |
| 179 |
|
| 180 |
/** Copy this string |
| 181 |
* |
| 182 |
* @return copy of the string or NULL on error |
| 183 |
*/ |
| 184 |
LSPString *copy() const; |
| 185 |
LSPString *copy(ssize_t first) const; |
| 186 |
LSPString *copy(ssize_t first, ssize_t last) const; |
| 187 |
inline LSPString *clone() const { return copy(); } |
| 188 |
inline LSPString *clone(ssize_t first) const { return copy(first); } |
| 189 |
inline LSPString *clone(ssize_t first, ssize_t last) const { return copy(first, last); } |
| 190 |
|
| 191 |
/** Allocate instance of string and drain self data to this instance |
| 192 |
* |
| 193 |
* @return not null pointer is there is enough memory |
| 194 |
*/ |
| 195 |
LSPString *release(); |
| 196 |
|
| 197 |
/** Insert data at specified position |
| 198 |
* |
| 199 |
* @param pos position |
| 200 |
* @param ch character to insert |
| 201 |
* @return true on success |
| 202 |
*/ |
| 203 |
bool insert(ssize_t pos, lsp_wchar_t ch); |
| 204 |
|
| 205 |
/** Insert character data at specified position |
| 206 |
* |
| 207 |
* @param pos position |
| 208 |
* @param arr array of characters to insert |
| 209 |
* @param n number of characters in array |
| 210 |
* @return true on success |
| 211 |
*/ |
| 212 |
bool insert(ssize_t pos, const lsp_wchar_t *arr, ssize_t n); |
| 213 |
|
| 214 |
/** Insert string at specified position |
| 215 |
* |
| 216 |
* @param pos position |
| 217 |
* @param src source string to insert |
| 218 |
* @return true on success |
| 219 |
*/ |
| 220 |
bool insert(ssize_t pos, const LSPString *src); |
| 221 |
|
| 222 |
/** Insert substring at specified position |
| 223 |
* |
| 224 |
* @param pos position |
| 225 |
* @param src source string to use substring for insert |
| 226 |
* @param first index of first character to use as sub-string of the source string |
| 227 |
* @return true on success |
| 228 |
*/ |
| 229 |
bool insert(ssize_t pos, const LSPString *src, ssize_t first); |
| 230 |
|
| 231 |
/** Insert substring at specified position |
| 232 |
* |
| 233 |
* @param pos position |
| 234 |
* @param src source string to use substring for insert |
| 235 |
* @param first index of first character to use as sub-string of the source string |
| 236 |
* @param last index of last character to use as sub-string of the source string |
| 237 |
* @return true on success |
| 238 |
*/ |
| 239 |
bool insert(ssize_t pos, const LSPString *src, ssize_t first, ssize_t last); |
| 240 |
|
| 241 |
/** Append data at the tail of string |
| 242 |
* |
| 243 |
* @param ch ASCII character to append |
| 244 |
* @return true on success |
| 245 |
*/ |
| 246 |
bool append(char ch); |
| 247 |
|
| 248 |
/** Append data at the tail of string |
| 249 |
* |
| 250 |
* @param ch character to append |
| 251 |
* @return true on success |
| 252 |
*/ |
| 253 |
bool append(lsp_wchar_t ch); |
| 254 |
|
| 255 |
/** Append data at the tail of string |
| 256 |
* |
| 257 |
* @param ch character to append |
| 258 |
* @return true on success |
| 259 |
*/ |
| 260 |
bool append(lsp_swchar_t ch); |
| 261 |
|
| 262 |
bool append_ascii(const char *arr, size_t n); |
| 263 |
bool append_utf8(const char *arr, size_t n); |
| 264 |
bool append_utf16(const lsp_utf16_t *arr, size_t n); |
| 265 |
bool append(const lsp_wchar_t *arr, size_t n); |
| 266 |
bool append(const lsp_wchar_t *arr); |
| 267 |
bool append(const LSPString *src); |
| 268 |
bool append(const LSPString *src, ssize_t first); |
| 269 |
bool append(const LSPString *src, ssize_t first, ssize_t last); |
| 270 |
inline bool append_ascii(const char *arr) { return append_ascii(arr, ::strlen(arr)); }; |
| 271 |
inline bool append_utf8(const char *arr) { return append_utf8(arr, ::strlen(arr)); }; |
| 272 |
bool append_utf16(const lsp_utf16_t *arr); |
| 273 |
|
| 274 |
bool prepend(lsp_wchar_t ch); |
| 275 |
bool prepend(const lsp_wchar_t *arr, size_t n); |
| 276 |
bool prepend_utf8(const char *arr, size_t n); |
| 277 |
bool prepend_ascii(const char *arr, size_t n); |
| 278 |
bool prepend(const LSPString *src); |
| 279 |
bool prepend(const LSPString *src, ssize_t first); |
| 280 |
bool prepend(const LSPString *src, ssize_t first, ssize_t last); |
| 281 |
inline bool prepend_ascii(const char *arr) { return prepend_ascii(arr, ::strlen(arr)); }; |
| 282 |
inline bool prepend_utf8(const char *arr) { return prepend_utf8(arr, ::strlen(arr)); }; |
| 283 |
|
| 284 |
/** Get unicode character at the specified position |
| 285 |
* |
| 286 |
* @param index index |
| 287 |
* @return character or 0 on error |
| 288 |
*/ |
| 289 |
lsp_wchar_t at(ssize_t index) const; |
| 290 |
lsp_wchar_t char_at(ssize_t index) const { return at(index); } |
| 291 |
|
| 292 |
/** |
| 293 |
* Get first character |
| 294 |
* @return first character on 0 on error |
| 295 |
*/ |
| 296 |
lsp_wchar_t first() const; |
| 297 |
|
| 298 |
/** |
| 299 |
* Get last character |
| 300 |
* @return last character on 0 on error |
| 301 |
*/ |
| 302 |
lsp_wchar_t last() const; |
| 303 |
|
| 304 |
/** Reverse the order of characters |
| 305 |
* |
| 306 |
*/ |
| 307 |
void reverse(); |
| 308 |
|
| 309 |
/** Shuffle characters |
| 310 |
* |
| 311 |
*/ |
| 312 |
void shuffle(); |
| 313 |
|
| 314 |
|
| 315 |
bool set(lsp_wchar_t ch); |
| 316 |
inline bool set(char ch) { return set(lsp_wchar_t(uint8_t(ch))); }; |
| 317 |
bool set(ssize_t pos, lsp_wchar_t ch); |
| 318 |
inline bool set_at(ssize_t pos, lsp_wchar_t ch) { return set(pos, ch); } |
| 319 |
bool set(const lsp_wchar_t *arr); |
| 320 |
bool set(const lsp_wchar_t *arr, size_t n); |
| 321 |
|
| 322 |
/** Copy contents from another string |
| 323 |
* @param src source string |
| 324 |
*/ |
| 325 |
bool set(const LSPString *src); |
| 326 |
|
| 327 |
/** Copy contents from substring of another string |
| 328 |
* @param src source string |
| 329 |
* @param first the first character of source substring |
| 330 |
*/ |
| 331 |
bool set(const LSPString *src, ssize_t first); |
| 332 |
|
| 333 |
/** Copy contents from substring of another string |
| 334 |
* @param src source string to obtain a substring |
| 335 |
* @param first the first character of source substring |
| 336 |
* @param last the last character of source substring |
| 337 |
*/ |
| 338 |
bool set(const LSPString *src, ssize_t first, ssize_t last); |
| 339 |
|
| 340 |
/** Different conversion routines |
| 341 |
* |
| 342 |
* @param s pointer to UTF-8 string |
| 343 |
* @param n number of octets in the string |
| 344 |
* @return true on success |
| 345 |
*/ |
| 346 |
bool set_utf8(const char *s, size_t n); |
| 347 |
inline bool set_utf8(const char *s) { return set_utf8(s, ::strlen(s)); }; |
| 348 |
|
| 349 |
bool set_utf16(const lsp_utf16_t *s, size_t n); |
| 350 |
bool set_utf16(const lsp_utf16_t *s); |
| 351 |
|
| 352 |
bool set_utf16le(const lsp_utf16_t *s, size_t n); |
| 353 |
bool set_utf16le(const lsp_utf16_t *s); |
| 354 |
|
| 355 |
bool set_utf16be(const lsp_utf16_t *s, size_t n); |
| 356 |
bool set_utf16be(const lsp_utf16_t *s); |
| 357 |
|
| 358 |
bool set_ascii(const char *s, size_t n); |
| 359 |
inline bool set_ascii(const char *s) { return set_ascii(s, ::strlen(s)); }; |
| 360 |
|
| 361 |
bool set_native(const char *s, size_t n, const char *charset = NULL); |
| 362 |
inline bool set_native(const char *s, const char *charset) { return set_native(s, ::strlen(s), charset); }; |
| 363 |
inline bool set_native(const char *s) { return set_native(s, ::strlen(s), NULL); }; |
| 364 |
|
| 365 |
const char *get_utf8(ssize_t first, ssize_t last) const; |
| 366 |
inline const char *get_utf8(ssize_t first) const { return get_utf8(first, nLength); }; |
| 367 |
const char *get_utf8() const { return get_utf8(0, nLength); }; |
| 368 |
|
| 369 |
const lsp_utf16_t *get_utf16(ssize_t first, ssize_t last) const; |
| 370 |
inline const lsp_utf16_t *get_utf16(ssize_t first) const { return get_utf16(first, nLength); }; |
| 371 |
inline const lsp_utf16_t *get_utf16() const { return get_utf16(0, nLength); }; |
| 372 |
|
| 373 |
const lsp_utf16_t *get_utf16le(ssize_t first, ssize_t last) const; |
| 374 |
inline const lsp_utf16_t *get_utf16le(ssize_t first) const { return get_utf16le(first, nLength); }; |
| 375 |
inline const lsp_utf16_t *get_utf16le() const { return get_utf16le(0, nLength); }; |
| 376 |
|
| 377 |
const lsp_utf16_t *get_utf16be(ssize_t first, ssize_t last) const; |
| 378 |
inline const lsp_utf16_t *get_utf16be(ssize_t first) const { return get_utf16be(first, nLength); }; |
| 379 |
inline const lsp_utf16_t *get_utf16be() const { return get_utf16be(0, nLength); }; |
| 380 |
|
| 381 |
const char *get_ascii(ssize_t first, ssize_t last) const; |
| 382 |
inline const char *get_ascii(ssize_t first) const { return get_ascii(first, nLength); }; |
| 383 |
inline const char *get_ascii() const { return get_ascii(0, nLength); }; |
| 384 |
|
| 385 |
const char *get_native(ssize_t first, ssize_t last, const char *charset = NULL) const; |
| 386 |
inline const char *get_native(const char *charset = NULL) const { return get_native(0, nLength, charset); } |
| 387 |
inline const char *get_native(ssize_t first, const char *charset = NULL) const { return get_native(first, nLength, charset); } |
| 388 |
|
| 389 |
inline size_t temporal_size() const { return (pTemp != NULL) ? pTemp->nOffset : 0; }; |
| 390 |
inline size_t temporal_capacity() const { return (pTemp != NULL) ? pTemp->nLength : 0; }; |
| 391 |
|
| 392 |
/** |
| 393 |
* Find number of matching characters from one string to another |
| 394 |
*/ |
| 395 |
size_t match(const LSPString *s, size_t index) const; |
| 396 |
inline size_t match(const LSPString *s) const { return match(s, 0); } |
| 397 |
size_t match_nocase(const LSPString *s, size_t index) const; |
| 398 |
inline size_t match_nocase(const LSPString *s) const { return match_nocase(s, 0); } |
| 399 |
|
| 400 |
/** |
| 401 |
* Clone string as set of UTF-8 characters. 1 character at the tail will contain additional end-of-line. |
| 402 |
*/ |
| 403 |
char *clone_utf8(size_t *bytes, ssize_t first, ssize_t last) const; |
| 404 |
inline char *clone_utf8(size_t *bytes, ssize_t first) const { return clone_utf8(bytes, first, nLength); }; |
| 405 |
inline char *clone_utf8(size_t *bytes) const { return clone_utf8(bytes, 0, nLength); }; |
| 406 |
inline char *clone_utf8(ssize_t first, ssize_t last) const { return clone_utf8(NULL, first, last); }; |
| 407 |
inline char *clone_utf8(ssize_t first) const { return clone_utf8(NULL, first, nLength); }; |
| 408 |
inline char *clone_utf8() const { return clone_utf8(NULL, 0, nLength); }; |
| 409 |
|
| 410 |
/** |
| 411 |
* Clone string as set of UTF-16 characters. 1 UTF-16 character at the tail will contain additional end-of-line. |
| 412 |
*/ |
| 413 |
lsp_utf16_t *clone_utf16(size_t *bytes, ssize_t first, ssize_t last) const; |
| 414 |
inline lsp_utf16_t *clone_utf16(size_t *bytes, ssize_t first) const { return clone_utf16(bytes, first, nLength); }; |
| 415 |
inline lsp_utf16_t *clone_utf16(size_t *bytes) const { return clone_utf16(bytes, 0, nLength); }; |
| 416 |
inline lsp_utf16_t *clone_utf16(ssize_t first, ssize_t last) const { return clone_utf16(NULL, first, last); }; |
| 417 |
inline lsp_utf16_t *clone_utf16(ssize_t first) const { return clone_utf16(NULL, first, nLength); }; |
| 418 |
inline lsp_utf16_t *clone_utf16() const { return clone_utf16(NULL, 0, nLength); }; |
| 419 |
|
| 420 |
lsp_utf16_t *clone_utf16le(size_t *bytes, ssize_t first, ssize_t last) const; |
| 421 |
inline lsp_utf16_t *clone_utf16le(size_t *bytes, ssize_t first) const { return clone_utf16le(bytes, first, nLength); }; |
| 422 |
inline lsp_utf16_t *clone_utf16le(size_t *bytes) const { return clone_utf16le(bytes, 0, nLength); }; |
| 423 |
inline lsp_utf16_t *clone_utf16le(ssize_t first, ssize_t last) const { return clone_utf16le(NULL, first, last); }; |
| 424 |
inline lsp_utf16_t *clone_utf16le(ssize_t first) const { return clone_utf16le(NULL, first, nLength); }; |
| 425 |
inline lsp_utf16_t *clone_utf16le() const { return clone_utf16le(NULL, 0, nLength); }; |
| 426 |
|
| 427 |
lsp_utf16_t *clone_utf16be(size_t *bytes, ssize_t first, ssize_t last) const; |
| 428 |
inline lsp_utf16_t *clone_utf16be(size_t *bytes, ssize_t first) const { return clone_utf16be(bytes, first, nLength); }; |
| 429 |
inline lsp_utf16_t *clone_utf16be(size_t *bytes) const { return clone_utf16be(bytes, 0, nLength); }; |
| 430 |
inline lsp_utf16_t *clone_utf16be(ssize_t first, ssize_t last) const { return clone_utf16be(NULL, first, last); }; |
| 431 |
inline lsp_utf16_t *clone_utf16be(ssize_t first) const { return clone_utf16be(NULL, first, nLength); }; |
| 432 |
inline lsp_utf16_t *clone_utf16be() const { return clone_utf16be(NULL, 0, nLength); }; |
| 433 |
/** |
| 434 |
* Clone string as set of UTF-16 characters. 1 ascii character at the tail will contain additional end-of-line. |
| 435 |
*/ |
| 436 |
char *clone_ascii(size_t *bytes, ssize_t first, ssize_t last) const; |
| 437 |
inline char *clone_ascii(size_t *bytes, ssize_t first) const { return clone_ascii(bytes, first, nLength); }; |
| 438 |
inline char *clone_ascii(size_t *bytes) const { return clone_ascii(bytes, 0, nLength); }; |
| 439 |
inline char *clone_ascii(ssize_t first, ssize_t last) const { return clone_ascii(NULL, first, last); }; |
| 440 |
inline char *clone_ascii(ssize_t first) const { return clone_ascii(NULL, first, nLength); }; |
| 441 |
inline char *clone_ascii() const { return clone_ascii(NULL, 0, nLength); }; |
| 442 |
|
| 443 |
/** |
| 444 |
* Clone string as set of UTF-16 characters. 4 ascii characters at the tail will contain additional end-of-line. |
| 445 |
*/ |
| 446 |
char *clone_native(size_t *bytes, ssize_t first, ssize_t last, const char *charset = NULL) const; |
| 447 |
inline char *clone_native(size_t *bytes, ssize_t first, const char *charset = NULL) const { return clone_native(bytes, first, nLength, charset); } |
| 448 |
inline char *clone_native(size_t *bytes, const char *charset = NULL) const { return clone_native(bytes, 0, nLength, charset); } |
| 449 |
inline char *clone_native(ssize_t first, ssize_t last, const char *charset = NULL) const { return clone_native(NULL, first, last, charset); } |
| 450 |
inline char *clone_native(const char *charset = NULL) const { return clone_native(NULL, 0, nLength, charset); } |
| 451 |
inline char *clone_native(ssize_t first, const char *charset = NULL) const { return clone_native(NULL, first, nLength, charset); } |
| 452 |
|
| 453 |
/** Replace the contents of the string |
| 454 |
* |
| 455 |
*/ |
| 456 |
bool replace(ssize_t pos, lsp_wchar_t ch); |
| 457 |
bool replace(ssize_t pos, const lsp_wchar_t *arr, size_t n); |
| 458 |
bool replace(ssize_t pos, const LSPString *src); |
| 459 |
bool replace(ssize_t pos, const LSPString *src, ssize_t start); |
| 460 |
bool replace(ssize_t pos, const LSPString *src, ssize_t first, ssize_t last); |
| 461 |
|
| 462 |
bool replace(ssize_t first, ssize_t last, lsp_wchar_t ch); |
| 463 |
bool replace(ssize_t first, ssize_t last, const lsp_wchar_t *arr, size_t n); |
| 464 |
bool replace(ssize_t first, ssize_t last, const LSPString *src); |
| 465 |
bool replace(ssize_t first, ssize_t last, const LSPString *src, ssize_t sfirst); |
| 466 |
bool replace(ssize_t first, ssize_t last, const LSPString *src, ssize_t sfirst, ssize_t slast); |
| 467 |
|
| 468 |
/** |
| 469 |
* Replace all characters in the string with the replacement |
| 470 |
* @param ch character to replace |
| 471 |
* @param rep replacement character |
| 472 |
* @return number of replacements |
| 473 |
*/ |
| 474 |
size_t replace_all(lsp_wchar_t ch, lsp_wchar_t rep); |
| 475 |
inline size_t replace_all(char ch, char rep) { return replace_all(lsp_wchar_t(uint8_t(ch)), lsp_wchar_t(uint8_t(rep))); }; |
| 476 |
inline size_t replace_all(char ch, lsp_wchar_t rep) { return replace_all(lsp_wchar_t(uint8_t(ch)), rep); }; |
| 477 |
inline size_t replace_all(lsp_wchar_t ch, char rep) { return replace_all(ch, lsp_wchar_t(uint8_t(rep))); }; |
| 478 |
|
| 479 |
/** Check ending of the string |
| 480 |
* |
| 481 |
*/ |
| 482 |
bool ends_with(lsp_wchar_t ch) const; |
| 483 |
inline bool ends_with(char ch) const { return ends_with(lsp_wchar_t(uint8_t(ch))); }; |
| 484 |
|
| 485 |
bool ends_with(const LSPString *src) const; |
| 486 |
|
| 487 |
bool ends_with_ascii(const char *src) const; |
| 488 |
bool ends_with_ascii_nocase(const char *src) const; |
| 489 |
|
| 490 |
bool ends_with_nocase(lsp_wchar_t ch) const; |
| 491 |
inline bool ends_with_nocase(char ch) const { return ends_with_nocase(lsp_wchar_t(uint8_t(ch))); }; |
| 492 |
bool ends_with_nocase(const LSPString *src) const; |
| 493 |
|
| 494 |
/** |
| 495 |
* Check that string starts with specified character |
| 496 |
* @param ch character to check |
| 497 |
* @param offset the offset to the beginning of the string |
| 498 |
* @return true if string starts with specified character |
| 499 |
*/ |
| 500 |
bool starts_with(lsp_wchar_t ch, size_t offset) const; |
| 501 |
|
| 502 |
/** |
| 503 |
* Check that string starts with specified character |
| 504 |
* @param ch character to check |
| 505 |
* @return true if string starts with specified character |
| 506 |
*/ |
| 507 |
bool starts_with(lsp_wchar_t ch) const { return starts_with(ch, 0); }; |
| 508 |
|
| 509 |
inline bool starts_with(char ch) const { return starts_with(lsp_wchar_t(uint8_t(ch)), 0); }; |
| 510 |
inline bool starts_with(char ch, size_t offset) const { return starts_with(lsp_wchar_t(uint8_t(ch)), offset); }; |
| 511 |
|
| 512 |
bool starts_with(const LSPString *src, size_t offset) const; |
| 513 |
inline bool starts_with(const LSPString *src) const { return starts_with(src, 0); }; |
| 514 |
|
| 515 |
bool starts_with_ascii(const char *src, size_t offset) const; |
| 516 |
inline bool starts_with_ascii(const char *src) const { return starts_with_ascii(src, 0); }; |
| 517 |
|
| 518 |
bool starts_with_nocase(lsp_wchar_t ch, size_t offset) const; |
| 519 |
bool starts_with_nocase(lsp_wchar_t ch) const { return starts_with(ch, 0); }; |
| 520 |
|
| 521 |
inline bool starts_with_nocase(char ch) const { return starts_with_nocase(lsp_wchar_t(uint8_t(ch)), 0); }; |
| 522 |
inline bool starts_with_nocase(char ch, size_t offset) const{ return starts_with_nocase(lsp_wchar_t(uint8_t(ch)), offset); }; |
| 523 |
|
| 524 |
bool starts_with_nocase(const LSPString *src, size_t offset) const; |
| 525 |
inline bool starts_with_nocase(const LSPString *src) const { return starts_with_nocase(src, 0); }; |
| 526 |
|
| 527 |
bool starts_with_ascii_nocase(const char *src, size_t offset) const; |
| 528 |
inline bool starts_with_ascii_nocase(const char *src) const { return starts_with_ascii_nocase(src, 0); }; |
| 529 |
|
| 530 |
/** Delete character sequence from the string |
| 531 |
* |
| 532 |
*/ |
| 533 |
bool remove(); |
| 534 |
bool remove(ssize_t first); |
| 535 |
bool remove(ssize_t first, ssize_t last); |
| 536 |
|
| 537 |
bool remove_last(); |
| 538 |
|
| 539 |
/** Try to find substring in a string |
| 540 |
* |
| 541 |
* @param start start location to begin search |
| 542 |
* @param str string to search |
| 543 |
* @return index of character or negative value |
| 544 |
*/ |
| 545 |
ssize_t index_of(ssize_t start, const LSPString *str) const; |
| 546 |
ssize_t index_of(const LSPString *str) const; |
| 547 |
ssize_t rindex_of(ssize_t start, const LSPString *str) const; |
| 548 |
ssize_t rindex_of(const LSPString *str) const; |
| 549 |
|
| 550 |
ssize_t index_of(ssize_t start, lsp_wchar_t ch) const; |
| 551 |
inline ssize_t index_of(ssize_t start, char ch) const { return index_of(start, lsp_wchar_t(uint8_t(ch))); }; |
| 552 |
ssize_t index_of(lsp_wchar_t ch) const; |
| 553 |
inline ssize_t index_of(char ch) const { return index_of(lsp_wchar_t(uint8_t(ch))); }; |
| 554 |
ssize_t rindex_of(ssize_t start, lsp_wchar_t ch) const; |
| 555 |
inline ssize_t rindex_of(ssize_t start, char ch) const { return rindex_of(start, lsp_wchar_t(uint8_t(ch))); }; |
| 556 |
ssize_t rindex_of(lsp_wchar_t ch) const; |
| 557 |
inline ssize_t rindex_of(char ch) const { return rindex_of(lsp_wchar_t(uint8_t(ch))); }; |
| 558 |
|
| 559 |
ssize_t index_of_nocase(ssize_t start, const LSPString *str) const; |
| 560 |
ssize_t index_of_nocase(const LSPString *str) const; |
| 561 |
ssize_t rindex_of_nocase(ssize_t start, const LSPString *str) const; |
| 562 |
ssize_t rindex_of_nocase(const LSPString *str) const; |
| 563 |
|
| 564 |
ssize_t index_of_nocase(ssize_t start, lsp_wchar_t ch) const; |
| 565 |
inline ssize_t index_of_nocase(ssize_t start, char ch) const { return index_of_nocase(start, lsp_wchar_t(uint8_t(ch))); }; |
| 566 |
ssize_t index_of_nocase(lsp_wchar_t ch) const; |
| 567 |
inline ssize_t index_of_nocase(char ch) const { return index_of_nocase(lsp_wchar_t(uint8_t(ch))); }; |
| 568 |
ssize_t rindex_of_nocase(ssize_t start, lsp_wchar_t ch) const; |
| 569 |
inline ssize_t rindex_of_nocase(ssize_t start, char ch) const { return rindex_of_nocase(start, lsp_wchar_t(uint8_t(ch))); }; |
| 570 |
ssize_t rindex_of_nocase(lsp_wchar_t ch) const; |
| 571 |
inline ssize_t rindex_of_nocase(char ch) const { return rindex_of_nocase(lsp_wchar_t(uint8_t(ch))); }; |
| 572 |
|
| 573 |
/** |
| 574 |
* Produce new object as substring of a string |
| 575 |
* @param first index of first character for a substring |
| 576 |
* @return pointer to substring or NULL on error |
| 577 |
*/ |
| 578 |
LSPString *substring(ssize_t first) const; |
| 579 |
|
| 580 |
/** |
| 581 |
* Produce new object as substring of a string |
| 582 |
* @param first index of first character for a substring |
| 583 |
* @param last index of last character for a substring |
| 584 |
* @return pointer to substring or NULL on error |
| 585 |
*/ |
| 586 |
LSPString *substring(ssize_t first, ssize_t last) const; |
| 587 |
|
| 588 |
/** Compare to another string |
| 589 |
* |
| 590 |
* @param src string to compare to |
| 591 |
* @return result of comparison |
| 592 |
*/ |
| 593 |
int compare_to(const lsp_wchar_t *src) const; |
| 594 |
int compare_to(const lsp_wchar_t *src, size_t n) const; |
| 595 |
inline int compare_to(const LSPString *src) const { return compare_to(src->pData, src->nLength); }; |
| 596 |
int compare_to_ascii(const char *src) const; |
| 597 |
int compare_to_utf8(const char *src) const; |
| 598 |
int compare_to_utf16(const lsp_utf16_t *src) const; |
| 599 |
|
| 600 |
int compare_to_nocase(const lsp_wchar_t *src) const; |
| 601 |
int compare_to_nocase(const lsp_wchar_t *src, size_t n) const; |
| 602 |
inline int compare_to_nocase(const LSPString *src) const { return compare_to_nocase(src->pData, src->nLength); }; |
| 603 |
int compare_to_ascii_nocase(const char *src) const; |
| 604 |
int compare_to_utf8_nocase(const char *src) const; |
| 605 |
int compare_to_utf16_nocase(const lsp_utf16_t *src) const; |
| 606 |
|
| 607 |
size_t tolower(); |
| 608 |
size_t tolower(ssize_t first); |
| 609 |
size_t tolower(ssize_t first, ssize_t last); |
| 610 |
|
| 611 |
size_t toupper(); |
| 612 |
size_t toupper(ssize_t first); |
| 613 |
size_t toupper(ssize_t first, ssize_t last); |
| 614 |
|
| 615 |
/** Check that string equals |
| 616 |
* |
| 617 |
* @param src string to compare |
| 618 |
* @return true if equals |
| 619 |
*/ |
| 620 |
bool equals(const lsp_wchar_t *src) const; |
| 621 |
bool equals(const lsp_wchar_t *src, size_t len) const; |
| 622 |
inline bool equals(const LSPString *src) const { return equals(src->pData, src->nLength); }; |
| 623 |
bool equals(const LSPString *src, ssize_t first, ssize_t last) const; |
| 624 |
|
| 625 |
bool equals_nocase(const lsp_wchar_t *src) const; |
| 626 |
bool equals_nocase(const lsp_wchar_t *src, size_t len) const; |
| 627 |
inline bool equals_nocase(const LSPString *src) const { return equals_nocase(src->pData, src->nLength); }; |
| 628 |
|
| 629 |
inline bool equals_ascii(const char *src) const { return compare_to_ascii(src) == 0; }; |
| 630 |
inline bool equals_ascii_nocase(const char *src) const { return compare_to_ascii_nocase(src) == 0; }; |
| 631 |
|
| 632 |
inline bool equals_utf8(const char *src) const { return compare_to_utf8(src) == 0; }; |
| 633 |
inline bool equals_utf8_nocase(const char *src) const { return compare_to_utf8_nocase(src) == 0; }; |
| 634 |
|
| 635 |
inline bool equals_utf16(const lsp_utf16_t *src) const { return compare_to_utf16(src) == 0; }; |
| 636 |
inline bool equals_utf16_nocase(const lsp_utf16_t *src) const { return compare_to_utf16_nocase(src) == 0; }; |
| 637 |
|
| 638 |
|
| 639 |
bool contains_at(ssize_t index, const lsp_wchar_t *src) const; |
| 640 |
bool contains_at(ssize_t index, const lsp_wchar_t *src, size_t len) const; |
| 641 |
inline bool contains_at(ssize_t index, const LSPString *src) const { return contains_at(index, src->pData, src->nLength); }; |
| 642 |
bool contains_at_ascii(ssize_t index, const char *src) const; |
| 643 |
bool contains_at_utf8(ssize_t index, const char *src) const; |
| 644 |
bool contains_at_utf16(ssize_t index, const lsp_utf16_t *src) const; |
| 645 |
|
| 646 |
/** Calculate number of character occurences |
| 647 |
* |
| 648 |
* @param ch character |
| 649 |
* @return number of character occurences |
| 650 |
*/ |
| 651 |
size_t count(lsp_wchar_t ch) const; |
| 652 |
size_t count(lsp_wchar_t ch, ssize_t first) const; |
| 653 |
size_t count(lsp_wchar_t ch, ssize_t first, ssize_t last) const; |
| 654 |
|
| 655 |
/** |
| 656 |
* Formatting functions, internally asprintf functin is used |
| 657 |
*/ |
| 658 |
ssize_t fmt_append_native(const char *fmt...); |
| 659 |
ssize_t fmt_preend_native(const char *fmt...); |
| 660 |
ssize_t fmt_native(const char *fmt...); |
| 661 |
|
| 662 |
ssize_t vfmt_append_native(const char *fmt, va_list args); |
| 663 |
ssize_t vfmt_preend_native(const char *fmt, va_list args); |
| 664 |
ssize_t vfmt_native(const char *fmt, va_list args); |
| 665 |
|
| 666 |
ssize_t fmt_append_ascii(const char *fmt...); |
| 667 |
ssize_t fmt_prepend_ascii(const char *fmt...); |
| 668 |
ssize_t fmt_ascii(const char *fmt...); |
| 669 |
|
| 670 |
ssize_t vfmt_append_ascii(const char *fmt, va_list args); |
| 671 |
ssize_t vfmt_prepend_ascii(const char *fmt, va_list args); |
| 672 |
ssize_t vfmt_ascii(const char *fmt, va_list args); |
| 673 |
|
| 674 |
ssize_t fmt_append_utf8(const char *fmt...); |
| 675 |
ssize_t fmt_prepend_utf8(const char *fmt...); |
| 676 |
ssize_t fmt_utf8(const char *fmt...); |
| 677 |
|
| 678 |
ssize_t vfmt_append_utf8(const char *fmt, va_list args); |
| 679 |
ssize_t vfmt_prepend_utf8(const char *fmt, va_list args); |
| 680 |
ssize_t vfmt_utf8(const char *fmt, va_list args); |
| 681 |
|
| 682 |
/** |
| 683 |
* Compute the hash code |
| 684 |
* @return hash code |
| 685 |
*/ |
| 686 |
size_t hash() const; |
| 687 |
|
| 688 |
/** |
| 689 |
* Convert line endings |
| 690 |
* @return true if the conversion has succeeded |
| 691 |
*/ |
| 692 |
bool to_dos(); |
| 693 |
bool to_unix(); |
| 694 |
}; |
| 695 |
|
| 696 |
// LLTL specialization for String class |
| 697 |
namespace lltl |
| 698 |
{ |
| 699 |
// Non-const specifications |
| 700 |
template <> |
| 701 |
struct hash_spec<LSPString>: public hash_iface |
| 702 |
{ |
| 703 |
static size_t hash_func(const void *ptr, size_t size); |
| 704 |
|
| 705 |
explicit hash_spec() |
| 706 |
{ |
| 707 |
hash = hash_func; |
| 708 |
} |
| 709 |
}; |
| 710 |
|
| 711 |
template <> |
| 712 |
struct compare_spec<LSPString>: public compare_iface |
| 713 |
{ |
| 714 |
static ssize_t cmp_func(const void *a, const void *b, size_t size); |
| 715 |
|
| 716 |
explicit compare_spec() |
| 717 |
{ |
| 718 |
compare = cmp_func; |
| 719 |
} |
| 720 |
}; |
| 721 |
|
| 722 |
template <> |
| 723 |
struct allocator_spec<LSPString>: public allocator_iface |
| 724 |
{ |
| 725 |
static void *clone_func(const void *src, size_t size); |
| 726 |
static void free_func(void *ptr); |
| 727 |
|
| 728 |
explicit allocator_spec() |
| 729 |
{ |
| 730 |
clone = clone_func; |
| 731 |
free = free_func; |
| 732 |
} |
| 733 |
}; |
| 734 |
|
| 735 |
// Const specifications |
| 736 |
template <> |
| 737 |
struct hash_spec<const LSPString>: public hash_iface |
| 738 |
{ |
| 739 |
static size_t hash_func(const void *ptr, size_t size); |
| 740 |
|
| 741 |
explicit hash_spec() |
| 742 |
{ |
| 743 |
hash = hash_func; |
| 744 |
} |
| 745 |
}; |
| 746 |
|
| 747 |
template <> |
| 748 |
struct compare_spec<const LSPString>: public compare_iface |
| 749 |
{ |
| 750 |
static ssize_t cmp_func(const void *a, const void *b, size_t size); |
| 751 |
|
| 752 |
explicit compare_spec() |
| 753 |
{ |
| 754 |
compare = cmp_func; |
| 755 |
} |
| 756 |
}; |
| 757 |
|
| 758 |
template <> |
| 759 |
struct allocator_spec<const LSPString>: public allocator_iface |
| 760 |
{ |
| 761 |
static void *clone_func(const void *src, size_t size); |
| 762 |
static void free_func(void *ptr); |
| 763 |
|
| 764 |
explicit allocator_spec() |
| 765 |
{ |
| 766 |
clone = clone_func; |
| 767 |
free = free_func; |
| 768 |
} |
| 769 |
}; |
| 770 |
} /* namespace lltl */ |
| 771 |
|
| 772 |
} /* namespace lsp */ |
| 773 |
|
| 774 |
#endif /* LSP_PLUG_IN_RUNTIME_LSPSTRING_H_ */ |