-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy patheol-convert.ahk
79 lines (62 loc) · 2.15 KB
/
eol-convert.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
; https://github.com/cocobelgica/AutoHotkey-Util/blob/master/eol.ahk
; DOS:=CRLF, Unix:=LF, Mac:=CR
; Underscore in name as to not match "TO_DO"
EolTo_Dos(str) {
return eol(str, "DOS")
}
EolToUnix(str) {
return eol(str, "LF")
}
EolToMac(str) {
return eol(str, "Mac")
}
/* Function: eol
* Convert newline in strings to/from "`r`n", "`n", "`r"
* Requirements:
* AutoHotkey v1.1+ OR v2.0-a+
* License:
* WTFPL [http://wtfpl.net]
* Syntax:
* out := eol( str [ , eol := "`n" ] )
* Parameters:
* out [retval] - output string
* str [in] - input string
* eol [in, opt] - the newline to convert to. Valid values are: '`r`n',
* '`n', '`r', 'CRLF', 'LF', 'CR', 'DOS', 'Unix' and 'Mac'.
*/
eol(str, eol:="`n")
{
; return RegexReplace(str, "\r\n", "`n")
if (str == "") ; do nothing if empty OR return eol??
return
static CRLF := "`r`n", LF := "`n", CR := "`r", DOS:=CRLF, Unix:=LF, Mac:=CR
if (InStr(" `r`n", eol) <= 1) ; make sure blank("") strings are detected
if !(eol ~= "i)^CR(LF)?|LF|dos|unix|mac$" ? eol := %eol% : 0)
throw Exception("Invalid 'eol' parameter", -1, eol)
if (eol == "`r`n") ; to CRLF (CRLF to LF -> CR to LF -> LF to CRLF)
eols := Array(["`r`n", "`n"], ["`r", "`n"], ["`n", "`r`n"])
else if (eol == "`n") ; to LF (CRLF to LF -> CR to LF)
eols := Array(["`r`n", "`n"], ["`r", "`n"])
else if (eol == "`r") ; to CR (CRLF to CR -> LF to CR)
eols := Array(["`r`n", "`r"], ["`n", "`r"])
for each, nl in eols
{
; v1.1+ and v2.0-a StringReplace/StrReplace workaround
; alternative: str := RegExReplace(str, nl[1], nl[2])
; If dual-compatibilty is not required, comment this out and uncomment
; either of the one below for your respective AHK version
p := A_Index>2 ? -1 : 0
while p := InStr(str, nl[1],, p + StrLen(nl[2]))
str := SubStr(str, 1, p-1) . nl[2] . SubStr(str, p + StrLen(nl[1]))
; v1.1+
; StringReplace, str, str, % nl[1], % nl[2], All
; v2.0-a
; str := StrReplace(str, nl[1], nl[2])
}
/* Add blank line at end(for LF and CR)??
if (eol != "`r`n") && StrLen(str)
if (SubStr(str, A_AhkVersion<"2" ? 0 : -1) != eol)
str .= eol
*/
return str
}