Return earlier if possible in makeSnippet
@@ -6,25 +6,27 @@ package com.yahoo.vespa.hosted.node.admin.task.util.text;
*/
public class SnippetGenerator {
- private static final int ASSUMED_OMIT_TEXT_LENGTH = omitText(1234).length();
-
- private static String omitText(int omittedLength) { return "[..." + omittedLength + " chars omitted]"; }
+ private static final String OMIT_PREFIX = "[...";
+ private static final String OMIT_SUFFIX = " chars omitted]";
+ private static final int ASSUMED_OMIT_TEXT_LENGTH = OMIT_PREFIX.length() + 4 + OMIT_SUFFIX.length();
/** Returns a snippet of approximate size. */
public String makeSnippet(String text, int sizeHint) {
- if (text.length() < sizeHint) return text;
+ if (text.length() <= Math.max(sizeHint, ASSUMED_OMIT_TEXT_LENGTH)) return text;
int maxSuffixLength = Math.max(0, (sizeHint - ASSUMED_OMIT_TEXT_LENGTH) / 2);
int maxPrefixLength = Math.max(0, sizeHint - ASSUMED_OMIT_TEXT_LENGTH - maxSuffixLength);
- String snippet =
- text.substring(0, maxPrefixLength) +
- omitText(text.length() - maxPrefixLength - maxSuffixLength) +
- text.substring(text.length() - maxSuffixLength);
+ String sizeString = Integer.toString(text.length() - maxPrefixLength - maxSuffixLength);
// It would be silly to return a snippet when the full text is barely longer.
// Note: Say ASSUMED_OMIT_TEXT_LENGTH=23: text will be returned whenever sizeHint<23 and text.length()<28.
- if (text.length() <= 1.05 * snippet.length() + 5) return text;
+ int snippetLength = maxPrefixLength + OMIT_PREFIX.length() + sizeString.length() + OMIT_SUFFIX.length() + maxSuffixLength;
+ if (text.length() <= 1.05 * snippetLength + 5) return text;
- return snippet;
+ return text.substring(0, maxPrefixLength) +
+ OMIT_PREFIX +
+ sizeString +
+ OMIT_SUFFIX +
+ text.substring(text.length() - maxSuffixLength);
}
}
