libzypp 17.37.17
JsonString.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#ifndef ZYPP_CORE_PARSER_JSON_JSON_STRING_DEFINED
10#define ZYPP_CORE_PARSER_JSON_JSON_STRING_DEFINED
11
12#include <string>
13
14namespace zypp::json {
15
16 namespace detail
17 {
18 inline std::string strEncode( std::string val_r )
19 {
20 typedef unsigned char uchar;
21
22 std::string::size_type add = 2; // enclosing "s
23 for( const auto &r : val_r )
24 {
25 if ( uchar(r) < 32u )
26 {
27 switch ( r )
28 {
29 case '\b':
30 case '\f':
31 case '\n':
32 case '\r':
33 case '\t':
34 add += 1; // "\c"
35 break;
36 default:
37 add += 5; // "\uXXXX"
38 break;
39 }
40 }
41 else
42 {
43 switch ( r )
44 {
45 case '"':
46 case '/':
47 case '\\':
48 add += 1; // \-escape
49 break;
50 }
51 }
52 }
53
54 val_r.resize( val_r.size() + add, '@' );
55 auto w( val_r.rbegin() );
56 auto r( w + add );
57
58 *w++ = '"';
59 for ( ; r != val_r.rend(); ++r )
60 {
61 if ( uchar(*r) < 32u )
62 {
63 static const char * digit = "0123456789abcdef";
64 switch ( *r )
65 {
66 case '\b': // "\c"
67 *w++ = 'b';
68 *w++ = '\\';
69 break;
70 case '\f': // "\c"
71 *w++ = 'f';
72 *w++ = '\\';
73 break;
74 case '\n': // "\c"
75 *w++ = 'n';
76 *w++ = '\\';
77 break;
78 case '\r': // "\c"
79 *w++ = 'r';
80 *w++ = '\\';
81 break;
82 case '\t': // "\c"
83 *w++ = 't';
84 *w++ = '\\';
85 break;
86 default: // "\uXXXX"
87 *w++ = digit[uchar(*r) % 15];
88 *w++ = digit[uchar(*r) / 16];
89 *w++ = '0';
90 *w++ = '0';
91 *w++ = 'u';
92 *w++ = '\\';
93 break;
94 }
95 }
96 else
97 {
98 switch ( (*w++ = *r) )
99 {
100 case '"':
101 case '/':
102 case '\\': // \-escape
103 *w++ = '\\';
104 break;
105 }
106 }
107 }
108 *w++ = '"';
109 return val_r;
110 }
111 } // namespace detail
112
113 class String {
114
115 public:
116 String() = default; //default false
117 ~String() = default;
118
119 String( const std::string &val ) : _value(val) { }
120 String( std::string &&val ) : _value( std::move(val) ) { }
121
122 String( std::nullptr_t ) : _value( "" ) {}
123
124 String( const char * val_r ) : _value( val_r ? val_r : "" ) {}
125
126 String( const String & ) = default;
127 String( String && ) = default;
128 String &operator=(const String &) = default;
129 String &operator=(String &&) = default;
130
131 String &operator=(const std::string &set ) {
132 _value = set;
133 return *this;
134 }
135
136 operator std::string() const {
137 return _value;
138 }
139
141 std::string asJSON() const
142 { return detail::strEncode(_value); }
143
145 std::ostream & dumpOn( std::ostream & str ) const
146 { return str << asJSON(); }
147
148 bool operator< ( const String &other ) const {
149 return _value < other._value;
150 }
151
152 friend bool operator== ( const String &lhs, const String &rhs ) {
153 return lhs._value == rhs._value;
154 }
155
156 private:
157 std::string _value;
158
159 };
160
162 inline std::ostream & operator<<( std::ostream & str, const String & obj )
163 {
164 return obj.dumpOn( str );
165 }
166
167}
168#endif
String(std::string &&val)
Definition JsonString.h:120
String(const std::string &val)
Definition JsonString.h:119
String(String &&)=default
String(std::nullptr_t)
Definition JsonString.h:122
String & operator=(const std::string &set)
Definition JsonString.h:131
friend bool operator==(const String &lhs, const String &rhs)
Definition JsonString.h:152
String(const char *val_r)
Definition JsonString.h:124
std::ostream & operator<<(std::ostream &str, const String &obj)
Stream output.
Definition JsonString.h:162
String & operator=(const String &)=default
std::string _value
Definition JsonString.h:157
String & operator=(String &&)=default
std::string asJSON() const
JSON representation.
Definition JsonString.h:141
String(const String &)=default
bool operator<(const String &other) const
Definition JsonString.h:148
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition JsonString.h:145
Definition Arch.h:364
String related utilities and Regular expression matching.
std::string strEncode(std::string val_r)
Definition JsonString.h:18