Sha256: 623957b973624f138a62152f21a6ad378e10b9d6f50d316abe3764a6b5d385c9

Contents?: true

Size: 1.95 KB

Versions: 27

Compression:

Stored size: 1.95 KB

Contents

// string literal characters cannot contain control codes
var CONTROL_CODES = [0, // null
7, // bell
8, // backspace
9, // horizontal
10, // line feed
11, // vertical tab
12, // form feed
13, // carriage return
26, // Control-Z
27, // escape
127 // delete
]; // escaped sequences can either be a two character hex value, or one of the
// following single character codes

function decodeControlCharacter(char) {
  switch (char) {
    case "t":
      return 0x09;

    case "n":
      return 0x0a;

    case "r":
      return 0x0d;

    case '"':
      return 0x22;

    case "′":
      return 0x27;

    case "\\":
      return 0x5c;
  }

  return -1;
}

var ESCAPE_CHAR = 92; // backslash

var QUOTE_CHAR = 34; // backslash
// parse string as per the spec:
// https://webassembly.github.io/spec/core/multipage/text/values.html#text-string

export function parseString(value) {
  var byteArray = [];
  var index = 0;

  while (index < value.length) {
    var charCode = value.charCodeAt(index);

    if (CONTROL_CODES.indexOf(charCode) !== -1) {
      throw new Error("ASCII control characters are not permitted within string literals");
    }

    if (charCode === QUOTE_CHAR) {
      throw new Error("quotes are not permitted within string literals");
    }

    if (charCode === ESCAPE_CHAR) {
      var firstChar = value.substr(index + 1, 1);
      var decodedControlChar = decodeControlCharacter(firstChar);

      if (decodedControlChar !== -1) {
        // single character escaped values, e.g. \r
        byteArray.push(decodedControlChar);
        index += 2;
      } else {
        // hex escaped values, e.g. \2a
        var hexValue = value.substr(index + 1, 2);

        if (!/^[0-9A-F]{2}$/i.test(hexValue)) {
          throw new Error("invalid character encoding");
        }

        byteArray.push(parseInt(hexValue, 16));
        index += 3;
      }
    } else {
      // ASCII encoded values
      byteArray.push(charCode);
      index++;
    }
  }

  return byteArray;
}

Version data entries

27 entries across 26 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.18.0 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.18.2 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.16.1 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.15.2 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.18.4 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.18.1 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.14.0 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
tang-0.2.1 spec/tang_app/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
tang-0.2.0 spec/tang_app/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
tang-0.1.0 spec/tang_app/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
tang-0.0.9 spec/tang_app/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
enju_library-0.3.8 spec/dummy/node_modules/@webassemblyjs/wast-parser/esm/string-literals.js
jester-data-8.0.0 node_modules/@webassemblyjs/wast-parser/esm/string-literals.js