Om
code_point_source.cpp
Go to the documentation of this file.
1 
15 #ifndef Om_Source_CodePointSource_
16 
18 
19  #ifdef Om_Macro_Test_
20 
21  #ifndef Om_Macro_Precompilation_
22 
23  #include "boost/test/unit_test.hpp"
24 
25  #endif
26 
27 namespace Om {
28 
29  namespace Source {
30 
31  BOOST_AUTO_TEST_SUITE(CodePointSourceTest)
32 
33  BOOST_AUTO_TEST_CASE(GeneralTest) {
34  std::string theString(
35  "\xC7\xBE"
36  "A"
37  );
38  CodePointSource<std::string::const_iterator> theSource(
39  theString.begin(),
40  theString.end()
41  );
42  BOOST_CHECK_EQUAL(
43  false,
44  !theSource
45  );
46  BOOST_CHECK_EQUAL(
47  510U,
48  *theSource
49  );
50  theSource.Pop();
51  BOOST_CHECK_EQUAL(
52  false,
53  !theSource
54  );
55  BOOST_CHECK_EQUAL(
56  65U,
57  *theSource
58  );
59  theSource.Pop();
60  BOOST_CHECK_EQUAL(
61  true,
62  !theSource
63  );
64  }
65 
66  BOOST_AUTO_TEST_SUITE_END()
67 
68  }
69 
70 }
71 
72  #endif
73 
74 #else
75 
76  #include "om/utf8.hpp"
77 
78  #ifndef Om_Macro_Precompilation_
79 
80  #include "boost/swap.hpp"
81 
82  #endif
83 
84 // MARK: - Om::Source::CodePointSource
85 
86  #define Template_ \
87  template <typename ThisCodeUnitIterator>
88 
89  #define Type_ \
90  Om::Source::CodePointSource<ThisCodeUnitIterator>
91 
92 // MARK: public (non-static)
93 
94 Template_
95 inline Type_::CodePointSource(
96  ThisCodeUnitIterator theInputStart,
97  ThisCodeUnitIterator const theInputEnd
98 ):
99 thisInputIterator(theInputStart),
100 thisInputEnd(theInputEnd),
101 thisCodePoint() {
102  this->Pop();
103 }
104 
105 Template_
106 inline Type_ & Type_::operator =(CodePointSource theCodePointSource) {
107  this->Swap(theCodePointSource);
108  return *this;
109 }
110 
111 Template_
112 inline bool Type_::operator !() const {
113  return !this->thisCodePoint;
114 }
115 
116 Template_
117 inline Om::CodePoint const & Type_::operator *() const {
118  assert(this->thisCodePoint);
119  return this->thisCodePoint;
120 }
121 
122 Template_
123 inline bool Type_::Equals(CodePointSource const & theCodePointSource) const {
124  return this->thisInputIterator == theCodePointSource.thisInputIterator;
125 }
126 
127 Template_
128 inline void Type_::Pop() {
129  if (this->thisInputEnd == this->thisInputIterator) {
130  this->thisCodePoint = 0;
131  } else {
132  this->thisCodePoint = Utf8::decode(
133  this->thisInputIterator,
134  this->thisInputEnd
135  );
136  switch (this->thisCodePoint) {
137  default:
138  return;
139  case boost::locale::utf::incomplete:
140  // Fall through.
141  case boost::locale::utf::illegal:
142  this->thisCodePoint = 0xFFFD /* Replacement character */;
143  // Fall through.
144  }
145  }
146 }
147 
148 Template_
149 inline void Type_::Swap(CodePointSource & theCodePointSource) {
150  boost::swap(
151  this->thisInputIterator,
152  theCodePointSource.thisInputIterator
153  );
154  boost::swap(
155  this->thisInputEnd,
156  theCodePointSource.thisInputEnd
157  );
158  boost::swap(
159  this->thisCodePoint,
160  theCodePointSource.thisCodePoint
161  );
162 }
163 
164  #undef Type_
165  #undef Template_
166 
167 // MARK: - Om::Source::CodePointSource<char const *>
168 
169  #define Type_ \
170  Om::Source::CodePointSource<char const *>
171 
172 // MARK: public (non-static)
173 
174 inline Type_::CodePointSource(
175  char const theCodeUnitIterator[]
176 ):
177 CodePointSource<CodeUnitSource>(
178  CodeUnitSource(theCodeUnitIterator),
179  CodeUnitSource("")
180 ) {}
181 
182  #undef Type_
183 
184 // MARK: - Om::Source::
185 
186  #define Template_ \
187  template <typename TheCodeUnitIterator>
188 
189  #define Type_ \
190  Om::Source::CodePointSource<TheCodeUnitIterator>
191 
192 Template_
193 inline bool Om::Source::operator ==(
194  Type_ const & theFirst,
195  Type_ const & theSecond
196 ) {
197  return theFirst.Equals(theSecond);
198 }
199 
200 Template_
201 inline bool Om::Source::operator !=(
202  Type_ const & theFirst,
203  Type_ const & theSecond
204 ) {
205  return !theFirst.Equals(theSecond);
206 }
207 
208 // MARK: - boost::
209 
210 Template_
211 inline void boost::swap(
212  Type_ & theFirst,
213  Type_ & theSecond
214 ) {
215  theFirst.Swap(theSecond);
216 }
217 
218  #undef Type_
219  #undef Template_
220 
221 #endif
Om header file.
bool operator==(CodePointSource< TheCodeUnitIterator > const &, CodePointSource< TheCodeUnitIterator > const &)
bool operator!=(CodePointSource< TheCodeUnitIterator > const &, CodePointSource< TheCodeUnitIterator > const &)
The Om library.
Definition: code_point.hpp:26
boost::locale::utf::code_point CodePoint
A UTF-8 code point.
Definition: code_point.hpp:32
void swap(Om::Language::Expression &, Om::Language::Expression &)
Om header file.