tile38/controller/json.go

18 lines
323 B
Go
Raw Permalink Normal View History

2016-03-05 02:08:16 +03:00
package controller
2016-11-07 23:04:21 +03:00
import "encoding/json"
2016-03-05 02:08:16 +03:00
func jsonString(s string) string {
for i := 0; i < len(s); i++ {
2016-09-11 17:49:48 +03:00
if s[i] < ' ' || s[i] == '\\' || s[i] == '"' || s[i] > 126 {
2016-03-05 02:08:16 +03:00
d, _ := json.Marshal(s)
return string(d)
}
}
2016-09-11 17:49:48 +03:00
b := make([]byte, len(s)+2)
b[0] = '"'
2016-11-07 23:04:21 +03:00
copy(b[1:], s)
2016-09-11 17:49:48 +03:00
b[len(b)-1] = '"'
2016-11-07 23:04:21 +03:00
return string(b)
2016-03-05 02:08:16 +03:00
}