Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80e521d

Browse files
authored
Merge pull request #128 from m-pavel/master
Replaced strings split with splitting by regexp
2 parents 5e33049 + 3331c67 commit 80e521d

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

encoding/wkt/unmarshal.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wkt
22

33
import (
44
"errors"
5+
"regexp"
56
"strconv"
67
"strings"
78

@@ -18,6 +19,10 @@ var (
1819

1920
// ErrUnsupportedGeometry is returned when geometry type is not supported by this lib.
2021
ErrUnsupportedGeometry = errors.New("wkt: unsupported geometry")
22+
23+
doubleParen = regexp.MustCompile(`\)[\s|\t]*\)[\s|\t]*,[\s|\t]*\([\s|\t]*\(`)
24+
singleParen = regexp.MustCompile(`\)[\s|\t]*,[\s|\t]*\(`)
25+
noParen = regexp.MustCompile(`[\s|\t]*,[\s|\t]*`)
2126
)
2227

2328
// UnmarshalPoint returns the point represented by the wkt string.
@@ -131,7 +136,6 @@ func trimSpaceBrackets(s string) string {
131136
if s[len(s)-1] == ')' {
132137
s = s[:len(s)-1]
133138
}
134-
135139
return strings.Trim(s, " ")
136140
}
137141

@@ -216,7 +220,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
216220
}
217221
s = strings.Replace(s, "MULTIPOINT", "", -1)
218222
s = trimSpaceBrackets(s)
219-
ps := strings.Split(s, ",")
223+
ps := splitByRegexp(s, noParen)
220224
mp := orb.MultiPoint{}
221225
for _, p := range ps {
222226
tp, err := parsePoint(trimSpaceBrackets(p))
@@ -241,9 +245,9 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
241245
}
242246
s = strings.Replace(s, "MULTILINESTRING", "", -1)
243247
ml := orb.MultiLineString{}
244-
for _, l := range strings.Split(trimSpaceBrackets(s), "),(") {
248+
for _, l := range splitByRegexp(trimSpaceBrackets(s), singleParen) {
245249
tl := orb.LineString{}
246-
for _, p := range strings.Split(trimSpaceBrackets(l), ",") {
250+
for _, p := range splitByRegexp(trimSpaceBrackets(l), noParen) {
247251
tp, err := parsePoint(trimSpaceBrackets(p))
248252
if err != nil {
249253
return nil, err
@@ -260,7 +264,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
260264
}
261265
s = strings.Replace(s, "LINESTRING", "", -1)
262266
s = trimSpaceBrackets(s)
263-
ps := strings.Split(s, ",")
267+
ps := splitByRegexp(s, noParen)
264268
ls := orb.LineString{}
265269
for _, p := range ps {
266270
tp, err := parsePoint(trimSpaceBrackets(p))
@@ -277,11 +281,12 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
277281
}
278282
s = strings.Replace(s, "MULTIPOLYGON", "", -1)
279283
mpol := orb.MultiPolygon{}
280-
for _, ps := range strings.Split(trimSpaceBrackets(s), ")),((") {
284+
285+
for _, ps := range splitByRegexp(trimSpaceBrackets(s), doubleParen) {
281286
pol := orb.Polygon{}
282-
for _, ls := range strings.Split(trimSpaceBrackets(ps), "),(") {
287+
for _, ls := range splitByRegexp(trimSpaceBrackets(ps), singleParen) {
283288
ring := orb.Ring{}
284-
for _, p := range strings.Split(ls, ",") {
289+
for _, p := range splitByRegexp(ls, noParen) {
285290
tp, err := parsePoint(trimSpaceBrackets(p))
286291
if err != nil {
287292
return nil, err
@@ -301,10 +306,10 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
301306
s = strings.Replace(s, "POLYGON", "", -1)
302307
s = trimSpaceBrackets(s)
303308

304-
rs := strings.Split(s, "),(")
309+
rs := splitByRegexp(s, singleParen)
305310
pol := make(orb.Polygon, 0, len(rs))
306311
for _, r := range rs {
307-
ps := strings.Split(trimSpaceBrackets(r), ",")
312+
ps := splitByRegexp(trimSpaceBrackets(r), noParen)
308313
ring := orb.Ring{}
309314
for _, p := range ps {
310315
tp, err := parsePoint(trimSpaceBrackets(p))
@@ -322,3 +327,15 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
322327

323328
return
324329
}
330+
331+
func splitByRegexp(s string, re *regexp.Regexp) []string {
332+
indexes := re.FindAllStringIndex(s, -1)
333+
start := 0
334+
result := make([]string, len(indexes)+1)
335+
for i, element := range indexes {
336+
result[i] = s[start:element[0]]
337+
start = element[1]
338+
}
339+
result[len(indexes)] = s[start:]
340+
return result
341+
}

encoding/wkt/unmarshal_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ func TestUnmarshalMultiPoint(t *testing.T) {
148148
s: "MULTIPOINT((1 2),(0.5 1.5))",
149149
expected: orb.MultiPoint{{1, 2}, {0.5, 1.5}},
150150
},
151+
{
152+
name: "spaces",
153+
s: "MULTIPOINT((1 2) , (0.5 1.5))",
154+
expected: orb.MultiPoint{{1, 2}, {0.5, 1.5}},
155+
},
151156
}
152157

153158
for _, tc := range cases {
@@ -215,6 +220,11 @@ func TestUnmarshalLineString(t *testing.T) {
215220
s: "LINESTRING(1 2,0.5 1.5)",
216221
expected: orb.LineString{{1, 2}, {0.5, 1.5}},
217222
},
223+
{
224+
name: "spaces",
225+
s: "LINESTRING(1 2 , 0.5 1.5)",
226+
expected: orb.LineString{{1, 2}, {0.5, 1.5}},
227+
},
218228
}
219229

220230
for _, tc := range cases {
@@ -354,6 +364,11 @@ func TestUnmarshalPolygon(t *testing.T) {
354364
s: "POLYGON((1 2,3 4),(5 6,7 8))",
355365
expected: orb.Polygon{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
356366
},
367+
{
368+
name: "two rings with spaces",
369+
s: "POLYGON((1 2,3 4) , (5 6 , 7 8))",
370+
expected: orb.Polygon{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
371+
},
357372
}
358373

359374
for _, tc := range cases {
@@ -421,6 +436,11 @@ func TestUnmarshalMutilPolygon(t *testing.T) {
421436
s: "MULTIPOLYGON(((1 2,3 4)),((5 6,7 8),(1 2,5 4)))",
422437
expected: orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}},
423438
},
439+
{
440+
name: "mulit-polygon with spaces",
441+
s: "MULTIPOLYGON(((1 2,3 4)) , ((5 6,7 8), (1 2,5 4)))",
442+
expected: orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}},
443+
},
424444
}
425445

426446
for _, tc := range cases {

0 commit comments

Comments
 (0)