Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replaced strings split with splitting by regexp #128

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions encoding/wkt/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wkt

import (
"errors"
"regexp"
"strconv"
"strings"

Expand All @@ -18,6 +19,10 @@ var (

// ErrUnsupportedGeometry is returned when geometry type is not supported by this lib.
ErrUnsupportedGeometry = errors.New("wkt: unsupported geometry")

doubleParen = regexp.MustCompile(`\)[\s|\t]*\)[\s|\t]*,[\s|\t]*\([\s|\t]*\(`)
singleParen = regexp.MustCompile(`\)[\s|\t]*,[\s|\t]*\(`)
noParen = regexp.MustCompile(`[\s|\t]*,[\s|\t]*`)
)

// UnmarshalPoint returns the point represented by the wkt string.
Expand Down Expand Up @@ -131,7 +136,6 @@ func trimSpaceBrackets(s string) string {
if s[len(s)-1] == ')' {
s = s[:len(s)-1]
}

return strings.Trim(s, " ")
}

Expand Down Expand Up @@ -216,7 +220,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "MULTIPOINT", "", -1)
s = trimSpaceBrackets(s)
ps := strings.Split(s, ",")
ps := splitByRegexp(s, noParen)
mp := orb.MultiPoint{}
for _, p := range ps {
tp, err := parsePoint(trimSpaceBrackets(p))
Expand All @@ -241,9 +245,9 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "MULTILINESTRING", "", -1)
ml := orb.MultiLineString{}
for _, l := range strings.Split(trimSpaceBrackets(s), "),(") {
for _, l := range splitByRegexp(trimSpaceBrackets(s), singleParen) {
tl := orb.LineString{}
for _, p := range strings.Split(trimSpaceBrackets(l), ",") {
for _, p := range splitByRegexp(trimSpaceBrackets(l), noParen) {
tp, err := parsePoint(trimSpaceBrackets(p))
if err != nil {
return nil, err
Expand All @@ -260,7 +264,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "LINESTRING", "", -1)
s = trimSpaceBrackets(s)
ps := strings.Split(s, ",")
ps := splitByRegexp(s, noParen)
ls := orb.LineString{}
for _, p := range ps {
tp, err := parsePoint(trimSpaceBrackets(p))
Expand All @@ -277,11 +281,12 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "MULTIPOLYGON", "", -1)
mpol := orb.MultiPolygon{}
for _, ps := range strings.Split(trimSpaceBrackets(s), ")),((") {

for _, ps := range splitByRegexp(trimSpaceBrackets(s), doubleParen) {
pol := orb.Polygon{}
for _, ls := range strings.Split(trimSpaceBrackets(ps), "),(") {
for _, ls := range splitByRegexp(trimSpaceBrackets(ps), singleParen) {
ring := orb.Ring{}
for _, p := range strings.Split(ls, ",") {
for _, p := range splitByRegexp(ls, noParen) {
tp, err := parsePoint(trimSpaceBrackets(p))
if err != nil {
return nil, err
Expand All @@ -301,10 +306,10 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
s = strings.Replace(s, "POLYGON", "", -1)
s = trimSpaceBrackets(s)

rs := strings.Split(s, "),(")
rs := splitByRegexp(s, singleParen)
pol := make(orb.Polygon, 0, len(rs))
for _, r := range rs {
ps := strings.Split(trimSpaceBrackets(r), ",")
ps := splitByRegexp(trimSpaceBrackets(r), noParen)
ring := orb.Ring{}
for _, p := range ps {
tp, err := parsePoint(trimSpaceBrackets(p))
Expand All @@ -322,3 +327,15 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {

return
}

func splitByRegexp(s string, re *regexp.Regexp) []string {
indexes := re.FindAllStringIndex(s, -1)
start := 0
result := make([]string, len(indexes)+1)
for i, element := range indexes {
result[i] = s[start:element[0]]
start = element[1]
}
result[len(indexes)] = s[start:]
return result
}
20 changes: 20 additions & 0 deletions encoding/wkt/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func TestUnmarshalMultiPoint(t *testing.T) {
s: "MULTIPOINT((1 2),(0.5 1.5))",
expected: orb.MultiPoint{{1, 2}, {0.5, 1.5}},
},
{
name: "spaces",
s: "MULTIPOINT((1 2) , (0.5 1.5))",
expected: orb.MultiPoint{{1, 2}, {0.5, 1.5}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -215,6 +220,11 @@ func TestUnmarshalLineString(t *testing.T) {
s: "LINESTRING(1 2,0.5 1.5)",
expected: orb.LineString{{1, 2}, {0.5, 1.5}},
},
{
name: "spaces",
s: "LINESTRING(1 2 , 0.5 1.5)",
expected: orb.LineString{{1, 2}, {0.5, 1.5}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -354,6 +364,11 @@ func TestUnmarshalPolygon(t *testing.T) {
s: "POLYGON((1 2,3 4),(5 6,7 8))",
expected: orb.Polygon{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
},
{
name: "two rings with spaces",
s: "POLYGON((1 2,3 4) , (5 6 , 7 8))",
expected: orb.Polygon{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -421,6 +436,11 @@ func TestUnmarshalMutilPolygon(t *testing.T) {
s: "MULTIPOLYGON(((1 2,3 4)),((5 6,7 8),(1 2,5 4)))",
expected: orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}},
},
{
name: "mulit-polygon with spaces",
s: "MULTIPOLYGON(((1 2,3 4)) , ((5 6,7 8), (1 2,5 4)))",
expected: orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}},
},
}

for _, tc := range cases {
Expand Down