Utf8Codec.h
1 /*
2  * Copyright (C) 2005-2013 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Utf8Codec_h
30 #define Pt_Utf8Codec_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/TextCodec.h>
34 #include <Pt/String.h>
35 #include <string>
36 
37 namespace Pt {
38 
43 class PT_API Utf8Codec : public TextCodec<Char, char>
44 {
45  public:
48  explicit Utf8Codec(std::size_t ref = 0);
49 
51  virtual ~Utf8Codec()
52  {}
53 
54  // inheritdoc
55  virtual result do_in(MBState& s, const char* fromBegin,
56  const char* fromEnd, const char*& fromNext,
57  Char* toBegin, Char* toEnd, Char*& toNext) const;
58 
59  // inheritdoc
60  virtual result do_out(MBState& s, const Char* fromBegin,
61  const Char* fromEnd, const Char*& fromNext,
62  char* toBegin, char* toEnd, char*& toNext) const;
63 
64  // inheritdoc
65  virtual bool do_always_noconv() const throw();
66 
67  // inheritdoc
68  virtual int do_length(MBState& s, const char* fromBegin, const char* fromEnd, std::size_t max) const;
69 
70  // inheritdoc
71  virtual int do_max_length() const throw();
72 
73  // inheritdoc
74  std::codecvt_base::result do_unshift(Pt::MBState&, char*, char*, char*&) const;
75 
76  // inheritdoc
77  int do_encoding() const throw();
78 
80  static String decode(const char* data, std::size_t size);
81 
83  static String decode(const std::string& data)
84  { return decode(data.data(), data.size()); }
85 
87  static std::string encode(const Char* data, std::size_t size);
88 
90  static std::string encode(const String& data)
91  { return encode(data.data(), data.size()); }
92 };
93 
94 } //namespace Pt
95 
96 #endif // Pt_Utf8Codec_h
size_type size() const
Returns the length of the string.
Definition: String.h:239
const Pt::Char * data() const
Returns the string character buffer.
Definition: String.h:259
Converts between character encodings.
Definition: TextCodec.h:38
Unicode character type.
Definition: String.h:66
static std::string encode(const String &data)
Encode to a UTF-8 string.
Definition: Utf8Codec.h:90
Unicode capable basic_string.
Definition: String.h:42
virtual ~Utf8Codec()
Destructor.
Definition: Utf8Codec.h:51
Convert between unicode and UTF-8.
Definition: Utf8Codec.h:43