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.

717 lines
17KB

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