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.

711 lines
17KB

  1. /*
  2. * Carla String
  3. * Copyright (C) 2013 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
  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
  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 + (fBufferLen-prefixLen), prefix, prefixLen) == 0);
  224. }
  225. /*
  226. * Check if the string ends with the character 'c'.
  227. */
  228. bool endsWith(const char c) const
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(c != '\0', false);
  231. return (fBufferLen > 0 && fBuffer[fBufferLen] == c);
  232. }
  233. /*
  234. * Check if the string ends with the string 'suffix'.
  235. */
  236. bool endsWith(const char* const suffix) const
  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
  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_stderr2("Carla assertion failure: \"ret >= 0\" in file %s, line %i, value %i", __FILE__, __LINE__, 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
  330. {
  331. if (found != nullptr)
  332. *found = false;
  333. if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0')
  334. return fBufferLen;
  335. size_t ret = fBufferLen+1;
  336. const char* tmpBuf = fBuffer;
  337. for (size_t i=0; i < fBufferLen; ++i)
  338. {
  339. if (std::strstr(tmpBuf, strBuf) == nullptr)
  340. {
  341. if (found != nullptr)
  342. *found = true;
  343. break;
  344. }
  345. --ret;
  346. ++tmpBuf;
  347. }
  348. return (ret > fBufferLen) ? fBufferLen : fBufferLen-ret;
  349. }
  350. /*
  351. * Clear the string.
  352. */
  353. void clear() noexcept
  354. {
  355. truncate(0);
  356. }
  357. /*
  358. * Replace all occurrences of character 'before' with character 'after'.
  359. */
  360. void replace(const char before, const char after)
  361. {
  362. CARLA_SAFE_ASSERT_RETURN(before != '\0' && after != '\0',);
  363. for (size_t i=0; i < fBufferLen; ++i)
  364. {
  365. if (fBuffer[i] == before)
  366. fBuffer[i] = after;
  367. else if (fBuffer[i] == '\0')
  368. break;
  369. }
  370. }
  371. /*
  372. * Truncate the string to size 'n'.
  373. */
  374. void truncate(const size_t n) noexcept
  375. {
  376. if (n >= fBufferLen)
  377. return;
  378. for (size_t i=n; i < fBufferLen; ++i)
  379. fBuffer[i] = '\0';
  380. fBufferLen = n;
  381. }
  382. /*
  383. * Convert all non-basic characters to '_'.
  384. */
  385. void toBasic() noexcept
  386. {
  387. for (size_t i=0; i < fBufferLen; ++i)
  388. {
  389. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  390. continue;
  391. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  392. continue;
  393. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  394. continue;
  395. if (fBuffer[i] == '_')
  396. continue;
  397. fBuffer[i] = '_';
  398. }
  399. }
  400. /*
  401. * Convert to all ascii characters to lowercase.
  402. */
  403. void toLower() noexcept
  404. {
  405. // Using '+=' temporarily converts char into int
  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. // Using '-=' temporarily converts char into int
  419. static const char kCharDiff('a' - 'A');
  420. for (size_t i=0; i < fBufferLen; ++i)
  421. {
  422. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  423. fBuffer[i] = static_cast<char>(fBuffer[i] - kCharDiff);
  424. }
  425. }
  426. /*
  427. * Return a duplicate string buffer.
  428. */
  429. const char* dup() const
  430. {
  431. return carla_strdup(fBuffer);
  432. }
  433. /*
  434. * Direct access to the string buffer.
  435. */
  436. const char* getBuffer() const
  437. {
  438. return fBuffer;
  439. }
  440. // -------------------------------------------------------------------
  441. // public operators
  442. operator const char*() const noexcept
  443. {
  444. return fBuffer;
  445. }
  446. char& operator[](const size_t pos) const noexcept
  447. {
  448. return fBuffer[pos];
  449. }
  450. bool operator==(const char* const strBuf) const
  451. {
  452. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  453. }
  454. bool operator==(const CarlaString& str) const
  455. {
  456. return operator==(str.fBuffer);
  457. }
  458. bool operator!=(const char* const strBuf) const
  459. {
  460. return !operator==(strBuf);
  461. }
  462. bool operator!=(const CarlaString& str) const
  463. {
  464. return !operator==(str.fBuffer);
  465. }
  466. CarlaString& operator=(const char* const strBuf)
  467. {
  468. _dup(strBuf);
  469. return *this;
  470. }
  471. CarlaString& operator=(const CarlaString& str)
  472. {
  473. return operator=(str.fBuffer);
  474. }
  475. CarlaString& operator+=(const char* const strBuf)
  476. {
  477. if (strBuf == nullptr)
  478. return *this;
  479. const size_t newBufSize = fBufferLen + std::strlen(strBuf) + 1;
  480. char newBuf[newBufSize];
  481. std::strcpy(newBuf, fBuffer);
  482. std::strcat(newBuf, strBuf);
  483. _dup(newBuf, newBufSize-1);
  484. return *this;
  485. }
  486. CarlaString& operator+=(const CarlaString& str)
  487. {
  488. return operator+=(str.fBuffer);
  489. }
  490. CarlaString operator+(const char* const strBuf)
  491. {
  492. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  493. char newBuf[newBufSize];
  494. std::strcpy(newBuf, fBuffer);
  495. if (strBuf != nullptr)
  496. std::strcat(newBuf, strBuf);
  497. return CarlaString(newBuf);
  498. }
  499. CarlaString operator+(const CarlaString& str)
  500. {
  501. return operator+(str.fBuffer);
  502. }
  503. // -------------------------------------------------------------------
  504. private:
  505. char* fBuffer; // the actual string buffer
  506. size_t fBufferLen; // string length
  507. bool fFirstInit; // true when first initiated
  508. /*
  509. * Shared init function.
  510. * Called on all constructors.
  511. */
  512. void _init() noexcept
  513. {
  514. fBuffer = nullptr;
  515. fBufferLen = 0;
  516. fFirstInit = true;
  517. }
  518. /*
  519. * Helper function.
  520. * Called whenever the string needs to be allocated.
  521. *
  522. * Notes:
  523. * - Allocates string only if first initiated, or if 'strBuf' is not null and new string contents are different
  524. * - If 'strBuf' is null 'size' must be 0
  525. */
  526. void _dup(const char* const strBuf, const size_t size = 0)
  527. {
  528. if (strBuf != nullptr)
  529. {
  530. // don't recreate string if contents match
  531. if (fFirstInit || std::strcmp(fBuffer, strBuf) != 0)
  532. {
  533. if (! fFirstInit)
  534. {
  535. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  536. delete[] fBuffer;
  537. }
  538. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  539. fBuffer = new char[fBufferLen+1];
  540. std::strcpy(fBuffer, strBuf);
  541. fBuffer[fBufferLen] = '\0';
  542. fFirstInit = false;
  543. }
  544. }
  545. else
  546. {
  547. CARLA_SAFE_ASSERT(size == 0);
  548. // don't recreate null string
  549. if (fFirstInit || fBufferLen != 0)
  550. {
  551. if (! fFirstInit)
  552. {
  553. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  554. delete[] fBuffer;
  555. }
  556. fBufferLen = 0;
  557. fBuffer = new char[1];
  558. fBuffer[0] = '\0';
  559. fFirstInit = false;
  560. }
  561. }
  562. }
  563. CARLA_LEAK_DETECTOR(CarlaString)
  564. CARLA_PREVENT_HEAP_ALLOCATION
  565. };
  566. // -----------------------------------------------------------------------
  567. static inline
  568. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  569. {
  570. const char* const strBufBefore = (const char*)strBefore;
  571. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  572. char newBuf[newBufSize];
  573. std::strcpy(newBuf, strBufBefore);
  574. std::strcat(newBuf, strBufAfter);
  575. return CarlaString(newBuf);
  576. }
  577. static inline
  578. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  579. {
  580. const char* const strBufAfter = (const char*)strAfter;
  581. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  582. char newBuf[newBufSize];
  583. std::strcpy(newBuf, strBufBefore);
  584. std::strcat(newBuf, strBufAfter);
  585. return CarlaString(newBuf);
  586. }
  587. // -----------------------------------------------------------------------
  588. #endif // CARLA_STRING_HPP_INCLUDED