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.

498 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_zeroChar(strBuf, 0xff+1);
  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_zeroChar(strBuf, 0xff+1);
  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_zeroChar(strBuf, 0xff+1);
  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_zeroChar(strBuf, 0xff+1);
  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_zeroChar(strBuf, 0xff+1);
  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_zeroChar(strBuf, 0xff+1);
  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_SAFE_ASSERT_RETURN(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. CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr, false)
  123. if (ignoreCase)
  124. return (strcasestr(fBuffer, strBuf) != nullptr);
  125. else
  126. return (std::strstr(fBuffer, strBuf) != nullptr);
  127. }
  128. bool contains(const CarlaString& str, const bool ignoreCase = false) const
  129. {
  130. return contains(str.fBuffer, ignoreCase);
  131. }
  132. #else
  133. bool contains(const char* const strBuf) const
  134. {
  135. CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr, false)
  136. return (std::strstr(fBuffer, strBuf) != nullptr);
  137. }
  138. bool contains(const CarlaString& str) const
  139. {
  140. return contains(str.fBuffer);
  141. }
  142. #endif
  143. bool isDigit(const size_t pos) const noexcept
  144. {
  145. CARLA_SAFE_ASSERT_RETURN(pos < fBufferLen, false)
  146. return (fBuffer[pos] >= '0' && fBuffer[pos] <= '9');
  147. }
  148. bool startsWith(const char* const prefix) const
  149. {
  150. CARLA_SAFE_ASSERT_RETURN(prefix != nullptr, false)
  151. const size_t prefixLen(std::strlen(prefix));
  152. if (fBufferLen < prefixLen)
  153. return false;
  154. return (std::strncmp(fBuffer + (fBufferLen-prefixLen), prefix, prefixLen) == 0);
  155. }
  156. bool endsWith(const char* const suffix) const
  157. {
  158. CARLA_SAFE_ASSERT_RETURN(suffix != nullptr, false)
  159. const size_t suffixLen(std::strlen(suffix));
  160. if (fBufferLen < suffixLen)
  161. return false;
  162. return (std::strncmp(fBuffer + (fBufferLen-suffixLen), suffix, suffixLen) == 0);
  163. }
  164. void clear() noexcept
  165. {
  166. truncate(0);
  167. }
  168. size_t find(const char c) const noexcept
  169. {
  170. for (size_t i=0; i < fBufferLen; ++i)
  171. {
  172. if (fBuffer[i] == c)
  173. return i;
  174. }
  175. return 0;
  176. }
  177. size_t rfind(const char c) const noexcept
  178. {
  179. for (size_t i=fBufferLen; i > 0; --i)
  180. {
  181. if (fBuffer[i-1] == c)
  182. return i-1;
  183. }
  184. return 0;
  185. }
  186. size_t rfind(const char* const strBuf) const
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr && strBuf[0] != '\0', fBufferLen)
  189. size_t ret = fBufferLen+1;
  190. const char* tmpBuf = fBuffer;
  191. for (size_t i=0; i < fBufferLen; ++i)
  192. {
  193. if (std::strstr(tmpBuf, strBuf) == nullptr)
  194. break;
  195. --ret;
  196. ++tmpBuf;
  197. }
  198. return (ret > fBufferLen) ? fBufferLen : fBufferLen-ret;
  199. }
  200. void replace(const char before, const char after) noexcept
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(before != '\0' && after != '\0',)
  203. for (size_t i=0; i < fBufferLen; ++i)
  204. {
  205. if (fBuffer[i] == before)
  206. fBuffer[i] = after;
  207. else if (fBuffer[i] == '\0')
  208. break;
  209. }
  210. }
  211. void truncate(const size_t n) noexcept
  212. {
  213. if (n >= fBufferLen)
  214. return;
  215. for (size_t i=n; i < fBufferLen; ++i)
  216. fBuffer[i] = '\0';
  217. fBufferLen = n;
  218. }
  219. void toBasic() noexcept
  220. {
  221. for (size_t i=0; i < fBufferLen; ++i)
  222. {
  223. if (fBuffer[i] >= '0' && fBuffer[i] <= '9')
  224. continue;
  225. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  226. continue;
  227. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  228. continue;
  229. if (fBuffer[i] == '_')
  230. continue;
  231. fBuffer[i] = '_';
  232. }
  233. }
  234. void toLower() noexcept
  235. {
  236. #ifndef BUILD_ANSI_TEST
  237. // Using '+=' temporarily converts char into int
  238. static const char kCharDiff('a' - 'A');
  239. for (size_t i=0; i < fBufferLen; ++i)
  240. {
  241. if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z')
  242. fBuffer[i] += kCharDiff;
  243. }
  244. #endif
  245. }
  246. void toUpper() noexcept
  247. {
  248. #ifndef BUILD_ANSI_TEST
  249. // Using '-=' temporarily converts char into int
  250. static const char kCharDiff('a' - 'A');
  251. for (size_t i=0; i < fBufferLen; ++i)
  252. {
  253. if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z')
  254. fBuffer[i] -= kCharDiff;
  255. }
  256. #endif
  257. }
  258. // -------------------------------------------------------------------
  259. // public operators
  260. operator const char*() const noexcept
  261. {
  262. return fBuffer;
  263. }
  264. char& operator[](const size_t pos) const noexcept
  265. {
  266. return fBuffer[pos];
  267. }
  268. bool operator==(const char* const strBuf) const
  269. {
  270. return (strBuf != nullptr && std::strcmp(fBuffer, strBuf) == 0);
  271. }
  272. bool operator==(const CarlaString& str) const
  273. {
  274. return operator==(str.fBuffer);
  275. }
  276. bool operator!=(const char* const strBuf) const
  277. {
  278. return !operator==(strBuf);
  279. }
  280. bool operator!=(const CarlaString& str) const
  281. {
  282. return !operator==(str.fBuffer);
  283. }
  284. CarlaString& operator=(const char* const strBuf)
  285. {
  286. _dup(strBuf);
  287. return *this;
  288. }
  289. CarlaString& operator=(const CarlaString& str)
  290. {
  291. return operator=(str.fBuffer);
  292. }
  293. CarlaString& operator+=(const char* const strBuf)
  294. {
  295. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  296. char newBuf[newBufSize];
  297. std::strcpy(newBuf, fBuffer);
  298. std::strcat(newBuf, strBuf);
  299. _dup(newBuf, newBufSize-1);
  300. return *this;
  301. }
  302. CarlaString& operator+=(const CarlaString& str)
  303. {
  304. return operator+=(str.fBuffer);
  305. }
  306. CarlaString operator+(const char* const strBuf)
  307. {
  308. const size_t newBufSize = fBufferLen + ((strBuf != nullptr) ? std::strlen(strBuf) : 0) + 1;
  309. char newBuf[newBufSize];
  310. std::strcpy(newBuf, fBuffer);
  311. std::strcat(newBuf, strBuf);
  312. return CarlaString(newBuf);
  313. }
  314. CarlaString operator+(const CarlaString& str)
  315. {
  316. return operator+(str.fBuffer);
  317. }
  318. // -------------------------------------------------------------------
  319. private:
  320. char* fBuffer;
  321. size_t fBufferLen;
  322. bool fFirstInit;
  323. void _init() noexcept
  324. {
  325. fBuffer = nullptr;
  326. fBufferLen = 0;
  327. fFirstInit = true;
  328. }
  329. // allocate string strBuf if not null
  330. // size > 0 only if strBuf is valid
  331. void _dup(const char* const strBuf, const size_t size = 0)
  332. {
  333. if (strBuf != nullptr)
  334. {
  335. // don't recreate string if contents match
  336. if (fFirstInit || std::strcmp(fBuffer, strBuf) != 0)
  337. {
  338. if (! fFirstInit)
  339. {
  340. CARLA_ASSERT(fBuffer != nullptr);
  341. delete[] fBuffer;
  342. }
  343. fBufferLen = (size > 0) ? size : std::strlen(strBuf);
  344. fBuffer = new char[fBufferLen+1];
  345. std::strcpy(fBuffer, strBuf);
  346. fBuffer[fBufferLen] = '\0';
  347. fFirstInit = false;
  348. }
  349. }
  350. else
  351. {
  352. CARLA_ASSERT(size == 0);
  353. // don't recreate null string
  354. if (fFirstInit || fBufferLen != 0)
  355. {
  356. if (! fFirstInit)
  357. {
  358. CARLA_ASSERT(fBuffer != nullptr);
  359. delete[] fBuffer;
  360. }
  361. fBufferLen = 0;
  362. fBuffer = new char[1];
  363. fBuffer[0] = '\0';
  364. fFirstInit = false;
  365. }
  366. }
  367. }
  368. CARLA_PREVENT_HEAP_ALLOCATION
  369. CARLA_LEAK_DETECTOR(CarlaString)
  370. };
  371. // -----------------------------------------------------------------------
  372. static inline
  373. CarlaString operator+(const CarlaString& strBefore, const char* const strBufAfter)
  374. {
  375. const char* const strBufBefore = (const char*)strBefore;
  376. const size_t newBufSize = strBefore.length() + ((strBufAfter != nullptr) ? std::strlen(strBufAfter) : 0) + 1;
  377. char newBuf[newBufSize];
  378. std::strcpy(newBuf, strBufBefore);
  379. std::strcat(newBuf, strBufAfter);
  380. return CarlaString(newBuf);
  381. }
  382. static inline
  383. CarlaString operator+(const char* const strBufBefore, const CarlaString& strAfter)
  384. {
  385. const char* const strBufAfter = (const char*)strAfter;
  386. const size_t newBufSize = ((strBufBefore != nullptr) ? std::strlen(strBufBefore) : 0) + strAfter.length() + 1;
  387. char newBuf[newBufSize];
  388. std::strcpy(newBuf, strBufBefore);
  389. std::strcat(newBuf, strBufAfter);
  390. return CarlaString(newBuf);
  391. }
  392. // -----------------------------------------------------------------------
  393. #endif // CARLA_STRING_HPP_INCLUDED