diff options
author | Martin Polden <mpolden@mpolden.no> | 2022-01-28 17:48:47 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2022-01-28 17:48:47 +0100 |
commit | eeed2728e76617b1b7503cc4070b9df2f4971a74 (patch) | |
tree | c05ad037b40e94317020dfeb9650626feb1c7f00 /vespajlib | |
parent | c58fe9e096554ec64a64060db4fc127f9d89f614 (diff) |
Move SnippetGenerator to vespajlib
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/SnippetGenerator.java | 34 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/text/SnippetGeneratorTest.java | 59 |
2 files changed, 93 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/SnippetGenerator.java b/vespajlib/src/main/java/com/yahoo/text/SnippetGenerator.java new file mode 100644 index 00000000000..756fe9b48e4 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/text/SnippetGenerator.java @@ -0,0 +1,34 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.text; + +/** + * Truncate text to a snippet suitable for logging. + * + * @author hakon + */ +public class SnippetGenerator { + + 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() <= 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 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. + int snippetLength = maxPrefixLength + OMIT_PREFIX.length() + sizeString.length() + OMIT_SUFFIX.length() + maxSuffixLength; + if (text.length() <= 1.05 * snippetLength + 5) return text; + + return text.substring(0, maxPrefixLength) + + OMIT_PREFIX + + sizeString + + OMIT_SUFFIX + + text.substring(text.length() - maxSuffixLength); + } +} diff --git a/vespajlib/src/test/java/com/yahoo/text/SnippetGeneratorTest.java b/vespajlib/src/test/java/com/yahoo/text/SnippetGeneratorTest.java new file mode 100644 index 00000000000..e8ffdb16978 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/text/SnippetGeneratorTest.java @@ -0,0 +1,59 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.text; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author hakon + */ +public class SnippetGeneratorTest { + private final SnippetGenerator generator = new SnippetGenerator(); + + private void assertSnippet(String text, int sizeHint, String expectedSnippet) { + assertEquals(expectedSnippet, generator.makeSnippet(text, sizeHint)); + } + + @Test + public void prefixSnippetForReallySmallSizeHint() { + assertSnippet( + "This is a long text that should be snippeted", 0, + "[...44 chars omitted]"); + + assertSnippet( + "This is a long text that should be snippeted", 1, + "[...44 chars omitted]"); + } + + @Test + public void snippet() { + assertSnippet( + "This is a long text that should be snippeted", 23, + "[...44 chars omitted]"); + + assertSnippet( + "This is a long text that should be snippeted", 24, + "T[...43 chars omitted]"); + + assertSnippet( + "This is a long text that should be snippeted", 30, + "This[...37 chars omitted]ted"); + + } + + @Test + public void noShorteningNeeded() { + assertSnippet( + "This is a long text that should be snippeted", 39, + "This is [...28 chars omitted]nippeted"); + + assertSnippet( + "This is a long text that should be snippeted", 40, + "This is a long text that should be snippeted"); + + assertSnippet( + "This is a long text that should be snippeted", 50, + "This is a long text that should be snippeted"); + } +} |