File: xtypes.go

package info (click to toggle)
golang-github-christrenkamp-goxpath 1.0~alpha3%2Bgit20170922.c385f95-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: sh: 16; makefile: 3; xml: 3
file content (113 lines) | stat: -rw-r--r-- 2,001 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package tree

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

//Boolean strings
const (
	True  = "true"
	False = "false"
)

//Bool is a boolean XPath type
type Bool bool

//ResValue satisfies the Res interface for Bool
func (b Bool) String() string {
	if b {
		return True
	}

	return False
}

//Bool satisfies the HasBool interface for Bool's
func (b Bool) Bool() Bool {
	return b
}

//Num satisfies the HasNum interface for Bool's
func (b Bool) Num() Num {
	if b {
		return Num(1)
	}

	return Num(0)
}

//Num is a number XPath type
type Num float64

//ResValue satisfies the Res interface for Num
func (n Num) String() string {
	if math.IsInf(float64(n), 0) {
		if math.IsInf(float64(n), 1) {
			return "Infinity"
		}
		return "-Infinity"
	}
	return fmt.Sprintf("%g", float64(n))
}

//Bool satisfies the HasBool interface for Num's
func (n Num) Bool() Bool {
	return n != 0
}

//Num satisfies the HasNum interface for Num's
func (n Num) Num() Num {
	return n
}

//String is string XPath type
type String string

//ResValue satisfies the Res interface for String
func (s String) String() string {
	return string(s)
}

//Bool satisfies the HasBool interface for String's
func (s String) Bool() Bool {
	return Bool(len(s) > 0)
}

//Num satisfies the HasNum interface for String's
func (s String) Num() Num {
	num, err := strconv.ParseFloat(strings.TrimSpace(string(s)), 64)
	if err != nil {
		return Num(math.NaN())
	}
	return Num(num)
}

//NodeSet is a node-set XPath type
type NodeSet []Node

//GetNodeNum converts the node to a string-value and to a number
func GetNodeNum(n Node) Num {
	return String(n.ResValue()).Num()
}

//String satisfies the Res interface for NodeSet
func (n NodeSet) String() string {
	if len(n) == 0 {
		return ""
	}

	return n[0].ResValue()
}

//Bool satisfies the HasBool interface for node-set's
func (n NodeSet) Bool() Bool {
	return Bool(len(n) > 0)
}

//Num satisfies the HasNum interface for NodeSet's
func (n NodeSet) Num() Num {
	return String(n.String()).Num()
}