-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.go
110 lines (105 loc) · 2.16 KB
/
util.go
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
// Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mo
import (
"bytes"
"strings"
)
func decodePoString(text string) string {
lines := strings.Split(text, "\n")
for i := 0; i < len(lines); i++ {
left := strings.Index(lines[i], `"`)
right := strings.LastIndex(lines[i], `"`)
if left < 0 || right < 0 || left == right {
lines[i] = ""
continue
}
line := lines[i][left+1 : right]
data := make([]byte, 0, len(line))
for i := 0; i < len(line); i++ {
if line[i] != '\\' {
data = append(data, line[i])
continue
}
if i+1 >= len(line) {
break
}
switch line[i+1] {
case 'n': // \\n -> \n
data = append(data, '\n')
i++
case 't': // \\t -> \n
data = append(data, '\t')
i++
case '\\': // \\\ -> ?
data = append(data, '\\')
i++
}
}
lines[i] = string(data)
}
return strings.Join(lines, "")
}
func encodePoString(text string) string {
var buf bytes.Buffer
lines := strings.Split(text, "\n")
for i := 0; i < len(lines); i++ {
if lines[i] == "" {
if i != len(lines)-1 {
buf.WriteString(`"\n"` + "\n")
}
continue
}
buf.WriteRune('"')
for _, r := range lines[i] {
switch r {
case '\\':
buf.WriteString(`\\`)
case '"':
buf.WriteString(`\"`)
case '\n':
buf.WriteString(`\n`)
case '\t':
buf.WriteString(`\t`)
default:
buf.WriteRune(r)
}
}
buf.WriteString(`\n"` + "\n")
}
return buf.String()
}
func encodeCommentPoString(text string) string {
var buf bytes.Buffer
lines := strings.Split(text, "\n")
if len(lines) > 1 {
buf.WriteString(`""` + "\n")
}
for i := 0; i < len(lines); i++ {
if len(lines) > 0 {
buf.WriteString("#| ")
}
buf.WriteRune('"')
for _, r := range lines[i] {
switch r {
case '\\':
buf.WriteString(`\\`)
case '"':
buf.WriteString(`\"`)
case '\n':
buf.WriteString(`\n`)
case '\t':
buf.WriteString(`\t`)
default:
buf.WriteRune(r)
}
}
if i < len(lines)-1 {
buf.WriteString(`\n"` + "\n")
} else {
buf.WriteString(`"`)
}
}
return buf.String()
}