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 <assert.h>
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  THROW_IO_ERROR( "invalid start byte" );
116  break;
117 
118  case 2:
119  if( ( s[1] & 0xc0 ) != 0x80 )
120  {
121  THROW_IO_ERROR( "invalid continuation byte" );
122  }
123 
124  ch = ((s[0] & 0x1f) << 6) +
125  ((s[1] & 0x3f) << 0);
126 
127  // assert( ch > 0x007F && ch <= 0x07FF );
128  break;
129 
130  case 3:
131  if( (s[1] & 0xc0) != 0x80 ||
132  (s[2] & 0xc0) != 0x80 ||
133  (s[0] == 0xE0 && s[1] < 0xA0)
134  // || (s[0] == 0xED && s[1] > 0x9F)
135  )
136  {
137  THROW_IO_ERROR( "invalid continuation byte" );
138  }
139 
140  ch = ((s[0] & 0x0f) << 12) +
141  ((s[1] & 0x3f) << 6 ) +
142  ((s[2] & 0x3f) << 0 );
143 
144  // assert( ch > 0x07FF && ch <= 0xFFFF );
145  break;
146 
147  case 4:
148  if( (s[1] & 0xc0) != 0x80 ||
149  (s[2] & 0xc0) != 0x80 ||
150  (s[3] & 0xc0) != 0x80 ||
151  (s[0] == 0xF0 && s[1] < 0x90) ||
152  (s[0] == 0xF4 && s[1] > 0x8F) )
153  {
154  THROW_IO_ERROR( "invalid continuation byte" );
155  }
156 
157  ch = ((s[0] & 0x7) << 18) +
158  ((s[1] & 0x3f) << 12) +
159  ((s[2] & 0x3f) << 6 ) +
160  ((s[3] & 0x3f) << 0 );
161 
162  // assert( ch > 0xFFFF && ch <= 0x10ffff );
163  break;
164  }
165 
166  if( aResult )
167  *aResult = ch;
168 
169  return len;
170 }
171 
172 
173 bool IsUTF8( const char* aString )
174 {
175  int len = strlen( aString );
176 
177  if( len )
178  {
179  const unsigned char* next = (unsigned char*) aString;
180  const unsigned char* end = next + len;
181 
182  try
183  {
184  while( next < end )
185  {
186  next += UTF8::uni_forward( next, NULL );
187  }
188 
189  // uni_forward() should find the exact end if it is truly UTF8
190  if( next > end )
191  return false;
192  }
193  catch( const IO_ERROR& )
194  {
195  return false;
196  }
197  }
198 
199  return true;
200 }
201 
202 
203 UTF8::UTF8( const wchar_t* txt )
204 {
205  try
206  {
207  std::vector< char > temp( wcslen( txt ) * 4 + 1 );
208  wxConvUTF8.WC2MB( temp.data(), txt, temp.size() );
209  m_s.assign( temp.data() );
210  }
211  catch(...)
212  {
213  auto string = wxSafeConvertWX2MB( txt );
214  m_s.assign( string );
215  }
216 
217  m_s.shrink_to_fit();
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  //TODO: Remove wchar use. Replace with std::byte*
228  wchar_t wide_chr[2]; // buffer to store wide chars (UTF16) read from aText
229  wide_chr[1] = 0;
230  wide_chr[0] = w_ch;
231  UTF8 substr( wide_chr );
232  m_s += substr.m_s;
233  }
234 
235  return (UTF8&) *this;
236 }
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:63
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:182
UTF8()
Definition: utf8.h:97
wxString wx_str() const
Definition: utf8.cpp:51
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:173
#define THROW_IO_ERROR(msg)
std::string::const_iterator end() const
Definition: utf8.h:197
UTF8 & operator+=(const UTF8 &str)
Definition: utf8.h:127
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:73
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76