_ctype.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. // WARNING: This is an internal header file, included by other C++
  19. // standard library headers. You should not attempt to use this header
  20. // file directly.
  21. #ifndef _STLP_INTERNAL_CTYPE_H
  22. #define _STLP_INTERNAL_CTYPE_H
  23. #ifndef _STLP_C_LOCALE_H
  24. # include <stl/c_locale.h>
  25. #endif
  26. #ifndef _STLP_INTERNAL_LOCALE_H
  27. # include <stl/_locale.h>
  28. #endif
  29. #ifndef _STLP_INTERNAL_ALGOBASE_H
  30. # include <stl/_algobase.h>
  31. #endif
  32. _STLP_BEGIN_NAMESPACE
  33. class _STLP_CLASS_DECLSPEC ctype_base {
  34. public:
  35. enum mask {
  36. space = _Locale_SPACE,
  37. print = _Locale_PRINT,
  38. cntrl = _Locale_CNTRL,
  39. upper = _Locale_UPPER,
  40. lower = _Locale_LOWER,
  41. alpha = _Locale_ALPHA,
  42. digit = _Locale_DIGIT,
  43. punct = _Locale_PUNCT,
  44. xdigit = _Locale_XDIGIT,
  45. alnum = alpha | digit,
  46. graph = alnum | punct
  47. };
  48. };
  49. // ctype<> template
  50. template <class charT> class ctype {};
  51. template <class charT> class ctype_byname {};
  52. //ctype specializations
  53. _STLP_TEMPLATE_NULL
  54. class _STLP_CLASS_DECLSPEC ctype<char> : public locale::facet, public ctype_base {
  55. #ifndef _STLP_NO_WCHAR_T
  56. # ifdef _STLP_MSVC
  57. typedef ctype<wchar_t> _Wctype;
  58. friend _Wctype;
  59. # else
  60. friend class ctype<wchar_t>;
  61. # endif
  62. #endif
  63. public:
  64. typedef char char_type;
  65. explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
  66. bool is(mask __m, char __c) const
  67. { return ((*(_M_ctype_table+(unsigned char)__c)) & __m) != 0; }
  68. const char* is(const char* __low, const char* __high, mask* __vec) const {
  69. for (const char* __p = __low;__p != __high; ++__p, ++__vec) {
  70. *__vec = _M_ctype_table[(unsigned char)*__p];
  71. }
  72. return __high;
  73. }
  74. const char* scan_is(mask __m, const char* __low, const char* __high) const;
  75. const char* scan_not(mask __m, const char* __low, const char* __high) const;
  76. char (toupper)(char __c) const { return do_toupper(__c); }
  77. const char* (toupper)(char* __low, const char* __high) const {
  78. return do_toupper(__low, __high);
  79. }
  80. char (tolower)(char __c) const { return do_tolower(__c); }
  81. const char* (tolower)(char* __low, const char* __high) const {
  82. return do_tolower(__low, __high);
  83. }
  84. char widen(char __c) const { return do_widen(__c); }
  85. const char* widen(const char* __low, const char* __high, char* __to) const {
  86. return do_widen(__low, __high, __to);
  87. }
  88. char narrow(char __c, char __dfault) const {
  89. return do_narrow(__c, __dfault);
  90. }
  91. const char* narrow(const char* __low, const char* __high,
  92. char __dfault, char* __to) const {
  93. return do_narrow(__low, __high, __dfault, __to);
  94. }
  95. static _STLP_STATIC_DECLSPEC locale::id id;
  96. _STLP_STATIC_CONSTANT(size_t, table_size = 256);
  97. protected:
  98. const mask* table() const _STLP_NOTHROW { return _M_ctype_table; }
  99. static const mask* _STLP_CALL classic_table() _STLP_NOTHROW;
  100. ~ctype();
  101. virtual char do_toupper(char __c) const;
  102. virtual char do_tolower(char __c) const;
  103. virtual const char* do_toupper(char* __low, const char* __high) const;
  104. virtual const char* do_tolower(char* __low, const char* __high) const;
  105. virtual char do_widen(char __c) const;
  106. virtual const char* do_widen(const char* __low, const char* __high,
  107. char* __to) const;
  108. virtual char do_narrow(char __c, char /* dfault */ ) const;
  109. virtual const char* do_narrow(const char* __low, const char* __high,
  110. char /* dfault */, char* __to) const;
  111. private:
  112. struct _Is_mask {
  113. mask __m;
  114. _Is_mask(mask __x): __m(__x) {}
  115. bool operator()(char __c) {return (__m & (unsigned char) __c) != 0;}
  116. };
  117. protected:
  118. const mask* _M_ctype_table;
  119. private:
  120. bool _M_delete;
  121. };
  122. _STLP_TEMPLATE_NULL
  123. class _STLP_CLASS_DECLSPEC ctype_byname<char>: public ctype<char> {
  124. friend class _Locale_impl;
  125. public:
  126. explicit ctype_byname(const char*, size_t = 0);
  127. ~ctype_byname();
  128. virtual char do_toupper(char __c) const;
  129. virtual char do_tolower(char __c) const;
  130. virtual const char* do_toupper(char*, const char*) const;
  131. virtual const char* do_tolower(char*, const char*) const;
  132. private:
  133. ctype_byname(_Locale_ctype* __ctype)
  134. : _M_ctype(__ctype)
  135. { _M_init(); }
  136. void _M_init();
  137. //explicitely defined as private to avoid warnings:
  138. typedef ctype_byname<char> _Self;
  139. ctype_byname(_Self const&);
  140. _Self& operator = (_Self const&);
  141. mask _M_byname_table[table_size];
  142. _Locale_ctype* _M_ctype;
  143. };
  144. # ifndef _STLP_NO_WCHAR_T
  145. _STLP_TEMPLATE_NULL
  146. class _STLP_CLASS_DECLSPEC ctype<wchar_t> : public locale::facet, public ctype_base {
  147. public:
  148. typedef wchar_t char_type;
  149. explicit ctype(size_t __refs = 0) : locale::facet(__refs) {}
  150. bool is(mask __m, wchar_t __c) const
  151. { return do_is(__m, __c); }
  152. const wchar_t* is(const wchar_t* __low, const wchar_t* __high,
  153. mask* __vec) const
  154. { return do_is(__low, __high, __vec); }
  155. const wchar_t* scan_is(mask __m,
  156. const wchar_t* __low, const wchar_t* __high) const
  157. { return do_scan_is(__m, __low, __high); }
  158. const wchar_t* scan_not (mask __m,
  159. const wchar_t* __low, const wchar_t* __high) const
  160. { return do_scan_not(__m, __low, __high); }
  161. wchar_t (toupper)(wchar_t __c) const { return do_toupper(__c); }
  162. const wchar_t* (toupper)(wchar_t* __low, const wchar_t* __high) const
  163. { return do_toupper(__low, __high); }
  164. wchar_t (tolower)(wchar_t __c) const { return do_tolower(__c); }
  165. const wchar_t* (tolower)(wchar_t* __low, const wchar_t* __high) const
  166. { return do_tolower(__low, __high); }
  167. wchar_t widen(char __c) const { return do_widen(__c); }
  168. const char* widen(const char* __low, const char* __high,
  169. wchar_t* __to) const
  170. { return do_widen(__low, __high, __to); }
  171. char narrow(wchar_t __c, char __dfault) const
  172. { return do_narrow(__c, __dfault); }
  173. const wchar_t* narrow(const wchar_t* __low, const wchar_t* __high,
  174. char __dfault, char* __to) const
  175. { return do_narrow(__low, __high, __dfault, __to); }
  176. static _STLP_STATIC_DECLSPEC locale::id id;
  177. protected:
  178. ~ctype();
  179. virtual bool do_is(mask __m, wchar_t __c) const;
  180. virtual const wchar_t* do_is(const wchar_t*, const wchar_t*, mask*) const;
  181. virtual const wchar_t* do_scan_is(mask,
  182. const wchar_t*, const wchar_t*) const;
  183. virtual const wchar_t* do_scan_not(mask,
  184. const wchar_t*, const wchar_t*) const;
  185. virtual wchar_t do_toupper(wchar_t __c) const;
  186. virtual const wchar_t* do_toupper(wchar_t*, const wchar_t*) const;
  187. virtual wchar_t do_tolower(wchar_t c) const;
  188. virtual const wchar_t* do_tolower(wchar_t*, const wchar_t*) const;
  189. virtual wchar_t do_widen(char c) const;
  190. virtual const char* do_widen(const char*, const char*, wchar_t*) const;
  191. virtual char do_narrow(wchar_t __c, char __dfault) const;
  192. virtual const wchar_t* do_narrow(const wchar_t*, const wchar_t*,
  193. char, char*) const;
  194. };
  195. _STLP_TEMPLATE_NULL
  196. class _STLP_CLASS_DECLSPEC ctype_byname<wchar_t>: public ctype<wchar_t> {
  197. friend class _Locale_impl;
  198. public:
  199. explicit ctype_byname(const char* __name, size_t __refs = 0);
  200. protected:
  201. ~ctype_byname();
  202. virtual bool do_is(mask __m, wchar_t __c) const;
  203. virtual const wchar_t* do_is(const wchar_t*, const wchar_t*, mask*) const;
  204. virtual const wchar_t* do_scan_is(mask,
  205. const wchar_t*, const wchar_t*) const;
  206. virtual const wchar_t* do_scan_not(mask,
  207. const wchar_t*, const wchar_t*) const;
  208. virtual wchar_t do_toupper(wchar_t __c) const;
  209. virtual const wchar_t* do_toupper(wchar_t*, const wchar_t*) const;
  210. virtual wchar_t do_tolower(wchar_t c) const;
  211. virtual const wchar_t* do_tolower(wchar_t*, const wchar_t*) const;
  212. private:
  213. ctype_byname(_Locale_ctype* __ctype)
  214. : _M_ctype(__ctype) {}
  215. //explicitely defined as private to avoid warnings:
  216. typedef ctype_byname<wchar_t> _Self;
  217. ctype_byname(_Self const&);
  218. _Self& operator = (_Self const&);
  219. _Locale_ctype* _M_ctype;
  220. };
  221. # endif /* WCHAR_T */
  222. _STLP_END_NAMESPACE
  223. #endif /* _STLP_INTERNAL_CTYPE_H */
  224. // Local Variables:
  225. // mode:C++
  226. // End: