Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 3a6cd1a

Browse files
author
Yuncong Zhang
committed
Make newLinePositions array.
1 parent 9aa23af commit 3a6cd1a

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

Runtime/ui/txt/linebreaker.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,32 @@ public static LineBreaker instance {
9595

9696
static LineBreaker _instance;
9797

98-
public static List<int> newLinePositions {
99-
get {
100-
if (_newLinePositions == null)
101-
_newLinePositions = new List<int>();
102-
return _newLinePositions;
98+
public static int[] newLinePositions(string text, out int count) {
99+
count = 0;
100+
for (var i = 0; i < text.Length; i++) {
101+
if (text[i] == '\n') {
102+
count++;
103+
}
104+
}
105+
106+
count++;
107+
108+
if (_newLinePositions == null || _newLinePositions.Length < count) {
109+
_newLinePositions = new int[count];
103110
}
111+
112+
count = 0;
113+
for (var i = 0; i < text.Length; i++) {
114+
if (text[i] == '\n') {
115+
_newLinePositions[count++] = i;
116+
}
117+
}
118+
_newLinePositions[count++] = text.Length;
119+
120+
return _newLinePositions;
104121
}
105122

106-
static List<int> _newLinePositions;
123+
static int[] _newLinePositions;
107124

108125
TextBuff _textBuf;
109126
float[] _charWidths;

Runtime/ui/txt/paragraph.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -807,13 +807,12 @@ int _computeLineBreak() {
807807
? this._paragraphStyle.maxLines ?? 1
808808
: this._paragraphStyle.maxLines ?? 0;
809809

810-
var newLinePositions = LineBreaker.newLinePositions;
811-
this._computeNewLinePositions(newLinePositions);
810+
var newLinePositions = LineBreaker.newLinePositions(this._text, out int newLineCount);
812811

813812
var lineBreaker = LineBreaker.instance;
814813
int runIndex = 0;
815814
int countRuns = 0;
816-
for (var newlineIndex = 0; newlineIndex < newLinePositions.Count; ++newlineIndex) {
815+
for (var newlineIndex = 0; newlineIndex < newLineCount; ++newlineIndex) {
817816
if (lineLimit != 0 && this._lineRanges.Count >= lineLimit) {
818817
break;
819818
}
@@ -910,17 +909,6 @@ void _updateBreaks(LineBreaker lineBreaker, int breaksCount, int blockStart, int
910909
}
911910
}
912911

913-
void _computeNewLinePositions(List<int> newLinePositions) {
914-
newLinePositions.Clear();
915-
for (var i = 0; i < this._text.Length; i++) {
916-
if (this._text[i] == '\n') {
917-
newLinePositions.Add(i);
918-
}
919-
}
920-
921-
newLinePositions.Add(this._text.Length);
922-
}
923-
924912
int _computeMaxWordCount() {
925913
int max = 0;
926914
for (int lineNumber = 0; lineNumber < this._lineRanges.Count; lineNumber++) {

0 commit comments

Comments
 (0)