From bdcbc9c7cc99bbc79e2dfa3b7b7197290bf5b41c Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Wed, 2 Nov 2016 06:35:05 -0700 Subject: [PATCH] strip whitespace from properties --- geojson/feature.go | 8 +++++--- geojson/object.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/geojson/feature.go b/geojson/feature.go index 569e28b9..cb209f40 100644 --- a/geojson/feature.go +++ b/geojson/feature.go @@ -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 diff --git a/geojson/object.go b/geojson/object.go index 25d96180..3031daa3 100644 --- a/geojson/object.go +++ b/geojson/object.go @@ -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) +}