standard.hpp 4.3 KB

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