| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Utilities for string newlines. |
| 17 |
* @author [email protected] (Nathan Naze) |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* Namespace for string utilities |
| 23 |
*/ |
| 24 |
goog.provide('goog.string.newlines'); |
| 25 |
goog.provide('goog.string.newlines.Line'); |
| 26 |
|
| 27 |
goog.require('goog.array'); |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Splits a string into lines, properly handling universal newlines. |
| 32 |
* @param {string} str String to split. |
| 33 |
* @param {boolean=} opt_keepNewlines Whether to keep the newlines in the |
| 34 |
* resulting strings. Defaults to false. |
| 35 |
* @return {!Array<string>} String split into lines. |
| 36 |
*/ |
| 37 |
goog.string.newlines.splitLines = function(str, opt_keepNewlines) { |
| 38 |
var lines = goog.string.newlines.getLines(str); |
| 39 |
return goog.array.map(lines, function(line) { |
| 40 |
return opt_keepNewlines ? line.getFullLine() : line.getContent(); |
| 41 |
}); |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Line metadata class that records the start/end indicies of lines |
| 48 |
* in a string. Can be used to implement common newline use cases such as |
| 49 |
* splitLines() or determining line/column of an index in a string. |
| 50 |
* Also implements methods to get line contents. |
| 51 |
* |
| 52 |
* Indexes are expressed as string indicies into string.substring(), inclusive |
| 53 |
* at the start, exclusive at the end. |
| 54 |
* |
| 55 |
* Create an array of these with goog.string.newlines.getLines(). |
| 56 |
* @param {string} string The original string. |
| 57 |
* @param {number} startLineIndex The index of the start of the line. |
| 58 |
* @param {number} endContentIndex The index of the end of the line, excluding |
| 59 |
* newlines. |
| 60 |
* @param {number} endLineIndex The index of the end of the line, index |
| 61 |
* newlines. |
| 62 |
* @constructor |
| 63 |
* @struct |
| 64 |
* @final |
| 65 |
*/ |
| 66 |
goog.string.newlines.Line = function( |
| 67 |
string, startLineIndex, endContentIndex, endLineIndex) { |
| 68 |
/** |
| 69 |
* The original string. |
| 70 |
* @type {string} |
| 71 |
*/ |
| 72 |
this.string = string; |
| 73 |
|
| 74 |
/** |
| 75 |
* Index of the start of the line. |
| 76 |
* @type {number} |
| 77 |
*/ |
| 78 |
this.startLineIndex = startLineIndex; |
| 79 |
|
| 80 |
/** |
| 81 |
* Index of the end of the line, excluding any newline characters. |
| 82 |
* Index is the first character after the line, suitable for |
| 83 |
* String.substring(). |
| 84 |
* @type {number} |
| 85 |
*/ |
| 86 |
this.endContentIndex = endContentIndex; |
| 87 |
|
| 88 |
/** |
| 89 |
* Index of the end of the line, excluding any newline characters. |
| 90 |
* Index is the first character after the line, suitable for |
| 91 |
* String.substring(). |
| 92 |
* @type {number} |
| 93 |
*/ |
| 94 |
|
| 95 |
this.endLineIndex = endLineIndex; |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* @return {string} The content of the line, excluding any newline characters. |
| 101 |
*/ |
| 102 |
goog.string.newlines.Line.prototype.getContent = function() { |
| 103 |
return this.string.substring(this.startLineIndex, this.endContentIndex); |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* @return {string} The full line, including any newline characters. |
| 109 |
*/ |
| 110 |
goog.string.newlines.Line.prototype.getFullLine = function() { |
| 111 |
return this.string.substring(this.startLineIndex, this.endLineIndex); |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* @return {string} The newline characters, if any ('\n', \r', '\r\n', '', etc). |
| 117 |
*/ |
| 118 |
goog.string.newlines.Line.prototype.getNewline = function() { |
| 119 |
return this.string.substring(this.endContentIndex, this.endLineIndex); |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Splits a string into an array of line metadata. |
| 125 |
* @param {string} str String to split. |
| 126 |
* @return {!Array<!goog.string.newlines.Line>} Array of line metadata. |
| 127 |
*/ |
| 128 |
goog.string.newlines.getLines = function(str) { |
| 129 |
// We use the constructor because literals are evaluated only once in |
| 130 |
// < ES 3.1. |
| 131 |
// See http://www.mail-archive.com/[email protected]/msg01796.html |
| 132 |
var re = RegExp('\r\n|\r|\n', 'g'); |
| 133 |
var sliceIndex = 0; |
| 134 |
var result; |
| 135 |
var lines = []; |
| 136 |
|
| 137 |
while (result = re.exec(str)) { |
| 138 |
var line = new goog.string.newlines.Line( |
| 139 |
str, sliceIndex, result.index, result.index + result[0].length); |
| 140 |
lines.push(line); |
| 141 |
|
| 142 |
// remember where to start the slice from |
| 143 |
sliceIndex = re.lastIndex; |
| 144 |
} |
| 145 |
|
| 146 |
// If the string does not end with a newline, add the last line. |
| 147 |
if (sliceIndex < str.length) { |
| 148 |
var line = |
| 149 |
new goog.string.newlines.Line(str, sliceIndex, str.length, str.length); |
| 150 |
lines.push(line); |
| 151 |
} |
| 152 |
|
| 153 |
return lines; |
| 154 |
}; |