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.

1183 lines
30KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 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 "../extra/ScopedSafeLocale.hpp"
  20. #include <algorithm>
  21. START_NAMESPACE_DISTRHO
  22. // -----------------------------------------------------------------------
  23. // String class
  24. class String
  25. {
  26. public:
  27. // -------------------------------------------------------------------
  28. // constructors (no explicit conversions allowed)
  29. /*
  30. * Empty string.
  31. */
  32. explicit String() noexcept
  33. : fBuffer(_null()),
  34. fBufferLen(0),
  35. fBufferAlloc(false) {}
  36. /*
  37. * Simple character.
  38. */
  39. explicit String(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 String(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 String(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 String(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 String(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 String(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 String(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 String(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 String(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 String(const float value) noexcept
  160. : fBuffer(_null()),
  161. fBufferLen(0),
  162. fBufferAlloc(false)
  163. {
  164. char strBuf[0xff+1];
  165. {
  166. const ScopedSafeLocale ssl;
  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. const ScopedSafeLocale ssl;
  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. String(const String& 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. ~String() noexcept
  206. {
  207. DISTRHO_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. DISTRHO_SAFE_ASSERT_RETURN(strBuf != nullptr, false);
  255. if (ignoreCase)
  256. {
  257. #ifdef __USE_GNU
  258. return (strcasestr(fBuffer, strBuf) != nullptr);
  259. #else
  260. String 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. DISTRHO_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. DISTRHO_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. DISTRHO_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. DISTRHO_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. DISTRHO_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. d_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. String& replace(const char before, const char after) noexcept
  435. {
  436. DISTRHO_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. * Remove all occurrences of character 'c', shifting and truncating the string as necessary.
  446. */
  447. String& remove(const char c) noexcept
  448. {
  449. DISTRHO_SAFE_ASSERT_RETURN(c != '\0', *this);
  450. if (fBufferLen == 0)
  451. return *this;
  452. for (std::size_t i=0; i < fBufferLen; ++i)
  453. {
  454. if (fBuffer[i] == c)
  455. {
  456. --fBufferLen;
  457. std::memmove(fBuffer+i, fBuffer+i+1, fBufferLen-i);
  458. }
  459. }
  460. fBuffer[fBufferLen] = '\0';
  461. return *this;
  462. }
  463. /*
  464. * Truncate the string to size 'n'.
  465. */
  466. String& truncate(const std::size_t n) noexcept
  467. {
  468. if (n >= fBufferLen)
  469. return *this;
  470. fBuffer[n] = '\0';
  471. fBufferLen = n;
  472. return *this;
  473. }
  474. /*
  475. * Convert all non-basic characters to '_'.
  476. */
  477. String& toBasic() noexcept
  478. {
  479. for (std::size_t i=0; i < fBufferLen; ++i)
  480. {
  481. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  482. continue;
  483. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  484. continue;
  485. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  486. continue;
  487. if (fBuffer[i] == '_')
  488. continue;
  489. fBuffer[i] = '_';
  490. }
  491. return *this;
  492. }
  493. /*
  494. * Convert all ascii characters to lowercase.
  495. */
  496. String& toLower() noexcept
  497. {
  498. static constexpr const char kCharDiff = 'a' - 'A';
  499. for (std::size_t i=0; i < fBufferLen; ++i)
  500. {
  501. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  502. fBuffer[i] = static_cast<char>(fBuffer[i] + kCharDiff);
  503. }
  504. return *this;
  505. }
  506. /*
  507. * Convert all ascii characters to uppercase.
  508. */
  509. String& toUpper() noexcept
  510. {
  511. static constexpr const char kCharDiff = 'a' - 'A';
  512. for (std::size_t i=0; i < fBufferLen; ++i)
  513. {
  514. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  515. fBuffer[i] = static_cast<char>(fBuffer[i] - kCharDiff);
  516. }
  517. return *this;
  518. }
  519. /*
  520. * Create a new string where all non-basic characters are converted to '_'.
  521. * @see toBasic()
  522. */
  523. String asBasic() const noexcept
  524. {
  525. String s(*this);
  526. return s.toBasic();
  527. }
  528. /*
  529. * Create a new string where all ascii characters are converted lowercase.
  530. * @see toLower()
  531. */
  532. String asLower() const noexcept
  533. {
  534. String s(*this);
  535. return s.toLower();
  536. }
  537. /*
  538. * Create a new string where all ascii characters are converted to uppercase.
  539. * @see toUpper()
  540. */
  541. String asUpper() const noexcept
  542. {
  543. String s(*this);
  544. return s.toUpper();
  545. }
  546. /*
  547. * Direct access to the string buffer (read-only).
  548. */
  549. const char* buffer() const noexcept
  550. {
  551. return fBuffer;
  552. }
  553. /*
  554. * Get and release the string buffer, while also clearing this string.
  555. * This allows to keep a pointer to the buffer after this object is deleted.
  556. * Result must be freed.
  557. */
  558. char* getAndReleaseBuffer() noexcept
  559. {
  560. char* ret = fBufferLen > 0 ? fBuffer : nullptr;
  561. fBuffer = _null();
  562. fBufferLen = 0;
  563. fBufferAlloc = false;
  564. return ret;
  565. }
  566. // -------------------------------------------------------------------
  567. // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  568. // Copyright (C) 2004-2008 René Nyffenegger
  569. static String asBase64(const void* const data, const std::size_t dataSize)
  570. {
  571. static constexpr const char* const kBase64Chars =
  572. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  573. "abcdefghijklmnopqrstuvwxyz"
  574. "0123456789+/";
  575. #ifndef _MSC_VER
  576. const std::size_t kTmpBufSize = std::min(d_nextPowerOf2(static_cast<uint32_t>(dataSize/3)), 65536U);
  577. #else
  578. constexpr std::size_t kTmpBufSize = 65536U;
  579. #endif
  580. const uchar* bytesToEncode((const uchar*)data);
  581. uint i=0, j=0;
  582. uint charArray3[3], charArray4[4];
  583. char strBuf[kTmpBufSize + 1];
  584. strBuf[kTmpBufSize] = '\0';
  585. std::size_t strBufIndex = 0;
  586. String ret;
  587. for (std::size_t s=0; s<dataSize; ++s)
  588. {
  589. charArray3[i++] = *(bytesToEncode++);
  590. if (i == 3)
  591. {
  592. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  593. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  594. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  595. charArray4[3] = charArray3[2] & 0x3f;
  596. for (i=0; i<4; ++i)
  597. strBuf[strBufIndex++] = kBase64Chars[charArray4[i]];
  598. if (strBufIndex >= kTmpBufSize-7)
  599. {
  600. strBuf[strBufIndex] = '\0';
  601. strBufIndex = 0;
  602. ret += strBuf;
  603. }
  604. i = 0;
  605. }
  606. }
  607. if (i != 0)
  608. {
  609. for (j=i; j<3; ++j)
  610. charArray3[j] = '\0';
  611. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  612. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  613. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  614. charArray4[3] = charArray3[2] & 0x3f;
  615. for (j=0; j<4 && i<3 && j<i+1; ++j)
  616. strBuf[strBufIndex++] = kBase64Chars[charArray4[j]];
  617. for (; i++ < 3;)
  618. strBuf[strBufIndex++] = '=';
  619. }
  620. if (strBufIndex != 0)
  621. {
  622. strBuf[strBufIndex] = '\0';
  623. ret += strBuf;
  624. }
  625. return ret;
  626. }
  627. /*
  628. * Convert to a URL encoded string.
  629. */
  630. String& urlEncode() noexcept
  631. {
  632. static constexpr const char* const kHexChars = "0123456789ABCDEF";
  633. if (fBufferLen == 0)
  634. return *this;
  635. char* const newbuf = static_cast<char*>(std::malloc(fBufferLen * 3 + 1));
  636. DISTRHO_SAFE_ASSERT_RETURN(newbuf != nullptr, *this);
  637. char* newbufptr = newbuf;
  638. for (std::size_t i=0; i < fBufferLen; ++i)
  639. {
  640. const char c = fBuffer[i];
  641. switch (c)
  642. {
  643. case '!': // 33
  644. case '#': // 35
  645. case '$': // 36
  646. case '&': // 38
  647. case '\'': // 39
  648. case '(': // 40
  649. case ')': // 41
  650. case '*': // 42
  651. case '+': // 43
  652. case ',': // 44
  653. case '-': // 45
  654. case '.': // 46
  655. case '/': // 47
  656. case '0': // 48
  657. case '1': // 49
  658. case '2': // 50
  659. case '3': // 51
  660. case '4': // 52
  661. case '5': // 53
  662. case '6': // 54
  663. case '7': // 55
  664. case '8': // 56
  665. case '9': // 57
  666. case ':': // 58
  667. case ';': // 59
  668. case '=': // 61
  669. case '?': // 63
  670. case '@': // 64
  671. case 'A': // 65
  672. case 'B': // 66
  673. case 'C': // 67
  674. case 'D': // 68
  675. case 'E': // 69
  676. case 'F': // 70
  677. case 'G': // 71
  678. case 'H': // 72
  679. case 'I': // 73
  680. case 'J': // 74
  681. case 'K': // 75
  682. case 'L': // 76
  683. case 'M': // 77
  684. case 'N': // 78
  685. case 'O': // 79
  686. case 'P': // 80
  687. case 'Q': // 81
  688. case 'R': // 82
  689. case 'S': // 83
  690. case 'T': // 84
  691. case 'U': // 85
  692. case 'V': // 86
  693. case 'W': // 87
  694. case 'X': // 88
  695. case 'Y': // 89
  696. case 'Z': // 90
  697. case '[': // 91
  698. case ']': // 93
  699. case '_': // 95
  700. case 'a': // 97
  701. case 'b': // 98
  702. case 'c': // 99
  703. case 'd': // 100
  704. case 'e': // 101
  705. case 'f': // 102
  706. case 'g': // 103
  707. case 'h': // 104
  708. case 'i': // 105
  709. case 'j': // 106
  710. case 'k': // 107
  711. case 'l': // 108
  712. case 'm': // 109
  713. case 'n': // 110
  714. case 'o': // 111
  715. case 'p': // 112
  716. case 'q': // 113
  717. case 'r': // 114
  718. case 's': // 115
  719. case 't': // 116
  720. case 'u': // 117
  721. case 'v': // 118
  722. case 'w': // 119
  723. case 'x': // 120
  724. case 'y': // 121
  725. case 'z': // 122
  726. case '~': // 126
  727. *newbufptr++ = c;
  728. break;
  729. default:
  730. *newbufptr++ = '%';
  731. *newbufptr++ = kHexChars[(c >> 4) & 0xf];
  732. *newbufptr++ = kHexChars[c & 0xf];
  733. break;
  734. }
  735. }
  736. *newbufptr = '\0';
  737. std::free(fBuffer);
  738. fBuffer = newbuf;
  739. fBufferLen = std::strlen(newbuf);
  740. fBufferAlloc = true;
  741. return *this;
  742. }
  743. /*
  744. * Convert to a URL decoded string.
  745. */
  746. String& urlDecode() noexcept
  747. {
  748. if (fBufferLen == 0)
  749. return *this;
  750. char* const newbuf = static_cast<char*>(std::malloc(fBufferLen + 1));
  751. DISTRHO_SAFE_ASSERT_RETURN(newbuf != nullptr, *this);
  752. char* newbufptr = newbuf;
  753. for (std::size_t i=0; i < fBufferLen; ++i)
  754. {
  755. const char c = fBuffer[i];
  756. if (c == '%')
  757. {
  758. DISTRHO_SAFE_ASSERT_CONTINUE(fBufferLen > i + 2);
  759. char c1 = fBuffer[i + 1];
  760. char c2 = fBuffer[i + 2];
  761. i += 2;
  762. /**/ if (c1 >= '0' && c1 <= '9')
  763. c1 -= '0';
  764. else if (c1 >= 'A' && c1 <= 'Z')
  765. c1 -= 'A' - 10;
  766. else if (c1 >= 'a' && c1 <= 'z')
  767. c1 -= 'a' - 10;
  768. else
  769. continue;
  770. /**/ if (c2 >= '0' && c2 <= '9')
  771. c2 -= '0';
  772. else if (c2 >= 'A' && c2 <= 'Z')
  773. c2 -= 'A' - 10;
  774. else if (c2 >= 'a' && c2 <= 'z')
  775. c2 -= 'a' - 10;
  776. else
  777. continue;
  778. *newbufptr++ = c1 << 4 | c2;
  779. }
  780. else
  781. {
  782. *newbufptr++ = c;
  783. }
  784. }
  785. *newbufptr = '\0';
  786. std::free(fBuffer);
  787. fBuffer = newbuf;
  788. fBufferLen = std::strlen(newbuf);
  789. fBufferAlloc = true;
  790. return *this;
  791. }
  792. // -------------------------------------------------------------------
  793. // public operators
  794. operator const char*() const noexcept
  795. {
  796. return fBuffer;
  797. }
  798. char operator[](const std::size_t pos) const noexcept
  799. {
  800. if (pos < fBufferLen)
  801. return fBuffer[pos];
  802. d_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  803. static char fallback;
  804. fallback = '\0';
  805. return fallback;
  806. }
  807. char& operator[](const std::size_t pos) noexcept
  808. {
  809. if (pos < fBufferLen)
  810. return fBuffer[pos];
  811. d_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  812. static char fallback;
  813. fallback = '\0';
  814. return fallback;
  815. }
  816. bool operator==(const char* const strBuf) const noexcept
  817. {
  818. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  819. }
  820. bool operator==(const String& str) const noexcept
  821. {
  822. return operator==(str.fBuffer);
  823. }
  824. bool operator!=(const char* const strBuf) const noexcept
  825. {
  826. return !operator==(strBuf);
  827. }
  828. bool operator!=(const String& str) const noexcept
  829. {
  830. return !operator==(str.fBuffer);
  831. }
  832. String& operator=(const char* const strBuf) noexcept
  833. {
  834. _dup(strBuf);
  835. return *this;
  836. }
  837. String& operator=(const String& str) noexcept
  838. {
  839. _dup(str.fBuffer);
  840. return *this;
  841. }
  842. String& operator+=(const char* const strBuf) noexcept
  843. {
  844. if (strBuf == nullptr || strBuf[0] == '\0')
  845. return *this;
  846. const std::size_t strBufLen = std::strlen(strBuf);
  847. // for empty strings, we can just take the appended string as our entire data
  848. if (isEmpty())
  849. {
  850. _dup(strBuf, strBufLen);
  851. return *this;
  852. }
  853. // we have some data ourselves, reallocate to add the new stuff
  854. char* const newBuf = static_cast<char*>(std::realloc(fBuffer, fBufferLen + strBufLen + 1));
  855. DISTRHO_SAFE_ASSERT_RETURN(newBuf != nullptr, *this);
  856. std::memcpy(newBuf + fBufferLen, strBuf, strBufLen + 1);
  857. fBuffer = newBuf;
  858. fBufferLen += strBufLen;
  859. return *this;
  860. }
  861. String& operator+=(const String& str) noexcept
  862. {
  863. return operator+=(str.fBuffer);
  864. }
  865. String operator+(const char* const strBuf) noexcept
  866. {
  867. if (strBuf == nullptr || strBuf[0] == '\0')
  868. return *this;
  869. if (isEmpty())
  870. return String(strBuf);
  871. const std::size_t strBufLen = std::strlen(strBuf);
  872. const std::size_t newBufSize = fBufferLen + strBufLen;
  873. char* const newBuf = static_cast<char*>(std::malloc(newBufSize + 1));
  874. DISTRHO_SAFE_ASSERT_RETURN(newBuf != nullptr, String());
  875. std::memcpy(newBuf, fBuffer, fBufferLen);
  876. std::memcpy(newBuf + fBufferLen, strBuf, strBufLen + 1);
  877. return String(newBuf, false);
  878. }
  879. String operator+(const String& str) noexcept
  880. {
  881. return operator+(str.fBuffer);
  882. }
  883. // needed for std::map compatibility
  884. bool operator<(const String& str) const noexcept
  885. {
  886. return std::strcmp(fBuffer, str.fBuffer) < 0;
  887. }
  888. // -------------------------------------------------------------------
  889. private:
  890. char* fBuffer; // the actual string buffer
  891. std::size_t fBufferLen; // string length
  892. bool fBufferAlloc; // wherever the buffer is allocated, not using _null()
  893. /*
  894. * Static null string.
  895. * Prevents allocation for new and/or empty strings.
  896. */
  897. static char* _null() noexcept
  898. {
  899. static char sNull = '\0';
  900. return &sNull;
  901. }
  902. /*
  903. * Helper function.
  904. * Called whenever the string needs to be allocated.
  905. *
  906. * Notes:
  907. * - Allocates string only if 'strBuf' is not null and new string contents are different
  908. * - If 'strBuf' is null, 'size' must be 0
  909. */
  910. void _dup(const char* const strBuf, const std::size_t size = 0) noexcept
  911. {
  912. if (strBuf != nullptr)
  913. {
  914. // don't recreate string if contents match
  915. if (std::strcmp(fBuffer, strBuf) == 0)
  916. return;
  917. if (fBufferAlloc)
  918. std::free(fBuffer);
  919. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  920. fBuffer = static_cast<char*>(std::malloc(fBufferLen + 1));
  921. if (fBuffer == nullptr)
  922. {
  923. fBuffer = _null();
  924. fBufferLen = 0;
  925. fBufferAlloc = false;
  926. return;
  927. }
  928. fBufferAlloc = true;
  929. std::strcpy(fBuffer, strBuf);
  930. fBuffer[fBufferLen] = '\0';
  931. }
  932. else
  933. {
  934. DISTRHO_SAFE_ASSERT_UINT(size == 0, static_cast<uint>(size));
  935. // don't recreate null string
  936. if (! fBufferAlloc)
  937. return;
  938. DISTRHO_SAFE_ASSERT(fBuffer != nullptr);
  939. std::free(fBuffer);
  940. fBuffer = _null();
  941. fBufferLen = 0;
  942. fBufferAlloc = false;
  943. }
  944. }
  945. DISTRHO_PREVENT_HEAP_ALLOCATION
  946. };
  947. // -----------------------------------------------------------------------
  948. static inline
  949. String operator+(const String& strBefore, const char* const strBufAfter) noexcept
  950. {
  951. if (strBufAfter == nullptr || strBufAfter[0] == '\0')
  952. return strBefore;
  953. if (strBefore.isEmpty())
  954. return String(strBufAfter);
  955. const std::size_t strBeforeLen = strBefore.length();
  956. const std::size_t strBufAfterLen = std::strlen(strBufAfter);
  957. const std::size_t newBufSize = strBeforeLen + strBufAfterLen;
  958. char* const newBuf = static_cast<char*>(malloc(newBufSize + 1));
  959. DISTRHO_SAFE_ASSERT_RETURN(newBuf != nullptr, String());
  960. std::memcpy(newBuf, strBefore.buffer(), strBeforeLen);
  961. std::memcpy(newBuf + strBeforeLen, strBufAfter, strBufAfterLen + 1);
  962. return String(newBuf, false);
  963. }
  964. static inline
  965. String operator+(const char* const strBufBefore, const String& strAfter) noexcept
  966. {
  967. if (strAfter.isEmpty())
  968. return String(strBufBefore);
  969. if (strBufBefore == nullptr || strBufBefore[0] == '\0')
  970. return strAfter;
  971. const std::size_t strBufBeforeLen = std::strlen(strBufBefore);
  972. const std::size_t strAfterLen = strAfter.length();
  973. const std::size_t newBufSize = strBufBeforeLen + strAfterLen;
  974. char* const newBuf = static_cast<char*>(malloc(newBufSize + 1));
  975. DISTRHO_SAFE_ASSERT_RETURN(newBuf != nullptr, String());
  976. std::memcpy(newBuf, strBufBefore, strBufBeforeLen);
  977. std::memcpy(newBuf + strBufBeforeLen, strAfter.buffer(), strAfterLen + 1);
  978. return String(newBuf, false);
  979. }
  980. // -----------------------------------------------------------------------
  981. END_NAMESPACE_DISTRHO
  982. #endif // DISTRHO_STRING_HPP_INCLUDED