Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active November 13, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davebrny/13b4ab574fd125d732749b406c4351d5 to your computer and use it in GitHub Desktop.
Save davebrny/13b4ab574fd125d732749b406c4351d5 to your computer and use it in GitHub Desktop.
🥓 (autohotkey) - strip comments, comments blocks and empty lines from a string
/*
[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
}
@davebrny
Copy link
Author

davebrny commented Feb 12, 2017

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 comments

    • if c is used then L & e are ignored
  • add f or 1 to set batchlines to -1   (run at full speed)

  • each sub-function can be used separately

 

option # removes function sublime-snippet trigger
b, c, m (default) strip(string) strip
b 2 blocks strip_blocks(string) stripb
c 3 comments (L&e) strip_comments(string) stripc
m 4 empty lines strip_empty(string) stripm
 
L 5 Line comments strip_lines(string) stripl
e 6 end of line comments strip_ends(string) stripe
 
`r carriage returns
`n linefeeds
`t tabs
`s spaces

 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment