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.

772 lines
18KB

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