@@ -41,8 +41,10 @@ public Policy createPolicy() {
41
41
PLATFORM_NATIVE ,
42
42
/** {@code \r\n} */
43
43
WINDOWS ,
44
- /** {@code \n} */
45
- UNIX ;
44
+ /** {@code \n} */
45
+ UNIX ,
46
+ /** {@code \r} */
47
+ MAC_CLASSIC ;
46
48
// @formatter:on
47
49
48
50
/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
@@ -75,6 +77,7 @@ public Policy createPolicy() {
75
77
case PLATFORM_NATIVE : return _platformNativePolicy ;
76
78
case WINDOWS : return WINDOWS_POLICY ;
77
79
case UNIX : return UNIX_POLICY ;
80
+ case MAC_CLASSIC : return MAC_CLASSIC_POLICY ;
78
81
default : throw new UnsupportedOperationException (this + " is a path-specific line ending." );
79
82
}
80
83
}
@@ -96,6 +99,7 @@ public String getEndingFor(File file) {
96
99
97
100
private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy (WINDOWS .str ());
98
101
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy (UNIX .str ());
102
+ private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy (MAC_CLASSIC .str ());
99
103
private static final String _platformNative = System .getProperty ("line.separator" );
100
104
private static final Policy _platformNativePolicy = new ConstantLineEndingPolicy (_platformNative );
101
105
private static final boolean nativeIsWin = _platformNative .equals (WINDOWS .str ());
@@ -117,6 +121,7 @@ public String str() {
117
121
case PLATFORM_NATIVE : return _platformNative ;
118
122
case WINDOWS : return "\r \n " ;
119
123
case UNIX : return "\n " ;
124
+ case MAC_CLASSIC : return "\r " ;
120
125
default : throw new UnsupportedOperationException (this + " is a path-specific line ending." );
121
126
}
122
127
}
@@ -137,12 +142,14 @@ public default boolean isUnix(File file) {
137
142
138
143
/** Returns a string with exclusively unix line endings. */
139
144
public static String toUnix (String input ) {
140
- int firstNewline = input .lastIndexOf ('\n' );
141
- if (firstNewline == -1 ) {
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 {
142
151
// fastest way to detect if a string is already unix-only
143
152
return input ;
144
- } else {
145
- return input .replace ("\r " , "" );
146
153
}
147
154
}
148
155
}
0 commit comments