Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaString.hpp 24KB

11 years ago
11 years ago
11 years ago
8 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Carla String
  3. * Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_STRING_HPP_INCLUDED
  18. #define CARLA_STRING_HPP_INCLUDED
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaScopeUtils.hpp"
  21. #include <algorithm>
  22. // -----------------------------------------------------------------------
  23. // CarlaString class
  24. class CarlaString
  25. {
  26. public:
  27. // -------------------------------------------------------------------
  28. // constructors (no explicit conversions allowed)
  29. /*
  30. * Empty string.
  31. */
  32. explicit CarlaString() noexcept
  33. : fBuffer(_null()),
  34. fBufferLen(0),
  35. fBufferAlloc(false) {}
  36. /*
  37. * Simple character.
  38. */
  39. explicit CarlaString(const char c) noexcept
  40. : fBuffer(_null()),
  41. fBufferLen(0),
  42. fBufferAlloc(false)
  43. {
  44. char ch[2];
  45. ch[0] = c;
  46. ch[1] = '\0';
  47. _dup(ch);
  48. }
  49. /*
  50. * Simple char string.
  51. */
  52. explicit CarlaString(char* const strBuf, const bool reallocData = true) noexcept
  53. : fBuffer(_null()),
  54. fBufferLen(0),
  55. fBufferAlloc(false)
  56. {
  57. if (reallocData || strBuf == nullptr)
  58. {
  59. _dup(strBuf);
  60. }
  61. else
  62. {
  63. fBuffer = strBuf;
  64. fBufferLen = std::strlen(strBuf);
  65. fBufferAlloc = true;
  66. }
  67. }
  68. /*
  69. * Simple const char string.
  70. */
  71. explicit CarlaString(const char* const strBuf) noexcept
  72. : fBuffer(_null()),
  73. fBufferLen(0),
  74. fBufferAlloc(false)
  75. {
  76. _dup(strBuf);
  77. }
  78. /*
  79. * Integer.
  80. */
  81. explicit CarlaString(const int value) noexcept
  82. : fBuffer(_null()),
  83. fBufferLen(0),
  84. fBufferAlloc(false)
  85. {
  86. char strBuf[0xff+1];
  87. std::snprintf(strBuf, 0xff, "%d", value);
  88. strBuf[0xff] = '\0';
  89. _dup(strBuf);
  90. }
  91. /*
  92. * Unsigned integer, possibly in hexadecimal.
  93. */
  94. explicit CarlaString(const unsigned int value, const bool hexadecimal = false) noexcept
  95. : fBuffer(_null()),
  96. fBufferLen(0),
  97. fBufferAlloc(false)
  98. {
  99. char strBuf[0xff+1];
  100. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%x" : "%u", value);
  101. strBuf[0xff] = '\0';
  102. _dup(strBuf);
  103. }
  104. /*
  105. * Long integer.
  106. */
  107. explicit CarlaString(const long value) noexcept
  108. : fBuffer(_null()),
  109. fBufferLen(0),
  110. fBufferAlloc(false)
  111. {
  112. char strBuf[0xff+1];
  113. std::snprintf(strBuf, 0xff, "%ld", value);
  114. strBuf[0xff] = '\0';
  115. _dup(strBuf);
  116. }
  117. /*
  118. * Long unsigned integer, possibly hexadecimal.
  119. */
  120. explicit CarlaString(const unsigned long value, const bool hexadecimal = false) noexcept
  121. : fBuffer(_null()),
  122. fBufferLen(0),
  123. fBufferAlloc(false)
  124. {
  125. char strBuf[0xff+1];
  126. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%lx" : "%lu", value);
  127. strBuf[0xff] = '\0';
  128. _dup(strBuf);
  129. }
  130. /*
  131. * Long long integer.
  132. */
  133. explicit CarlaString(const long long value) noexcept
  134. : fBuffer(_null()),
  135. fBufferLen(0),
  136. fBufferAlloc(false)
  137. {
  138. char strBuf[0xff+1];
  139. std::snprintf(strBuf, 0xff, "%lld", value);
  140. strBuf[0xff] = '\0';
  141. _dup(strBuf);
  142. }
  143. /*
  144. * Long long unsigned integer, possibly hexadecimal.
  145. */
  146. explicit CarlaString(const unsigned long long value, const bool hexadecimal = false) noexcept
  147. : fBuffer(_null()),
  148. fBufferLen(0),
  149. fBufferAlloc(false)
  150. {
  151. char strBuf[0xff+1];
  152. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%llx" : "%llu", value);
  153. strBuf[0xff] = '\0';
  154. _dup(strBuf);
  155. }
  156. /*
  157. * Single-precision floating point number.
  158. */
  159. explicit CarlaString(const float value) noexcept
  160. : fBuffer(_null()),
  161. fBufferLen(0),
  162. fBufferAlloc(false)
  163. {
  164. char strBuf[0xff+1];
  165. {
  166. const CarlaScopedLocale csl;
  167. std::snprintf(strBuf, 0xff, "%.12g", static_cast<double>(value));
  168. }
  169. strBuf[0xff] = '\0';
  170. _dup(strBuf);
  171. }
  172. /*
  173. * Double-precision floating point number.
  174. */
  175. explicit CarlaString(const double value) noexcept
  176. : fBuffer(_null()),
  177. fBufferLen(0),
  178. fBufferAlloc(false)
  179. {
  180. char strBuf[0xff+1];
  181. {
  182. const CarlaScopedLocale csl;
  183. std::snprintf(strBuf, 0xff, "%.24g", value);
  184. }
  185. strBuf[0xff] = '\0';
  186. _dup(strBuf);
  187. }
  188. // -------------------------------------------------------------------
  189. // non-explicit constructor
  190. /*
  191. * Create string from another string.
  192. */
  193. CarlaString(const CarlaString& str) noexcept
  194. : fBuffer(_null()),
  195. fBufferLen(0),
  196. fBufferAlloc(false)
  197. {
  198. _dup(str.fBuffer);
  199. }
  200. // -------------------------------------------------------------------
  201. // destructor
  202. /*
  203. * Destructor.
  204. */
  205. ~CarlaString() noexcept
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  208. if (fBufferAlloc)
  209. std::free(fBuffer);
  210. fBuffer = nullptr;
  211. fBufferLen = 0;
  212. fBufferAlloc = false;
  213. }
  214. // -------------------------------------------------------------------
  215. // public methods
  216. /*
  217. * Get length of the string.
  218. */
  219. std::size_t length() const noexcept
  220. {
  221. return fBufferLen;
  222. }
  223. /*
  224. * Check if the string is empty.
  225. */
  226. bool isEmpty() const noexcept
  227. {
  228. return (fBufferLen == 0);
  229. }
  230. /*
  231. * Check if the string is not empty.
  232. */
  233. bool isNotEmpty() const noexcept
  234. {
  235. return (fBufferLen != 0);
  236. }
  237. /*
  238. * Check if the string contains a specific character, case-sensitive.
  239. */
  240. bool contains(const char c) const noexcept
  241. {
  242. for (std::size_t i=0; i<fBufferLen; ++i)
  243. {
  244. if (fBuffer[i] == c)
  245. return true;
  246. }
  247. return false;
  248. }
  249. /*
  250. * Check if the string contains another string, optionally ignoring case.
  251. */
  252. bool contains(const char* const strBuf, const bool ignoreCase = false) const noexcept
  253. {
  254. CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr, false);
  255. if (ignoreCase)
  256. {
  257. #ifdef __USE_GNU
  258. return (strcasestr(fBuffer, strBuf) != nullptr);
  259. #else
  260. CarlaString tmp1(fBuffer), tmp2(strBuf);
  261. // memory allocation failed or empty string(s)
  262. if (tmp1.fBuffer == _null() || tmp2.fBuffer == _null())
  263. return false;
  264. tmp1.toLower();
  265. tmp2.toLower();
  266. return (std::strstr(tmp1, tmp2) != nullptr);
  267. #endif
  268. }
  269. return (std::strstr(fBuffer, strBuf) != nullptr);
  270. }
  271. /*
  272. * Check if character at 'pos' is a digit.
  273. */
  274. bool isDigit(const std::size_t pos) const noexcept
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(pos < fBufferLen, false);
  277. return (fBuffer[pos] >= '0' && fBuffer[pos] <= '9');
  278. }
  279. /*
  280. * Check if the string starts with the character 'c'.
  281. */
  282. bool startsWith(const char c) const noexcept
  283. {
  284. CARLA_SAFE_ASSERT_RETURN(c != '\0', false);
  285. return (fBufferLen > 0 && fBuffer[0] == c);
  286. }
  287. /*
  288. * Check if the string starts with the string 'prefix'.
  289. */
  290. bool startsWith(const char* const prefix) const noexcept
  291. {
  292. CARLA_SAFE_ASSERT_RETURN(prefix != nullptr, false);
  293. const std::size_t prefixLen(std::strlen(prefix));
  294. if (fBufferLen < prefixLen)
  295. return false;
  296. return (std::strncmp(fBuffer, prefix, prefixLen) == 0);
  297. }
  298. /*
  299. * Check if the string ends with the character 'c'.
  300. */
  301. bool endsWith(const char c) const noexcept
  302. {
  303. CARLA_SAFE_ASSERT_RETURN(c != '\0', false);
  304. return (fBufferLen > 0 && fBuffer[fBufferLen-1] == c);
  305. }
  306. /*
  307. * Check if the string ends with the string 'suffix'.
  308. */
  309. bool endsWith(const char* const suffix) const noexcept
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(suffix != nullptr, false);
  312. const std::size_t suffixLen(std::strlen(suffix));
  313. if (fBufferLen < suffixLen)
  314. return false;
  315. return (std::strncmp(fBuffer + (fBufferLen-suffixLen), suffix, suffixLen) == 0);
  316. }
  317. /*
  318. * Find the first occurrence of character 'c' in the string.
  319. * Returns "length()" if the character is not found.
  320. */
  321. std::size_t find(const char c, bool* const found = nullptr) const noexcept
  322. {
  323. if (fBufferLen == 0 || c == '\0')
  324. {
  325. if (found != nullptr)
  326. *found = false;
  327. return fBufferLen;
  328. }
  329. for (std::size_t i=0; i < fBufferLen; ++i)
  330. {
  331. if (fBuffer[i] == c)
  332. {
  333. if (found != nullptr)
  334. *found = true;
  335. return i;
  336. }
  337. }
  338. if (found != nullptr)
  339. *found = false;
  340. return fBufferLen;
  341. }
  342. /*
  343. * Find the first occurrence of string 'strBuf' in the string.
  344. * Returns "length()" if the string is not found.
  345. */
  346. std::size_t find(const char* const strBuf, bool* const found = nullptr) const noexcept
  347. {
  348. if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0')
  349. {
  350. if (found != nullptr)
  351. *found = false;
  352. return fBufferLen;
  353. }
  354. if (char* const subStrBuf = std::strstr(fBuffer, strBuf))
  355. {
  356. const ssize_t ret(subStrBuf - fBuffer);
  357. if (ret < 0)
  358. {
  359. // should never happen!
  360. carla_safe_assert_int("ret >= 0", __FILE__, __LINE__, int(ret));
  361. if (found != nullptr)
  362. *found = false;
  363. return fBufferLen;
  364. }
  365. if (found != nullptr)
  366. *found = true;
  367. return static_cast<std::size_t>(ret);
  368. }
  369. if (found != nullptr)
  370. *found = false;
  371. return fBufferLen;
  372. }
  373. /*
  374. * Find the last occurrence of character 'c' in the string.
  375. * Returns "length()" if the character is not found.
  376. */
  377. std::size_t rfind(const char c, bool* const found = nullptr) const noexcept
  378. {
  379. if (fBufferLen == 0 || c == '\0')
  380. {
  381. if (found != nullptr)
  382. *found = false;
  383. return fBufferLen;
  384. }
  385. for (std::size_t i=fBufferLen; i > 0; --i)
  386. {
  387. if (fBuffer[i-1] == c)
  388. {
  389. if (found != nullptr)
  390. *found = true;
  391. return i-1;
  392. }
  393. }
  394. if (found != nullptr)
  395. *found = false;
  396. return fBufferLen;
  397. }
  398. /*
  399. * Find the last occurrence of string 'strBuf' in the string.
  400. * Returns "length()" if the string is not found.
  401. */
  402. std::size_t rfind(const char* const strBuf, bool* const found = nullptr) const noexcept
  403. {
  404. if (found != nullptr)
  405. *found = false;
  406. if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0')
  407. return fBufferLen;
  408. const std::size_t strBufLen(std::strlen(strBuf));
  409. std::size_t ret = fBufferLen;
  410. const char* tmpBuf = fBuffer;
  411. for (std::size_t i=0; i < fBufferLen; ++i)
  412. {
  413. if (std::strstr(tmpBuf+1, strBuf) == nullptr && std::strncmp(tmpBuf, strBuf, strBufLen) == 0)
  414. {
  415. if (found != nullptr)
  416. *found = true;
  417. break;
  418. }
  419. --ret;
  420. ++tmpBuf;
  421. }
  422. return fBufferLen-ret;
  423. }
  424. /*
  425. * Clear the string.
  426. */
  427. void clear() noexcept
  428. {
  429. truncate(0);
  430. }
  431. /*
  432. * Replace all occurrences of character 'before' with character 'after'.
  433. */
  434. CarlaString& replace(const char before, const char after) noexcept
  435. {
  436. CARLA_SAFE_ASSERT_RETURN(before != '\0' && after != '\0', *this);
  437. for (std::size_t i=0; i < fBufferLen; ++i)
  438. {
  439. if (fBuffer[i] == before)
  440. fBuffer[i] = after;
  441. }
  442. return *this;
  443. }
  444. /*
  445. * Truncate the string to size 'n'.
  446. */
  447. CarlaString& truncate(const std::size_t n) noexcept
  448. {
  449. if (n >= fBufferLen)
  450. return *this;
  451. fBuffer[n] = '\0';
  452. fBufferLen = n;
  453. return *this;
  454. }
  455. /*
  456. * Convert all non-basic characters to '_'.
  457. */
  458. CarlaString& toBasic() noexcept
  459. {
  460. for (std::size_t i=0; i < fBufferLen; ++i)
  461. {
  462. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  463. continue;
  464. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  465. continue;
  466. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  467. continue;
  468. if (fBuffer[i] == '_')
  469. continue;
  470. fBuffer[i] = '_';
  471. }
  472. return *this;
  473. }
  474. /*
  475. * Convert all ascii characters to lowercase.
  476. */
  477. CarlaString& toLower() noexcept
  478. {
  479. static const char kCharDiff('a' - 'A');
  480. for (std::size_t i=0; i < fBufferLen; ++i)
  481. {
  482. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  483. fBuffer[i] = static_cast<char>(fBuffer[i] + kCharDiff);
  484. }
  485. return *this;
  486. }
  487. /*
  488. * Convert all ascii characters to uppercase.
  489. */
  490. CarlaString& toUpper() noexcept
  491. {
  492. static const char kCharDiff('a' - 'A');
  493. for (std::size_t i=0; i < fBufferLen; ++i)
  494. {
  495. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  496. fBuffer[i] = static_cast<char>(fBuffer[i] - kCharDiff);
  497. }
  498. return *this;
  499. }
  500. /*
  501. * Direct access to the string buffer (read-only).
  502. */
  503. const char* buffer() const noexcept
  504. {
  505. return fBuffer;
  506. }
  507. /*
  508. * Return a duplicate string buffer.
  509. * May throw.
  510. */
  511. const char* dup() const
  512. {
  513. return carla_strdup(fBuffer);
  514. }
  515. /*
  516. * Return a duplicate string buffer or null.
  517. */
  518. const char* dupSafe() const noexcept
  519. {
  520. return carla_strdup_safe(fBuffer);
  521. }
  522. /*
  523. * Release the buffer pointer while clearing this string.
  524. * This allows to keep a pointer to the buffer after this object is deleted.
  525. */
  526. char* releaseBufferPointer() noexcept
  527. {
  528. char* ret = fBufferLen > 0 ? fBuffer : nullptr;
  529. fBuffer = _null();
  530. fBufferLen = 0;
  531. fBufferAlloc = false;
  532. return ret;
  533. }
  534. // -------------------------------------------------------------------
  535. // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  536. // Copyright (C) 2004-2008 René Nyffenegger
  537. static CarlaString asBase64(const void* const data, const std::size_t dataSize)
  538. {
  539. static const char* const kBase64Chars =
  540. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  541. "abcdefghijklmnopqrstuvwxyz"
  542. "0123456789+/";
  543. const std::size_t kTmpBufSize = std::min(carla_nextPowerOf2(static_cast<uint32_t>(dataSize/3)), 65536U);
  544. const uchar* bytesToEncode((const uchar*)data);
  545. uint i=0, j=0;
  546. uint charArray3[3], charArray4[4];
  547. char strBuf[kTmpBufSize+1];
  548. strBuf[kTmpBufSize] = '\0';
  549. std::size_t strBufIndex = 0;
  550. CarlaString ret;
  551. for (std::size_t s=0; s<dataSize; ++s)
  552. {
  553. charArray3[i++] = *(bytesToEncode++);
  554. if (i == 3)
  555. {
  556. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  557. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  558. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  559. charArray4[3] = charArray3[2] & 0x3f;
  560. for (i=0; i<4; ++i)
  561. strBuf[strBufIndex++] = kBase64Chars[charArray4[i]];
  562. if (strBufIndex >= kTmpBufSize-7)
  563. {
  564. strBuf[strBufIndex] = '\0';
  565. strBufIndex = 0;
  566. ret += strBuf;
  567. }
  568. i = 0;
  569. }
  570. }
  571. if (i != 0)
  572. {
  573. for (j=i; j<3; ++j)
  574. charArray3[j] = '\0';
  575. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  576. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  577. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  578. charArray4[3] = charArray3[2] & 0x3f;
  579. for (j=0; j<4 && i<3 && j<i+1; ++j)
  580. strBuf[strBufIndex++] = kBase64Chars[charArray4[j]];
  581. for (; i++ < 3;)
  582. strBuf[strBufIndex++] = '=';
  583. }
  584. if (strBufIndex != 0)
  585. {
  586. strBuf[strBufIndex] = '\0';
  587. ret += strBuf;
  588. }
  589. return ret;
  590. }
  591. // -------------------------------------------------------------------
  592. // public operators
  593. operator const char*() const noexcept
  594. {
  595. return fBuffer;
  596. }
  597. char operator[](const std::size_t pos) const noexcept
  598. {
  599. if (pos < fBufferLen)
  600. return fBuffer[pos];
  601. carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  602. static char fallback;
  603. fallback = '\0';
  604. return fallback;
  605. }
  606. char& operator[](const std::size_t pos) noexcept
  607. {
  608. if (pos < fBufferLen)
  609. return fBuffer[pos];
  610. carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  611. static char fallback;
  612. fallback = '\0';
  613. return fallback;
  614. }
  615. bool operator==(const char* const strBuf) const noexcept
  616. {
  617. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  618. }
  619. bool operator==(const CarlaString& str) const noexcept
  620. {
  621. return operator==(str.fBuffer);
  622. }
  623. bool operator!=(const char* const strBuf) const noexcept
  624. {
  625. return !operator==(strBuf);
  626. }
  627. bool operator!=(const CarlaString& str) const noexcept
  628. {
  629. return !operator==(str.fBuffer);
  630. }
  631. CarlaString& operator=(const char* const strBuf) noexcept
  632. {
  633. _dup(strBuf);
  634. return *this;
  635. }
  636. CarlaString& operator=(const CarlaString& str) noexcept
  637. {
  638. _dup(str.fBuffer);
  639. return *this;
  640. }
  641. CarlaString& operator+=(const char* const strBuf) noexcept
  642. {
  643. if (strBuf == nullptr || strBuf[0] == '\0')
  644. return *this;
  645. const std::size_t strBufLen = std::strlen(strBuf);
  646. // for empty strings, we can just take the appended string as our entire data
  647. if (isEmpty())
  648. {
  649. _dup(strBuf, strBufLen);
  650. return *this;
  651. }
  652. // we have some data ourselves, reallocate to add the new stuff
  653. char* const newBuf = (char*)realloc(fBuffer, fBufferLen + strBufLen + 1);
  654. CARLA_SAFE_ASSERT_RETURN(newBuf != nullptr, *this);
  655. std::memcpy(newBuf + fBufferLen, strBuf, strBufLen + 1);
  656. fBuffer = newBuf;
  657. fBufferLen += strBufLen;
  658. return *this;
  659. }
  660. CarlaString& operator+=(const CarlaString& str) noexcept
  661. {
  662. return operator+=(str.fBuffer);
  663. }
  664. CarlaString operator+(const char* const strBuf) noexcept
  665. {
  666. if (strBuf == nullptr || strBuf[0] == '\0')
  667. return *this;
  668. if (isEmpty())
  669. return CarlaString(strBuf);
  670. const std::size_t strBufLen = std::strlen(strBuf);
  671. const std::size_t newBufSize = fBufferLen + strBufLen;
  672. char* const newBuf = (char*)std::malloc(newBufSize + 1);
  673. CARLA_SAFE_ASSERT_RETURN(newBuf != nullptr, CarlaString());
  674. std::memcpy(newBuf, fBuffer, fBufferLen);
  675. std::memcpy(newBuf + fBufferLen, strBuf, strBufLen + 1);
  676. return CarlaString(newBuf, false);
  677. }
  678. CarlaString operator+(const CarlaString& str) noexcept
  679. {
  680. return operator+(str.fBuffer);
  681. }
  682. // needed for std::map compatibility
  683. bool operator<(const CarlaString& str) const noexcept
  684. {
  685. return std::strcmp(fBuffer, str.fBuffer) < 0;
  686. }
  687. // -------------------------------------------------------------------
  688. private:
  689. char* fBuffer; // the actual string buffer
  690. std::size_t fBufferLen; // string length
  691. bool fBufferAlloc; // wherever the buffer is allocated, not using _null()
  692. /*
  693. * Static null string.
  694. * Prevents allocation for new and/or empty strings.
  695. */
  696. static char* _null() noexcept
  697. {
  698. static char sNull = '\0';
  699. return &sNull;
  700. }
  701. /*
  702. * Helper function.
  703. * Called whenever the string needs to be allocated.
  704. *
  705. * Notes:
  706. * - Allocates string only if 'strBuf' is not null and new string contents are different
  707. * - If 'strBuf' is null, 'size' must be 0
  708. */
  709. void _dup(const char* const strBuf, const std::size_t size = 0) noexcept
  710. {
  711. if (strBuf != nullptr)
  712. {
  713. // don't recreate string if contents match
  714. if (std::strcmp(fBuffer, strBuf) == 0)
  715. return;
  716. if (fBufferAlloc)
  717. std::free(fBuffer);
  718. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  719. fBuffer = (char*)std::malloc(fBufferLen+1);
  720. if (fBuffer == nullptr)
  721. {
  722. fBuffer = _null();
  723. fBufferLen = 0;
  724. fBufferAlloc = false;
  725. return;
  726. }
  727. fBufferAlloc = true;
  728. std::strcpy(fBuffer, strBuf);
  729. fBuffer[fBufferLen] = '\0';
  730. }
  731. else
  732. {
  733. CARLA_SAFE_ASSERT_UINT(size == 0, static_cast<uint>(size));
  734. // don't recreate null string
  735. if (! fBufferAlloc)
  736. return;
  737. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  738. std::free(fBuffer);
  739. fBuffer = _null();
  740. fBufferLen = 0;
  741. fBufferAlloc = false;
  742. }
  743. }
  744. CARLA_PREVENT_HEAP_ALLOCATION
  745. };
  746. // -----------------------------------------------------------------------
  747. static inline
  748. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter) noexcept
  749. {
  750. if (strBufAfter == nullptr || strBufAfter[0] == '\0')
  751. return strBefore;
  752. if (strBefore.isEmpty())
  753. return CarlaString(strBufAfter);
  754. const std::size_t strBeforeLen = strBefore.length();
  755. const std::size_t strBufAfterLen = std::strlen(strBufAfter);
  756. const std::size_t newBufSize = strBeforeLen + strBufAfterLen;
  757. char* const newBuf = (char*)std::malloc(newBufSize + 1);
  758. CARLA_SAFE_ASSERT_RETURN(newBuf != nullptr, CarlaString());
  759. std::memcpy(newBuf, strBefore.buffer(), strBeforeLen);
  760. std::memcpy(newBuf + strBeforeLen, strBufAfter, strBufAfterLen + 1);
  761. return CarlaString(newBuf, false);
  762. }
  763. static inline
  764. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter) noexcept
  765. {
  766. if (strAfter.isEmpty())
  767. return CarlaString(strBufBefore);
  768. if (strBufBefore == nullptr || strBufBefore[0] == '\0')
  769. return strAfter;
  770. const std::size_t strBufBeforeLen = std::strlen(strBufBefore);
  771. const std::size_t strAfterLen = strAfter.length();
  772. const std::size_t newBufSize = strBufBeforeLen + strAfterLen;
  773. char* const newBuf = (char*)std::malloc(newBufSize + 1);
  774. CARLA_SAFE_ASSERT_RETURN(newBuf != nullptr, CarlaString());
  775. std::memcpy(newBuf, strBufBefore, strBufBeforeLen);
  776. std::memcpy(newBuf + strBufBeforeLen, strAfter.buffer(), strAfterLen + 1);
  777. return CarlaString(newBuf, false);
  778. }
  779. // -----------------------------------------------------------------------
  780. #endif // CARLA_STRING_HPP_INCLUDED