Commit 4da0d0a4 by ibaker Committed by Ian Baker

In-line calls to SpannableStringBuilder.length() in TtmlNode

This avoids keeping a redundant (and potentially out of sync) copy of
the same info in builderLength.

PiperOrigin-RevId: 290709360
parent 3aa52c23
...@@ -364,8 +364,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -364,8 +364,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
private static void cleanUpText(SpannableStringBuilder builder) { private static void cleanUpText(SpannableStringBuilder builder) {
// Having joined the text elements, we need to do some final cleanup on the result. // Having joined the text elements, we need to do some final cleanup on the result.
// 1. Collapse multiple consecutive spaces into a single space. // 1. Collapse multiple consecutive spaces into a single space.
int builderLength = builder.length(); for (int i = 0; i < builder.length(); i++) {
for (int i = 0; i < builderLength; i++) {
if (builder.charAt(i) == ' ') { if (builder.charAt(i) == ' ') {
int j = i + 1; int j = i + 1;
while (j < builder.length() && builder.charAt(j) == ' ') { while (j < builder.length() && builder.charAt(j) == ' ') {
...@@ -374,36 +373,30 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -374,36 +373,30 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
int spacesToDelete = j - (i + 1); int spacesToDelete = j - (i + 1);
if (spacesToDelete > 0) { if (spacesToDelete > 0) {
builder.delete(i, i + spacesToDelete); builder.delete(i, i + spacesToDelete);
builderLength -= spacesToDelete;
} }
} }
} }
// 2. Remove any spaces from the start of each line. // 2. Remove any spaces from the start of each line.
if (builderLength > 0 && builder.charAt(0) == ' ') { if (builder.length() > 0 && builder.charAt(0) == ' ') {
builder.delete(0, 1); builder.delete(0, 1);
builderLength--;
} }
for (int i = 0; i < builderLength - 1; i++) { for (int i = 0; i < builder.length() - 1; i++) {
if (builder.charAt(i) == '\n' && builder.charAt(i + 1) == ' ') { if (builder.charAt(i) == '\n' && builder.charAt(i + 1) == ' ') {
builder.delete(i + 1, i + 2); builder.delete(i + 1, i + 2);
builderLength--;
} }
} }
// 3. Remove any spaces from the end of each line. // 3. Remove any spaces from the end of each line.
if (builderLength > 0 && builder.charAt(builderLength - 1) == ' ') { if (builder.length() > 0 && builder.charAt(builder.length() - 1) == ' ') {
builder.delete(builderLength - 1, builderLength); builder.delete(builder.length() - 1, builder.length());
builderLength--;
} }
for (int i = 0; i < builderLength - 1; i++) { for (int i = 0; i < builder.length() - 1; i++) {
if (builder.charAt(i) == ' ' && builder.charAt(i + 1) == '\n') { if (builder.charAt(i) == ' ' && builder.charAt(i + 1) == '\n') {
builder.delete(i, i + 1); builder.delete(i, i + 1);
builderLength--;
} }
} }
// 4. Trim a trailing newline, if there is one. // 4. Trim a trailing newline, if there is one.
if (builderLength > 0 && builder.charAt(builderLength - 1) == '\n') { if (builder.length() > 0 && builder.charAt(builder.length() - 1) == '\n') {
builder.delete(builderLength - 1, builderLength); builder.delete(builder.length() - 1, builder.length());
/*builderLength--;*/
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment