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 #if 0 // some unit tests:
222 
223 #include <stdio.h>
224 
225 wxString wxFunctionTaking_wxString( const wxString& wx )
226 {
227  printf( "%s:'%s'\n", __func__, (char*) UTF8( wx ) );
228  printf( "%s:'%s'\n", __func__, (const char*) UTF8( wx ) );
229  printf( "%s:'%s'\n", __func__, UTF8( wx ).c_str() );
230 
231  return wx;
232 }
233 
234 int main()
235 {
236  std::string str = "input";
237 
238  UTF8 u0 = L"wide string";
239  UTF8 u1 = "initial";
240  wxString wx = wxT( "input2" );
241 
242  printf( "u0:'%s'\n", u0.c_str() );
243  printf( "u1:'%s'\n", u1.c_str() );
244 
245  u1 = str;
246 
247  wxString wx2 = u1;
248 
249  // force a std::string into a UTF8, then into a wxString, then copy construct:
250  wxString wx3 = (UTF8&) u1;
251 
252  UTF8 u2 = wx2;
253 
254  u2 += 'X';
255 
256  printf( "u2:'%s'\n", u2.c_str() );
257 
258  // key accomplishments here:
259  // 1) passing a UTF8 to a function which normally takes a wxString.
260  // 2) return a wxString back into a UTF8.
261  UTF8 result = wxFunctionTaking_wxString( u2 );
262 
263  printf( "result:'%s'\n", result.c_str() );
264 
265  // test the unicode iterator:
266  for( UTF8::uni_iter it = u2.ubegin(); it < u2.uend(); )
267  {
268  // test post-increment:
269  printf( " _%02x_", *it++ );
270  }
271 
272  printf( "\n" );
273 
274  UTF8::uni_iter it = u2.ubegin();
275 
276  UTF8::uni_iter it2 = it++;
277 
278  printf( "post_inc:'%c' should be 'i'\n", *it2 );
279 
280  it2 = ++it;
281 
282  printf( "pre_inc:'%c' should be 'p'\n", *it2 );
283 
284  printf( "u[1]:'%c' should be 'n'\n", u2[1] );
285 
286  return 0;
287 }
288 
289 #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
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:278
wxString wx_str() const
Definition: utf8.cpp:48
int main(int argc, char **argv)
std::string m_s
Definition: utf8.h:305
bool IsUTF8(const char *aString)
Function IsUTF8 tests a c-string to see if it is UTF8 encoded.
Definition: utf8.cpp:170
class uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded ...
Definition: utf8.h:200
uni_iter uend() const
Function uend returns a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:287
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