Last active
November 13, 2017 11:20
-
-
Save davebrny/13b4ab574fd125d732749b406c4351d5 to your computer and use it in GitHub Desktop.
🥓 (autohotkey) - strip comments, comments blocks and empty lines from a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
[strip] | |
version = 2.3.1 | |
description = strip comments, comment blocks and empty lines from a string | |
author = davebrny | |
source = https://gist.github.com/davebrny/13b4ab574fd125d732749b406c4351d5 | |
*/ | |
strip(string, option="") { | |
if option contains f,1 | |
{ | |
revert_batch := a_batchLines | |
setBatchLines, -1 | |
option := (option = "f") ? ("") : (option) ; clear if no other options | |
} | |
if option contains 2,3,4,5,6 | |
{ ; convert numbers to letters | |
for what, with in {"2":"b", "3":"c", "4":"m", "5":"L", "6":"e"} | |
stringReplace, option, option, % what, % with, all | |
} | |
else if (option = "") | |
option := "bcm" ; default settings | |
; ------------------------ | |
if inStr(option, "b") and if inStr(string, "/*") | |
string := strip_blocks(string) | |
if inStr(option, "c") | |
string := strip_comments(string) | |
if inStr(option, "L") and !inStr(option, "c") | |
string := strip_lines(string) | |
if inStr(option, "e") and !inStr(option, "c") | |
string := strip_ends(string) | |
if inStr(option, "m") | |
string := strip_empty(string) | |
if inStr(option, "`r") | |
string := strReplace(string, "`r", "") | |
if inStr(option, "`n") | |
string := strReplace(string, "`n", "") | |
if inStr(option, "`t") or inStr(option, a_tab) | |
string := strReplace(string, a_tab, "") | |
if inStr(option, "`s") or inStr(option, a_space) | |
string := strReplace(string, a_space, "") | |
; ------------------------ | |
if (revert_batch) | |
setBatchLines, % revert_batch | |
return string | |
} | |
;# strip comment blocks | |
strip_blocks(string) { | |
original_string := string | |
if inStr(string, "`r`n") | |
stringReplace, string, string, `r`n, `n, all | |
if (subStr(string, 1, 2) = "/*") ; if at very first line | |
stringReplace, string, string, % "/*", % "`n/*" | |
strReplace(string, "`n/*", "", block_start) ; get number of blocks | |
strReplace(string, "`n*/", "", block_end) | |
block_count := (block_start > block_end) ? (block_end) : (block_start) | |
loop, % block_count | |
{ | |
if (a_index = 1) ; get text before 1st block | |
{ | |
stringGetPos, block_start, string, % "`n/*", L1 | |
stringMid, first_section, string, block_start, , L | |
new_string .= first_section "`n" | |
} | |
else ; every other loop (text between blocks) | |
{ | |
stringGetPos, block_start, string, % "`n/*", L%a_index% | |
stringMid, middle_sections, string, block_start, , L | |
stringGetPos, block_end, middle_sections, % "`n*/", R1, | |
stringMid, middle_sections, middle_sections, block_end + 5 | |
new_string .= middle_sections "`n" | |
} | |
} ; text after last block | |
stringGetPos, block_end, string, % "`n*/", R1 | |
stringMid, end_section, string, block_end + 5 | |
new_string .= end_section | |
if inStr(original_string, "`r`n") | |
stringReplace, new_string, new_string, `n, `r`n, all | |
return new_string | |
} | |
;# strip line AND end of line comments | |
strip_comments(string) { | |
loop, parse, string, `n, `r | |
{ | |
if (inStr(Ltrim(a_loopField), ";") = 1) | |
continue ; skip line comments | |
if (inStr(a_loopField, ";") and inStr(Ltrim(a_loopField), ";") != 1) ; strip end comments | |
new_lines .= regExReplace(a_loopField, "^;.*$|\s+;.*$") "`r`n" ; (regex from awannaknow) | |
else new_lines .= a_loopField "`r`n" | |
} | |
stringTrimRight, new_lines, new_lines, 2 | |
return new_lines | |
} | |
;# strip line comments | |
strip_lines(string) { | |
loop, parse, string, `n, `r | |
{ | |
if (inStr(Ltrim(a_loopField), ";") = 1) | |
continue ; skip line comments | |
new_lines .= a_loopField "`r`n" | |
} | |
stringTrimRight, new_lines, new_lines, 2 | |
return new_lines | |
} | |
;# strip end of line comments | |
strip_ends(string) { | |
loop, parse, string, `n, `r | |
{ | |
if (inStr(a_loopField, ";") and inStr(Ltrim(a_loopField), ";") != 1) | |
new_lines .= regExReplace(a_loopField, "^;.*$|\s+;.*$") "`r`n" | |
else new_lines .= a_loopField "`r`n" | |
} | |
stringTrimRight, new_lines, new_lines, 2 | |
return new_lines | |
} | |
;# strip out empty lines & lines that only contain whitespace | |
strip_empty(string) { | |
loop, | |
{ | |
stringReplace, string, string, `n`n , `n , all | |
stringReplace, string, string, `r`n`r`n, `r`n, all | |
} | |
until !InStr(string, "`n`n") and !InStr(string, "`r`n`r`n") | |
if inStr(string, "`r`n") | |
new_line := "`r`n" | |
else new_line := "`n" | |
loop, parse, string, `n, `r | |
{ | |
if a_loopField is not space | |
new_string .= a_loopField . new_line | |
} | |
new_string := trim(new_string, new_line) | |
return new_string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
strip()
strip comments, comments blocks and empty lines from a string
strip(string [, options])
Â
options:
if no options are used it defaults to
bcm
c
removes both line comments and end of line commentsc
is used thenL
&e
are ignoredadd
f
or1
to set batchlines to -1 Â (run at full speed)each sub-function can be used separately
Â
strip(string)
2
strip_blocks(string)
3
(L&e)
strip_comments(string)
4
strip_empty(string)
5
strip_lines(string)
6
strip_ends(string)
Â