strip whitespace from properties

This commit is contained in:
Josh Baker 2016-11-02 06:35:05 -07:00
parent 659a715065
commit bdcbc9c7cc
2 changed files with 45 additions and 3 deletions

View File

@ -46,9 +46,11 @@ func fillFeatureMap(json string) (Feature, []byte, error) {
}
id := gjson.Get(json, "id")
if id.Exists() || propsExists {
raw := make([]byte, len(id.Raw)+len(props.Raw)+1)
copy(raw, id.Raw)
copy(raw[len(id.Raw)+1:], props.Raw)
idRaw := stripWhitespace(id.Raw)
propsRaw := stripWhitespace(props.Raw)
raw := make([]byte, len(idRaw)+len(propsRaw)+1)
copy(raw, idRaw)
copy(raw[len(idRaw)+1:], propsRaw)
g.idprops = string(raw)
}
return g, nil, err

View File

@ -344,3 +344,43 @@ func mustMarshalString(s string) bool {
}
return false
}
func stripWhitespace(s string) string {
var p []byte
var str bool
var escs int
for i := 0; i < len(s); i++ {
c := s[i]
if str {
// We're inside of a string. Look out for '"' and '\' characters.
if c == '\\' {
// Increment the escape character counter.
escs++
} else {
if c == '"' && escs%2 == 0 {
// We reached the end of string
str = false
}
// Reset the escape counter
escs = 0
}
} else if c == '"' {
// We encountared a double quote character.
str = true
} else if c <= ' ' {
// Ignore the whitespace
if p == nil {
p = []byte(s[:i])
}
continue
}
// Append the character
if p != nil {
p = append(p, c)
}
}
if p == nil {
return s
}
return string(p)
}