DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (4a108e94d3e2)

VCS Links

Line Code
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "JSStreamWriter.h"

#include "mozilla/ArrayUtils.h" // for ArrayLength
#include "nsDataHashtable.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsUTF8Utils.h"

#if defined(_MSC_VER) && _MSC_VER < 1900
 #define snprintf _snprintf
#endif

#define ARRAY (void*)1
#define OBJECT (void*)2

// Escape a UTF8 string to a stream. When an illegal encoding
// is found it will insert "INVALID" and the function will return.
static void EscapeToStream(std::ostream& stream, const char* str) {
  stream << "\"";

  size_t len = strlen(str);
  const char* end = &str[len];
  while (str < end) {
    bool err;
    const char* utf8CharStart = str;
    uint32_t ucs4Char = UTF8CharEnumerator::NextChar(&str, end, &err);

    if (err) {
      // Encoding error
      stream << "INVALID\"";
      return;
    }

    // See http://www.ietf.org/rfc/rfc4627.txt?number=4627
    // characters that must be escaped: quotation mark,
    // reverse solidus, and the control characters
    // (U+0000 through U+001F).
    if (ucs4Char == '\"') {
      stream << "\\\"";
    } else if (ucs4Char == '\\') {
      stream << "\\\\";
    } else if (ucs4Char > 0xFF) {
      char16_t chr[2];
      ConvertUTF8toUTF16 encoder(chr);
      encoder.write(utf8CharStart, uint32_t(str-utf8CharStart));
      char escChar[13];
      snprintf(escChar, mozilla::ArrayLength(escChar), "\\u%04X\\u%04X", chr[0], chr[1]);
      stream << escChar;
    } else if (ucs4Char < 0x1F || ucs4Char > 0xFF) {
      char escChar[7];
      snprintf(escChar, mozilla::ArrayLength(escChar), "\\u%04X", ucs4Char);
      stream << escChar;
    } else {
      stream << char(ucs4Char);
    }
  }
  stream << "\"";
}

JSStreamWriter::JSStreamWriter(std::ostream& aStream)
  : mStream(aStream)
  , mNeedsComma(false)
  , mNeedsName(false)
{ }

JSStreamWriter::~JSStreamWriter()
{
  MOZ_ASSERT(mStack.GetSize() == 0);
}

void
JSStreamWriter::BeginObject()
{
  MOZ_ASSERT(!mNeedsName);
  if (mNeedsComma && mStack.Peek() == ARRAY) {
    mStream << ",";
  }
  mStream << "{";
  mNeedsComma = false;
  mNeedsName = true;
  mStack.Push(OBJECT);
}

void
JSStreamWriter::EndObject()
{
  MOZ_ASSERT(mStack.Peek() == OBJECT);
  mStream << "}";
  mNeedsComma = true;
  mNeedsName = false;
  mStack.Pop();
  if (mStack.GetSize() > 0 && mStack.Peek() == OBJECT) {
    mNeedsName = true;
  }
}

void
JSStreamWriter::BeginArray()
{
  MOZ_ASSERT(!mNeedsName);
  if (mNeedsComma && mStack.Peek() == ARRAY) {
    mStream << ",";
  }
  mStream << "[";
  mNeedsComma = false;
  mStack.Push(ARRAY);
}

void
JSStreamWriter::EndArray()
{
  MOZ_ASSERT(!mNeedsName);
  MOZ_ASSERT(mStack.Peek() == ARRAY);
  mStream << "]";
  mNeedsComma = true;
  mStack.Pop();
  if (mStack.GetSize() > 0 && mStack.Peek() == OBJECT) {
    mNeedsName = true;
  }
}

void
JSStreamWriter::Name(const char *aName)
{
  MOZ_ASSERT(mNeedsName);
  if (mNeedsComma && mStack.Peek() == OBJECT) {
    mStream << ",";
  }
  EscapeToStream(mStream, aName);
  mStream << ":";
  mNeedsName = false;
}

void
JSStreamWriter::Value(int aValue)
{
  MOZ_ASSERT(!mNeedsName);
  if (mNeedsComma && mStack.Peek() == ARRAY) {
    mStream << ",";
  }
  mStream << aValue;
  mNeedsComma = true;
  if (mStack.Peek() == OBJECT) {
    mNeedsName = true;
  }
}

void
JSStreamWriter::Value(unsigned aValue)
{
  MOZ_ASSERT(!mNeedsName);
  if (mNeedsComma && mStack.Peek() == ARRAY) {
    mStream << ",";
  }
  mStream << aValue;
  mNeedsComma = true;
  if (mStack.Peek() == OBJECT) {
    mNeedsName = true;
  }
}

void
JSStreamWriter::Value(double aValue)
{
  MOZ_ASSERT(!mNeedsName);
  if (mNeedsComma && mStack.Peek() == ARRAY) {
    mStream << ",";
  }
  mStream.precision(18);
  mStream << aValue;
  mNeedsComma = true;
  if (mStack.Peek() == OBJECT) {
    mNeedsName = true;
  }
}

void
JSStreamWriter::Value(const char *aValue)
{
  MOZ_ASSERT(!mNeedsName);
  if (mNeedsComma && mStack.Peek() == ARRAY) {
    mStream << ",";
  }
  EscapeToStream(mStream, aValue);
  mNeedsComma = true;
  if (mStack.Peek() == OBJECT) {
    mNeedsName = true;
  }
}