summaryrefslogtreecommitdiffstats
path: root/base/string_escape.cc
blob: 0fecfa1ef576f94a8d745a327d35b17c84860ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string_escape.h"

#include <string>

#include "base/string_util.h"

namespace string_escape {

// Try to escape |c| as a "SingleEscapeCharacter" (\n, etc).  If successful,
// returns true and appends the escape sequence to |dst|.  This isn't required
// by the spec, but it's more readable by humans than the \uXXXX alternatives.
template<typename CHAR>
static bool JsonSingleEscapeChar(const CHAR c, std::string* dst) {
  // WARNING: if you add a new case here, you need to update the reader as well.
  // Note: \v is in the reader, but not here since the JSON spec doesn't
  // allow it.
  switch (c) {
    case '\b':
      dst->append("\\b");
      break;
    case '\f':
      dst->append("\\f");
      break;
    case '\n':
      dst->append("\\n");
      break;
    case '\r':
      dst->append("\\r");
      break;
    case '\t':
      dst->append("\\t");
      break;
    case '\\':
      dst->append("\\\\");
      break;
    case '"':
      dst->append("\\\"");
      break;
    default:
      return false;
  }
  return true;
}

template <class STR>
void JsonDoubleQuoteT(const STR& str,
                      bool put_in_quotes,
                      std::string* dst) {
  if (put_in_quotes)
    dst->push_back('"');

  for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) {
    typename ToUnsigned<typename STR::value_type>::Unsigned c = *it;
    if (!JsonSingleEscapeChar(c, dst)) {
      if (c < 32 || c > 126) {
        // Technically, we could also pass through c > 126 as UTF8, but this is
        // also optional.  It would also be a pain to implement here.
        unsigned int as_uint = static_cast<unsigned int>(c);
        StringAppendF(dst, "\\u%04X", as_uint);
      } else {
        unsigned char ascii = static_cast<unsigned char>(*it);
        dst->push_back(ascii);
      }
    }
  }

  if (put_in_quotes)
    dst->push_back('"');
}

void JsonDoubleQuote(const std::string& str,
                     bool put_in_quotes,
                     std::string* dst) {
  JsonDoubleQuoteT(str, put_in_quotes, dst);
}

void JsonDoubleQuote(const string16& str,
                     bool put_in_quotes,
                     std::string* dst) {
  JsonDoubleQuoteT(str, put_in_quotes, dst);
}

}  // namespace string_escape