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 18KB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 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
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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_PREVENT_HEAP_ALLOCATION
  599. };
  600. // -----------------------------------------------------------------------
  601. static inline
  602. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter) noexcept
  603. {
  604. const char* const strBufBefore = strBefore.buffer();
  605. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  606. char newBuf[newBufSize];
  607. std::strcpy(newBuf, strBufBefore);
  608. std::strcat(newBuf, strBufAfter);
  609. return CarlaString(newBuf);
  610. }
  611. static inline
  612. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter) noexcept
  613. {
  614. const char* const strBufAfter = strAfter.buffer();
  615. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  616. char newBuf[newBufSize];
  617. std::strcpy(newBuf, strBufBefore);
  618. std::strcat(newBuf, strBufAfter);
  619. return CarlaString(newBuf);
  620. }
  621. // -----------------------------------------------------------------------
  622. #endif // CARLA_STRING_HPP_INCLUDED