Skip to content

Commit fd60dfb

Browse files
committed
refactor: use char instead of String in DiffSymbol
This makes the assumption of single character `DiffSymbol`s explicit.
1 parent f22b3cd commit fd60dfb

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

src/main/java/org/variantsync/diffdetective/variation/VariationUnparser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ public static <L extends Label> String unparseDiff(VariationDiff<L> diff) throws
4444
* @return the state before or after the diff
4545
*/
4646
public static String undiff(String diff, Time time) {
47-
String excludedDiffSymbol = DiffType.thatExistsOnlyAt(time.other()).symbol;
47+
char excludedDiffSymbol = DiffType.thatExistsOnlyAt(time.other()).symbol;
4848

4949
String result = diff
5050
.lines()
51-
.filter(line -> !line.startsWith(excludedDiffSymbol))
52-
// TODO assumes that all diff symbols are exactly 1 char long
51+
.filter(line -> line.isEmpty() || line.charAt(0) != excludedDiffSymbol)
5352
.map(line -> line.isEmpty() ? "" : line.substring(1))
5453
.collect(Collectors.joining("\n"));
5554

src/main/java/org/variantsync/diffdetective/variation/diff/DiffType.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
* These values correspond to the domain of the Delta function from our paper.
1313
*/
1414
public enum DiffType {
15-
ADD("+"),
16-
REM("-"),
17-
NON(" ");
15+
ADD('+'),
16+
REM('-'),
17+
NON(' ');
1818

19-
public final String symbol;
19+
public final char symbol;
2020

21-
DiffType(String symbol) {
21+
DiffType(char symbol) {
2222
this.symbol = symbol;
2323
}
2424

@@ -139,15 +139,20 @@ public DiffType inverse() {
139139
* @return The type of edit of <code>line</code> or null if its an invalid diff line.
140140
*/
141141
public static DiffType ofDiffLine(String line) {
142-
if (line.startsWith(ADD.symbol)) {
143-
return ADD;
144-
} else if (line.startsWith(REM.symbol)) {
145-
return REM;
146-
} else if (line.startsWith(NON.symbol) || line.isEmpty()) {
142+
if (line.isEmpty()) {
147143
// Diff lines should ideally have at least one character specifying a line's type (i.e., one of the diff
148144
// type characters: '+', '-', or ' '). However, this is not necessarily the case and unchanged lines may
149145
// be empty. We thus treat empty lines in a diff as unchanged (i.e., NON),
150146
return NON;
147+
}
148+
149+
final char diffSymbol = line.charAt(0);
150+
if (diffSymbol == ADD.symbol) {
151+
return ADD;
152+
} else if (diffSymbol == REM.symbol) {
153+
return REM;
154+
} else if (diffSymbol == NON.symbol) {
155+
return NON;
151156
} else {
152157
return null;
153158
}

0 commit comments

Comments
 (0)