DISTRHO Plugin Framework
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.

935 lines
24KB

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