Argo  1.0
A C++ library for handling JSON.
utf8.hpp
Go to the documentation of this file.
1 #ifndef _json_utf8_hpp_
2 #define _json_utf8_hpp_
3 
4 /*
5  * Copyright (c) 2017 Andrew Haisley
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
27 
28 #include <memory>
29 #include <string>
30 
31 #include "common.hpp"
32 
33 namespace NAMESPACE
34 {
41  class utf8
42  {
43  public:
44 
50  static std::unique_ptr<std::string> json_string_to_utf8(const std::string &src);
51 
57  static std::unique_ptr<std::string> utf8_to_json_string(const std::string &src);
58 
59  private:
60 
61  static char next_char(const std::string &s, size_t &index);
62 
63  static unsigned int from_hex(char c);
64 
65  static char to_hex(unsigned int i);
66 
67  static void set_and_check_unicode_byte(std::string &dst, size_t &dst_index, char c);
68 
69  static void parse_unicode(
70  const std::string &src,
71  size_t &src_index,
72  std::string &dst,
73  size_t &dst_index);
74 
75  static char32_t parse_hex(
76  const std::string &src,
77  size_t &src_index);
78 
79  static unsigned int utf8_length(uint8_t c);
80 
81  static bool valid_unicode(char32_t uc);
82 
83  static void add_hex_string(std::unique_ptr<std::string> &dst, char32_t uc);
84  };
85 }
86 
87 #endif
88 
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
Common defs needed everywhere and, as far as is possible, platform specific changes.
Methods to convert JSON format string to UTF-8 and vice-versa.
Definition: utf8.hpp:41