<commit_before>// SVG Path transformations library
//
// Usage:
//
//    SvgPath('...')
//      .translate(-150, -100)
//      .scale(0.5)
//      .translate(-150, -100)
//      .toFixed(1)
//      .toString()
//

'use strict';


// Class constructor
//
function SvgPath(pathString) {
  if (!(this instanceof SvgPath)) { return new SvgPath(pathString); }

  // Array of path segments.
  // Each segment is array [command, param1, param2, ...]
  this.segments = this.parsePath(pathString);
}


var pathCommands = /[MmZzLlHhVvCcSsQqTtAa][^MmZzLlHhVvCcSsQqTtAa]*/gm;
var pathValues = /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/g;


// Parser code is shamelessly borrowed from Raphael
// https://github.com/DmitryBaranovskiy/raphael/
//
SvgPath.prototype.parsePath = function(pathString) {

  if (!pathString) { return []; }

  var data = [];
  var paramCounts = { a: 7, c: 6, h: 1, l: 2, m: 2, r: 4, q: 4, s: 4, t: 2, v: 1, z: 0 };

  pathString.replace(pathCommands, function(segmentString) {
    var cmd = segmentString[0]; // first character of segment is a command name
    var paramsStr = segmentString.substr(1); // get command parameters - all except command name
    var cmdLowerCase = cmd.toLowerCase();
    var params = [];

    paramsStr.replace(pathValues, function(paramStr) {
      params.push(+paramStr);
    });

    if (cmdLowerCase === "m" && params.length > 2) {
      data.push([cmd].concat(params.splice(0, 2)));
      cmdLowerCase = "l";
      cmd = (cmd === "m") ? "l" : "L";
    }

    if (cmdLowerCase === "r") {
      data.push([cmd].concat(params));
    } else {

      while (params.length >= paramCounts[cmdLowerCase]) {
        data.push([cmd].concat(params.splice(0, paramCounts[cmdLowerCase])));
        if (!paramCounts[cmdLowerCase]) {
          break;
        }
      }
    }
  });

  // First command MUST be always M or m.
  // Make it always M, to avoid unnecesary checks in translate/abs
  if (data[0][0].toLowerCase() !== 'm') {
    data = [];
  } else {
    data[0][0] = 'M';
  }

  return data;
};


// Convert processed SVG Path back to string
//
SvgPath.prototype.toString = function() {
  var elements = [];

  for (var s=0; s<this.segments.length; s++) {
    // remove repeating commands names
    if (s>0 && (this.segments[s][0] === this.segments[s-1][0])) {
      elements.push(this.segments[s].slice(1));
      continue;
    }
    elements.push(this.segments[s]);
  }<commit_msg>speed optimization<commit_after>// SVG Path transformations library
//
// Usage:
//
//    SvgPath('...')
//      .translate(-150, -100)
//      .scale(0.5)
//      .translate(-150, -100)
//      .toFixed(1)
//      .toString()
//

'use strict';


// Class constructor
//
function SvgPath(pathString) {
  if (!(this instanceof SvgPath)) { return new SvgPath(pathString); }

  // Array of path segments.
  // Each segment is array [command, param1, param2, ...]
  this.segments = this.parsePath(pathString);
}


var pathCommands = /[MmZzLlHhVvCcSsQqTtAa][^MmZzLlHhVvCcSsQqTtAa]*/gm;
var pathValues = /[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?/g;


// Parser code is shamelessly borrowed from Raphael
// https://github.com/DmitryBaranovskiy/raphael/
//
SvgPath.prototype.parsePath = function(pathString) {

  if (!pathString) { return []; }

  var data = [];
  var paramCounts = { a: 7, c: 6, h: 1, l: 2, m: 2, r: 4, q: 4, s: 4, t: 2, v: 1, z: 0 };

  pathString.replace(pathCommands, function(segmentString) {
    var cmd = segmentString[0]; // first character of segment is a command name
    var paramsStr = segmentString.substr(1); // get command parameters - all except command name
    var cmdLowerCase = cmd.toLowerCase();
    var params = [];

    paramsStr.replace(pathValues, function(paramStr) {
      params.push(+paramStr);
      return '';
    });

    if (cmdLowerCase === "m" && params.length > 2) {
      data.push([cmd].concat(params.splice(0, 2)));
      cmdLowerCase = "l";
      cmd = (cmd === "m") ? "l" : "L";
    }

    if (cmdLowerCase === "r") {
      data.push([cmd].concat(params));
    } else {

      while (params.length >= paramCounts[cmdLowerCase]) {
        data.push([cmd].concat(params.splice(0, paramCounts[cmdLowerCase])));
        if (!paramCounts[cmdLowerCase]) {
          break;
        }
      }
    }

    return '';
  });

  // First command MUST be always M or m.
  // Make it always M, to avoid unnecesary checks in translate/abs
  if (data[0][0].toLowerCase() !== 'm') {
    data = [];
  } else {
    data[0][0] = 'M';
  }

  return data;
};


// Convert processed SVG Path back to string
//
SvgPath.prototype.toString = function() {
  var elements = [];

  for (var s=0; s<this.segments.length; s++) {
    // remove repeating commands names
    if (s>0 && (this.segments[s][0] === this.segments[s-1][0])) {
      elements.push(this.segments[s].slice(1));
      continue;
    }
    elements.push(this.segments[s]);
  }
{'fraction_non_alphanumeric': 0.18468468468468469, 'fraction_numerical': 0.020475020475020474, 'mean_word_length': 2.7318563789152024, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2671276', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1734, 'n_words': 397}