-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
76 lines (62 loc) · 1.56 KB
/
parse.go
File metadata and controls
76 lines (62 loc) · 1.56 KB
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
package main
import (
"strings"
)
func doGetTokens(s string) (ans []string) {
s = strings.TrimSuffix(s, "©leetcode")
parts := strings.Split(s, " = ")
if len(parts) == 1 {
// If there is no '=' in the string, return the string as a single element
s = strings.TrimSuffix(parts[0], "©leetcode")
ans = append(ans, unquoteString(s))
return ans
}
for i, part := range parts {
if i == 0 {
continue
}
part = unquoteString(removePostfix(part))
part = strings.TrimSpace(part)
ans = append(ans, part)
}
return ans
}
// remove all of the characters after the last ','
// If it is a string with '"', then return the string up to the last '"'
// If it is a slice with ']', then return the string up to the last ']'
func removePostfix(s string) string {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '"' || s[i] == ']' {
return s[:i+1]
}
if s[i] == ',' {
return s[:i]
}
}
return s
}
func rawStrTo1DStrSlice(s string) []string {
s = strings.Trim(s, "[]")
parts := strings.Split(s, ",")
result := make([]string, len(parts))
for i, part := range parts {
part = unquoteString(strings.TrimSpace(part))
result[i] = part
}
return result
}
func unquoteString(s string) string {
if strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"") {
return strings.Trim(s, "\"")
}
return s
}
func rawStrTo2DStrSlice(s string) [][]string {
s = strings.Trim(s, "[]")
innerSlices := strings.Split(s, "],[")
result := make([][]string, len(innerSlices))
for i, inner := range innerSlices {
result[i] = rawStrTo1DStrSlice(inner)
}
return result
}