v
Зеркало из https://github.com/vlang/v
1module txtar
2
3// Ported from https://cs.opensource.google/go/x/tools/+/master:txtar/archive.go
4import strings
5
6// Archive is a collection of files
7pub struct Archive {
8pub mut:
9comment string // the start of the archive; contains potentially multiple lines, before the files
10files []File // a series of files
11}
12
13// File is a single file in an Archive. Each starting with a `-- FILENAME --` line.
14pub struct File {
15pub mut:
16path string // 'abc/def.v' from the `-- abc/def.v --` header
17content string // everything after that, till the next `-- name --` line.
18}
19
20// str returns a string representation of the `a` Archive.
21// It is suitable for storing in a text file.
22// It is also in the same format, that txtar.parse/1 expects.
23pub fn (a &Archive) str() string {
24mut sb := strings.new_builder(a.comment.len + 200 * a.files.len)
25sb.write_string(fix_nl(a.comment))
26for f in a.files {
27sb.write_string('-- ${f.path} --\n')
28sb.write_string(fix_nl(f.content))
29}
30return sb.str()
31}
32
33// parse parses the serialized form of an Archive.
34// The returned Archive holds slices of data.
35pub fn parse(content string) Archive {
36mut a := Archive{}
37comment, mut name, mut data := find_file_marker(content)
38a.comment = comment
39for name != '' {
40mut f := File{name, ''}
41f.content, name, data = find_file_marker(data)
42a.files << f
43}
44return a
45}
46
47const nlm = '\n-- '
48const mstart = '-- '
49const mend = ' --'
50
51// find_file_marker finds the next file marker in data, extracts the file name,
52// and returns the data before the marker, the file name, and the data after the marker.
53// If there is no next marker, find_file_marker returns fixNL(data), '', ''.
54fn find_file_marker(data string) (string, string, string) {
55mut i := 0
56for i < data.len {
57name, after := is_marker(data[i..])
58if name != '' {
59return data[..i], name, after
60}
61j := data[i..].index(nlm) or { return fix_nl(data), '', '' }
62i += j + 1 // positioned at start of new possible marker
63}
64return '', '', ''
65}
66
67// is_marker checks whether the data begins with a file marker line.
68// If so, it returns the name from the line, and the data after the line.
69// Otherwise it returns name == "".
70fn is_marker(data string) (string, string) {
71if !data.starts_with(mstart) {
72return '', ''
73}
74mut ndata := data
75mut after := ''
76i := data.index_u8(`\n`)
77if i >= 0 {
78ndata, after = data[..i], data[i + 1..]
79}
80if !(ndata.ends_with(mend) && ndata.len >= mstart.len + mend.len) {
81return '', ''
82}
83name := ndata[mstart.len..ndata.len - mend.len].trim_space()
84return name, after
85}
86
87// fix_nl returns the data, if it is empty, or if it ends in \n.
88// Otherwise it returns data + a final \n added.
89fn fix_nl(data string) string {
90if data.len == 0 || data[data.len - 1] == `\n` {
91return data
92}
93return '${data}\n'
94}
95