pod_char_traits.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // POD character, std::char_traits specialization -*- C++ -*-
  2. // Copyright (C) 2002-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/pod_char_traits.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. // Gabriel Dos Reis <gdr@integrable-solutions.net>
  24. // Benjamin Kosnik <bkoz@redhat.com>
  25. #ifndef _POD_CHAR_TRAITS_H
  26. #define _POD_CHAR_TRAITS_H 1
  27. #pragma GCC system_header
  28. #include <string>
  29. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. // POD character abstraction.
  33. // NB: The char_type parameter is a subset of int_type, as to allow
  34. // int_type to properly hold the full range of char_type values as
  35. // well as EOF.
  36. /// @brief A POD class that serves as a character abstraction class.
  37. template<typename _Value, typename _Int, typename _St = std::mbstate_t>
  38. struct character
  39. {
  40. typedef _Value value_type;
  41. typedef _Int int_type;
  42. typedef _St state_type;
  43. typedef character<_Value, _Int, _St> char_type;
  44. value_type value;
  45. template<typename V2>
  46. static char_type
  47. from(const V2& v)
  48. {
  49. char_type ret = { static_cast<value_type>(v) };
  50. return ret;
  51. }
  52. template<typename V2>
  53. static V2
  54. to(const char_type& c)
  55. {
  56. V2 ret = { static_cast<V2>(c.value) };
  57. return ret;
  58. }
  59. };
  60. template<typename _Value, typename _Int, typename _St>
  61. inline bool
  62. operator==(const character<_Value, _Int, _St>& lhs,
  63. const character<_Value, _Int, _St>& rhs)
  64. { return lhs.value == rhs.value; }
  65. template<typename _Value, typename _Int, typename _St>
  66. inline bool
  67. operator<(const character<_Value, _Int, _St>& lhs,
  68. const character<_Value, _Int, _St>& rhs)
  69. { return lhs.value < rhs.value; }
  70. _GLIBCXX_END_NAMESPACE_VERSION
  71. } // namespace
  72. namespace std _GLIBCXX_VISIBILITY(default)
  73. {
  74. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  75. /// char_traits<__gnu_cxx::character> specialization.
  76. template<typename _Value, typename _Int, typename _St>
  77. struct char_traits<__gnu_cxx::character<_Value, _Int, _St> >
  78. {
  79. typedef __gnu_cxx::character<_Value, _Int, _St> char_type;
  80. typedef typename char_type::int_type int_type;
  81. typedef typename char_type::state_type state_type;
  82. typedef fpos<state_type> pos_type;
  83. typedef streamoff off_type;
  84. static void
  85. assign(char_type& __c1, const char_type& __c2)
  86. { __c1 = __c2; }
  87. static bool
  88. eq(const char_type& __c1, const char_type& __c2)
  89. { return __c1 == __c2; }
  90. static bool
  91. lt(const char_type& __c1, const char_type& __c2)
  92. { return __c1 < __c2; }
  93. static int
  94. compare(const char_type* __s1, const char_type* __s2, size_t __n)
  95. {
  96. for (size_t __i = 0; __i < __n; ++__i)
  97. if (!eq(__s1[__i], __s2[__i]))
  98. return lt(__s1[__i], __s2[__i]) ? -1 : 1;
  99. return 0;
  100. }
  101. static size_t
  102. length(const char_type* __s)
  103. {
  104. const char_type* __p = __s;
  105. while (__p->value)
  106. ++__p;
  107. return (__p - __s);
  108. }
  109. static const char_type*
  110. find(const char_type* __s, size_t __n, const char_type& __a)
  111. {
  112. for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
  113. if (*__p == __a)
  114. return __p;
  115. return 0;
  116. }
  117. static char_type*
  118. move(char_type* __s1, const char_type* __s2, size_t __n)
  119. {
  120. if (__n == 0)
  121. return __s1;
  122. return static_cast<char_type*>
  123. (__builtin_memmove(__s1, __s2, __n * sizeof(char_type)));
  124. }
  125. static char_type*
  126. copy(char_type* __s1, const char_type* __s2, size_t __n)
  127. {
  128. if (__n == 0)
  129. return __s1;
  130. std::copy(__s2, __s2 + __n, __s1);
  131. return __s1;
  132. }
  133. static char_type*
  134. assign(char_type* __s, size_t __n, char_type __a)
  135. {
  136. std::fill_n(__s, __n, __a);
  137. return __s;
  138. }
  139. static char_type
  140. to_char_type(const int_type& __i)
  141. { return char_type::template from(__i); }
  142. static int_type
  143. to_int_type(const char_type& __c)
  144. { return char_type::template to<int_type>(__c); }
  145. static bool
  146. eq_int_type(const int_type& __c1, const int_type& __c2)
  147. { return __c1 == __c2; }
  148. static int_type
  149. eof()
  150. {
  151. int_type __r = { static_cast<typename __gnu_cxx::__conditional_type
  152. <std::__is_integer<int_type>::__value,
  153. int_type, int>::__type>(-1) };
  154. return __r;
  155. }
  156. static int_type
  157. not_eof(const int_type& __c)
  158. { return eq_int_type(__c, eof()) ? int_type() : __c; }
  159. };
  160. _GLIBCXX_END_NAMESPACE_VERSION
  161. } // namespace
  162. #endif