Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Unified Diff: third_party/WebKit/Source/devtools/front_end/common/Text.js

Issue 1809533003: DevTools: remove illusionary caching from String.prototype.lineEndings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: String.prototype.lineEndings -> String.prototype.computeLineEndings Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/common/Text.js
diff --git a/third_party/WebKit/Source/devtools/front_end/common/Text.js b/third_party/WebKit/Source/devtools/front_end/common/Text.js
new file mode 100644
index 0000000000000000000000000000000000000000..4e02e37d912ecfe86b03dc0f7d4bf69008564004
--- /dev/null
+++ b/third_party/WebKit/Source/devtools/front_end/common/Text.js
@@ -0,0 +1,97 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @constructor
+ * @param {string} value
+ */
+WebInspector.Text = function(value)
+{
+ this._value = value;
+}
+
+WebInspector.Text.prototype = {
+ /**
+ * @return {!Array<number>}
+ */
+ lineEndings: function()
+ {
+ if (!this._lineEndings)
+ this._lineEndings = this._value.computeLineEndings();
+ return this._lineEndings;
+ },
+
+ /**
+ * @return {string}
+ */
+ value: function()
+ {
+ return this._value;
+ },
+
+ /**
+ * @return {number}
+ */
+ lineCount: function()
+ {
+ var lineEndings = this.lineEndings();
+ return lineEndings.length;
+ },
+
+ /**
+ * @param {number} lineNumber
+ * @param {number} columNumber
+ * @return {number}
+ */
+ offsetFromPosition: function(lineNumber, columNumber)
+ {
+ return (lineNumber ? this.lineEndings()[lineNumber - 1] + 1 : 0) + columNumber;
+ },
+
+ /**
+ * @return {string}
+ */
+ lineAt: function(lineNumber)
+ {
+ var lineEndings = this.lineEndings();
+ var lineStart = lineNumber > 0 ? lineEndings[lineNumber - 1] + 1 : 0;
+ var lineEnd = lineEndings[lineNumber];
+ var lineContent = this._value.substring(lineStart, lineEnd);
+ if (lineContent.length > 0 && lineContent.charAt(lineContent.length - 1) === "\r")
+ lineContent = lineContent.substring(0, lineContent.length - 1);
+ return lineContent;
+ },
+
+ /**
+ * @param {!WebInspector.TextRange} range
+ * @return {!WebInspector.SourceRange}
+ */
+ toSourceRange: function(range)
+ {
+ var start = this.offsetFromPosition(range.startLine, range.startColumn);
+ var end = this.offsetFromPosition(range.endLine, range.endColumn);
+ return new WebInspector.SourceRange(start, end - start);
+ },
+
+ /**
+ * @param {!WebInspector.TextRange} range
+ * @param {string} replacement
+ * @return {string}
+ */
+ replaceRange: function(range, replacement)
+ {
+ var sourceRange = this.toSourceRange(range);
+ return this._value.substring(0, sourceRange.offset) + replacement + this._value.substring(sourceRange.offset + sourceRange.length);
+ },
+
+ /**
+ * @param {!WebInspector.TextRange} range
+ * @return {string}
+ */
+ extract: function(range)
+ {
+ var sourceRange = this.toSourceRange(range);
+ return this._value.substr(sourceRange.offset, sourceRange.length);
+ },
+}

Powered by Google App Engine
This is Rietveld 408576698