File: prelude.lua

package info (click to toggle)
golang-github-brentp-goluaez 0.0~git20160116.dd35d08-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 92 kB
  • sloc: makefile: 4
file content (43 lines) | stat: -rw-r--r-- 969 bytes parent folder | download
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
-- remove whitespace at the ends of a string.
function string:strip()
    return self:match'^%s*(.*%S)' or ''
end

-- split a string by a separator
function string:split(sep)
    local sep, fields = sep or "\t", {}
    local pattern = string.format("[^%s]+", sep)
    for tok in self:gmatch(pattern) do fields[#fields+1] = tok end
    return fields
end

----------------------------------------------------
-- Testing Functions Don't copy these to Go code  --
----------------------------------------------------
function test_strip()
    a = " aaa "
    assert(a:strip() == "aaa")
    a = " aaa\t"
    assert(a:strip() == "aaa")
    a = " aaa    "
    assert(a:strip() == "aaa")
end

function test_split()
    v = string.split("x xx x", "%s")
    assert(#v == 3)
    assert(v[1] == "x")
    assert(v[2] == "xx")

    v = string.split("x xx\tx", "%s")
    assert(v[1] == "x")
    assert(v[2] == "xx")
end


function test()
    test_strip()
    test_split()
end

test()