46 lines
643 B
Go
46 lines
643 B
Go
|
package escape
|
||
|
|
||
|
import "bytes"
|
||
|
|
||
|
func Bytes(in []byte) []byte {
|
||
|
for b, esc := range Codes {
|
||
|
in = bytes.Replace(in, []byte{b}, esc, -1)
|
||
|
}
|
||
|
return in
|
||
|
}
|
||
|
|
||
|
func Unescape(in []byte) []byte {
|
||
|
i := 0
|
||
|
inLen := len(in)
|
||
|
var out []byte
|
||
|
|
||
|
for {
|
||
|
if i >= inLen {
|
||
|
break
|
||
|
}
|
||
|
if in[i] == '\\' && i+1 < inLen {
|
||
|
switch in[i+1] {
|
||
|
case ',':
|
||
|
out = append(out, ',')
|
||
|
i += 2
|
||
|
continue
|
||
|
case '"':
|
||
|
out = append(out, '"')
|
||
|
i += 2
|
||
|
continue
|
||
|
case ' ':
|
||
|
out = append(out, ' ')
|
||
|
i += 2
|
||
|
continue
|
||
|
case '=':
|
||
|
out = append(out, '=')
|
||
|
i += 2
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
out = append(out, in[i])
|
||
|
i += 1
|
||
|
}
|
||
|
return out
|
||
|
}
|