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.

CarlaString.hpp 17KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. static const char kCharDiff('a' - 'A');
  406. for (size_t i=0; i < fBufferLen; ++i)
  407. {
  408. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  409. fBuffer[i] = static_cast<char>(fBuffer[i] + kCharDiff);
  410. }
  411. }
  412. /*
  413. * Convert to all ascii characters to uppercase.
  414. */
  415. void toUpper() noexcept
  416. {
  417. static const char kCharDiff('a' - 'A');
  418. for (size_t i=0; i < fBufferLen; ++i)
  419. {
  420. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  421. fBuffer[i] = static_cast<char>(fBuffer[i] - kCharDiff);
  422. }
  423. }
  424. /*
  425. * Return a duplicate string buffer.
  426. */
  427. const char* dup() const
  428. {
  429. return carla_strdup(fBuffer);
  430. }
  431. /*
  432. * Direct access to the string buffer.
  433. */
  434. const char* getBuffer() const
  435. {
  436. return fBuffer;
  437. }
  438. // -------------------------------------------------------------------
  439. // public operators
  440. operator const char*() const noexcept
  441. {
  442. return fBuffer;
  443. }
  444. char& operator[](const size_t pos) const noexcept
  445. {
  446. return fBuffer[pos];
  447. }
  448. bool operator==(const char* const strBuf) const
  449. {
  450. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  451. }
  452. bool operator==(const CarlaString& str) const
  453. {
  454. return operator==(str.fBuffer);
  455. }
  456. bool operator!=(const char* const strBuf) const
  457. {
  458. return !operator==(strBuf);
  459. }
  460. bool operator!=(const CarlaString& str) const
  461. {
  462. return !operator==(str.fBuffer);
  463. }
  464. CarlaString& operator=(const char* const strBuf)
  465. {
  466. _dup(strBuf);
  467. return *this;
  468. }
  469. CarlaString& operator=(const CarlaString& str)
  470. {
  471. return operator=(str.fBuffer);
  472. }
  473. CarlaString& operator+=(const char* const strBuf)
  474. {
  475. if (strBuf == nullptr)
  476. return *this;
  477. const size_t newBufSize = fBufferLen + std::strlen(strBuf) + 1;
  478. char newBuf[newBufSize];
  479. std::strcpy(newBuf, fBuffer);
  480. std::strcat(newBuf, strBuf);
  481. _dup(newBuf, newBufSize-1);
  482. return *this;
  483. }
  484. CarlaString& operator+=(const CarlaString& str)
  485. {
  486. return operator+=(str.fBuffer);
  487. }
  488. CarlaString operator+(const char* const strBuf)
  489. {
  490. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  491. char newBuf[newBufSize];
  492. std::strcpy(newBuf, fBuffer);
  493. if (strBuf != nullptr)
  494. std::strcat(newBuf, strBuf);
  495. return CarlaString(newBuf);
  496. }
  497. CarlaString operator+(const CarlaString& str)
  498. {
  499. return operator+(str.fBuffer);
  500. }
  501. // -------------------------------------------------------------------
  502. private:
  503. char* fBuffer; // the actual string buffer
  504. size_t fBufferLen; // string length
  505. bool fFirstInit; // true when first initiated
  506. /*
  507. * Shared init function.
  508. * Called on all constructors.
  509. */
  510. void _init() noexcept
  511. {
  512. fBuffer = nullptr;
  513. fBufferLen = 0;
  514. fFirstInit = true;
  515. }
  516. /*
  517. * Helper function.
  518. * Called whenever the string needs to be allocated.
  519. *
  520. * Notes:
  521. * - Allocates string only if first initiated, or if 'strBuf' is not null and new string contents are different
  522. * - If 'strBuf' is null 'size' must be 0
  523. */
  524. void _dup(const char* const strBuf, const size_t size = 0)
  525. {
  526. if (strBuf != nullptr)
  527. {
  528. // don't recreate string if contents match
  529. if (fFirstInit || std::strcmp(fBuffer, strBuf) != 0)
  530. {
  531. if (! fFirstInit)
  532. {
  533. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  534. delete[] fBuffer;
  535. }
  536. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  537. fBuffer = new char[fBufferLen+1];
  538. std::strcpy(fBuffer, strBuf);
  539. fBuffer[fBufferLen] = '\0';
  540. fFirstInit = false;
  541. }
  542. }
  543. else
  544. {
  545. CARLA_SAFE_ASSERT(size == 0);
  546. // don't recreate null string
  547. if (fFirstInit || fBufferLen != 0)
  548. {
  549. if (! fFirstInit)
  550. {
  551. CARLA_SAFE_ASSERT(fBuffer != nullptr);
  552. delete[] fBuffer;
  553. }
  554. fBufferLen = 0;
  555. fBuffer = new char[1];
  556. fBuffer[0] = '\0';
  557. fFirstInit = false;
  558. }
  559. }
  560. }
  561. CARLA_LEAK_DETECTOR(CarlaString)
  562. CARLA_PREVENT_HEAP_ALLOCATION
  563. };
  564. // -----------------------------------------------------------------------
  565. static inline
  566. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  567. {
  568. const char* const strBufBefore = (const char*)strBefore;
  569. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  570. char newBuf[newBufSize];
  571. std::strcpy(newBuf, strBufBefore);
  572. std::strcat(newBuf, strBufAfter);
  573. return CarlaString(newBuf);
  574. }
  575. static inline
  576. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  577. {
  578. const char* const strBufAfter = (const char*)strAfter;
  579. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  580. char newBuf[newBufSize];
  581. std::strcpy(newBuf, strBufBefore);
  582. std::strcat(newBuf, strBufAfter);
  583. return CarlaString(newBuf);
  584. }
  585. // -----------------------------------------------------------------------
  586. #endif // CARLA_STRING_HPP_INCLUDED