2
0

eliminate regex dep

This commit is contained in:
bakmataliev
2020-09-15 13:24:17 +03:00
parent cd9b888ff6
commit 6777e0294b
2 changed files with 31 additions and 36 deletions
+21 -26
View File
@@ -1,11 +1,11 @@
package pgtype package pgtype
import ( import (
"bytes"
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math" "math"
"regexp"
"strconv" "strconv"
"strings" "strings"
@@ -23,8 +23,6 @@ type Point struct {
Status Status Status Status
} }
var nullRE = regexp.MustCompile("^null$")
func (dst *Point) Set(src interface{}) error { func (dst *Point) Set(src interface{}) error {
if src == nil { if src == nil {
dst.Status = Null dst.Status = Null
@@ -47,34 +45,31 @@ func (dst *Point) Set(src interface{}) error {
return nil return nil
} }
var pointRE = regexp.MustCompile("^\\(\\d+\\.\\d+,\\s?\\d+\\.\\d+\\)$") func parsePoint(src []byte) (*Point, error) {
var chunkRE = regexp.MustCompile("\\d+\\.\\d+") if src == nil || bytes.Compare(src, []byte("null")) == 0 {
return &Point{Status: Null}, nil
}
func parsePoint(p []byte) (*Point, error) { if len(src) < 5 {
err := errors.Errorf("cannot parse %s", p) return nil, errors.Errorf("invalid length for point: %v", len(src))
if pointRE.Match(p) { }
chunks := chunkRE.FindAll(p, 2)
if len(chunks) != 2 { parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
if len(parts) < 2 {
return nil, errors.Errorf("invalid format for point")
}
x, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return nil, err return nil, err
} }
x, xErr := strconv.ParseFloat(string(chunks[0]), 64)
y, yErr := strconv.ParseFloat(string(chunks[1]), 64) y, err := strconv.ParseFloat(parts[1], 64)
if xErr != nil || yErr != nil { if err != nil {
return nil, err return nil, err
} }
return &Point{
P: Vec2{ return &Point{P: Vec2{x, y}, Status: Present}, nil
X: x,
Y: y,
},
Status: Present,
}, nil
} else if nullRE.Match(p) {
return &Point{
Status: Null,
}, nil
}
return nil, err
} }
func (dst Point) Get() interface{} { func (dst Point) Get() interface{} {