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.

763 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. std::snprintf(strBuf, 0xff, "%d", value);
  73. strBuf[0xff] = '\0';
  74. _init();
  75. _dup(strBuf);
  76. }
  77. /*
  78. * Unsigned integer, possibly in hexadecimal.
  79. */
  80. explicit CarlaString(const unsigned int value, const bool hexadecimal = false) noexcept
  81. {
  82. char strBuf[0xff+1];
  83. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%x" : "%u", value);
  84. strBuf[0xff] = '\0';
  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. std::snprintf(strBuf, 0xff, "%ld", value);
  95. strBuf[0xff] = '\0';
  96. _init();
  97. _dup(strBuf);
  98. }
  99. /*
  100. * Long unsigned integer, possibly hexadecimal.
  101. */
  102. explicit CarlaString(const unsigned long value, const bool hexadecimal = false) noexcept
  103. {
  104. char strBuf[0xff+1];
  105. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%lx" : "%lu", value);
  106. strBuf[0xff] = '\0';
  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. std::snprintf(strBuf, 0xff, "%lld", value);
  117. strBuf[0xff] = '\0';
  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. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%llx" : "%llu", value);
  128. strBuf[0xff] = '\0';
  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. std::snprintf(strBuf, 0xff, "%f", value);
  139. strBuf[0xff] = '\0';
  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. std::snprintf(strBuf, 0xff, "%g", value);
  150. strBuf[0xff] = '\0';
  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. * Check if character at 'pos' is a digit.
  225. */
  226. bool isDigit(const size_t pos) const noexcept
  227. {
  228. CARLA_SAFE_ASSERT_RETURN(pos < fBufferLen, false);
  229. return (fBuffer[pos] >= '0' && fBuffer[pos] <= '9');
  230. }
  231. /*
  232. * Check if the string starts with the character 'c'.
  233. */
  234. bool startsWith(const char c) const noexcept
  235. {
  236. CARLA_SAFE_ASSERT_RETURN(c != '\0', false);
  237. return (fBufferLen > 0 && fBuffer[0] == c);
  238. }
  239. /*
  240. * Check if the string starts with the string 'prefix'.
  241. */
  242. bool startsWith(const char* const prefix) const noexcept
  243. {
  244. CARLA_SAFE_ASSERT_RETURN(prefix != nullptr, false);
  245. const size_t prefixLen(std::strlen(prefix));
  246. if (fBufferLen < prefixLen)
  247. return false;
  248. return (std::strncmp(fBuffer, prefix, prefixLen) == 0);
  249. }
  250. /*
  251. * Check if the string ends with the character 'c'.
  252. */
  253. bool endsWith(const char c) const noexcept
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(c != '\0', false);
  256. return (fBufferLen > 0 && fBuffer[fBufferLen-1] == c);
  257. }
  258. /*
  259. * Check if the string ends with the string 'suffix'.
  260. */
  261. bool endsWith(const char* const suffix) const noexcept
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(suffix != nullptr, false);
  264. const size_t suffixLen(std::strlen(suffix));
  265. if (fBufferLen < suffixLen)
  266. return false;
  267. return (std::strncmp(fBuffer + (fBufferLen-suffixLen), suffix, suffixLen) == 0);
  268. }
  269. /*
  270. * Find the first occurrence of character 'c' in the string.
  271. * Returns "length()" if the character is not found.
  272. */
  273. size_t find(const char c, bool* const found = nullptr) const noexcept
  274. {
  275. if (fBufferLen == 0 || c == '\0')
  276. {
  277. if (found != nullptr)
  278. *found = false;
  279. return fBufferLen;
  280. }
  281. for (size_t i=0; i < fBufferLen; ++i)
  282. {
  283. if (fBuffer[i] == c)
  284. {
  285. if (found != nullptr)
  286. *found = true;
  287. return i;
  288. }
  289. }
  290. if (found != nullptr)
  291. *found = false;
  292. return fBufferLen;
  293. }
  294. /*
  295. * Find the first occurrence of string 'strBuf' in the string.
  296. * Returns "length()" if the string is not found.
  297. */
  298. size_t find(const char* const strBuf, bool* const found = nullptr) const noexcept
  299. {
  300. if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0')
  301. {
  302. if (found != nullptr)
  303. *found = false;
  304. return fBufferLen;
  305. }
  306. if (char* const subStrBuf = std::strstr(fBuffer, strBuf))
  307. {
  308. const ssize_t ret(subStrBuf - fBuffer);
  309. if (ret < 0)
  310. {
  311. // should never happen!
  312. carla_safe_assert_int("ret >= 0", __FILE__, __LINE__, int(ret));
  313. if (found != nullptr)
  314. *found = false;
  315. return fBufferLen;
  316. }
  317. if (found != nullptr)
  318. *found = true;
  319. return static_cast<size_t>(ret);
  320. }
  321. if (found != nullptr)
  322. *found = false;
  323. return fBufferLen;
  324. }
  325. /*
  326. * Find the last occurrence of character 'c' in the string.
  327. * Returns "length()" if the character is not found.
  328. */
  329. size_t rfind(const char c, bool* const found = nullptr) const noexcept
  330. {
  331. if (fBufferLen == 0 || c == '\0')
  332. {
  333. if (found != nullptr)
  334. *found = false;
  335. return fBufferLen;
  336. }
  337. for (size_t i=fBufferLen; i > 0; --i)
  338. {
  339. if (fBuffer[i-1] == c)
  340. {
  341. if (found != nullptr)
  342. *found = true;
  343. return i-1;
  344. }
  345. }
  346. if (found != nullptr)
  347. *found = false;
  348. return fBufferLen;
  349. }
  350. /*
  351. * Find the last occurrence of string 'strBuf' in the string.
  352. * Returns "length()" if the string is not found.
  353. */
  354. size_t rfind(const char* const strBuf, bool* const found = nullptr) const noexcept
  355. {
  356. if (found != nullptr)
  357. *found = false;
  358. if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0')
  359. return fBufferLen;
  360. const size_t strBufLen(std::strlen(strBuf));
  361. size_t ret = fBufferLen;
  362. const char* tmpBuf = fBuffer;
  363. for (size_t i=0; i < fBufferLen; ++i)
  364. {
  365. if (std::strstr(tmpBuf+1, strBuf) == nullptr && std::strncmp(tmpBuf, strBuf, strBufLen) == 0)
  366. {
  367. if (found != nullptr)
  368. *found = true;
  369. break;
  370. }
  371. --ret;
  372. ++tmpBuf;
  373. }
  374. return fBufferLen-ret;
  375. }
  376. /*
  377. * Clear the string.
  378. */
  379. void clear() noexcept
  380. {
  381. truncate(0);
  382. }
  383. /*
  384. * Replace all occurrences of character 'before' with character 'after'.
  385. */
  386. void replace(const char before, const char after) noexcept
  387. {
  388. CARLA_SAFE_ASSERT_RETURN(before != '\0' && after != '\0',);
  389. for (size_t i=0; i < fBufferLen; ++i)
  390. {
  391. if (fBuffer[i] == before)
  392. fBuffer[i] = after;
  393. }
  394. }
  395. /*
  396. * Truncate the string to size 'n'.
  397. */
  398. void truncate(const size_t n) noexcept
  399. {
  400. if (n >= fBufferLen)
  401. return;
  402. for (size_t i=n; i < fBufferLen; ++i)
  403. fBuffer[i] = '\0';
  404. fBufferLen = n;
  405. }
  406. /*
  407. * Convert all non-basic characters to '_'.
  408. */
  409. void toBasic() noexcept
  410. {
  411. for (size_t i=0; i < fBufferLen; ++i)
  412. {
  413. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  414. continue;
  415. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  416. continue;
  417. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  418. continue;
  419. if (fBuffer[i] == '_')
  420. continue;
  421. fBuffer[i] = '_';
  422. }
  423. }
  424. /*
  425. * Convert to all ascii characters to lowercase.
  426. */
  427. void toLower() noexcept
  428. {
  429. static const char kCharDiff('a' - 'A');
  430. for (size_t i=0; i < fBufferLen; ++i)
  431. {
  432. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  433. fBuffer[i] = static_cast<char>(fBuffer[i] + kCharDiff);
  434. }
  435. }
  436. /*
  437. * Convert to all ascii characters to uppercase.
  438. */
  439. void toUpper() noexcept
  440. {
  441. static const char kCharDiff('a' - 'A');
  442. for (size_t i=0; i < fBufferLen; ++i)
  443. {
  444. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  445. fBuffer[i] = static_cast<char>(fBuffer[i] - kCharDiff);
  446. }
  447. }
  448. /*
  449. * Direct access to the string buffer (read-only).
  450. */
  451. const char* buffer() const noexcept
  452. {
  453. return fBuffer;
  454. }
  455. /*
  456. * Return a duplicate string buffer.
  457. * May throw.
  458. */
  459. const char* dup() const
  460. {
  461. return carla_strdup(fBuffer);
  462. }
  463. // -------------------------------------------------------------------
  464. // public operators
  465. operator const char*() const noexcept
  466. {
  467. return fBuffer;
  468. }
  469. char operator[](const size_t pos) const noexcept
  470. {
  471. if (pos < fBufferLen)
  472. return fBuffer[pos];
  473. carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  474. static char fallback;
  475. fallback = '\0';
  476. return fallback;
  477. }
  478. char& operator[](const size_t pos) noexcept
  479. {
  480. if (pos < fBufferLen)
  481. return fBuffer[pos];
  482. carla_safe_assert("pos < fBufferLen", __FILE__, __LINE__);
  483. static char fallback;
  484. fallback = '\0';
  485. return fallback;
  486. }
  487. bool operator==(const char* const strBuf) const noexcept
  488. {
  489. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  490. }
  491. bool operator==(const CarlaString& str) const noexcept
  492. {
  493. return operator==(str.fBuffer);
  494. }
  495. bool operator!=(const char* const strBuf) const noexcept
  496. {
  497. return !operator==(strBuf);
  498. }
  499. bool operator!=(const CarlaString& str) const noexcept
  500. {
  501. return !operator==(str.fBuffer);
  502. }
  503. CarlaString& operator=(const char* const strBuf) noexcept
  504. {
  505. _dup(strBuf);
  506. return *this;
  507. }
  508. CarlaString& operator=(const CarlaString& str) noexcept
  509. {
  510. _dup(str.fBuffer);
  511. return *this;
  512. }
  513. CarlaString& operator+=(const char* const strBuf) noexcept
  514. {
  515. if (strBuf == nullptr)
  516. return *this;
  517. const size_t newBufSize = fBufferLen + std::strlen(strBuf) + 1;
  518. char newBuf[newBufSize];
  519. std::strcpy(newBuf, fBuffer);
  520. std::strcat(newBuf, strBuf);
  521. _dup(newBuf, newBufSize-1);
  522. return *this;
  523. }
  524. CarlaString& operator+=(const CarlaString& str) noexcept
  525. {
  526. return operator+=(str.fBuffer);
  527. }
  528. CarlaString operator+(const char* const strBuf) noexcept
  529. {
  530. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  531. char newBuf[newBufSize];
  532. std::strcpy(newBuf, fBuffer);
  533. if (strBuf != nullptr)
  534. std::strcat(newBuf, strBuf);
  535. return CarlaString(newBuf);
  536. }
  537. CarlaString operator+(const CarlaString& str) noexcept
  538. {
  539. return operator+(str.fBuffer);
  540. }
  541. // -------------------------------------------------------------------
  542. private:
  543. char* fBuffer; // the actual string buffer
  544. size_t fBufferLen; // string length
  545. /*
  546. * Static null string.
  547. * Prevents allocation for new and/or empty strings.
  548. */
  549. static char* _null() noexcept
  550. {
  551. static char sNull = '\0';
  552. return &sNull;
  553. }
  554. /*
  555. * Shared init function.
  556. * Called on all constructors.
  557. */
  558. void _init() noexcept
  559. {
  560. fBuffer = _null();
  561. fBufferLen = 0;
  562. }
  563. /*
  564. * Helper function.
  565. * Called whenever the string needs to be allocated.
  566. *
  567. * Notes:
  568. * - Allocates string only if 'strBuf' is not null and new string contents are different
  569. * - If 'strBuf' is null, 'size' must be 0
  570. */
  571. void _dup(const char* const strBuf, const size_t size = 0) noexcept
  572. {
  573. if (strBuf != nullptr)
  574. {
  575. // don't recreate string if contents match
  576. if (std::strcmp(fBuffer, strBuf) == 0)
  577. return;
  578. if (fBuffer != _null())
  579. std::free(fBuffer);
  580. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  581. fBuffer = (char*)std::malloc(fBufferLen+1);
  582. if (fBuffer == nullptr)
  583. return _init();
  584. std::strcpy(fBuffer, strBuf);
  585. fBuffer[fBufferLen] = '\0';
  586. }
  587. else
  588. {
  589. CARLA_SAFE_ASSERT(size == 0);
  590. // don't recreate null string
  591. if (fBuffer == _null())
  592. return;
  593. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  594. std::free(fBuffer);
  595. _init();
  596. }
  597. }
  598. CARLA_LEAK_DETECTOR(CarlaString)
  599. CARLA_PREVENT_HEAP_ALLOCATION
  600. };
  601. // -----------------------------------------------------------------------
  602. static inline
  603. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter) noexcept
  604. {
  605. const char* const strBufBefore = strBefore.buffer();
  606. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  607. char newBuf[newBufSize];
  608. std::strcpy(newBuf, strBufBefore);
  609. std::strcat(newBuf, strBufAfter);
  610. return CarlaString(newBuf);
  611. }
  612. static inline
  613. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter) noexcept
  614. {
  615. const char* const strBufAfter = strAfter.buffer();
  616. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  617. char newBuf[newBufSize];
  618. std::strcpy(newBuf, strBufBefore);
  619. std::strcat(newBuf, strBufAfter);
  620. return CarlaString(newBuf);
  621. }
  622. // -----------------------------------------------------------------------
  623. #endif // CARLA_STRING_HPP_INCLUDED