unichar.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/unichar.h
  3. // Purpose: wxUniChar and wxUniCharRef classes
  4. // Author: Vaclav Slavik
  5. // Created: 2007-03-19
  6. // Copyright: (c) 2007 REA Elektronik GmbH
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_UNICHAR_H_
  10. #define _WX_UNICHAR_H_
  11. #include "wx/defs.h"
  12. #include "wx/chartype.h"
  13. #include "wx/stringimpl.h"
  14. class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
  15. class WXDLLIMPEXP_FWD_BASE wxString;
  16. // This class represents single Unicode character. It can be converted to
  17. // and from char or wchar_t and implements commonly used character operations.
  18. class WXDLLIMPEXP_BASE wxUniChar
  19. {
  20. public:
  21. // NB: this is not wchar_t on purpose, it needs to represent the entire
  22. // Unicode code points range and wchar_t may be too small for that
  23. // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
  24. typedef wxUint32 value_type;
  25. wxUniChar() : m_value(0) {}
  26. // Create the character from 8bit character value encoded in the current
  27. // locale's charset.
  28. wxUniChar(char c) { m_value = From8bit(c); }
  29. wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
  30. #define wxUNICHAR_DEFINE_CTOR(type) \
  31. wxUniChar(type c) { m_value = (value_type)c; }
  32. wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_CTOR)
  33. #undef wxUNICHAR_DEFINE_CTOR
  34. wxUniChar(const wxUniCharRef& c);
  35. // Returns Unicode code point value of the character
  36. value_type GetValue() const { return m_value; }
  37. #if wxUSE_UNICODE_UTF8
  38. // buffer for single UTF-8 character
  39. struct Utf8CharBuffer
  40. {
  41. char data[5];
  42. operator const char*() const { return data; }
  43. };
  44. // returns the character encoded as UTF-8
  45. // (NB: implemented in stringops.cpp)
  46. Utf8CharBuffer AsUTF8() const;
  47. #endif // wxUSE_UNICODE_UTF8
  48. // Returns true if the character is an ASCII character:
  49. bool IsAscii() const { return m_value < 0x80; }
  50. // Returns true if the character is representable as a single byte in the
  51. // current locale encoding and return this byte in output argument c (which
  52. // must be non-NULL)
  53. bool GetAsChar(char *c) const
  54. {
  55. #if wxUSE_UNICODE
  56. if ( !IsAscii() )
  57. {
  58. #if !wxUSE_UTF8_LOCALE_ONLY
  59. if ( GetAsHi8bit(m_value, c) )
  60. return true;
  61. #endif // !wxUSE_UTF8_LOCALE_ONLY
  62. return false;
  63. }
  64. #endif // wxUSE_UNICODE
  65. *c = wx_truncate_cast(char, m_value);
  66. return true;
  67. }
  68. // Conversions to char and wchar_t types: all of those are needed to be
  69. // able to pass wxUniChars to verious standard narrow and wide character
  70. // functions
  71. operator char() const { return To8bit(m_value); }
  72. operator unsigned char() const { return (unsigned char)To8bit(m_value); }
  73. #define wxUNICHAR_DEFINE_OPERATOR_PAREN(type) \
  74. operator type() const { return (type)m_value; }
  75. wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_OPERATOR_PAREN)
  76. #undef wxUNICHAR_DEFINE_OPERATOR_PAREN
  77. // We need this operator for the "*p" part of expressions like "for (
  78. // const_iterator p = begin() + nStart; *p; ++p )". In this case,
  79. // compilation would fail without it because the conversion to bool would
  80. // be ambiguous (there are all these int types conversions...). (And adding
  81. // operator unspecified_bool_type() would only makes the ambiguity worse.)
  82. operator bool() const { return m_value != 0; }
  83. bool operator!() const { return !((bool)*this); }
  84. // And this one is needed by some (not all, but not using ifdefs makes the
  85. // code easier) compilers to parse "str[0] && *p" successfully
  86. bool operator&&(bool v) const { return (bool)*this && v; }
  87. // Assignment operators:
  88. wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; }
  89. wxUniChar& operator=(const wxUniCharRef& c);
  90. wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
  91. wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
  92. #define wxUNICHAR_DEFINE_OPERATOR_EQUAL(type) \
  93. wxUniChar& operator=(type c) { m_value = (value_type)c; return *this; }
  94. wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_OPERATOR_EQUAL)
  95. #undef wxUNICHAR_DEFINE_OPERATOR_EQUAL
  96. // Comparison operators:
  97. #define wxDEFINE_UNICHAR_CMP_WITH_INT(T, op) \
  98. bool operator op(T c) const { return m_value op (value_type)c; }
  99. // define the given comparison operator for all the types
  100. #define wxDEFINE_UNICHAR_OPERATOR(op) \
  101. bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
  102. bool operator op(char c) const { return m_value op From8bit(c); } \
  103. bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
  104. wxDO_FOR_INT_TYPES_1(wxDEFINE_UNICHAR_CMP_WITH_INT, op)
  105. wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
  106. #undef wxDEFINE_UNICHAR_OPERATOR
  107. #undef wxDEFINE_UNCHAR_CMP_WITH_INT
  108. // this is needed for expressions like 'Z'-c
  109. int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
  110. int operator-(char c) const { return m_value - From8bit(c); }
  111. int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
  112. int operator-(wchar_t c) const { return m_value - (value_type)c; }
  113. private:
  114. // notice that we implement these functions inline for 7-bit ASCII
  115. // characters purely for performance reasons
  116. static value_type From8bit(char c)
  117. {
  118. #if wxUSE_UNICODE
  119. if ( (unsigned char)c < 0x80 )
  120. return c;
  121. return FromHi8bit(c);
  122. #else
  123. return c;
  124. #endif
  125. }
  126. static char To8bit(value_type c)
  127. {
  128. #if wxUSE_UNICODE
  129. if ( c < 0x80 )
  130. return wx_truncate_cast(char, c);
  131. return ToHi8bit(c);
  132. #else
  133. return wx_truncate_cast(char, c);
  134. #endif
  135. }
  136. // helpers of the functions above called to deal with non-ASCII chars
  137. static value_type FromHi8bit(char c);
  138. static char ToHi8bit(value_type v);
  139. static bool GetAsHi8bit(value_type v, char *c);
  140. private:
  141. value_type m_value;
  142. };
  143. // Writeable reference to a character in wxString.
  144. //
  145. // This class can be used in the same way wxChar is used, except that changing
  146. // its value updates the underlying string object.
  147. class WXDLLIMPEXP_BASE wxUniCharRef
  148. {
  149. private:
  150. typedef wxStringImpl::iterator iterator;
  151. // create the reference
  152. #if wxUSE_UNICODE_UTF8
  153. wxUniCharRef(wxString& str, iterator pos) : m_str(str), m_pos(pos) {}
  154. #else
  155. wxUniCharRef(iterator pos) : m_pos(pos) {}
  156. #endif
  157. public:
  158. // NB: we have to make this public, because we don't have wxString
  159. // declaration available here and so can't declare wxString::iterator
  160. // as friend; so at least don't use a ctor but a static function
  161. // that must be used explicitly (this is more than using 'explicit'
  162. // keyword on ctor!):
  163. #if wxUSE_UNICODE_UTF8
  164. static wxUniCharRef CreateForString(wxString& str, iterator pos)
  165. { return wxUniCharRef(str, pos); }
  166. #else
  167. static wxUniCharRef CreateForString(iterator pos)
  168. { return wxUniCharRef(pos); }
  169. #endif
  170. wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
  171. #if wxUSE_UNICODE_UTF8
  172. wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
  173. #endif // wxUSE_UNICODE_UTF8
  174. bool IsAscii() const { return UniChar().IsAscii(); }
  175. bool GetAsChar(char *c) const { return UniChar().GetAsChar(c); }
  176. // Assignment operators:
  177. #if wxUSE_UNICODE_UTF8
  178. wxUniCharRef& operator=(const wxUniChar& c);
  179. #else
  180. wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
  181. #endif
  182. wxUniCharRef& operator=(const wxUniCharRef& c)
  183. { if (&c != this) *this = c.UniChar(); return *this; }
  184. #define wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL(type) \
  185. wxUniCharRef& operator=(type c) { return *this = wxUniChar(c); }
  186. wxDO_FOR_CHAR_INT_TYPES(wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL)
  187. #undef wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL
  188. // Conversions to the same types as wxUniChar is convertible too:
  189. #define wxUNICHAR_REF_DEFINE_OPERATOR_PAREN(type) \
  190. operator type() const { return UniChar(); }
  191. wxDO_FOR_CHAR_INT_TYPES(wxUNICHAR_REF_DEFINE_OPERATOR_PAREN)
  192. #undef wxUNICHAR_REF_DEFINE_OPERATOR_PAREN
  193. // see wxUniChar::operator bool etc. for explanation
  194. operator bool() const { return (bool)UniChar(); }
  195. bool operator!() const { return !UniChar(); }
  196. bool operator&&(bool v) const { return UniChar() && v; }
  197. #define wxDEFINE_UNICHARREF_CMP_WITH_INT(T, op) \
  198. bool operator op(T c) const { return UniChar() op c; }
  199. // Comparison operators:
  200. #define wxDEFINE_UNICHARREF_OPERATOR(op) \
  201. bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
  202. bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
  203. wxDO_FOR_CHAR_INT_TYPES_1(wxDEFINE_UNICHARREF_CMP_WITH_INT, op)
  204. wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
  205. #undef wxDEFINE_UNICHARREF_OPERATOR
  206. #undef wxDEFINE_UNICHARREF_CMP_WITH_INT
  207. // for expressions like c-'A':
  208. int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
  209. int operator-(const wxUniChar& c) const { return UniChar() - c; }
  210. int operator-(char c) const { return UniChar() - c; }
  211. int operator-(unsigned char c) const { return UniChar() - c; }
  212. int operator-(wchar_t c) const { return UniChar() - c; }
  213. private:
  214. #if wxUSE_UNICODE_UTF8
  215. wxUniChar UniChar() const;
  216. #else
  217. wxUniChar UniChar() const { return *m_pos; }
  218. #endif
  219. friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
  220. private:
  221. // reference to the string and pointer to the character in string
  222. #if wxUSE_UNICODE_UTF8
  223. wxString& m_str;
  224. #endif
  225. iterator m_pos;
  226. };
  227. inline wxUniChar::wxUniChar(const wxUniCharRef& c)
  228. {
  229. m_value = c.UniChar().m_value;
  230. }
  231. inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
  232. {
  233. m_value = c.UniChar().m_value;
  234. return *this;
  235. }
  236. // Comparison operators for the case when wxUniChar(Ref) is the second operand
  237. // implemented in terms of member comparison functions
  238. wxDEFINE_COMPARISONS_BY_REV(char, const wxUniChar&)
  239. wxDEFINE_COMPARISONS_BY_REV(char, const wxUniCharRef&)
  240. wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniChar&)
  241. wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniCharRef&)
  242. wxDEFINE_COMPARISONS_BY_REV(const wxUniChar&, const wxUniCharRef&)
  243. // for expressions like c-'A':
  244. inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
  245. inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
  246. inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
  247. #endif /* _WX_UNICHAR_H_ */