]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/wave/cpplexer/convert_trigraphs.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / wave / cpplexer / convert_trigraphs.hpp
CommitLineData
7c673cae
FG
1/*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3
4 Grammar for universal character validation (see C++ standard: Annex E)
b32b8144 5
7c673cae
FG
6 http://www.boost.org/
7
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=============================================================================*/
20effc67
TL
12#if !defined(BOOST_CONVERT_TRIGRAPHS_HK050403_INCLUDED)
13#define BOOST_CONVERT_TRIGRAPHS_HK050403_INCLUDED
7c673cae
FG
14
15#include <boost/wave/wave_config.hpp>
16#include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
17
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
21#endif
22
23///////////////////////////////////////////////////////////////////////////////
24namespace boost {
25namespace wave {
26namespace cpplexer {
27namespace impl {
28
29///////////////////////////////////////////////////////////////////////////////
30//
31// Test, whether the given string represents a valid trigraph sequence
32//
33///////////////////////////////////////////////////////////////////////////////
34template <typename StringT>
b32b8144 35inline bool
7c673cae
FG
36is_trigraph(StringT const& trigraph)
37{
38 if (trigraph.size() < 3 || '?' != trigraph[0] || '?' != trigraph[1])
39 return false;
b32b8144 40
7c673cae
FG
41 switch (trigraph[2]) {
42 case '\'': case '=': case '/': case '(':
43 case ')': case '<': case '>': case '!':
44 case '-':
45 break;
46
47 default:
48 return false;
49 }
50
51 return true;
52}
53
54///////////////////////////////////////////////////////////////////////////////
55//
56// convert_trigraph
57//
b32b8144 58// The function convert_trigraph() converts a single trigraph character
7c673cae
FG
59// sequence into the corresponding character.
60//
61// If the given character sequence doesn't form a valid trigraph sequence
b32b8144 62// no conversion is performed.
7c673cae
FG
63//
64///////////////////////////////////////////////////////////////////////////////
65template <typename StringT>
66inline StringT
67convert_trigraph(StringT const &trigraph)
68{
69StringT result (trigraph);
70
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;
82 }
83 }
84 return result;
85}
86
87///////////////////////////////////////////////////////////////////////////////
88//
89// convert_trigraphs
90//
b32b8144 91// The function convert_trigraph() converts all trigraphs in the given
7c673cae
FG
92// string into the corresponding characters.
93//
b32b8144
FG
94// If one of the given character sequences doesn't form a valid trigraph
95// sequence no conversion is performed.
7c673cae
FG
96//
97///////////////////////////////////////////////////////////////////////////////
98template <typename StringT>
99inline StringT
100convert_trigraphs(StringT const &value)
101{
102 StringT result;
103 typename StringT::size_type pos = 0;
104 typename StringT::size_type pos1 = value.find_first_of ("?", 0);
105 if (StringT::npos != pos1) {
106 do {
107 result += value.substr(pos, pos1-pos);
b32b8144 108 StringT trigraph (value.substr(pos1));
7c673cae
FG
109 if (is_trigraph(trigraph)) {
110 result += convert_trigraph(trigraph);
111 pos1 = value.find_first_of ("?", pos = pos1+3);
112 }
113 else {
114 result += value[pos1];
115 pos1 = value.find_first_of ("?", pos = pos1+1);
116 }
117 } while (StringT::npos != pos1);
118 result += value.substr(pos);
119 }
120 else {
121 result = value;
122 }
123 return result;
124}
125
126///////////////////////////////////////////////////////////////////////////////
b32b8144 127} // namespace impl
7c673cae
FG
128} // namespace cpplexer
129} // namespace wave
130} // namespace boost
131
132// the suffix header occurs after all of the code
133#ifdef BOOST_HAS_ABI_HEADERS
134#include BOOST_ABI_SUFFIX
135#endif
136
20effc67 137#endif // !defined(BOOST_CONVERT_TRIGRAPHS_HK050403_INCLUDED)
7c673cae
FG
138
139