@@ -5,7 +5,7 @@ import Map;
55import Set ;
66import String ;
77
8- set [str ] newLineCharacters = {
8+ list [str ] newLineCharacters = [
99 "\u000A" , // LF
1010 "\u000B" , // VT
1111 "\u000C" , // FF
@@ -14,11 +14,16 @@ set[str] newLineCharacters = {
1414 "\u0085" , // NEL
1515 "\u2028" , // LS
1616 "\u2029" // PS
17- } ;
17+ ] ;
1818
1919private bool bySize (str a , str b ) = size (a ) < size (b );
20+ private bool (str , str ) byIndex (list [str ] indices ) {
21+ return bool (str a , str b ) {
22+ return indexOf (indices , a ) < indexOf (indices , b );
23+ };
24+ }
2025
21- str mostUsedNewline (str input , set [str ] lineseps = newLineCharacters , str (set [str ]) tieBreaker = getFirstFrom ) {
26+ str mostUsedNewline (str input , list [str ] lineseps = newLineCharacters , str (list [str ]) tieBreaker = getFirstFrom ) {
2227 linesepCounts = (nl : 0 | nl <- lineseps );
2328 for (nl <- sort (lineseps , bySize )) {
2429 int count = size (findAll (input , nl ));
@@ -30,7 +35,7 @@ str mostUsedNewline(str input, set[str] lineseps = newLineCharacters, str(set[st
3035 }
3136
3237 byCount = invert (linesepCounts );
33- return tieBreaker (byCount [max (domain (byCount ))]);
38+ return tieBreaker (sort ( byCount [max (domain (byCount ))], byIndex ( lineseps )) );
3439}
3540
3641tuple [str indentation , str rest ] splitIndentation (/^<indentation :\s *><rest :.*> /)
@@ -64,7 +69,7 @@ test bool mostUsedNewlineTestTie()
6469test bool mostUsedNewlineTestGreedy ()
6570 = mostUsedNewline ("\r\n\r\n\n " ) == "\r\n " ;
6671
67- str insertFinalNewline (str input , set [str ] lineseps = newLineCharacters )
72+ str insertFinalNewline (str input , list [str ] lineseps = newLineCharacters )
6873 = any (nl <- lineseps , endsWith (input , nl ))
6974 ? input
7075 : input + mostUsedNewline (input )
@@ -82,7 +87,7 @@ test bool insertFinalNewlineTestMixed()
8287 = insertFinalNewline ("a\n b\r\n " )
8388 == "a\n b\r\n " ;
8489
85- str trimFinalNewline (str input , set [str ] lineseps = newLineCharacters ) {
90+ str trimFinalNewline (str input , list [str ] lineseps = newLineCharacters ) {
8691 orderedSeps = reverse (sort (lineseps , bySize ));
8792 while (nl <- orderedSeps , endsWith (input , nl )) {
8893 input = input [0 ..-size (nl )];
@@ -99,7 +104,7 @@ test bool trimFinalNewlineTestEndOnly()
99104test bool trimFinalNewlineTestWhiteSpace ()
100105 = trimFinalNewline ("a\n\n\n b\n\n " ) == "a\n\n\n b\n\n " ;
101106
102- list [tuple [str , str ]] separateLines (str input , set [str ] lineseps = newLineCharacters ) {
107+ list [tuple [str , str ]] separateLines (str input , list [str ] lineseps = newLineCharacters ) {
103108 orderedSeps = reverse (sort (lineseps , bySize ));
104109
105110 list [tuple [str , str ]] lines = [];
@@ -123,7 +128,7 @@ list[tuple[str, str]] separateLines(str input, set[str] lineseps = newLineCharac
123128str mergeLines (list [tuple [str , str ]] lines )
124129 = ("" | it + line + sep | <line , sep > <- lines );
125130
126- str perLine (str input , str (str ) lineFunc , set [str ] lineseps = newLineCharacters )
131+ str perLine (str input , str (str ) lineFunc , list [str ] lineseps = newLineCharacters )
127132 = mergeLines ([<lineFunc (l ), nl > | <l , nl > <- separateLines (input , lineseps =lineseps )]);
128133
129134test bool perLineTest ()
0 commit comments