v2 / vlib / x / json2 / attr_utils.v
54 lines · 50 sloc · 1.08 KB · 8986645ee93015bf9b44e7839c3a3370aff4f51b
Raw
1module json2
2
3@[inline]
4fn unquote_attr_value(value string) string {
5 mut unquoted := value.trim_space()
6 if unquoted.len > 1 {
7 if (unquoted[0] == `'` && unquoted[unquoted.len - 1] == `'`)
8 || (unquoted[0] == `"` && unquoted[unquoted.len - 1] == `"`) {
9 unquoted = unquoted[1..unquoted.len - 1]
10 }
11 }
12 return unquoted
13}
14
15@[inline]
16fn json_attr_value(attr string) ?string {
17 if !attr.starts_with('json:') {
18 return none
19 }
20 return unquote_attr_value(attr[5..])
21}
22
23@[markused]
24fn json_attr_value_range(attr string) ?(int, int) {
25 if !attr.starts_with('json:') {
26 return none
27 }
28 mut start := 5
29 for start < attr.len && attr[start] in [` `, `\t`, `\n`, `\r`] {
30 start++
31 }
32 mut end := attr.len
33 for end > start && attr[end - 1] in [` `, `\t`, `\n`, `\r`] {
34 end--
35 }
36 if end - start > 1 {
37 if (attr[start] == `'` && attr[end - 1] == `'`)
38 || (attr[start] == `"` && attr[end - 1] == `"`) {
39 start++
40 end--
41 }
42 }
43 return start, end
44}
45
46@[inline]
47fn enum_uses_json_as_number[T]() bool {
48 $for attr in T.attributes {
49 if attr.name == 'json_as_number' {
50 return true
51 }
52 }
53 return false
54}
55