KiCad PCB EDA Suite
utf8.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013-2017 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2013-2017 KiCad Developers, see CHANGELOG.TXT for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <utf8.h>
26 #include <ki_exception.h>
27 
28 /* THROW_IO_ERROR needs this, but it includes this file, so until some
29  factoring of THROW_IO_ERROR into a separate header, defer and use the asserts.
30 #include <richio.h>
31 */
32 
33 #include <assert.h>
34 
35 
36 /*
37  These are not inlined so that code space is saved by encapsulating the
38  creation of intermediate objects and the referencing of wxConvUTF8.
39 */
40 
41 
42 UTF8::UTF8( const wxString& o ) :
43  m_s( (const char*) o.utf8_str() )
44 {
45 }
46 
47 
48 wxString UTF8::wx_str() const
49 {
50  return wxString( c_str(), wxConvUTF8 );
51 }
52 
53 
54 UTF8::operator wxString () const
55 {
56  return wxString( c_str(), wxConvUTF8 );
57 }
58 
59 
60 UTF8& UTF8::operator=( const wxString& o )
61 {
62  m_s = (const char*) o.utf8_str();
63  return *this;
64 }
65 
66 
67 // There is no wxWidgets function that does this, because wchar_t is 16 bits
68 // on windows and wx wants to encode the output in UTF16 for such.
69 
70 int UTF8::uni_forward( const unsigned char* aSequence, unsigned* aResult )
71 {
72  unsigned ch = *aSequence;
73 
74  if( ch < 0x80 )
75  {
76  if( aResult )
77  *aResult = ch;
78  return 1;
79  }
80 
81  const unsigned char* s = aSequence;
82 
83  static const unsigned char utf8_len[] = {
84  // Map encoded prefix byte to sequence length. Zero means
85  // illegal prefix. See RFC 3629 for details
86  /*
87  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00-0F
88  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
89  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
90  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
91  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
92  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
93  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
94  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70-7F
95  */
96  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
97  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
100  0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0-C1 + C2-CF
101  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // D0-DF
102  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // E0-EF
103  4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-F4 + F5-FF
104  };
105 
106  int len = utf8_len[ *s - 0x80 /* top half of table is missing */ ];
107 
108  switch( len )
109  {
110  default:
111  case 0:
112  THROW_IO_ERROR( "invalid start byte" );
113  break;
114 
115  case 2:
116  if( ( s[1] & 0xc0 ) != 0x80 )
117  {
118  THROW_IO_ERROR( "invalid continuation byte" );
119  }
120 
121  ch = ((s[0] & 0x1f) << 6) +
122  ((s[1] & 0x3f) << 0);
123 
124  // assert( ch > 0x007F && ch <= 0x07FF );
125  break;
126 
127  case 3:
128  if( (s[1] & 0xc0) != 0x80 ||
129  (s[2] & 0xc0) != 0x80 ||
130  (s[0] == 0xE0 && s[1] < 0xA0)
131  // || (s[0] == 0xED && s[1] > 0x9F)
132  )
133  {
134  THROW_IO_ERROR( "invalid continuation byte" );
135  }
136 
137  ch = ((s[0] & 0x0f) << 12) +
138  ((s[1] & 0x3f) << 6 ) +
139  ((s[2] & 0x3f) << 0 );
140 
141  // assert( ch > 0x07FF && ch <= 0xFFFF );
142  break;
143 
144  case 4:
145  if( (s[1] & 0xc0) != 0x80 ||
146  (s[2] & 0xc0) != 0x80 ||
147  (s[3] & 0xc0) != 0x80 ||
148  (s[0] == 0xF0 && s[1] < 0x90) ||
149  (s[0] == 0xF4 && s[1] > 0x8F) )
150  {
151  THROW_IO_ERROR( "invalid continuation byte" );
152  }
153 
154  ch = ((s[0] & 0x7) << 18) +
155  ((s[1] & 0x3f) << 12) +
156  ((s[2] & 0x3f) << 6 ) +
157  ((s[3] & 0x3f) << 0 );
158 
159  // assert( ch > 0xFFFF && ch <= 0x10ffff );
160  break;
161  }
162 
163  if( aResult )
164  *aResult = ch;
165 
166  return len;
167 }
168 
169 
170 bool IsUTF8( const char* aString )
171 {
172  int len = strlen( aString );
173 
174  if( len )
175  {
176  const unsigned char* next = (unsigned char*) aString;
177  const unsigned char* end = next + len;
178 
179  try
180  {
181  while( next < end )
182  {
183  next += UTF8::uni_forward( next, NULL );
184  }
185 
186  // uni_forward() should find the exact end if it is truly UTF8
187  if( next > end )
188  return false;
189  }
190  catch( const IO_ERROR& ioe )
191  {
192  return false;
193  }
194  }
195 
196  return true;
197 }
198 
199 
200 UTF8::UTF8( const wchar_t* txt ) :
201  // size initial string safely large enough, then shrink to known size later.
202  m_s( wcslen( txt ) * 4, 0 )
203 {
204  /*
205 
206  "this" string was sized to hold the worst case UTF8 encoded byte
207  sequence, and was initialized with all nul bytes. Overwrite some of
208  those nuls, then resize, shrinking down to actual size.
209 
210  Use the wx 2.8 function, not new FromWChar(). It knows about wchar_t
211  possibly being 16 bits wide on Windows and holding UTF16 input.
212 
213  */
214 
215  int sz = wxConvUTF8.WC2MB( (char*) m_s.data(), txt, m_s.size() );
216 
217  m_s.resize( sz );
218 }
219 
220 
221 UTF8& UTF8::operator+=( unsigned w_ch )
222 {
223  if( w_ch <= 0x7F )
224  m_s.operator+=( char( w_ch ) );
225  else
226  {
227  wchar_t wide_chr[2]; // buffer to store wide chars (UTF16) read from aText
228  wide_chr[1] = 0;
229  wide_chr[0] = w_ch;
230  UTF8 substr( wide_chr );
231  m_s += substr.m_s;
232  }
233 
234  return (UTF8&) *this;
235 }
236 
237 
238 #if 0 // some unit tests:
239 
240 #include <stdio.h>
241 
242 wxString wxFunctionTaking_wxString( const wxString& wx )
243 {
244  printf( "%s:'%s'\n", __func__, (char*) UTF8( wx ) );
245  printf( "%s:'%s'\n", __func__, (const char*) UTF8( wx ) );
246  printf( "%s:'%s'\n", __func__, UTF8( wx ).c_str() );
247 
248  return wx;
249 }
250 
251 int main()
252 {
253  std::string str = "input";
254 
255  UTF8 u0 = L"wide string";
256  UTF8 u1 = "initial";
257  wxString wx = wxT( "input2" );
258 
259  printf( "u0:'%s'\n", u0.c_str() );
260  printf( "u1:'%s'\n", u1.c_str() );
261 
262  u1 = str;
263 
264  wxString wx2 = u1;
265 
266  // force a std::string into a UTF8, then into a wxString, then copy construct:
267  wxString wx3 = (UTF8&) u1;
268 
269  UTF8 u2 = wx2;
270 
271  u2 += 'X';
272 
273  printf( "u2:'%s'\n", u2.c_str() );
274 
275  // key accomplishments here:
276  // 1) passing a UTF8 to a function which normally takes a wxString.
277  // 2) return a wxString back into a UTF8.
278  UTF8 result = wxFunctionTaking_wxString( u2 );
279 
280  printf( "result:'%s'\n", result.c_str() );
281 
282  // test the unicode iterator:
283  for( UTF8::uni_iter it = u2.ubegin(); it < u2.uend(); )
284  {
285  // test post-increment:
286  printf( " _%02x_", *it++ );
287  }
288 
289  printf( "\n" );
290 
291  UTF8::uni_iter it = u2.ubegin();
292 
293  UTF8::uni_iter it2 = it++;
294 
295  printf( "post_inc:'%c' should be 'i'\n", *it2 );
296 
297  it2 = ++it;
298 
299  printf( "pre_inc:'%c' should be 'p'\n", *it2 );
300 
301  printf( "u[1]:'%c' should be 'n'\n", u2[1] );
302 
303  return 0;
304 }
305 
306 #endif
CITER next(CITER it)
Definition: ptree.cpp:130
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
UTF8 & operator=(const wxString &o)
Definition: utf8.cpp:60
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:182
UTF8()
Definition: utf8.h:97
uni_iter ubegin() const
Function ubegin returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition: utf8.h:285
wxString wx_str() const
Definition: utf8.cpp:48
int main(int argc, char **argv)
std::string m_s
Definition: utf8.h:312
bool IsUTF8(const char *aString)
Function IsUTF8 tests a c-string to see if it is UTF8 encoded.
Definition: utf8.cpp:170
std::string::const_iterator end() const
Definition: utf8.h:197
UTF8 & operator+=(const UTF8 &str)
Definition: utf8.h:127
class uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded ...
Definition: utf8.h:207
uni_iter uend() const
Function uend returns a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:294
const char * c_str() const
Definition: utf8.h:107
static int uni_forward(const unsigned char *aSequence, unsigned *aResult=NULL)
Function uni_forward advances over a single UTF8 encoded multibyte character, capturing the unicode c...
Definition: utf8.cpp:70
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38