Skip to content

Commit ea534cf

Browse files
committed
chore: SP-2487 add extractFilePathFromWFP() utils function
1 parent 0ec8201 commit ea534cf

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/main/java/com/scanoss/utils/WinnowingUtils.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package com.scanoss.utils;
2424

25+
import org.jetbrains.annotations.NotNull;
2526
/**
2627
* SCANOSS Winnowing Utils Class
2728
* <p>
@@ -47,4 +48,37 @@ public static char normalize(char c) {
4748
return 0;
4849
}
4950
}
50-
}
51+
52+
/**
53+
* Extract the file path from a WFP.
54+
* The format of the input string is as follows:
55+
* file=e8585d8740d6664fda9e242a1d68b0f0,1815,demo.ts
56+
* 4=30777ca8,e9227657,81c8e7e1
57+
* 9=831bd2c5,701a2c74
58+
* This method extracts and returns the `demo.ts` part.
59+
*
60+
* @param wfpEntry the WFP entry as a string
61+
* @return the extracted file path, or null if not found
62+
*/
63+
public static String extractFilePathFromWFP(@NotNull String wfpEntry) {
64+
if (wfpEntry.isEmpty()) {
65+
return null;
66+
}
67+
for (String line : wfpEntry.split("\n")) {
68+
if (line.startsWith("file=")) {
69+
// Find the position after "file=" and first two commas
70+
int filePrefix = "file=".length();
71+
int firstComma = line.indexOf(',', filePrefix);
72+
if (firstComma == -1) return null;
73+
74+
int secondComma = line.indexOf(',', firstComma + 1);
75+
if (secondComma == -1) return null;
76+
77+
// Return everything after the second comma
78+
return line.substring(secondComma + 1);
79+
}
80+
}
81+
return null;
82+
}
83+
84+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.scanoss.utils;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class WinnowingUtilsTest {
8+
9+
@Test
10+
public void testExtractFilePathFromWFP_ValidEntry_ReturnsFilePath() {
11+
String wfpEntry = "file=sha1,md5,/path/to/file\n";
12+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
13+
assertEquals("/path/to/file", result);
14+
}
15+
16+
@Test
17+
public void testExtractFilePathFromWFP_EmptyString_ReturnsNull() {
18+
String wfpEntry = "";
19+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
20+
assertNull(result);
21+
}
22+
23+
@Test
24+
public void testExtractFilePathFromWFP_NoFileLine_ReturnsNull() {
25+
String wfpEntry = "another=line\nsomething=else";
26+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
27+
assertNull(result);
28+
}
29+
30+
@Test
31+
public void testExtractFilePathFromWFP_MultipleLinesWithValidFileLine_ReturnsFilePath() {
32+
String wfpEntry = "something=else\nfile=sha1,md5,/valid/path\nanother=line";
33+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
34+
assertEquals("/valid/path", result);
35+
}
36+
37+
@Test
38+
public void testExtractFilePathFromWFP_FileLineWithExtraCommas_ReturnsPath() {
39+
String wfpEntry = "file=sha1,md5,/path,with,comma";
40+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
41+
assertEquals("/path,with,comma", result);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)