Skip to content

Commit

Permalink
Merge PR #50 from d-g-laptev:master
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Jun 14, 2020
2 parents 51d8936 + 31b43ad commit 070fdf4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 27 deletions.
72 changes: 47 additions & 25 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"sync"
"unicode"
)

var builderPool = sync.Pool{New: func() interface{} {
return &strings.Builder{}
}}

// The XPath function list.

func predicate(q query) func(NodeNavigator) bool {
Expand Down Expand Up @@ -58,6 +63,7 @@ func lastFunc(q query, t iterator) interface{} {
// countFunc is a XPath Node Set functions count(node-set).
func countFunc(q query, t iterator) interface{} {
var count = 0
q = functionArgs(q)
test := predicate(q)
switch typ := q.Evaluate(t).(type) {
case query:
Expand All @@ -73,7 +79,7 @@ func countFunc(q query, t iterator) interface{} {
// sumFunc is a XPath Node Set functions sum(node-set).
func sumFunc(q query, t iterator) interface{} {
var sum float64
switch typ := q.Evaluate(t).(type) {
switch typ := functionArgs(q).Evaluate(t).(type) {
case query:
for node := typ.Select(t); node != nil; node = typ.Select(t) {
if v, err := strconv.ParseFloat(node.Value(), 64); err == nil {
Expand Down Expand Up @@ -116,19 +122,19 @@ func asNumber(t iterator, o interface{}) float64 {

// ceilingFunc is a XPath Node Set functions ceiling(node-set).
func ceilingFunc(q query, t iterator) interface{} {
val := asNumber(t, q.Evaluate(t))
val := asNumber(t, functionArgs(q).Evaluate(t))
return math.Ceil(val)
}

// floorFunc is a XPath Node Set functions floor(node-set).
func floorFunc(q query, t iterator) interface{} {
val := asNumber(t, q.Evaluate(t))
val := asNumber(t, functionArgs(q).Evaluate(t))
return math.Floor(val)
}

// roundFunc is a XPath Node Set functions round(node-set).
func roundFunc(q query, t iterator) interface{} {
val := asNumber(t, q.Evaluate(t))
val := asNumber(t, functionArgs(q).Evaluate(t))
//return math.Round(val)
return round(val)
}
Expand Down Expand Up @@ -201,7 +207,7 @@ func asBool(t iterator, v interface{}) bool {
case *NodeIterator:
return v.MoveNext()
case bool:
return bool(v)
return v
case float64:
return v != 0
case string:
Expand Down Expand Up @@ -239,19 +245,19 @@ func asString(t iterator, v interface{}) string {

// booleanFunc is a XPath functions boolean([node-set]).
func booleanFunc(q query, t iterator) interface{} {
v := q.Evaluate(t)
v := functionArgs(q).Evaluate(t)
return asBool(t, v)
}

// numberFunc is a XPath functions number([node-set]).
func numberFunc(q query, t iterator) interface{} {
v := q.Evaluate(t)
v := functionArgs(q).Evaluate(t)
return asNumber(t, v)
}

// stringFunc is a XPath functions string([node-set]).
func stringFunc(q query, t iterator) interface{} {
v := q.Evaluate(t)
v := functionArgs(q).Evaluate(t)
return asString(t, v)
}

Expand Down Expand Up @@ -338,15 +344,10 @@ func containsFunc(arg1, arg2 query) func(query, iterator) interface{} {
}
}

var (
regnewline = regexp.MustCompile(`[\r\n\t]`)
regseqspace = regexp.MustCompile(`\s{2,}`)
)

// normalizespaceFunc is XPath functions normalize-space(string?)
func normalizespaceFunc(q query, t iterator) interface{} {
var m string
switch typ := q.Evaluate(t).(type) {
switch typ := functionArgs(q).Evaluate(t).(type) {
case string:
m = typ
case query:
Expand All @@ -356,10 +357,26 @@ func normalizespaceFunc(q query, t iterator) interface{} {
}
m = node.Value()
}
m = strings.TrimSpace(m)
m = regnewline.ReplaceAllString(m, " ")
m = regseqspace.ReplaceAllString(m, " ")
return m
var b = builderPool.Get().(*strings.Builder)
b.Grow(len(m))

runeStr := []rune(strings.TrimSpace(m))
l := len(runeStr)
for i := range runeStr {
r := runeStr[i]
isSpace := unicode.IsSpace(r)
if !(isSpace && (i+1 < l && unicode.IsSpace(runeStr[i+1]))) {
if isSpace {
r = ' '
}
b.WriteRune(r)
}
}
result := b.String()
b.Reset()
builderPool.Put(b)

return result
}

// substringFunc is XPath functions substring function returns a part of a given string.
Expand Down Expand Up @@ -466,7 +483,7 @@ func translateFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
src := asString(t, functionArgs(arg2).Evaluate(t))
dst := asString(t, functionArgs(arg3).Evaluate(t))

var replace []string
replace := make([]string, 0, len(src))
for i, s := range src {
d := ""
if i < len(dst) {
Expand All @@ -491,7 +508,7 @@ func replaceFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {

// notFunc is XPATH functions not(expression) function operation.
func notFunc(q query, t iterator) interface{} {
switch v := q.Evaluate(t).(type) {
switch v := functionArgs(q).Evaluate(t).(type) {
case bool:
return !v
case query:
Expand All @@ -507,20 +524,25 @@ func notFunc(q query, t iterator) interface{} {
// concat( string1 , string2 [, stringn]* )
func concatFunc(args ...query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var a []string
b := builderPool.Get().(*strings.Builder)
for _, v := range args {
v = functionArgs(v)

switch v := v.Evaluate(t).(type) {
case string:
a = append(a, v)
b.WriteString(v)
case query:
node := v.Select(t)
if node != nil {
a = append(a, node.Value())
b.WriteString(node.Value())
}
}
}
return strings.Join(a, "")
result := b.String()
b.Reset()
builderPool.Put(b)

return result
}
}

Expand Down
48 changes: 48 additions & 0 deletions func_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package xpath

import "testing"

type testQuery string

func (t testQuery) Select(_ iterator) NodeNavigator {
panic("implement me")
}

func (t testQuery) Clone() query {
return t
}

func (t testQuery) Evaluate(_ iterator) interface{} {
return string(t)
}

const strForNormalization = "\t \rloooooooonnnnnnngggggggg \r \n tes \u00a0 t strin \n\n \r g "
const expectedStrAfterNormalization = `loooooooonnnnnnngggggggg tes t strin g`

func Test_NormalizeSpaceFunc(t *testing.T) {
result := normalizespaceFunc(testQuery(strForNormalization), nil).(string)
if expectedStrAfterNormalization != result {
t.Fatalf("unexpected result '%s'", result)
}
}

func Test_ConcatFunc(t *testing.T) {
result := concatFunc(testQuery("a"), testQuery("b"))(nil, nil).(string)
if "ab" != result {
t.Fatalf("unexpected result '%s'", result)
}
}

func Benchmark_NormalizeSpaceFunc(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = normalizespaceFunc(testQuery(strForNormalization), nil)
}
}

func Benchmark_ConcatFunc(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = concatFunc(testQuery("a"), testQuery("b"))(nil, nil)
}
}
2 changes: 1 addition & 1 deletion operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func cmpNodeSetNodeSet(t iterator, op string, m, n interface{}) bool {
if y == nil {
return false
}
return cmpStringStringF(op,x.Value(),y.Value())
return cmpStringStringF(op, x.Value(), y.Value())
}

func cmpStringNumeric(t iterator, op string, m, n interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestFunction(t *testing.T) {

func TestTransformFunctionReverse(t *testing.T) {
nodes := selectNodes(html, "reverse(//li)")
expectedReversedNodeValues := []string { "", "login", "about", "Home" }
expectedReversedNodeValues := []string{"", "login", "about", "Home"}
if len(nodes) != len(expectedReversedNodeValues) {
t.Fatalf("reverse(//li) should return %d <li> nodes", len(expectedReversedNodeValues))
}
Expand Down

0 comments on commit 070fdf4

Please sign in to comment.