fix #2244 add appendLineNumber

This commit is contained in:
Shinsuke Sugaya 2019-09-09 22:58:30 +09:00
parent f00996405d
commit 9422af78fa
2 changed files with 23 additions and 0 deletions

View file

@ -252,4 +252,16 @@ public class DocumentHelper {
return hash;
}
public String appendLineNumber(final String prefix, final String content) {
if (content == null) {
return StringUtil.EMPTY;
}
final String[] values = content.split("\n");
final StringBuilder buf = new StringBuilder((int) (content.length() * 1.3));
buf.append(prefix).append(1).append(':').append(values[0]);
for (int i = 1; i < values.length; i++) {
buf.append('\n').append(prefix).append(i + 1).append(':').append(values[i]);
}
return buf.toString();
}
}

View file

@ -174,4 +174,15 @@ public class DocumentHelperTest extends UnitFessTestCase {
assertNull(documentHelper.decodeSimilarDocHash(null));
}
public void test_appendLineNumber() {
DocumentHelper documentHelper = new DocumentHelper();
assertEquals("", documentHelper.appendLineNumber("L", null));
assertEquals("L1:", documentHelper.appendLineNumber("L", ""));
assertEquals("L1:aaa", documentHelper.appendLineNumber("L", "aaa"));
assertEquals("L1:aaa", documentHelper.appendLineNumber("L", "aaa\n"));
assertEquals("L1:aaa\nL2:bbb", documentHelper.appendLineNumber("L", "aaa\nbbb"));
assertEquals("L1:aaa\nL2:bbb\nL3:ccc", documentHelper.appendLineNumber("L", "aaa\nbbb\nccc"));
}
}