Skip to content

Commit

Permalink
refactor BstTextPrefixer.textPrefix (#11746)
Browse files Browse the repository at this point in the history
* refactor BstTextPrefixer.textPrefix

1. add more unit test about nested brace cases
2. refactor the `textPrefix` method to make it clear

* rewrite unit test

use @ParameterizedTest and @CsvSource to rewrite unit test
  • Loading branch information
leaf-soba authored Sep 10, 2024
1 parent ce9e34f commit 14ba56d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 54 deletions.
88 changes: 52 additions & 36 deletions src/main/java/org/jabref/logic/bst/util/BstTextPrefixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,65 @@ public class BstTextPrefixer {
private BstTextPrefixer() {
}

public static String textPrefix(int inNumOfChars, String toPrefix) {
int numOfChars = inNumOfChars;
public static String textPrefix(int numOfChars, String toPrefix) {
StringBuilder sb = new StringBuilder();

PrefixState prefixState = new PrefixState(0, 0, numOfChars);
char[] cs = toPrefix.toCharArray();
int n = cs.length;
int i = 0;

int braceLevel = 0;
while (prefixState.index < cs.length && prefixState.numOfChars > 0) {
char c = cs[prefixState.index++];
handleOpeningBrace(cs, prefixState, c);
handleClosingBrace(prefixState, toPrefix, c);
boolean isNormalCharacter = c != '{' && c != '}';
if (isNormalCharacter) {
prefixState.numOfChars--;
}
}
sb.append(toPrefix, 0, prefixState.index);
// Append any remaining closing braces if unbalanced
while (prefixState.braceLevel-- > 0) {
sb.append('}');
}
return sb.toString();
}

while ((i < n) && (numOfChars > 0)) {
char c = cs[i];
i++;
if (c == '{') {
braceLevel++;
if ((braceLevel == 1) && (i < n) && (cs[i] == '\\')) {
i++; // skip backslash
while ((i < n) && (braceLevel > 0)) {
if (cs[i] == '}') {
braceLevel--;
} else if (cs[i] == '{') {
braceLevel++;
}
i++;
}
numOfChars--;
}
} else if (c == '}') {
if (braceLevel > 0) {
braceLevel--;
} else {
LOGGER.warn("Unbalanced brace in string for purify$: {}", toPrefix);
private static void handleOpeningBrace(char[] cs, PrefixState prefixState, char c) {
if (c != '{') {
return;
}
prefixState.braceLevel++;
if ((prefixState.braceLevel == 1) && (prefixState.index < cs.length) && (cs[prefixState.index] == '\\')) {
prefixState.index++; // skip backslash
while ((prefixState.index < cs.length) && (prefixState.braceLevel > 0)) {
if (cs[prefixState.index] == '}') {
prefixState.braceLevel--;
} else if (cs[prefixState.index] == '{') {
prefixState.braceLevel++;
}
} else {
numOfChars--;
prefixState.index++;
}
prefixState.numOfChars--;
}
sb.append(toPrefix, 0, i);
while (braceLevel > 0) {
sb.append('}');
braceLevel--;
}

private static void handleClosingBrace(PrefixState prefixState, String toPrefix, char c) {
if (c != '}') {
return;
}
if (prefixState.braceLevel > 0) {
prefixState.braceLevel--;
} else {
LOGGER.warn("Unbalanced brace in string for purify$: {}", toPrefix);
}
}

return sb.toString();
private static class PrefixState {
public int index;
public int braceLevel;
public int numOfChars;
public PrefixState(int index, int braceLevel, int numOfChars) {
this.index = index;
this.braceLevel = braceLevel;
this.numOfChars = numOfChars;
}
}
}
42 changes: 24 additions & 18 deletions src/test/java/org/jabref/logic/bst/util/BstTextPrefixerTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package org.jabref.logic.bst.util;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class BstTextPrefixerTest {

@Test
void prefix() {
assertPrefix("i", "i");
assertPrefix("0I~ ", "0I~ ");
assertPrefix("Hi Hi", "Hi Hi ");
assertPrefix("{\\oe}", "{\\oe}");
assertPrefix("Hi {\\oe }H", "Hi {\\oe }Hi ");
assertPrefix("Jonat", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
assertPrefix("{\\'e}", "{\\'e}");
assertPrefix("{\\'{E}}doua", "{\\'{E}}douard Masterly");
assertPrefix("Ulric", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
}

private static void assertPrefix(final String string, final String string2) {
assertEquals(string, BstTextPrefixer.textPrefix(5, string2));
public class BstTextPrefixerTest {
@ParameterizedTest
@CsvSource({
"i, i",
"0I~ , 0I~ ",
"Hi Hi, Hi Hi ",
"{\\oe}, {\\oe}",
"Hi {\\oe }H, Hi {\\oe }Hi ",
"Jonat, Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin",
"{\\'e}, {\\'e}",
"{\\'{E}}doua, {\\'{E}}douard Masterly",
"Ulric, Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot",
"abcd{e}, abcd{efg}hi",
"ab{cd}e, ab{cd}efghi",
"ab{cd}e, ab{cd}efghi{}",
"Hi {{\\o}}, Hi {{\\oe }}Hi ",
"Hi {\\{oe }}H, Hi {\\{oe }}Hi ",
"Hi {\\\"oe }H, Hi {\\\"oe }Hi ",
"Hi {\\{\\oe }}H, Hi {\\{\\oe }}Hi "
})
void assertPrefix(final String expectedResult, final String toPrefixInput) {
assertEquals(expectedResult, BstTextPrefixer.textPrefix(5, toPrefixInput));
}
}

0 comments on commit 14ba56d

Please sign in to comment.