Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Created the SolidusEscaper class.

  • Loading branch information...
commit 881693fc88a2e944b4b58960f317f97f0b6383b9 1 parent 7c1c9c9
@madbranch madbranch authored
Showing with 54 additions and 0 deletions.
  1. +19 −0 include/JsonBox/SolidusEscaper.h
  2. +35 −0 src/SolidusEscaper.cpp
View
19 include/JsonBox/SolidusEscaper.h
@@ -0,0 +1,19 @@
+#ifndef JB_SOLIDUS_ESCAPER_H
+#define JB_SOLIDUS_ESCAPER_H
+
+#include <streambuf>
+
+namespace JsonBox {
+ class SolidusEscaper {
+ public:
+ SolidusEscaper();
+
+ std::streambuf::int_type operator()(std::streambuf &destination,
+ std::streambuf::int_type character);
+ private:
+ bool afterBackSlash;
+ bool inString;
+ };
+}
+
+#endif
View
35 src/SolidusEscaper.cpp
@@ -0,0 +1,35 @@
+#include <JsonBox/SolidusEscaper.h>
+
+#include <JsonBox/Grammar.h>
+
+namespace JsonBox {
+ SolidusEscaper::SolidusEscaper() : afterBackSlash(false), inString(false) {
+ }
+
+ std::streambuf::int_type SolidusEscaper::operator()(std::streambuf &destination,
+ std::streambuf::int_type character) {
+ bool notEscaped = true;
+ std::streambuf::char_type tmpChar = std::streambuf::traits_type::to_char_type(character);
+
+ // If we encounter a quotation mark.
+ if (tmpChar == Strings::Json::Escape::QUOTATION_MARK) {
+ // If we're not in a string, we change that. If we're in a string,
+ // we change that only if we're not after an escape back slash.
+ inString = !inString || (afterBackSlash);
+
+ } else if (inString && !afterBackSlash) {
+ // If we are in a string definition and we're not after a backslash
+ // escape.
+ if (tmpChar == Strings::Std::SOLIDUS) {
+ destination.sputn(Strings::Json::SOLIDUS.c_str(), Strings::Json::SOLIDUS.size());
+ notEscaped = false;
+
+ }
+
+ }
+
+ // We determine if we start a backslash escape or not.
+ afterBackSlash = inString && !afterBackSlash && (tmpChar == '\\');
+ return (notEscaped) ? (destination.sputc(tmpChar)) : (0);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.