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 #include <wx/strconv.h>
28 #include <wx/buffer.h>
29 #include <vector>
30 
31 /* THROW_IO_ERROR needs this, but it includes this file, so until some
32  factoring of THROW_IO_ERROR into a separate header, defer and use the asserts.
33 #include <richio.h>
34 */
35 
36 #include <cassert>
37 
38 
39 /*
40  These are not inlined so that code space is saved by encapsulating the
41  creation of intermediate objects and the referencing of wxConvUTF8.
42 */
43 
44 
45 UTF8::UTF8( const wxString& o ) :
46  m_s( (const char*) o.utf8_str() )
47 {
48 }
49 
50 
51 wxString UTF8::wx_str() const
52 {
53  return wxString( c_str(), wxConvUTF8 );
54 }
55 
56 
57 UTF8::operator wxString () const
58 {
59  return wxString( c_str(), wxConvUTF8 );
60 }
61 
62 
63 UTF8& UTF8::operator=( const wxString& o )
64 {
65  m_s = (const char*) o.utf8_str();
66  return *this;
67 }
68 
69 
70 // There is no wxWidgets function that does this, because wchar_t is 16 bits
71 // on windows and wx wants to encode the output in UTF16 for such.
72 
73 int UTF8::uni_forward( const unsigned char* aSequence, unsigned* aResult )
74 {
75  unsigned ch = *aSequence;
76 
77  if( ch < 0x80 )
78  {
79  if( aResult )
80  *aResult = ch;
81  return 1;
82  }
83 
84  const unsigned char* s = aSequence;
85 
86  static const unsigned char utf8_len[] = {
87  // Map encoded prefix byte to sequence length. Zero means
88  // illegal prefix. See RFC 3629 for details
89  /*
90  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00-0F
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,
95  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
96  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
97  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70-7F
98  */
99  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-8F
100  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B0-BF
103  0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0-C1 + C2-CF
104  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // D0-DF
105  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // E0-EF
106  4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // F0-F4 + F5-FF
107  };
108 
109  int len = utf8_len[ *s - 0x80 /* top half of table is missing */ ];
110 
111  switch( len )
112  {
113  default:
114  case 0:
115  if( aResult )
116  wxFAIL_MSG( "uni_forward: invalid start byte" );
117 
118  return 0;
119  break;
120 
121  case 2:
122  if( ( s[1] & 0xc0 ) != 0x80 )
123  {
124  if( aResult )
125  wxFAIL_MSG( "uni_forward: invalid continuation byte" );
126 
127  return 0;
128  }
129 
130  ch = ((s[0] & 0x1f) << 6) +
131  ((s[1] & 0x3f) << 0);
132 
133  // assert( ch > 0x007F && ch <= 0x07FF );
134  break;
135 
136  case 3:
137  if( (s[1] & 0xc0) != 0x80 ||
138  (s[2] & 0xc0) != 0x80 ||
139  (s[0] == 0xE0 && s[1] < 0xA0)
140  // || (s[0] == 0xED && s[1] > 0x9F)
141  )
142  {
143  if( aResult )
144  wxFAIL_MSG( "uni_forward: invalid continuation byte" );
145 
146  return 0;
147  }
148 
149  ch = ((s[0] & 0x0f) << 12) +
150  ((s[1] & 0x3f) << 6 ) +
151  ((s[2] & 0x3f) << 0 );
152 
153  // assert( ch > 0x07FF && ch <= 0xFFFF );
154  break;
155 
156  case 4:
157  if( (s[1] & 0xc0) != 0x80 ||
158  (s[2] & 0xc0) != 0x80 ||
159  (s[3] & 0xc0) != 0x80 ||
160  (s[0] == 0xF0 && s[1] < 0x90) ||
161  (s[0] == 0xF4 && s[1] > 0x8F) )
162  {
163  if( aResult )
164  wxFAIL_MSG( "uni_forward: invalid continuation byte" );
165 
166  return 0;
167  }
168 
169  ch = ((s[0] & 0x7) << 18) +
170  ((s[1] & 0x3f) << 12) +
171  ((s[2] & 0x3f) << 6 ) +
172  ((s[3] & 0x3f) << 0 );
173 
174  // assert( ch > 0xFFFF && ch <= 0x10ffff );
175  break;
176  }
177 
178  if( aResult )
179  *aResult = ch;
180 
181  return len;
182 }
183 
184 
185 bool IsUTF8( const char* aString )
186 {
187  int len = strlen( aString );
188 
189  if( len )
190  {
191  const unsigned char* next = (unsigned char*) aString;
192  const unsigned char* end = next + len;
193 
194  while( next < end )
195  {
196  int charLen = UTF8::uni_forward( next, NULL );
197 
198  if( charLen == 0 )
199  return false;
200 
201  next += charLen;
202  }
203 
204  // uni_forward() should find the exact end if it is truly UTF8
205  if( next > end )
206  return false;
207  }
208 
209  return true;
210 }
211 
212 
213 UTF8::UTF8( const wchar_t* txt )
214 {
215  try
216  {
217  std::vector< char > temp( wcslen( txt ) * 4 + 1 );
218  wxConvUTF8.WC2MB( temp.data(), txt, temp.size() );
219  m_s.assign( temp.data() );
220  }
221  catch(...)
222  {
223  auto string = wxSafeConvertWX2MB( txt );
224  m_s.assign( string );
225  }
226 
227  m_s.shrink_to_fit();
228 }
229 
230 
231 UTF8& UTF8::operator+=( unsigned w_ch )
232 {
233  if( w_ch <= 0x7F )
234  m_s.operator+=( char( w_ch ) );
235  else
236  {
237  //TODO: Remove wchar use. Replace with std::byte*
238  wchar_t wide_chr[2]; // buffer to store wide chars (UTF16) read from aText
239  wide_chr[1] = 0;
240  wide_chr[0] = w_ch;
241  UTF8 substr( wide_chr );
242  m_s += substr.m_s;
243  }
244 
245  return *this;
246 }
CITER next(CITER it)
Definition: ptree.cpp:126
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
UTF8 & operator=(const wxString &o)
Definition: utf8.cpp:63
UTF8()
Definition: utf8.h:97
const char * c_str() const
Definition: utf8.h:107
#define NULL
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:185
UTF8 & operator+=(const UTF8 &str)
Definition: utf8.h:127
wxString wx_str() const
Definition: utf8.cpp:51
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:73
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:182