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.

714 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 "CarlaUtils.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. #ifndef BUILD_ANSI_TEST
  406. // Using '+=' temporarily converts char into int
  407. static const char kCharDiff('a' - 'A');
  408. for (size_t i=0; i < fBufferLen; ++i)
  409. {
  410. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  411. fBuffer[i] += kCharDiff;
  412. }
  413. #endif
  414. }
  415. /*
  416. * Convert to all ascii characters to uppercase.
  417. */
  418. void toUpper() noexcept
  419. {
  420. #ifndef BUILD_ANSI_TEST
  421. // Using '-=' temporarily converts char into int
  422. static const char kCharDiff('a' - 'A');
  423. for (size_t i=0; i < fBufferLen; ++i)
  424. {
  425. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  426. fBuffer[i] -= kCharDiff;
  427. }
  428. #endif
  429. }
  430. /*
  431. * Return a duplicate string buffer.
  432. */
  433. const char* dup() const
  434. {
  435. return carla_strdup(fBuffer);
  436. }
  437. /*
  438. * Direct access to the string buffer.
  439. */
  440. const char* getBuffer() const
  441. {
  442. return fBuffer;
  443. }
  444. // -------------------------------------------------------------------
  445. // public operators
  446. operator const char*() const noexcept
  447. {
  448. return fBuffer;
  449. }
  450. char& operator[](const size_t pos) const noexcept
  451. {
  452. return fBuffer[pos];
  453. }
  454. bool operator==(const char* const strBuf) const
  455. {
  456. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  457. }
  458. bool operator==(const CarlaString& str) const
  459. {
  460. return operator==(str.fBuffer);
  461. }
  462. bool operator!=(const char* const strBuf) const
  463. {
  464. return !operator==(strBuf);
  465. }
  466. bool operator!=(const CarlaString& str) const
  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_PREVENT_HEAP_ALLOCATION
  568. };
  569. // -----------------------------------------------------------------------
  570. static inline
  571. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  572. {
  573. const char* const strBufBefore = (const char*)strBefore;
  574. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  575. char newBuf[newBufSize];
  576. std::strcpy(newBuf, strBufBefore);
  577. std::strcat(newBuf, strBufAfter);
  578. return CarlaString(newBuf);
  579. }
  580. static inline
  581. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  582. {
  583. const char* const strBufAfter = (const char*)strAfter;
  584. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  585. char newBuf[newBufSize];
  586. std::strcpy(newBuf, strBufBefore);
  587. std::strcat(newBuf, strBufAfter);
  588. return CarlaString(newBuf);
  589. }
  590. // -----------------------------------------------------------------------
  591. #endif // CARLA_STRING_HPP_INCLUDED