summaryrefslogtreecommitdiff
path: root/tnslc/utils.tnsl
blob: a1c5884c340d7d9a43ed1908007459c497aa9f8d (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
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
/; cstr_len (~uint8 cstr) [int]
    int i = 0
    /; loop (cstr{i} !== 0) [i++] ;/
    return i
;/

/; cstr_eq (~uint8 a, b) [bool]
    int ln = cstr_len(a)
    /; if (ln !== cstr_len(b))
        return false
    ;/

    /; loop (int i = 0; i < ln) [i++]
        /; if (a{i} !== b{i})
            return false
        ;/
    ;/

    return true
;/

{}uint8 csv_pr = "\0\0"

/; in_csv ({}uint8 csv, ~uint8 cstr) [int]
    int sl = cstr_len(cstr)
    int cl = len csv
    int seen = 0
    int idx = 0

    /; loop (int i = 0; i < cl) [i++]
        
        /; if (seen < 0)
            /; if (csv{i} == ',')
                seen = 0
                idx++
            ;/
            continue
        ;/

        /; if (seen !< sl)
            /; if (csv{i} == ',')
                return idx
            ;/
            seen = -1
        ;; else if (cstr{seen} == csv{i})
            csv_pr{0} = cstr{seen}
            # _printf(~csv_pr{0})
            seen++
        ;; else if (csv{i} == ',')
            idx++
            seen = 0
        ;; else
            seen = -1
        ;/
    ;/

    /; if (seen == sl)
        return idx
    ;/
    
    return 0 - 1
;/

/; cstr_copy (~uint8 from, to)
    int ln = cstr_len(from)

    /; loop (int i = 0; i !> ln) [i++]
        to{i} = from{i}
    ;/
;/

/; cstr_make_copy(~uint8 to_copy) [~uint8]
    int l = cstr_len(to_copy)
    ~uint8 out = _alloc(l + 1)
    out{l} = 0
    cstr_copy(to_copy, out)
    return out
;/

/; contains_char ({}uint8 arr, uint8 c) [bool]
    /; loop (int i = 0; i < len arr) [i++]
        /; if (arr{i} == c)
            return true
        ;/
    ;/
    return false
;/

/; cstr_contains (~uint8 cstr, uint8 c) [bool]
    int j = cstr_len(cstr)
    /; loop (int i = 0; i < j) [i++]
        /; if (cstr{i} == c)
            return true
        ;/
    ;/
    return false
;/

/; write_to_file(~void file, ~uint8 string)
    int ln = cstr_len(string)
    /; loop (int i = 0; i < ln) [i++]
        _write_byte(file, string + i)
    ;/
;/

/; cstr_add(~uint8 a, b) [~uint8]
	int al = cstr_len(a)
	int bl = cstr_len(b)
	~uint8 out = _alloc(al + bl + 1)

	/; loop (int i = 0; i < al) [i++]
		out{i} = a{i}
	;/

	/; loop (int i = 0, i < bl) [i++]
		out{al + i} = b{i}
	;/

	out{al + bl} = 0
	return out
;/