standard.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_STANDARD_APRIL_26_2006_1106PM)
  8. #define BOOST_SPIRIT_STANDARD_APRIL_26_2006_1106PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <cctype>
  13. #include <climits>
  14. #include <boost/assert.hpp>
  15. #include <boost/cstdint.hpp>
  16. namespace boost { namespace spirit { namespace char_encoding
  17. {
  18. ///////////////////////////////////////////////////////////////////////////
  19. // Test characters for specified conditions (using std functions)
  20. ///////////////////////////////////////////////////////////////////////////
  21. struct standard
  22. {
  23. typedef char char_type;
  24. typedef unsigned char classify_type;
  25. static bool
  26. isascii_(int ch)
  27. {
  28. return 0 == (ch & ~0x7f);
  29. }
  30. static bool
  31. ischar(int ch)
  32. {
  33. // uses all 8 bits
  34. // we have to watch out for sign extensions
  35. return (0 == (ch & ~0xff) || ~0 == (ch | 0xff)) != 0;
  36. }
  37. // *** Note on assertions: The precondition is that the calls to
  38. // these functions do not violate the required range of ch (int)
  39. // which is that strict_ischar(ch) should be true. It is the
  40. // responsibility of the caller to make sure this precondition is not
  41. // violated.
  42. static bool
  43. strict_ischar(int ch)
  44. {
  45. // ch should be representable as an unsigned char
  46. return ch >= 0 && ch <= UCHAR_MAX;
  47. }
  48. static bool
  49. isalnum(int ch)
  50. {
  51. BOOST_ASSERT(strict_ischar(ch));
  52. return std::isalnum(ch) != 0;
  53. }
  54. static bool
  55. isalpha(int ch)
  56. {
  57. BOOST_ASSERT(strict_ischar(ch));
  58. return std::isalpha(ch) != 0;
  59. }
  60. static bool
  61. isdigit(int ch)
  62. {
  63. BOOST_ASSERT(strict_ischar(ch));
  64. return std::isdigit(ch) != 0;
  65. }
  66. static bool
  67. isxdigit(int ch)
  68. {
  69. BOOST_ASSERT(strict_ischar(ch));
  70. return std::isxdigit(ch) != 0;
  71. }
  72. static bool
  73. iscntrl(int ch)
  74. {
  75. BOOST_ASSERT(strict_ischar(ch));
  76. return std::iscntrl(ch) != 0;
  77. }
  78. static bool
  79. isgraph(int ch)
  80. {
  81. BOOST_ASSERT(strict_ischar(ch));
  82. return std::isgraph(ch) != 0;
  83. }
  84. static bool
  85. islower(int ch)
  86. {
  87. BOOST_ASSERT(strict_ischar(ch));
  88. return std::islower(ch) != 0;
  89. }
  90. static bool
  91. isprint(int ch)
  92. {
  93. BOOST_ASSERT(strict_ischar(ch));
  94. return std::isprint(ch) != 0;
  95. }
  96. static bool
  97. ispunct(int ch)
  98. {
  99. BOOST_ASSERT(strict_ischar(ch));
  100. return std::ispunct(ch) != 0;
  101. }
  102. static bool
  103. isspace(int ch)
  104. {
  105. BOOST_ASSERT(strict_ischar(ch));
  106. return std::isspace(ch) != 0;
  107. }
  108. static bool
  109. isblank BOOST_PREVENT_MACRO_SUBSTITUTION (int ch)
  110. {
  111. BOOST_ASSERT(strict_ischar(ch));
  112. return (ch == ' ' || ch == '\t');
  113. }
  114. static bool
  115. isupper(int ch)
  116. {
  117. BOOST_ASSERT(strict_ischar(ch));
  118. return std::isupper(ch) != 0;
  119. }
  120. ///////////////////////////////////////////////////////////////////////////////
  121. // Simple character conversions
  122. ///////////////////////////////////////////////////////////////////////////////
  123. static int
  124. tolower(int ch)
  125. {
  126. BOOST_ASSERT(strict_ischar(ch));
  127. return std::tolower(ch);
  128. }
  129. static int
  130. toupper(int ch)
  131. {
  132. BOOST_ASSERT(strict_ischar(ch));
  133. return std::toupper(ch);
  134. }
  135. static ::boost::uint32_t
  136. toucs4(int ch)
  137. {
  138. BOOST_ASSERT(strict_ischar(ch));
  139. return ch;
  140. }
  141. };
  142. }}}
  143. #endif