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.

471 lines
11KB

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