The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

460 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_CHARPOINTER_UTF16_JUCEHEADER__
  19. #define __JUCE_CHARPOINTER_UTF16_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Wraps a pointer to a null-terminated UTF-16 character string, and provides
  23. various methods to operate on the data.
  24. @see CharPointer_UTF8, CharPointer_UTF32
  25. */
  26. class CharPointer_UTF16
  27. {
  28. public:
  29. #if JUCE_NATIVE_WCHAR_IS_UTF16
  30. typedef wchar_t CharType;
  31. #else
  32. typedef int16 CharType;
  33. #endif
  34. inline explicit CharPointer_UTF16 (const CharType* const rawPointer) throw()
  35. : data (const_cast <CharType*> (rawPointer))
  36. {
  37. }
  38. inline CharPointer_UTF16 (const CharPointer_UTF16& other) throw()
  39. : data (other.data)
  40. {
  41. }
  42. inline CharPointer_UTF16& operator= (const CharPointer_UTF16& other) throw()
  43. {
  44. data = other.data;
  45. return *this;
  46. }
  47. inline CharPointer_UTF16& operator= (const CharType* text) throw()
  48. {
  49. data = const_cast <CharType*> (text);
  50. return *this;
  51. }
  52. /** This is a pointer comparison, it doesn't compare the actual text. */
  53. inline bool operator== (const CharPointer_UTF16& other) const throw()
  54. {
  55. return data == other.data;
  56. }
  57. /** This is a pointer comparison, it doesn't compare the actual text. */
  58. inline bool operator!= (const CharPointer_UTF16& other) const throw()
  59. {
  60. return data == other.data;
  61. }
  62. /** Returns the address that this pointer is pointing to. */
  63. inline CharType* getAddress() const throw() { return data; }
  64. /** Returns the address that this pointer is pointing to. */
  65. inline operator const CharType*() const throw() { return data; }
  66. /** Returns true if this pointer is pointing to a null character. */
  67. inline bool isEmpty() const throw() { return *data == 0; }
  68. /** Returns the unicode character that this pointer is pointing to. */
  69. juce_wchar operator*() const throw()
  70. {
  71. uint32 n = (uint32) (uint16) *data;
  72. if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) data[1]) >= 0xdc00)
  73. n = 0x10000 + (((n - 0xd800) << 10) | (((uint32) (uint16) data[1]) - 0xdc00));
  74. return (juce_wchar) n;
  75. }
  76. /** Moves this pointer along to the next character in the string. */
  77. CharPointer_UTF16& operator++() throw()
  78. {
  79. const juce_wchar n = *data++;
  80. if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) *data) >= 0xdc00)
  81. ++data;
  82. return *this;
  83. }
  84. /** Returns the character that this pointer is currently pointing to, and then
  85. advances the pointer to point to the next character. */
  86. juce_wchar getAndAdvance() throw()
  87. {
  88. uint32 n = (uint32) (uint16) *data++;
  89. if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) *data) >= 0xdc00)
  90. n = 0x10000 + ((((n - 0xd800) << 10) | (((uint32) (uint16) *data++) - 0xdc00)));
  91. return (juce_wchar) n;
  92. }
  93. /** Moves this pointer along to the next character in the string. */
  94. CharPointer_UTF16 operator++ (int) throw()
  95. {
  96. CharPointer_UTF16 temp (*this);
  97. ++*this;
  98. return temp;
  99. }
  100. /** Moves this pointer forwards by the specified number of characters. */
  101. void operator+= (int numToSkip) throw()
  102. {
  103. jassert (numToSkip >= 0);
  104. while (--numToSkip >= 0)
  105. ++*this;
  106. }
  107. /** Returns the character at a given character index from the start of the string. */
  108. juce_wchar operator[] (const int characterIndex) const throw()
  109. {
  110. CharPointer_UTF16 p (*this);
  111. p += characterIndex;
  112. return *p;
  113. }
  114. /** Returns a pointer which is moved forwards from this one by the specified number of characters. */
  115. CharPointer_UTF16 operator+ (const int numToSkip) const throw()
  116. {
  117. CharPointer_UTF16 p (*this);
  118. p += numToSkip;
  119. return p;
  120. }
  121. /** Writes a unicode character to this string, and advances this pointer to point to the next position. */
  122. void write (juce_wchar charToWrite) throw()
  123. {
  124. if (charToWrite >= 0x10000)
  125. {
  126. charToWrite -= 0x10000;
  127. *data++ = (CharType) (0xd800 + (charToWrite >> 10));
  128. *data++ = (CharType) (0xdc00 + (charToWrite & 0x3ff));
  129. }
  130. else
  131. {
  132. *data++ = (CharType) charToWrite;
  133. }
  134. }
  135. /** Writes a null character to this string (leaving the pointer's position unchanged). */
  136. inline void writeNull() const throw()
  137. {
  138. *data = 0;
  139. }
  140. /** Returns the number of characters in this string. */
  141. size_t length() const throw()
  142. {
  143. const CharType* d = data;
  144. size_t count = 0;
  145. for (;;)
  146. {
  147. const int n = *d++;
  148. if (n >= 0xd800 && n <= 0xdfff)
  149. {
  150. if (*d++ == 0)
  151. break;
  152. }
  153. else if (n == 0)
  154. break;
  155. ++count;
  156. }
  157. return count;
  158. }
  159. /** Returns the number of characters in this string, or the given value, whichever is lower. */
  160. size_t lengthUpTo (const size_t maxCharsToCount) const throw()
  161. {
  162. return CharacterFunctions::lengthUpTo (*this, maxCharsToCount);
  163. }
  164. /** Returns the number of bytes that are used to represent this string.
  165. This includes the terminating null character.
  166. */
  167. size_t sizeInBytes() const throw()
  168. {
  169. return sizeof (CharType) * (findNullIndex (data) + 1);
  170. }
  171. /** Returns the number of bytes that would be needed to represent the given
  172. unicode character in this encoding format.
  173. */
  174. static size_t getBytesRequiredFor (const juce_wchar charToWrite) throw()
  175. {
  176. return (charToWrite >= 0x10000) ? (sizeof (CharType) * 2) : sizeof (CharType);
  177. }
  178. /** Returns the number of bytes that would be needed to represent the given
  179. string in this encoding format.
  180. The value returned does NOT include the terminating null character.
  181. */
  182. template <class CharPointer>
  183. static size_t getBytesRequiredFor (CharPointer text) throw()
  184. {
  185. size_t count = 0;
  186. juce_wchar n;
  187. while ((n = text.getAndAdvance()) != 0)
  188. count += getBytesRequiredFor (n);
  189. return count;
  190. }
  191. /** Returns a pointer to the null character that terminates this string. */
  192. CharPointer_UTF16 findTerminatingNull() const throw()
  193. {
  194. const CharType* t = data;
  195. while (*t != 0)
  196. ++t;
  197. return CharPointer_UTF16 (t);
  198. }
  199. /** Copies a source string to this pointer, advancing this pointer as it goes. */
  200. template <typename CharPointer>
  201. void writeAll (const CharPointer& src) throw()
  202. {
  203. CharacterFunctions::copyAll (*this, src);
  204. }
  205. /** Copies a source string to this pointer, advancing this pointer as it goes. */
  206. void writeAll (const CharPointer_UTF16& src) throw()
  207. {
  208. const CharType* s = src.data;
  209. while ((*data = *s) != 0)
  210. {
  211. ++data;
  212. ++s;
  213. }
  214. }
  215. /** Copies a source string to this pointer, advancing this pointer as it goes.
  216. The maxDestBytes parameter specifies the maximum number of bytes that can be written
  217. to the destination buffer before stopping.
  218. */
  219. template <typename CharPointer>
  220. int writeWithDestByteLimit (const CharPointer& src, const int maxDestBytes) throw()
  221. {
  222. return CharacterFunctions::copyWithDestByteLimit (*this, src, maxDestBytes);
  223. }
  224. /** Copies a source string to this pointer, advancing this pointer as it goes.
  225. The maxChars parameter specifies the maximum number of characters that can be
  226. written to the destination buffer before stopping (including the terminating null).
  227. */
  228. template <typename CharPointer>
  229. void writeWithCharLimit (const CharPointer& src, const int maxChars) throw()
  230. {
  231. CharacterFunctions::copyWithCharLimit (*this, src, maxChars);
  232. }
  233. /** Compares this string with another one. */
  234. template <typename CharPointer>
  235. int compare (const CharPointer& other) const throw()
  236. {
  237. return CharacterFunctions::compare (*this, other);
  238. }
  239. /** Compares this string with another one, up to a specified number of characters. */
  240. template <typename CharPointer>
  241. int compareUpTo (const CharPointer& other, const int maxChars) const throw()
  242. {
  243. return CharacterFunctions::compareUpTo (*this, other, maxChars);
  244. }
  245. /** Compares this string with another one. */
  246. template <typename CharPointer>
  247. int compareIgnoreCase (const CharPointer& other) const throw()
  248. {
  249. return CharacterFunctions::compareIgnoreCase (*this, other);
  250. }
  251. /** Compares this string with another one, up to a specified number of characters. */
  252. template <typename CharPointer>
  253. int compareIgnoreCaseUpTo (const CharPointer& other, const int maxChars) const throw()
  254. {
  255. return CharacterFunctions::compareIgnoreCaseUpTo (*this, other, maxChars);
  256. }
  257. #if JUCE_WINDOWS && ! DOXYGEN
  258. int compareIgnoreCase (const CharPointer_UTF16& other) const throw()
  259. {
  260. return _wcsicmp (data, other.data);
  261. }
  262. int compareIgnoreCaseUpTo (const CharPointer_UTF16& other, int maxChars) const throw()
  263. {
  264. return _wcsnicmp (data, other.data, maxChars);
  265. }
  266. int indexOf (const CharPointer_UTF16& stringToFind) const throw()
  267. {
  268. const CharType* const t = wcsstr (data, stringToFind.getAddress());
  269. return t == 0 ? -1 : (int) (t - data);
  270. }
  271. #endif
  272. /** Returns the character index of a substring, or -1 if it isn't found. */
  273. template <typename CharPointer>
  274. int indexOf (const CharPointer& stringToFind) const throw()
  275. {
  276. return CharacterFunctions::indexOf (*this, stringToFind);
  277. }
  278. /** Returns the character index of a unicode character, or -1 if it isn't found. */
  279. int indexOf (const juce_wchar charToFind) const throw()
  280. {
  281. return CharacterFunctions::indexOfChar (*this, charToFind);
  282. }
  283. /** Returns the character index of a unicode character, or -1 if it isn't found. */
  284. int indexOf (const juce_wchar charToFind, const bool ignoreCase) const throw()
  285. {
  286. return ignoreCase ? CharacterFunctions::indexOfCharIgnoreCase (*this, charToFind)
  287. : CharacterFunctions::indexOfChar (*this, charToFind);
  288. }
  289. /** Returns true if the first character of this string is whitespace. */
  290. bool isWhitespace() const throw() { return CharacterFunctions::isWhitespace (operator*()) != 0; }
  291. /** Returns true if the first character of this string is a digit. */
  292. bool isDigit() const throw() { return CharacterFunctions::isDigit (operator*()) != 0; }
  293. /** Returns true if the first character of this string is a letter. */
  294. bool isLetter() const throw() { return CharacterFunctions::isLetter (operator*()) != 0; }
  295. /** Returns true if the first character of this string is a letter or digit. */
  296. bool isLetterOrDigit() const throw() { return CharacterFunctions::isLetterOrDigit (operator*()) != 0; }
  297. /** Returns true if the first character of this string is upper-case. */
  298. bool isUpperCase() const throw() { return CharacterFunctions::isUpperCase (operator*()) != 0; }
  299. /** Returns true if the first character of this string is lower-case. */
  300. bool isLowerCase() const throw() { return CharacterFunctions::isLowerCase (operator*()) != 0; }
  301. /** Returns an upper-case version of the first character of this string. */
  302. juce_wchar toUpperCase() const throw() { return CharacterFunctions::toUpperCase (operator*()); }
  303. /** Returns a lower-case version of the first character of this string. */
  304. juce_wchar toLowerCase() const throw() { return CharacterFunctions::toLowerCase (operator*()); }
  305. /** Parses this string as a 32-bit integer. */
  306. int getIntValue32() const throw()
  307. {
  308. #if JUCE_WINDOWS
  309. return _wtoi (data);
  310. #else
  311. return CharacterFunctions::getIntValue <int, CharPointer_UTF16> (*this);
  312. #endif
  313. }
  314. /** Parses this string as a 64-bit integer. */
  315. int64 getIntValue64() const throw()
  316. {
  317. #if JUCE_WINDOWS
  318. return _wtoi64 (data);
  319. #else
  320. return CharacterFunctions::getIntValue <int64, CharPointer_UTF16> (*this);
  321. #endif
  322. }
  323. /** Parses this string as a floating point double. */
  324. double getDoubleValue() const throw() { return CharacterFunctions::getDoubleValue (*this); }
  325. /** Returns the first non-whitespace character in the string. */
  326. CharPointer_UTF16 findEndOfWhitespace() const throw() { return CharacterFunctions::findEndOfWhitespace (*this); }
  327. /** Returns true if the given unicode character can be represented in this encoding. */
  328. static bool canRepresent (juce_wchar character) throw()
  329. {
  330. return ((unsigned int) character) < (unsigned int) 0x10ffff
  331. && (((unsigned int) character) < 0xd800 || ((unsigned int) character) > 0xdfff);
  332. }
  333. /** Returns true if this data contains a valid string in this encoding. */
  334. static bool isValidString (const CharType* dataToTest, int maxBytesToRead)
  335. {
  336. maxBytesToRead /= sizeof (CharType);
  337. while (--maxBytesToRead >= 0 && *dataToTest != 0)
  338. {
  339. const uint32 n = (uint32) (uint16) *dataToTest++;
  340. if (n >= 0xd800)
  341. {
  342. if (n > 0x10ffff)
  343. return false;
  344. if (n <= 0xdfff)
  345. {
  346. if (n > 0xdc00)
  347. return false;
  348. const uint32 nextChar = (uint32) (uint16) *dataToTest++;
  349. if (nextChar < 0xdc00 || nextChar > 0xdfff)
  350. return false;
  351. }
  352. }
  353. }
  354. return true;
  355. }
  356. /** These values are the byte-order-mark (BOM) values for a UTF-16 stream. */
  357. enum
  358. {
  359. byteOrderMarkBE1 = 0xfe,
  360. byteOrderMarkBE2 = 0xff,
  361. byteOrderMarkLE1 = 0xff,
  362. byteOrderMarkLE2 = 0xfe
  363. };
  364. private:
  365. CharType* data;
  366. static int findNullIndex (const CharType* const t) throw()
  367. {
  368. int n = 0;
  369. while (t[n] != 0)
  370. ++n;
  371. return n;
  372. }
  373. };
  374. #endif // __JUCE_CHARPOINTER_UTF16_JUCEHEADER__