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.

501 lines
12KB

  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. explicit CarlaString()
  28. {
  29. _init();
  30. _dup(nullptr);
  31. }
  32. explicit CarlaString(char* const strBuf)
  33. {
  34. _init();
  35. _dup(strBuf);
  36. }
  37. explicit CarlaString(const char* const strBuf)
  38. {
  39. _init();
  40. _dup(strBuf);
  41. }
  42. explicit CarlaString(const int value)
  43. {
  44. char strBuf[0xff+1];
  45. carla_fill<char>(strBuf, 0xff+1, '\0');
  46. std::snprintf(strBuf, 0xff, "%d", value);
  47. _init();
  48. _dup(strBuf);
  49. }
  50. explicit CarlaString(const unsigned int value, const bool hexadecimal = false)
  51. {
  52. char strBuf[0xff+1];
  53. carla_fill<char>(strBuf, 0xff+1, '\0');
  54. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%x" : "%u", value);
  55. _init();
  56. _dup(strBuf);
  57. }
  58. explicit CarlaString(const long int value)
  59. {
  60. char strBuf[0xff+1];
  61. carla_fill<char>(strBuf, 0xff+1, '\0');
  62. std::snprintf(strBuf, 0xff, "%ld", value);
  63. _init();
  64. _dup(strBuf);
  65. }
  66. explicit CarlaString(const unsigned long int value, const bool hexadecimal = false)
  67. {
  68. char strBuf[0xff+1];
  69. carla_fill<char>(strBuf, 0xff+1, '\0');
  70. std::snprintf(strBuf, 0xff, hexadecimal ? "0x%lx" : "%lu", value);
  71. _init();
  72. _dup(strBuf);
  73. }
  74. explicit CarlaString(const float value)
  75. {
  76. char strBuf[0xff+1];
  77. carla_fill<char>(strBuf, 0xff+1, '\0');
  78. std::snprintf(strBuf, 0xff, "%f", value);
  79. _init();
  80. _dup(strBuf);
  81. }
  82. explicit CarlaString(const double value)
  83. {
  84. char strBuf[0xff+1];
  85. carla_fill<char>(strBuf, 0xff+1, '\0');
  86. std::snprintf(strBuf, 0xff, "%g", value);
  87. _init();
  88. _dup(strBuf);
  89. }
  90. // -------------------------------------------------------------------
  91. // non-explicit constructor
  92. CarlaString(const CarlaString& str)
  93. {
  94. _init();
  95. _dup(str.fBuffer);
  96. }
  97. // -------------------------------------------------------------------
  98. // destructor
  99. ~CarlaString()
  100. {
  101. CARLA_ASSERT(fBuffer != nullptr);
  102. delete[] fBuffer;
  103. fBuffer = nullptr;
  104. }
  105. // -------------------------------------------------------------------
  106. // public methods
  107. size_t length() const noexcept
  108. {
  109. return fBufferLen;
  110. }
  111. bool isEmpty() const noexcept
  112. {
  113. return (fBufferLen == 0);
  114. }
  115. bool isNotEmpty() const noexcept
  116. {
  117. return (fBufferLen != 0);
  118. }
  119. #ifdef __USE_GNU
  120. bool contains(const char* const strBuf, const bool ignoreCase = false) const
  121. {
  122. if (strBuf == nullptr)
  123. return false;
  124. if (ignoreCase)
  125. return (strcasestr(fBuffer, strBuf) != nullptr);
  126. else
  127. return (std::strstr(fBuffer, strBuf) != nullptr);
  128. }
  129. bool contains(const CarlaString& str, const bool ignoreCase = false) const
  130. {
  131. return contains(str.fBuffer, ignoreCase);
  132. }
  133. #else
  134. bool contains(const char* const strBuf) const
  135. {
  136. if (strBuf == nullptr)
  137. return false;
  138. return (std::strstr(fBuffer, strBuf) != nullptr);
  139. }
  140. bool contains(const CarlaString& str) const
  141. {
  142. return contains(str.fBuffer);
  143. }
  144. #endif
  145. bool isDigit(const size_t pos) const noexcept
  146. {
  147. if (pos >= fBufferLen)
  148. return false;
  149. return (fBuffer[pos] >= '0' && fBuffer[pos] <= '9');
  150. }
  151. bool startsWith(const char* const prefix) const
  152. {
  153. if (prefix == nullptr)
  154. return false;
  155. const size_t prefixLen(std::strlen(prefix));
  156. if (fBufferLen < prefixLen)
  157. return false;
  158. return (std::strncmp(fBuffer + (fBufferLen-prefixLen), prefix, prefixLen) == 0);
  159. }
  160. bool endsWith(const char* const suffix) const
  161. {
  162. if (suffix == nullptr)
  163. return false;
  164. const size_t suffixLen(std::strlen(suffix));
  165. if (fBufferLen < suffixLen)
  166. return false;
  167. return (std::strncmp(fBuffer + (fBufferLen-suffixLen), suffix, suffixLen) == 0);
  168. }
  169. void clear() noexcept
  170. {
  171. truncate(0);
  172. }
  173. size_t find(const char c) const noexcept
  174. {
  175. for (size_t i=0; i < fBufferLen; ++i)
  176. {
  177. if (fBuffer[i] == c)
  178. return i;
  179. }
  180. return 0;
  181. }
  182. size_t rfind(const char c) const noexcept
  183. {
  184. for (size_t i=fBufferLen; i > 0; --i)
  185. {
  186. if (fBuffer[i-1] == c)
  187. return i-1;
  188. }
  189. return 0;
  190. }
  191. size_t rfind(const char* const strBuf) const
  192. {
  193. if (strBuf == nullptr || strBuf[0] == '\0')
  194. return fBufferLen;
  195. size_t ret = fBufferLen+1;
  196. const char* tmpBuf = fBuffer;
  197. for (size_t i=0; i < fBufferLen; ++i)
  198. {
  199. if (std::strstr(tmpBuf, strBuf) == nullptr)
  200. break;
  201. --ret;
  202. ++tmpBuf;
  203. }
  204. return (ret > fBufferLen) ? fBufferLen : fBufferLen-ret;
  205. }
  206. void replace(const char before, const char after) noexcept
  207. {
  208. if (after == '\0')
  209. return;
  210. for (size_t i=0; i < fBufferLen; ++i)
  211. {
  212. if (fBuffer[i] == before)
  213. fBuffer[i] = after;
  214. else if (fBuffer[i] == '\0')
  215. break;
  216. }
  217. }
  218. void truncate(const size_t n) noexcept
  219. {
  220. if (n >= fBufferLen)
  221. return;
  222. for (size_t i=n; i < fBufferLen; ++i)
  223. fBuffer[i] = '\0';
  224. fBufferLen = n;
  225. }
  226. void toBasic() noexcept
  227. {
  228. for (size_t i=0; i < fBufferLen; ++i)
  229. {
  230. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  231. continue;
  232. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  233. continue;
  234. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  235. continue;
  236. if (fBuffer[i] == '_')
  237. continue;
  238. fBuffer[i] = '_';
  239. }
  240. }
  241. #ifndef BUILD_ANSI_TEST
  242. // Using '+=' and '-=' temporarily converts char into int
  243. void toLower() noexcept
  244. {
  245. static const char kCharDiff('a' - 'A');
  246. for (size_t i=0; i < fBufferLen; ++i)
  247. {
  248. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  249. fBuffer[i] += kCharDiff;
  250. }
  251. }
  252. void toUpper() noexcept
  253. {
  254. static const char kCharDiff('a' - 'A');
  255. for (size_t i=0; i < fBufferLen; ++i)
  256. {
  257. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  258. fBuffer[i] -= kCharDiff;
  259. }
  260. }
  261. #endif
  262. // -------------------------------------------------------------------
  263. // public operators
  264. operator const char*() const noexcept
  265. {
  266. return fBuffer;
  267. }
  268. char& operator[](const size_t pos) const noexcept
  269. {
  270. return fBuffer[pos];
  271. }
  272. bool operator==(const char* const strBuf) const
  273. {
  274. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  275. }
  276. bool operator==(const CarlaString& str) const
  277. {
  278. return operator==(str.fBuffer);
  279. }
  280. bool operator!=(const char* const strBuf) const
  281. {
  282. return !operator==(strBuf);
  283. }
  284. bool operator!=(const CarlaString& str) const
  285. {
  286. return !operator==(str.fBuffer);
  287. }
  288. CarlaString& operator=(const char* const strBuf)
  289. {
  290. _dup(strBuf);
  291. return *this;
  292. }
  293. CarlaString& operator=(const CarlaString& str)
  294. {
  295. return operator=(str.fBuffer);
  296. }
  297. CarlaString& operator+=(const char* const strBuf)
  298. {
  299. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  300. char newBuf[newBufSize];
  301. std::strcpy(newBuf, fBuffer);
  302. std::strcat(newBuf, strBuf);
  303. _dup(newBuf, newBufSize-1);
  304. return *this;
  305. }
  306. CarlaString& operator+=(const CarlaString& str)
  307. {
  308. return operator+=(str.fBuffer);
  309. }
  310. CarlaString operator+(const char* const strBuf)
  311. {
  312. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  313. char newBuf[newBufSize];
  314. std::strcpy(newBuf, fBuffer);
  315. std::strcat(newBuf, strBuf);
  316. return CarlaString(newBuf);
  317. }
  318. CarlaString operator+(const CarlaString& str)
  319. {
  320. return operator+(str.fBuffer);
  321. }
  322. // -------------------------------------------------------------------
  323. private:
  324. char* fBuffer;
  325. size_t fBufferLen;
  326. bool fFirstInit;
  327. void _init() noexcept
  328. {
  329. fBuffer = nullptr;
  330. fBufferLen = 0;
  331. fFirstInit = true;
  332. }
  333. // allocate string strBuf if not null
  334. // size > 0 only if strBuf is valid
  335. void _dup(const char* const strBuf, const size_t size = 0)
  336. {
  337. if (strBuf != nullptr)
  338. {
  339. // don't recreate string if contents match
  340. if (fFirstInit || std::strcmp(fBuffer, strBuf) != 0)
  341. {
  342. if (! fFirstInit)
  343. {
  344. CARLA_ASSERT(fBuffer != nullptr);
  345. delete[] fBuffer;
  346. }
  347. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  348. fBuffer = new char[fBufferLen+1];
  349. std::strcpy(fBuffer, strBuf);
  350. fBuffer[fBufferLen] = '\0';
  351. fFirstInit = false;
  352. }
  353. }
  354. else
  355. {
  356. CARLA_ASSERT(size == 0);
  357. // don't recreate null string
  358. if (fFirstInit || fBufferLen != 0)
  359. {
  360. if (! fFirstInit)
  361. {
  362. CARLA_ASSERT(fBuffer != nullptr);
  363. delete[] fBuffer;
  364. }
  365. fBufferLen = 0;
  366. fBuffer = new char[1];
  367. fBuffer[0] = '\0';
  368. fFirstInit = false;
  369. }
  370. }
  371. }
  372. CARLA_LEAK_DETECTOR(CarlaString)
  373. CARLA_PREVENT_HEAP_ALLOCATION
  374. };
  375. // -----------------------------------------------------------------------
  376. static inline
  377. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  378. {
  379. const char* const strBufBefore = (const char*)strBefore;
  380. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  381. char newBuf[newBufSize];
  382. std::strcpy(newBuf, strBufBefore);
  383. std::strcat(newBuf, strBufAfter);
  384. return CarlaString(newBuf);
  385. }
  386. static inline
  387. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  388. {
  389. const char* const strBufAfter = (const char*)strAfter;
  390. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  391. char newBuf[newBufSize];
  392. std::strcpy(newBuf, strBufBefore);
  393. std::strcat(newBuf, strBufAfter);
  394. return CarlaString(newBuf);
  395. }
  396. #endif // CARLA_STRING_HPP_INCLUDED