-
Notifications
You must be signed in to change notification settings - Fork 148
/
findhighbit.lua
90 lines (74 loc) · 2.04 KB
/
findhighbit.lua
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
-- This Lua script recursively searches the current directory for source code
-- files that contain bytes >= 0x80 without including a UTF8 byte order mark.
--
-- Run this via:
-- lua52.exe findhighbit.lua
local bad = 0
local total = 0
local bom_pattern = "^\xef\xbb\xbf"
local highbit_pattern = "[\x80-\xff]"
local function scan(name, dolines)
local f = io.open(name, "rb")
if not f then
return
end
total = total + 1
if not dolines then
-- This "*a" optimization saves around 5-10% elapsed time.
local content = f:read("*a")
if not content:find(bom_pattern) and content:find(highbit_pattern) then
total = total - 1
scan(name, true)
end
else
local n = 0
for line in f:lines() do
n = n + 1
if n == 1 and line:find(bom_pattern) then
break
end
local pos = line:find(highbit_pattern)
if pos then
print(name.." -- \x1b[91mline "..n..", pos "..pos.."\x1b[m")
bad = bad + 1
break
end
end
end
f:close()
end
local function can_scan(name)
return not name:find("\\%.[^\\]*\\") and not name:find("\\examples\\")
end
local function traverse(dir)
local f = io.popen(string.format('2>nul dir /b /s /a:-d "%s"', dir))
if not f then
return
end
for name in f:lines() do
if can_scan(name) then
name = name:lower()
if name:find("%.[hc]$") or name:find("%.cpp$") then
scan(name)
end
end
end
f:close()
end
local function analyze()
bad = 0
total = 0
local start = os.clock()
traverse(".")
local elapsed = os.clock() - start
if bad > 0 then
print()
end
print(string.format("%u file(s) scanned in %.2f sec.", total, elapsed))
if bad > 0 then
print("\x1b[91m"..bad.." file(s) missing UTF8 BOM.\x1b[m")
else
print("\x1b[92mAll files ok.\x1b[m")
end
end
analyze()