1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
4 Grammar for universal character validation (see C++ standard: Annex E)
8 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
9 Software License, Version 1.0. (See accompanying file
10 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 =============================================================================*/
12 #if !defined(CONVERT_TRIGRAPHS_HK050403_INCLUDED)
13 #define CONVERT_TRIGRAPHS_HK050403_INCLUDED
15 #include <boost/wave/wave_config.hpp>
16 #include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
18 // this must occur after all of the includes and before any code appears
19 #ifdef BOOST_HAS_ABI_HEADERS
20 #include BOOST_ABI_PREFIX
23 ///////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////
31 // Test, whether the given string represents a valid trigraph sequence
33 ///////////////////////////////////////////////////////////////////////////////
34 template <typename StringT>
36 is_trigraph(StringT const& trigraph)
38 if (trigraph.size() < 3 || '?' != trigraph[0] || '?' != trigraph[1])
41 switch (trigraph[2]) {
42 case '\'': case '=': case '/': case '(':
43 case ')': case '<': case '>': case '!':
54 ///////////////////////////////////////////////////////////////////////////////
58 // The function convert_trigraph() converts a single trigraph character
59 // sequence into the corresponding character.
61 // If the given character sequence doesn't form a valid trigraph sequence
62 // no conversion is performed.
64 ///////////////////////////////////////////////////////////////////////////////
65 template <typename StringT>
67 convert_trigraph(StringT const &trigraph)
69 StringT result (trigraph);
71 if (is_trigraph(trigraph)) {
72 switch (trigraph[2]) {
73 case '\'': result = "^"; break;
74 case '=': result = "#"; break;
75 case '/': result = "\\"; break;
76 case '(': result = "["; break;
77 case ')': result = "]"; break;
78 case '<': result = "{"; break;
79 case '>': result = "}"; break;
80 case '!': result = "|"; break;
81 case '-': result = "~"; break;
87 ///////////////////////////////////////////////////////////////////////////////
91 // The function convert_trigraph() converts all trigraphs in the given
92 // string into the corresponding characters.
94 // If one of the given character sequences doesn't form a valid trigraph
95 // sequence no conversion is performed.
97 ///////////////////////////////////////////////////////////////////////////////
98 template <typename StringT>
100 convert_trigraphs(StringT const &value)
103 typename StringT::size_type pos = 0;
104 typename StringT::size_type pos1 = value.find_first_of ("?", 0);
105 if (StringT::npos != pos1) {
107 result += value.substr(pos, pos1-pos);
108 StringT trigraph (value.substr(pos1));
109 if (is_trigraph(trigraph)) {
110 result += convert_trigraph(trigraph);
111 pos1 = value.find_first_of ("?", pos = pos1+3);
114 result += value[pos1];
115 pos1 = value.find_first_of ("?", pos = pos1+1);
117 } while (StringT::npos != pos1);
118 result += value.substr(pos);
126 ///////////////////////////////////////////////////////////////////////////////
128 } // namespace cpplexer
132 // the suffix header occurs after all of the code
133 #ifdef BOOST_HAS_ABI_HEADERS
134 #include BOOST_ABI_SUFFIX
137 #endif // !defined(CONVERT_TRIGRAPHS_HK050403_INCLUDED)