all files / lib/ ranges.js

78.38% Statements 58/74
66.67% Branches 20/30
54.55% Functions 6/11
78.38% Lines 58/74
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   148044× 148044×     148044× 148044× 148044× 148044× 148044×             74014×     74014×           132× 132×     132× 132× 132×                                                                               148176× 148176×     148176× 147818×   358× 358× 358× 50638× 50638× 2584×     358× 358× 358× 358×     148044× 148044× 148044× 19184×   128860× 5452×   123408× 123408× 123408× 347306× 347306× 123408×   223898× 131772×   92126×              
(function() {
  var buildRowOffsets, lastRowOffsets, lastRowOffsetsPrefix, lastRowOffsetsSource, locToPos, locsToRange, offsetToPos, offsetToRow, offsetsToRange, rowColToPos, rowColsToRange, stringifyPos, stringifyRange;
 
  module.exports.offsetToPos = offsetToPos = function(offset, source, prefix) {
    var col, row, rowOffsets;
    Iif (prefix == null) {
      prefix = '';
    }
    rowOffsets = buildRowOffsets(source, prefix);
    offset -= prefix.length;
    row = offsetToRow(offset, rowOffsets);
    col = offset - rowOffsets[row];
    return {
      ofs: offset,
      row: row,
      col: col
    };
  };
 
  module.exports.offsetsToRange = offsetsToRange = function(start, end, source, prefix) {
    Iif (prefix == null) {
      prefix = '';
    }
    return {
      start: offsetToPos(start, source, prefix),
      end: offsetToPos(end, source, prefix)
    };
  };
 
  module.exports.rowColToPos = rowColToPos = function(row, col, source, prefix) {
    var offset, rowOffsets;
    Iif (prefix == null) {
      prefix = '';
    }
    rowOffsets = buildRowOffsets(source, prefix);
    offset = rowOffsets[row] + col;
    return {
      ofs: offset,
      row: row,
      col: col
    };
  };
 
  module.exports.rowColsToRange = rowColsToRange = function(start, end, source, prefix) {
    if (prefix == null) {
      prefix = '';
    }
    return {
      start: rowColToPos(start.row, start.col, source, prefix),
      end: rowColToPos(end.row, end.col, source, prefix)
    };
  };
 
  module.exports.locToPos = locToPos = function(loc, source, prefix) {
    if (prefix == null) {
      prefix = '';
    }
    return rowColToPos(loc.line, loc.column, source, prefix);
  };
 
  module.exports.locsToRange = locsToRange = function(start, end, source, prefix) {
    if (prefix == null) {
      prefix = '';
    }
    return {
      start: locToPos(start, source, prefix),
      end: locToPos(end, source, prefix)
    };
  };
 
  module.exports.stringifyPos = stringifyPos = function(pos) {
    return "{ofs: " + pos.ofs + ", row: " + pos.row + ", col: " + pos.col + "}";
  };
 
  module.exports.stringifyRange = stringifyRange = function(start, end) {
    return "[" + (stringifyPos(start)) + ", " + (stringifyPos(end)) + "]";
  };
 
  lastRowOffsets = null;
 
  lastRowOffsetsSource = null;
 
  lastRowOffsetsPrefix = null;
 
  buildRowOffsets = function(source, prefix) {
    var c, offset, rowOffsets, _i, _len, _ref;
    Iif (prefix == null) {
      prefix = '';
    }
    if (source === lastRowOffsetsSource && prefix === lastRowOffsetsPrefix) {
      return lastRowOffsets;
    }
    rowOffsets = [0];
    _ref = source.substr(prefix.length);
    for (offset = _i = 0, _len = _ref.length; _i < _len; offset = ++_i) {
      c = _ref[offset];
      if (c === '\n') {
        rowOffsets.push(offset + 1);
      }
    }
    lastRowOffsets = rowOffsets;
    lastRowOffsetsSource = source;
    lastRowOffsetsPrefix = prefix;
    return rowOffsets;
  };
 
  offsetToRow = function(offset, rowOffsets) {
    var alen, hi, lo, mid;
    alen = rowOffsets.length;
    if (offset <= 0) {
      return 0;
    }
    if (offset >= rowOffsets[alen - 1]) {
      return alen - 1;
    }
    lo = 0;
    hi = alen - 1;
    while (lo < hi) {
      mid = ~~((hi + lo) / 2);
      if (offset >= rowOffsets[mid] && offset < rowOffsets[mid + 1]) {
        return mid;
      }
      if (offset < rowOffsets[mid]) {
        hi = mid;
      } else {
        lo = mid;
      }
    }
    throw new Error("Bug in offsetToRow()");
  };
 
}).call(this);