Skip to content

Commit e6e7ca5

Browse files
committed
Coords: added saving leading spaces
1 parent 8d67c90 commit e6e7ca5

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/main/java/org/scm4j/commons/Coords.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,35 @@ public class Coords {
1515
public String getComment() {
1616
return commentStr;
1717
}
18-
18+
19+
public String ltrim(String s) {
20+
int i = 0;
21+
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
22+
i++;
23+
}
24+
return s.substring(i);
25+
}
26+
27+
public String rtrim(String s) {
28+
int i = s.length() - 1;
29+
while (i > 0 && Character.isWhitespace(s.charAt(i))) {
30+
i--;
31+
}
32+
return s.substring(0, i + 1);
33+
}
34+
1935
public Coords(String coordsString) {
2036
String str = coordsString;
2137
// Comment
2238
{
2339
Integer pos = coordsString.indexOf("#");
24-
40+
2541
if (pos > 0) {
2642
// add spaces between valuable chars and # to comment
27-
commentStr = StringUtils.difference(str.substring(0, pos).trim(), str.substring(0, pos)) + str.substring(pos);
28-
str = str.substring(0, pos).trim();
29-
this.coordsStringNoComment = str;
43+
commentStr = StringUtils.difference(str.substring(0, pos).trim(), ltrim(str.substring(0, pos)))
44+
+ str.substring(pos);
45+
str = rtrim(str.substring(0, pos));
46+
this.coordsStringNoComment = str.trim();
3047
} else {
3148
commentStr = "";
3249
this.coordsStringNoComment = coordsString;
@@ -93,25 +110,25 @@ public String toString() {
93110

94111
public String toString(String versionStr) {
95112
String str = versionStr + classifier + extension;
96-
return getName() + (str.isEmpty() ? "" : ":" + str) + commentStr;
113+
return getName() + (str.isEmpty() ? "" : ":" + str) + commentStr;
97114
}
98-
115+
99116
public String getName() {
100117
return groupId + ":" + artifactId;
101118
}
102-
119+
103120
public String getGroupId() {
104121
return groupId;
105122
}
106-
123+
107124
public String getArtifactId() {
108125
return artifactId;
109126
}
110127

111128
public String getExtension() {
112129
return extension;
113130
}
114-
131+
115132
public String getClassifier() {
116133
return classifier;
117134
}

src/test/java/org/scm4j/commons/CoordsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Coords dc(String coords) {
2525
public void testComment() {
2626
assertEquals("", dc("com.myproject:c1").getComment());
2727
assertEquals("#", dc("com.myproject:c1#").getComment());
28+
assertEquals(" # ", dc(" com.myproject:c1 # ").getComment());
2829
assertEquals("#...$ #", dc("com.myproject:c1#...$ #").getComment());
2930
}
3031

0 commit comments

Comments
 (0)