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 21KB

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