-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.lua
More file actions
184 lines (160 loc) · 6.31 KB
/
Copy pathclean.lua
File metadata and controls
184 lines (160 loc) · 6.31 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
local io_open = io.open
local string_find = string.find
local string_sub = string.sub
local table_concat = table.concat
local EXCLUDE_DIRS = {
"target",
".git",
"neoutl-wgpu",
}
local EXT_CONFIGS = {
rs = { jump = '[%/r%"]' }, lua = { jump = '[%-%"%\'%[]' }, slang = { jump = '[%/r%"]' }, json = { jump = '[%"]' }, toml = { jump = '[%#%"%\']' }, yaml = { jump = '[%#%"%\']' } }
local function clean_comments(content, ext)
local len = #content
local result = {}
local r_idx = 1
local last_pos = 1
local i = 1
local config = EXT_CONFIGS[ext]
while i <= len do
local next_idx = string_find(content, config.jump, i)
if not next_idx then break end
i = next_idx
local b1 = string_sub(content, i, i)
if b1 == '"' or b1 == "'" then
local q = b1
i = i + 1
while i <= len do
local _, end_idx = string_find(content, q, i, true)
if not end_idx then i = len + 1 break end
local esc_count = 0
local check_idx = end_idx - 1
while check_idx >= i and string_sub(content, check_idx, check_idx) == '\\' do
esc_count = esc_count + 1
check_idx = check_idx - 1
end
if esc_count % 2 == 0 then
i = end_idx + 1
break
else
i = end_idx + 1
end
end
elseif (ext == "rs" or ext == "slang") and b1 == 'r' then
local n2 = string_sub(content, i+1, i+2)
if string_sub(n2, 1, 1) == '"' then
i = i + 2
local _, end_idx = string_find(content, '"', i, true)
i = end_idx and (end_idx + 1) or (len + 1)
elseif string_sub(n2, 1, 1) == '#' then
local _, start_sharp_end = string_find(content, '"', i + 2, true)
if start_sharp_end then
local sharps = string_sub(content, i + 1, start_sharp_end - 1)
local close_pattern = '"' .. sharps
local _, end_idx = string_find(content, close_pattern, start_sharp_end, true)
i = end_idx and (end_idx + #close_pattern) or (len + 1)
else
i = i + 1
end
else
i = i + 1
end
elseif (ext == "rs" or ext == "slang") and b1 == '/' then
local b2 = string_sub(content, i+1, i+1)
if b2 == '/' then
result[r_idx] = string_sub(content, last_pos, i - 1)
r_idx = r_idx + 1
local _, e = string_find(content, "\n", i + 2, true)
i = e and (e + 1) or (len + 1)
last_pos = i
elseif b2 == '*' then
result[r_idx] = string_sub(content, last_pos, i - 1)
r_idx = r_idx + 1
local _, e = string_find(content, "*/", i + 2, true)
i = e and (e + 2) or (len + 1)
last_pos = i
else
i = i + 1
end
elseif ext == "lua" and b1 == '[' then
if string_sub(content, i+1, i+1) == '[' then
local _, end_idx = string_find(content, ']]', i + 2, true)
i = end_idx and (end_idx + 2) or (len + 1)
else
i = i + 1
end
elseif ext == "lua" and b1 == '-' then
if string_sub(content, i+1, i+1) == '-' then
result[r_idx] = string_sub(content, last_pos, i - 1)
r_idx = r_idx + 1
if string_sub(content, i+2, i+3) == '[[' then
local _, e = string_find(content, "]]", i + 4, true)
i = e and (e + 2) or (len + 1)
else
local _, e = string_find(content, "\n", i + 2, true)
i = e and (e + 1) or (len + 1)
end
last_pos = i
else
i = i + 1
end
elseif (ext == "toml" or ext == "yaml") and b1 == '#' then
result[r_idx] = string_sub(content, last_pos, i - 1)
r_idx = r_idx + 1
local _, e = string_find(content, "\n", i + 1, true)
i = e and (e + 1) or (len + 1)
last_pos = i
else
i = i + 1
end
end
if r_idx > 1 then
result[r_idx] = string_sub(content, last_pos, len)
return table_concat(result)
end
return nil
end
local function remove_comments_from_file(filepath, ext)
local file = io_open(filepath, "r")
if not file then return end
local content = file:read("*all")
file:close()
local cleaned = clean_comments(content, ext)
if cleaned then
local wfile = io_open(filepath, "w")
if wfile then
wfile:write(cleaned)
wfile:close()
print("Cleaned (" .. ext .. "): " .. filepath)
end
end
end
local function pattern_escape(s)
return s:gsub("[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1")
end
local function is_excluded(filepath)
for _, dir in ipairs(EXCLUDE_DIRS) do
local d = pattern_escape(dir)
if filepath:find("[/\\]" .. d .. "[/\\]") or filepath:find("^%.?[/\\]?" .. d .. "[/\\]") then
return true
end
end
return false
end
local function scan_project()
local cmd = (os.getenv("WINDIR") or os.getenv("windir"))
and 'dir /b /s *.rs *.lua *.slang *.json *.toml *.yaml 2>nul'
or 'find . -type f \\( -name "*.rs" -o -name "*.lua" -o -name "*.slang" -o -name "*.json" -o -name "*.toml" -o -name "*.yaml" \\) -print'
local p = io.popen(cmd)
if not p then return end
for file in p:lines() do
if file ~= "" and not is_excluded(file) then
local ext = string_sub(file, #file - 3) ext = ext:match("%.([^%.]+)$") or file:match("%.([^%.]+)$")
if ext and EXT_CONFIGS[ext] then
remove_comments_from_file(file, ext)
end
end
end
p:close()
end
scan_project()