File: stringfns.go

package info (click to toggle)
golang-github-christrenkamp-goxpath 1.0~alpha3%2Bgit20170922.c385f95-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 308 kB
  • sloc: sh: 16; makefile: 3; xml: 3
file content (141 lines) | stat: -rw-r--r-- 2,902 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package intfns

import (
	"math"
	"regexp"
	"strings"

	"github.com/ChrisTrenkamp/goxpath/tree"
)

func _string(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	if len(args) == 1 {
		return tree.String(args[0].String()), nil
	}

	return tree.String(c.NodeSet.String()), nil
}

func concat(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ret := ""

	for _, i := range args {
		ret += i.String()
	}

	return tree.String(ret), nil
}

func startsWith(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	return tree.Bool(strings.Index(args[0].String(), args[1].String()) == 0), nil
}

func contains(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	return tree.Bool(strings.Contains(args[0].String(), args[1].String())), nil
}

func substringBefore(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ind := strings.Index(args[0].String(), args[1].String())
	if ind == -1 {
		return tree.String(""), nil
	}

	return tree.String(args[0].String()[:ind]), nil
}

func substringAfter(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ind := strings.Index(args[0].String(), args[1].String())
	if ind == -1 {
		return tree.String(""), nil
	}

	return tree.String(args[0].String()[ind+len(args[1].String()):]), nil
}

func substring(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	str := args[0].String()

	bNum, bErr := round(c, args[1])
	if bErr != nil {
		return nil, bErr
	}

	b := bNum.(tree.Num).Num()

	if float64(b-1) >= float64(len(str)) || math.IsNaN(float64(b)) {
		return tree.String(""), nil
	}

	if len(args) == 2 {
		if b <= 1 {
			b = 1
		}

		return tree.String(str[int(b)-1:]), nil
	}

	eNum, eErr := round(c, args[2])
	if eErr != nil {
		return nil, eErr
	}

	e := eNum.(tree.Num).Num()

	if e <= 0 || math.IsNaN(float64(e)) || (math.IsInf(float64(b), 0) && math.IsInf(float64(e), 0)) {
		return tree.String(""), nil
	}

	if b <= 1 {
		e = b + e - 1
		b = 1
	}

	if float64(b+e-1) >= float64(len(str)) {
		e = tree.Num(len(str)) - b + 1
	}

	return tree.String(str[int(b)-1 : int(b+e)-1]), nil
}

func stringLength(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	var str string
	if len(args) == 1 {
		str = args[0].String()
	} else {
		str = c.NodeSet.String()
	}

	return tree.Num(len(str)), nil
}

var spaceTrim = regexp.MustCompile(`\s+`)

func normalizeSpace(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	var str string
	if len(args) == 1 {
		str = args[0].String()
	} else {
		str = c.NodeSet.String()
	}

	str = strings.TrimSpace(str)

	return tree.String(spaceTrim.ReplaceAllString(str, " ")), nil
}

func translate(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ret := args[0].String()
	src := args[1].String()
	repl := args[2].String()

	for i := range src {
		r := ""
		if i < len(repl) {
			r = string(repl[i])
		}

		ret = strings.Replace(ret, string(src[i]), r, -1)
	}

	return tree.String(ret), nil
}