File tree Expand file tree Collapse file tree 2 files changed +19
-8
lines changed
lib/src/main/java/com/diffplug/spotless
testlib/src/test/java/com/diffplug/spotless Expand file tree Collapse file tree 2 files changed +19
-8
lines changed Original file line number Diff line number Diff line change @@ -142,14 +142,17 @@ public default boolean isUnix(File file) {
142
142
143
143
/** Returns a string with exclusively unix line endings. */
144
144
public static String toUnix (String input ) {
145
- if (input .lastIndexOf (WINDOWS .str ()) > -1 ) {
146
- return input .replace ("\r " , "" );
147
- } else if (input .lastIndexOf (MAC_CLASSIC .str ()) > -1 ) {
148
- // replace mac classic '\r' with unix line endings '\n'
149
- return input .replace (MAC_CLASSIC .str (), UNIX .str ());
150
- } else {
151
- // fastest way to detect if a string is already unix-only
145
+ int lastCarriageReturn = input .lastIndexOf ('\r' );
146
+ if (lastCarriageReturn == -1 ) {
152
147
return input ;
148
+ } else {
149
+ if (input .lastIndexOf ("\r \n " ) == -1 ) {
150
+ // it is MAC_CLASSIC \r
151
+ return input .replace ('\r' , '\n' );
152
+ } else {
153
+ // it is WINDOWS \r\n
154
+ return input .replace ("\r " , "" );
155
+ }
153
156
}
154
157
}
155
158
}
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2022 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
24
25
+ import org .junit .jupiter .api .Assertions ;
25
26
import org .junit .jupiter .api .Test ;
26
27
27
28
import com .diffplug .common .base .StandardSystemProperty ;
28
29
import com .diffplug .spotless .generic .EndWithNewlineStep ;
29
30
30
31
class FormatterTest {
32
+ @ Test
33
+ void toUnix () {
34
+ Assertions .assertEquals ("1\n 2\n 3" , LineEnding .toUnix ("1\n 2\n 3" ));
35
+ Assertions .assertEquals ("1\n 2\n 3" , LineEnding .toUnix ("1\r 2\r 3" ));
36
+ Assertions .assertEquals ("1\n 2\n 3" , LineEnding .toUnix ("1\r \n 2\r \n 3" ));
37
+ }
38
+
31
39
// Formatter normally needs to be closed, but no resources will be leaked in this special case
32
40
@ Test
33
41
void equality () {
You can’t perform that action at this time.
0 commit comments