-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_arg.v
52 lines (46 loc) · 1.11 KB
/
cmd_arg.v
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
module vredis
import strings
type CmdArg = f64 | i64 | int | string | u64
type CmdArgs = []CmdArg
pub fn (mut arg CmdArg) safe() CmdArg {
mut cmd_arg_str := ''
if arg is string {
cmd_arg_str = arg as string
cmd_arg_str = cmd_arg_str.replace('\r', '\\r')
cmd_arg_str = cmd_arg_str.replace('\n', '\\n')
cmd_arg_str = cmd_arg_str.replace('"', '\\"')
cmd_arg_str = '"${cmd_arg_str}"'
} else if arg is int {
cmd_arg_str = (arg as int).str()
} else if arg is i64 {
cmd_arg_str = (arg as i64).str()
} else if arg is f64 {
cmd_arg_str = (arg as f64).str()
} else if arg is u64 {
cmd_arg_str = (arg as u64).str()
} else {
cmd_arg_str = arg.str()
}
return cmd_arg_str
}
pub fn (mut args CmdArgs) add(params ...CmdArg) {
if params.len > 0 {
args << params
}
}
pub fn (mut args CmdArgs) build() string {
mut len := 0
for i, mut arg in args {
arg = arg.safe()
len += (arg as string).len
args[i] = arg
}
mut buf := strings.new_builder(len + args.len + 2)
for mut arg in args {
buf.write_string(arg as string)
buf.write_string(' ')
}
buf.delete_last()
buf.write_string(crlf)
return buf.str()
}