-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc.lua
66 lines (57 loc) · 1.49 KB
/
wc.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
-- Simulates wc -c flag (counts bytes)
local function countBytes(fileContent)
return #fileContent
end
-- Simulates wc -l flag (counts lines)
local function countLines(fileContent)
local count = 0
for _ in string.gmatch(fileContent, "[^\n]*\n?") do
count = count + 1
end
return count
end
-- Simulates wc -w flag (counts words)
local function countWords(fileContent)
local count = 0
for _ in string.gmatch(fileContent, "%S+") do
count = count + 1
end
return count
end
-- Simulates wc -m flag (counts characters)
local function countChars(s)
local count = 0
for _, _ in utf8.codes(s) do
count = count + 1
end
return count
end
-- Main loop
local option = arg[1]
local filename = arg[2]
local fileContent = ""
local function countOption(option, content)
local countFunctions = {
["-c"] = countBytes,
["-l"] = countLines,
["-w"] = countWords,
["-m"] = countChars,
}
return countFunctions[option](content)
end
if filename then
local f = assert(io.open(filename, "rb"))
fileContent = f:read("*a")
f:close()
else
fileContent = io.stdin:read("*a")
end
if option and countOption(option, fileContent) then
local count = countOption(option, fileContent)
print(count .. " " .. filename)
else
local lines = countLines(fileContent)
local words = countWords(fileContent)
local bytes = countBytes(fileContent)
print(string.format("%7d %7d %7d", lines, words, bytes))
end