hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/gils/wc.rv
blob: 75a3ed270c89b26d2722f9608c513d8c563d5c9b (plain)
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
# word count

const context = env.ctx
if context.argv:len() < 2 do
  fail("usage: wc <path>\n")
end else do
  const path = expand_path(context.argv[1])
  const size = fs:size(path)

  if size < 0 do
    fail("wc: #{path}: no such file\n")
  end else do
    const contents = if (size > 0) fs:read_bytes(path, 0, size) else ""
    let line_count = 0
    let word_count = 0
    let in_word = :false

    for index in 0..contents:len() do
      const character = contents[index]
      if (character == "\n")
        line_count += 1
      if character == " " or character == "\n" or character == "\t" do
        in_word = :false
      end else if in_word == :false do
        word_count += 1
        in_word = :true
      end
    end

    puts("  #{line_count}  #{word_count}  #{size}  #{context.argv[1]}\n")
  end
end