ewmscp  ..
Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
escapismC Class Reference
Inheritance diagram for escapismC:
[legend]
Collaboration diagram for escapismC:
[legend]

Private Member Functions

void escape (const std::string &unEscaped, std::string &escaped) const override
 
void deEscape (const std::string &escaped, std::string &unEscaped) const override
 

Static Private Member Functions

static const std::map< char, char > & getEscapeMap ()
 
static const std::map< char, char > invertEscapeMap ()
 
static const std::map< char, char > & getUnEscapeMap ()
 
static charCodingBitPatternType initCodeList ()
 
static bool unencoded (char c)
 

Static Private Attributes

static escapismFactoryClassTemplate< escapismCfactory
 

Additional Inherited Members

- Static Public Member Functions inherited from escapism
static const escapismnewEscaper (const std::string &name)
 
static void addAllowedNamesToOption (options::single< std::string > &option)
 
static options::single< std::string > * newEscaperOption (const std::string &name, const std::string &description, const std::string &defaultValue="Url")
 

Detailed Description

Definition at line 137 of file escapism.cpp.

Member Function Documentation

◆ deEscape()

void escapismC::deEscape ( const std::string &  escaped,
std::string &  unEscaped 
) const
inlineoverrideprivatevirtual

Implements escapism.

Definition at line 196 of file escapism.cpp.

196  {
197  unEscaped.reserve(escaped.size()); // de-Escaped is always smaller, so this always fits and saves time
198  std::remove_reference<decltype(unEscaped)>::type::value_type code;
199  enum {normal, escaping, second3bits, last3bits} state = normal;
200  for (auto c : escaped) {
201  if (state == normal) {
202  if (c == '\\') {
203  state = escaping;
204  continue;
205  }
206  unEscaped.push_back(c);
207  } else if (state == escaping) {
208  auto enc = getUnEscapeMap().find(c);
209  if (enc != getUnEscapeMap().end()) {
210  unEscaped.push_back(enc->second);
211  state = normal;
212  continue;
213  }
214  if ('0' <= c && c <= '7') {
215  decltype(code) threeBits;
216  threeBits = c - '0';
217  code = threeBits << 6;
218  state = second3bits;
219  continue;
220  } else {
221  throw std::invalid_argument("bad encoding");
222  }
223  } else if (state == second3bits) {
224  if ('0' <= c && c <= '7') {
225  decltype(code) threeBits;
226  threeBits = c - '0';
227  code |= threeBits << 3;
228  state = last3bits;
229  continue;
230  } else {
231  throw std::invalid_argument("bad encoding");
232  }
233  } else if (state == last3bits) {
234  if ('0' <= c && c <= '7') {
235  decltype(code) threeBits;
236  threeBits = c - '0';
237  code |= threeBits << 0;
238  unEscaped.push_back(code);
239  state = normal;
240  continue;
241  } else {
242  throw std::invalid_argument("bad encoding");
243  }
244  }
245  }
246 
247  if (state != normal) {
248  throw std::invalid_argument("bad encoding");
249  }
250  }

References getUnEscapeMap().

Here is the call graph for this function:

◆ escape()

void escapismC::escape ( const std::string &  unEscaped,
std::string &  escaped 
) const
inlineoverrideprivatevirtual

Implements escapism.

Definition at line 179 of file escapism.cpp.

179  {
180  for (auto c : unEscaped) {
181  if (unencoded(c)) {
182  escaped.push_back(c);
183  } else {
184  escaped.push_back('\\');
185  auto enc = getEscapeMap().find(c);
186  if (enc != getEscapeMap().end()) {
187  escaped.push_back(enc->second);
188  } else {
189  escaped.push_back('0' + ((c >> 6) & 0x07));
190  escaped.push_back('0' + ((c >> 3) & 0x07));
191  escaped.push_back('0' + ((c >> 0) & 0x07));
192  }
193  }
194  }
195  }

References getEscapeMap(), and unencoded().

Here is the call graph for this function:

◆ getEscapeMap()

static const std::map<char, char>& escapismC::getEscapeMap ( )
inlinestaticprivate

Definition at line 139 of file escapism.cpp.

139  {
140  static std::remove_reference<decltype(getEscapeMap())>::type escapeMap{
141  {'\a', 'a'},
142  {'\b', 'b'},
143  {'\f', 'f'},
144  {'\n', 'n'},
145  {'\r', 'r'},
146  {'\v', 'v'},
147  {'\\', '\\'},
148  {' ', ' '},
149  {'\'', '\''},
150  {'"', '"'},
151  };
152  return escapeMap;
153  }

Referenced by escape(), initCodeList(), and invertEscapeMap().

Here is the caller graph for this function:

◆ getUnEscapeMap()

static const std::map<char, char>& escapismC::getUnEscapeMap ( )
inlinestaticprivate

Definition at line 161 of file escapism.cpp.

161  {
162  static std::remove_reference<decltype(getUnEscapeMap())>::type unEscapeMap(invertEscapeMap());
163  return unEscapeMap;
164  }

References invertEscapeMap().

Referenced by deEscape().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initCodeList()

static charCodingBitPatternType escapismC::initCodeList ( )
inlinestaticprivate

Definition at line 166 of file escapism.cpp.

166  {
167  charCodingBitPatternType codingPattern = {{0ull, 0ull, 0ull, 0ull}};
168  for (char c = ' '; c < 127; c++) {
169  if (getEscapeMap().find(c) == getEscapeMap().end()) {
171  }
172  }
173  return codingPattern;
174  }

References escapism::charCodingBitPatternType::bits, getEscapeMap(), escapism::charCodingBitPatternType::index(), and escapism::charCodingBitPatternType::mask().

Referenced by unencoded().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ invertEscapeMap()

static const std::map<char, char> escapismC::invertEscapeMap ( )
inlinestaticprivate

Definition at line 154 of file escapism.cpp.

154  {
155  std::map<char, char> map;
156  for (const auto& item : getEscapeMap()) {
157  map.emplace(item.second, item.first);
158  }
159  return map;
160  }

References getEscapeMap().

Referenced by getUnEscapeMap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unencoded()

static bool escapismC::unencoded ( char  c)
inlinestaticprivate

Definition at line 175 of file escapism.cpp.

175  {
176  static charCodingBitPatternType codingPattern = initCodeList();
177  return (codingPattern.bits[charCodingBitPatternType::index(c)] & charCodingBitPatternType::mask(c)) != 0ull;
178  }

References escapism::charCodingBitPatternType::bits, escapism::charCodingBitPatternType::index(), initCodeList(), and escapism::charCodingBitPatternType::mask().

Referenced by escape().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ factory

escapismFactoryClassTemplate<escapismC> escapismC::factory
staticprivate

Definition at line 138 of file escapism.cpp.


The documentation for this class was generated from the following file:
escapismC::getEscapeMap
static const std::map< char, char > & getEscapeMap()
Definition: escapism.cpp:139
escapismC::initCodeList
static charCodingBitPatternType initCodeList()
Definition: escapism.cpp:166
escapismC::unencoded
static bool unencoded(char c)
Definition: escapism.cpp:175
escapismC::invertEscapeMap
static const std::map< char, char > invertEscapeMap()
Definition: escapism.cpp:154
escapism::charCodingBitPatternType::mask
static std::uint64_t mask(char c)
Definition: escapism.h:14
escapism::charCodingBitPatternType::index
static int index(char c)
Definition: escapism.h:11
escapismC::getUnEscapeMap
static const std::map< char, char > & getUnEscapeMap()
Definition: escapism.cpp:161