libzypp  17.38.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 #include <ostream>
14 
15 #include <zypp-core/Globals.h>
16 
17 namespace zypp::json {
18 
19  namespace detail
20  {
21  inline std::string strEncode( std::string val_r )
22  {
23  typedef unsigned char uchar;
24 
25  std::string::size_type add = 2; // enclosing "s
26  for( const auto &r : val_r )
27  {
28  if ( uchar(r) < 32u )
29  {
30  switch ( r )
31  {
32  case '\b':
33  case '\f':
34  case '\n':
35  case '\r':
36  case '\t':
37  add += 1; // "\c"
38  break;
39  default:
40  add += 5; // "\uXXXX"
41  break;
42  }
43  }
44  else
45  {
46  switch ( r )
47  {
48  case '"':
49  case '/':
50  case '\\':
51  add += 1; // \-escape
52  break;
53  }
54  }
55  }
56 
57  val_r.resize( val_r.size() + add, '@' );
58  auto w( val_r.rbegin() );
59  auto r( w + add );
60 
61  *w++ = '"';
62  for ( ; r != val_r.rend(); ++r )
63  {
64  if ( uchar(*r) < 32u )
65  {
66  static const char * digit = "0123456789abcdef";
67  switch ( *r )
68  {
69  case '\b': // "\c"
70  *w++ = 'b';
71  *w++ = '\\';
72  break;
73  case '\f': // "\c"
74  *w++ = 'f';
75  *w++ = '\\';
76  break;
77  case '\n': // "\c"
78  *w++ = 'n';
79  *w++ = '\\';
80  break;
81  case '\r': // "\c"
82  *w++ = 'r';
83  *w++ = '\\';
84  break;
85  case '\t': // "\c"
86  *w++ = 't';
87  *w++ = '\\';
88  break;
89  default: // "\uXXXX"
90  *w++ = digit[uchar(*r) % 15];
91  *w++ = digit[uchar(*r) / 16];
92  *w++ = '0';
93  *w++ = '0';
94  *w++ = 'u';
95  *w++ = '\\';
96  break;
97  }
98  }
99  else
100  {
101  switch ( (*w++ = *r) )
102  {
103  case '"':
104  case '/':
105  case '\\': // \-escape
106  *w++ = '\\';
107  break;
108  }
109  }
110  }
111  *w++ = '"';
112  return val_r;
113  }
114  } // namespace detail
115 
116  class ZYPP_API String {
117 
118  public:
119  String() = default; //default false
120  ~String() = default;
121 
122  String( const std::string &val ) : _value(val) { }
123  String( std::string &&val ) : _value( std::move(val) ) { }
124 
125  String( std::nullptr_t ) : _value( "" ) {}
126 
127  String( const char * val_r ) : _value( val_r ? val_r : "" ) {}
128 
129  String( const String & ) = default;
130  String( String && ) = default;
131  String &operator=(const String &) = default;
132  String &operator=(String &&) = default;
133 
134  String &operator=(const std::string &set ) {
135  _value = set;
136  return *this;
137  }
138 
139  operator std::string() const {
140  return _value;
141  }
142 
144  std::string asJSON() const
145  { return detail::strEncode(_value); }
146 
148  std::ostream & dumpOn( std::ostream & str ) const
149  { return str << asJSON(); }
150 
151  bool operator< ( const String &other ) const {
152  return _value < other._value;
153  }
154 
155  friend bool operator== ( const String &lhs, const String &rhs ) {
156  return lhs._value == rhs._value;
157  }
158 
159  private:
160  std::string _value;
161 
162  };
163 
165  inline ZYPP_API std::ostream & operator<<( std::ostream & str, const String & obj )
166  {
167  return obj.dumpOn( str );
168  }
169 
170 }
171 #endif
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition: Capability.h:309
ZYPP_API std::ostream & operator<<(std::ostream &str, const Bool &obj)
relates: Bool Stream output
Definition: JsonBool.h:59
String & operator=(const std::string &set)
Definition: JsonString.h:134
String related utilities and Regular expression matching.
Definition: ansi.h:854
Edition * _value
Definition: SysContent.cc:311
std::ostream & dumpOn(std::ostream &str) const
Stream output.
Definition: JsonString.h:148
std::string strEncode(std::string val_r)
Definition: JsonString.h:21
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition: Arch.h:173
String(std::nullptr_t)
Definition: JsonString.h:125
Provides API related macros.
String(const std::string &val)
Definition: JsonString.h:122
String(const char *val_r)
Definition: JsonString.h:127
bool operator<(const Capability &lhs, const Capability &rhs)
relates: Capability Arbitrary order.
Definition: Capability.h:317
std::string asJSON() const
JSON representation.
Definition: JsonString.h:144
std::string _value
Definition: JsonString.h:160
SolvableIdType size_type
Definition: PoolDefines.h:66
String(std::string &&val)
Definition: JsonString.h:123