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.

444 lines
9.9KB

  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. }
  98. // ---------------------------------------------
  99. // public methods
  100. size_t length() const
  101. {
  102. return bufferLen;
  103. }
  104. bool isEmpty() const
  105. {
  106. return (bufferLen == 0);
  107. }
  108. bool isNotEmpty() const
  109. {
  110. return (bufferLen != 0);
  111. }
  112. #ifdef __USE_GNU
  113. bool contains(const char* const strBuf, const bool ignoreCase = false) const
  114. {
  115. if (strBuf == nullptr)
  116. return false;
  117. if (ignoreCase)
  118. return (strcasestr(buffer, strBuf) != nullptr);
  119. else
  120. return (std::strstr(buffer, strBuf) != nullptr);
  121. }
  122. bool contains(const CarlaString& str, const bool ignoreCase = false) const
  123. {
  124. return contains(str.buffer, ignoreCase);
  125. }
  126. #else
  127. bool contains(const char* const strBuf) const
  128. {
  129. if (strBuf == nullptr)
  130. return false;
  131. return (std::strstr(buffer, strBuf) != nullptr);
  132. }
  133. bool contains(const CarlaString& str) const
  134. {
  135. return contains(str.buffer);
  136. }
  137. #endif
  138. bool isDigit(const size_t pos) const
  139. {
  140. if (pos >= bufferLen)
  141. return false;
  142. return (buffer[pos] >= '0' && buffer[pos] <= '9');
  143. }
  144. void clear()
  145. {
  146. truncate(0);
  147. }
  148. size_t find(const char c) const
  149. {
  150. for (size_t i=0; i < bufferLen; ++i)
  151. {
  152. if (buffer[i] == c)
  153. return i;
  154. }
  155. return 0;
  156. }
  157. size_t rfind(const char c) const
  158. {
  159. for (size_t i=bufferLen; i > 0; --i)
  160. {
  161. if (buffer[i-1] == c)
  162. return i-1;
  163. }
  164. return 0;
  165. }
  166. void replace(const char before, const char after)
  167. {
  168. if (after == '\0')
  169. return;
  170. for (size_t i=0; i < bufferLen; ++i)
  171. {
  172. if (buffer[i] == before)
  173. buffer[i] = after;
  174. else if (buffer[i] == '\0')
  175. break;
  176. }
  177. }
  178. void truncate(const size_t n)
  179. {
  180. if (n >= bufferLen)
  181. return;
  182. for (size_t i=n; i < bufferLen; ++i)
  183. buffer[i] = '\0';
  184. bufferLen = n;
  185. }
  186. void toBasic()
  187. {
  188. for (size_t i=0; i < bufferLen; ++i)
  189. {
  190. if (buffer[i] >= '0' && buffer[i] <= '9')
  191. continue;
  192. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  193. continue;
  194. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  195. continue;
  196. if (buffer[i] == '_')
  197. continue;
  198. buffer[i] = '_';
  199. }
  200. }
  201. void toLower()
  202. {
  203. static const char kCharDiff = 'a' - 'A';
  204. for (size_t i=0; i < bufferLen; ++i)
  205. {
  206. if (buffer[i] >= 'A' && buffer[i] <= 'Z')
  207. buffer[i] += kCharDiff;
  208. }
  209. }
  210. void toUpper()
  211. {
  212. static const char kCharDiff = 'a' - 'A';
  213. for (size_t i=0; i < bufferLen; ++i)
  214. {
  215. if (buffer[i] >= 'a' && buffer[i] <= 'z')
  216. buffer[i] -= kCharDiff;
  217. }
  218. }
  219. // ---------------------------------------------
  220. // public operators
  221. operator const char*() const
  222. {
  223. return buffer;
  224. }
  225. char& operator[](const size_t pos)
  226. {
  227. return buffer[pos];
  228. }
  229. bool operator==(const char* const strBuf) const
  230. {
  231. return (strBuf != nullptr && std::strcmp(buffer, strBuf) == 0);
  232. }
  233. bool operator==(const CarlaString& str) const
  234. {
  235. return operator==(str.buffer);
  236. }
  237. bool operator!=(const char* const strBuf) const
  238. {
  239. return !operator==(strBuf);
  240. }
  241. bool operator!=(const CarlaString& str) const
  242. {
  243. return !operator==(str.buffer);
  244. }
  245. CarlaString& operator=(const char* const strBuf)
  246. {
  247. _dup(strBuf);
  248. return *this;
  249. }
  250. CarlaString& operator=(const CarlaString& str)
  251. {
  252. return operator=(str.buffer);
  253. }
  254. CarlaString& operator+=(const char* const strBuf)
  255. {
  256. const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  257. char newBuf[newBufSize];
  258. std::strcpy(newBuf, buffer);
  259. std::strcat(newBuf, strBuf);
  260. _dup(newBuf, newBufSize-1);
  261. return *this;
  262. }
  263. CarlaString& operator+=(const CarlaString& str)
  264. {
  265. return operator+=(str.buffer);
  266. }
  267. CarlaString operator+(const char* const strBuf)
  268. {
  269. const size_t newBufSize = bufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  270. char newBuf[newBufSize];
  271. std::strcpy(newBuf, buffer);
  272. std::strcat(newBuf, strBuf);
  273. return CarlaString(newBuf);
  274. }
  275. CarlaString operator+(const CarlaString& str)
  276. {
  277. return operator+(str.buffer);
  278. }
  279. // ---------------------------------------------
  280. private:
  281. char* buffer;
  282. size_t bufferLen;
  283. bool firstInit;
  284. void _init()
  285. {
  286. buffer = nullptr;
  287. bufferLen = 0;
  288. firstInit = true;
  289. }
  290. // allocate string strBuf if not null
  291. // size > 0 only if strBuf is valid
  292. void _dup(const char* const strBuf, const size_t size = 0)
  293. {
  294. if (strBuf != nullptr)
  295. {
  296. // don't recreate string if contents match
  297. if (firstInit || std::strcmp(buffer, strBuf) != 0)
  298. {
  299. if (! firstInit)
  300. {
  301. CARLA_ASSERT(buffer != nullptr);
  302. delete[] buffer;
  303. }
  304. bufferLen = (size > 0) ? size : std::strlen(strBuf);
  305. buffer = new char[bufferLen+1];
  306. std::strcpy(buffer, strBuf);
  307. buffer[bufferLen] = '\0';
  308. firstInit = false;
  309. }
  310. }
  311. else
  312. {
  313. CARLA_ASSERT(size == 0);
  314. // don't recreate null string
  315. if (firstInit || bufferLen != 0)
  316. {
  317. if (! firstInit)
  318. {
  319. CARLA_ASSERT(buffer != nullptr);
  320. delete[] buffer;
  321. }
  322. bufferLen = 0;
  323. buffer = new char[1];
  324. buffer[0] = '\0';
  325. firstInit = false;
  326. }
  327. }
  328. }
  329. CARLA_LEAK_DETECTOR(CarlaString)
  330. CARLA_PREVENT_HEAP_ALLOCATION
  331. };
  332. static inline
  333. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  334. {
  335. const char* const strBufBefore = (const char*)strBefore;
  336. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  337. char newBuf[newBufSize];
  338. std::strcpy(newBuf, strBufBefore);
  339. std::strcat(newBuf, strBufAfter);
  340. return CarlaString(newBuf);
  341. }
  342. static inline
  343. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  344. {
  345. const char* const strBufAfter = (const char*)strAfter;
  346. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  347. char newBuf[newBufSize];
  348. std::strcpy(newBuf, strBufBefore);
  349. std::strcat(newBuf, strBufAfter);
  350. return CarlaString(newBuf);
  351. }
  352. // -------------------------------------------------
  353. #endif // __CARLA_UTILS_HPP__