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.

845 lines
20KB

  1. /*
  2. * Carla String
  3. * Copyright (C) 2013-2014 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 <cctype>
  21. #include <sys/types.h>
  22. #include <vector>
  23. namespace std {
  24. #ifdef CARLA_OS_HAIKU
  25. using ::snprintf;
  26. #endif
  27. using ::ssize_t; // FIXME?
  28. }
  29. // -----------------------------------------------------------------------
  30. // Helpers
  31. namespace Base64Helpers {
  32. }
  33. // -----------------------------------------------------------------------
  34. // CarlaString class
  35. class CarlaString
  36. {
  37. public:
  38. // -------------------------------------------------------------------
  39. // constructors (no explicit conversions allowed)
  40. /*
  41. * Empty string.
  42. */
  43. explicit CarlaString() noexcept
  44. {
  45. _init();
  46. }
  47. /*
  48. * Simple character.
  49. */
  50. explicit CarlaString(const char c) noexcept
  51. {
  52. char ch[2];
  53. ch[0] = c;
  54. ch[1] = '\0';
  55. _init();
  56. _dup(ch);
  57. }
  58. /*
  59. * Simple char string.
  60. */
  61. explicit CarlaString(char* const strBuf) noexcept
  62. {
  63. _init();
  64. _dup(strBuf);
  65. }
  66. /*
  67. * Simple const char string.
  68. */
  69. explicit CarlaString(const char* const strBuf) noexcept
  70. {
  71. _init();
  72. _dup(strBuf);
  73. }
  74. /*
  75. * Integer.
  76. */
  77. explicit CarlaString(const int value) noexcept
  78. {
  79. char strBuf[0xff+1];
  80. std::snprintf(strBuf, 0xff, "%d", value);
  81. strBuf[0xff] = '\0';
  82. _init();
  83. _dup(strBuf);
  84. }
  85. /*
  86. * Unsigned integer, possibly in hexadecimal.
  87. */
  88. explicit CarlaString(const unsigned int value, const bool hexadecimal = false) noexcept
  89. {
  90. char strBuf[0xff+1];
  91. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%x" : "%u", value);
  92. strBuf[0xff] = '\0';
  93. _init();
  94. _dup(strBuf);
  95. }
  96. /*
  97. * Long integer.
  98. */
  99. explicit CarlaString(const long value) noexcept
  100. {
  101. char strBuf[0xff+1];
  102. std::snprintf(strBuf, 0xff, "%ld", value);
  103. strBuf[0xff] = '\0';
  104. _init();
  105. _dup(strBuf);
  106. }
  107. /*
  108. * Long unsigned integer, possibly hexadecimal.
  109. */
  110. explicit CarlaString(const unsigned long value, const bool hexadecimal = false) noexcept
  111. {
  112. char strBuf[0xff+1];
  113. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%lx" : "%lu", value);
  114. strBuf[0xff] = '\0';
  115. _init();
  116. _dup(strBuf);
  117. }
  118. /*
  119. * Long long integer.
  120. */
  121. explicit CarlaString(const long long value) noexcept
  122. {
  123. char strBuf[0xff+1];
  124. std::snprintf(strBuf, 0xff, "%lld", value);
  125. strBuf[0xff] = '\0';
  126. _init();
  127. _dup(strBuf);
  128. }
  129. /*
  130. * Long long unsigned integer, possibly hexadecimal.
  131. */
  132. explicit CarlaString(const unsigned long long value, const bool hexadecimal = false) noexcept
  133. {
  134. char strBuf[0xff+1];
  135. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%llx" : "%llu", value);
  136. strBuf[0xff] = '\0';
  137. _init();
  138. _dup(strBuf);
  139. }
  140. /*
  141. * Single-precision floating point number.
  142. */
  143. explicit CarlaString(const float value) noexcept
  144. {
  145. char strBuf[0xff+1];
  146. std::snprintf(strBuf, 0xff, "%f", value);
  147. strBuf[0xff] = '\0';
  148. _init();
  149. _dup(strBuf);
  150. }
  151. /*
  152. * Double-precision floating point number.
  153. */
  154. explicit CarlaString(const double value) noexcept
  155. {
  156. char strBuf[0xff+1];
  157. std::snprintf(strBuf, 0xff, "%g", value);
  158. strBuf[0xff] = '\0';
  159. _init();
  160. _dup(strBuf);
  161. }
  162. // -------------------------------------------------------------------
  163. // non-explicit constructor
  164. /*
  165. * Create string from another string.
  166. */
  167. CarlaString(const CarlaString& str) noexcept
  168. {
  169. _init();
  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 std::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. void replace(const char before, const char after) noexcept
  395. {
  396. CARLA_SAFE_ASSERT_RETURN(before != '\0' && after != '\0',);
  397. for (std::size_t i=0; i < fBufferLen; ++i)
  398. {
  399. if (fBuffer[i] == before)
  400. fBuffer[i] = after;
  401. }
  402. }
  403. /*
  404. * Truncate the string to size 'n'.
  405. */
  406. void truncate(const std::size_t n) noexcept
  407. {
  408. if (n >= fBufferLen)
  409. return;
  410. for (std::size_t i=n; i < fBufferLen; ++i)
  411. fBuffer[i] = '\0';
  412. fBufferLen = n;
  413. }
  414. /*
  415. * Convert all non-basic characters to '_'.
  416. */
  417. void toBasic() noexcept
  418. {
  419. for (std::size_t i=0; i < fBufferLen; ++i)
  420. {
  421. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  422. continue;
  423. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  424. continue;
  425. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  426. continue;
  427. if (fBuffer[i] == '_')
  428. continue;
  429. fBuffer[i] = '_';
  430. }
  431. }
  432. /*
  433. * Convert to all ascii characters to lowercase.
  434. */
  435. void toLower() noexcept
  436. {
  437. static const char kCharDiff('a' - 'A');
  438. for (std::size_t i=0; i < fBufferLen; ++i)
  439. {
  440. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  441. fBuffer[i] = static_cast<char>(fBuffer[i] + kCharDiff);
  442. }
  443. }
  444. /*
  445. * Convert to all ascii characters to uppercase.
  446. */
  447. void toUpper() noexcept
  448. {
  449. static const char kCharDiff('a' - 'A');
  450. for (std::size_t i=0; i < fBufferLen; ++i)
  451. {
  452. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  453. fBuffer[i] = static_cast<char>(fBuffer[i] - kCharDiff);
  454. }
  455. }
  456. /*
  457. * Direct access to the string buffer (read-only).
  458. */
  459. const char* buffer() const noexcept
  460. {
  461. return fBuffer;
  462. }
  463. /*
  464. * Return a duplicate string buffer.
  465. * May throw.
  466. */
  467. const char* dup() const
  468. {
  469. return carla_strdup(fBuffer);
  470. }
  471. // -------------------------------------------------------------------
  472. // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
  473. // Copyright (C) 2004-2008 René Nyffenegger
  474. static CarlaString asBase64(const void* const data, const std::size_t dataSize)
  475. {
  476. static const char* const kBase64Chars =
  477. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  478. "abcdefghijklmnopqrstuvwxyz"
  479. "0123456789+/";
  480. const uchar* bytesToEncode((const uchar*)data);
  481. uint i=0, j=0;
  482. uint charArray3[3], charArray4[4];
  483. char strBuf[0xff+1];
  484. strBuf[0xff] = '\0';
  485. int strBufIndex = 0;
  486. CarlaString ret;
  487. for (std::size_t s=0; s<dataSize; ++s)
  488. {
  489. charArray3[i++] = *(bytesToEncode++);
  490. if (i == 3)
  491. {
  492. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  493. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  494. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  495. charArray4[3] = charArray3[2] & 0x3f;
  496. for (i=0; i<4; ++i)
  497. strBuf[strBufIndex++] = kBase64Chars[charArray4[i]];
  498. if (strBufIndex >= 0xff-7)
  499. {
  500. strBuf[strBufIndex] = '\0';
  501. strBufIndex = 0;
  502. ret += strBuf;
  503. }
  504. i = 0;
  505. }
  506. }
  507. if (i != 0)
  508. {
  509. for (j=i; j<3; ++j)
  510. charArray3[j] = '\0';
  511. charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  512. charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  513. charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  514. charArray4[3] = charArray3[2] & 0x3f;
  515. for (j=0; j<4 && i<3 && j<i+1; ++j)
  516. strBuf[strBufIndex++] = kBase64Chars[charArray4[j]];
  517. for (; i++ < 3;)
  518. strBuf[strBufIndex++] = '=';
  519. strBuf[strBufIndex] = '\0';
  520. ret += strBuf;
  521. }
  522. return ret;
  523. }
  524. // -------------------------------------------------------------------
  525. // public operators
  526. operator const char*() const noexcept
  527. {
  528. return fBuffer;
  529. }
  530. char operator[](const std::size_t pos) const noexcept
  531. {
  532. if (pos < fBufferLen)
  533. return fBuffer[pos];
  534. carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  535. static char fallback;
  536. fallback = '\0';
  537. return fallback;
  538. }
  539. char& operator[](const std::size_t pos) noexcept
  540. {
  541. if (pos < fBufferLen)
  542. return fBuffer[pos];
  543. carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  544. static char fallback;
  545. fallback = '\0';
  546. return fallback;
  547. }
  548. bool operator==(const char* const strBuf) const noexcept
  549. {
  550. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  551. }
  552. bool operator==(const CarlaString& str) const noexcept
  553. {
  554. return operator==(str.fBuffer);
  555. }
  556. bool operator!=(const char* const strBuf) const noexcept
  557. {
  558. return !operator==(strBuf);
  559. }
  560. bool operator!=(const CarlaString& str) const noexcept
  561. {
  562. return !operator==(str.fBuffer);
  563. }
  564. CarlaString& operator=(const char* const strBuf) noexcept
  565. {
  566. _dup(strBuf);
  567. return *this;
  568. }
  569. CarlaString& operator=(const CarlaString& str) noexcept
  570. {
  571. _dup(str.fBuffer);
  572. return *this;
  573. }
  574. CarlaString& operator+=(const char* const strBuf) noexcept
  575. {
  576. if (strBuf == nullptr)
  577. return *this;
  578. const std::size_t newBufSize = fBufferLen + std::strlen(strBuf) + 1;
  579. char newBuf[newBufSize];
  580. std::strcpy(newBuf, fBuffer);
  581. std::strcat(newBuf, strBuf);
  582. _dup(newBuf, newBufSize-1);
  583. return *this;
  584. }
  585. CarlaString& operator+=(const CarlaString& str) noexcept
  586. {
  587. return operator+=(str.fBuffer);
  588. }
  589. CarlaString operator+(const char* const strBuf) noexcept
  590. {
  591. const std::size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  592. char newBuf[newBufSize];
  593. std::strcpy(newBuf, fBuffer);
  594. if (strBuf != nullptr)
  595. std::strcat(newBuf, strBuf);
  596. return CarlaString(newBuf);
  597. }
  598. CarlaString operator+(const CarlaString& str) noexcept
  599. {
  600. return operator+(str.fBuffer);
  601. }
  602. // -------------------------------------------------------------------
  603. private:
  604. char* fBuffer; // the actual string buffer
  605. std::size_t fBufferLen; // string length
  606. /*
  607. * Static null string.
  608. * Prevents allocation for new and/or empty strings.
  609. */
  610. static char* _null() noexcept
  611. {
  612. static char sNull = '\0';
  613. return &sNull;
  614. }
  615. /*
  616. * Shared init function.
  617. * Called on all constructors.
  618. */
  619. void _init() noexcept
  620. {
  621. fBuffer = _null();
  622. fBufferLen = 0;
  623. }
  624. /*
  625. * Helper function.
  626. * Called whenever the string needs to be allocated.
  627. *
  628. * Notes:
  629. * - Allocates string only if 'strBuf' is not null and new string contents are different
  630. * - If 'strBuf' is null, 'size' must be 0
  631. */
  632. void _dup(const char* const strBuf, const std::size_t size = 0) noexcept
  633. {
  634. if (strBuf != nullptr)
  635. {
  636. // don't recreate string if contents match
  637. if (std::strcmp(fBuffer, strBuf) == 0)
  638. return;
  639. if (fBuffer != _null())
  640. std::free(fBuffer);
  641. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  642. fBuffer = (char*)std::malloc(fBufferLen+1);
  643. if (fBuffer == nullptr)
  644. return _init();
  645. std::strcpy(fBuffer, strBuf);
  646. fBuffer[fBufferLen] = '\0';
  647. }
  648. else
  649. {
  650. CARLA_SAFE_ASSERT(size == 0);
  651. // don't recreate null string
  652. if (fBuffer == _null())
  653. return;
  654. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  655. std::free(fBuffer);
  656. _init();
  657. }
  658. }
  659. CARLA_PREVENT_HEAP_ALLOCATION
  660. };
  661. // -----------------------------------------------------------------------
  662. static inline
  663. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter) noexcept
  664. {
  665. const char* const strBufBefore = strBefore.buffer();
  666. const std::size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  667. char newBuf[newBufSize];
  668. std::strcpy(newBuf, strBufBefore);
  669. std::strcat(newBuf, strBufAfter);
  670. return CarlaString(newBuf);
  671. }
  672. static inline
  673. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter) noexcept
  674. {
  675. const char* const strBufAfter = strAfter.buffer();
  676. const std::size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  677. char newBuf[newBufSize];
  678. std::strcpy(newBuf, strBufBefore);
  679. std::strcat(newBuf, strBufAfter);
  680. return CarlaString(newBuf);
  681. }
  682. // -----------------------------------------------------------------------
  683. #endif // CARLA_STRING_HPP_INCLUDED