summaryrefslogtreecommitdiffstats
path: root/third_party/json_minify/minify-sans-regexp.js
blob: 99c817c2eb92ecb9044fb10cfdf3a4f8c152780b (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
88
89
90
91
/*!
 * `JSON.minify()`
 * This version does not use regular expressions.
 *
 * Copyright 2011, Kyle Simpson.
 * Copyright 2012, Kit Cambridge.
 *
 * Released under the MIT License.
*/

;(function () {
  var JSON = this.JSON;

  // Create the global JSON object if it doesn't exist.
  if (Object(JSON) !== JSON) {
    JSON = this.JSON = {};
  }

  JSON.minify = function (source) {
    var index = 0, length = source.length, result = "", symbol, position;
    while (index < length) {
      symbol = source.charAt(index);
      switch (symbol) {
        // Ignore whitespace tokens. According to ES 5.1 section 15.12.1.1,
        // whitespace tokens include tabs, carriage returns, line feeds, and
        // space characters.
        // -----------------------------------------------------------------
        case "\t":
        case "\r":
        case "\n":
        case " ":
          index += 1;
          break;
        // Ignore line and block comments.
        // -------------------------------
        case "/":
          symbol = source.charAt(index += 1);
          switch (symbol) {
            // Line comments.
            // -------------
            case "/":
              position = source.indexOf("\n", index);
              if (position < 0) {
                // Check for CR-style line endings.
                position = source.indexOf("\r", index);
              }
              index = position > -1 ? position : length;
              break;
            // Block comments.
            // ---------------
            case "*":
              position = source.indexOf("*/", index);
              if (position > -1) {
                // Advance the scanner's position past the end of the comment.
                index = position += 2;
                break;
              }
              throw SyntaxError("Unterminated block comment.");
            default:
              throw SyntaxError("Invalid comment.");
          }
          break;
        // Parse strings separately to ensure that any whitespace characters and
        // JavaScript-style comments within them are preserved.
        // ---------------------------------------------------------------------
        case '"':
          position = index;
          while (index < length) {
            symbol = source.charAt(index += 1);
            if (symbol == "\\") {
              // Skip past escaped characters.
              index += 1;
            } else if (symbol == '"') {
              break;
            }
          }
          if (source.charAt(index) == '"') {
            result += source.slice(position, index += 1);
            break;
          }
          throw SyntaxError("Unterminated string.");
        // Preserve all other characters.
        // ------------------------------
        default:
          result += symbol;
          index += 1;
      }
    }
    return result;
  };
}).call(this);