Skip to content

Commit 1c92525

Browse files
committed
- [+] add substr template function
1 parent eee87fb commit 1c92525

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

cmd/easygen/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import (
4141

4242
var (
4343
progname = "easygen"
44-
version = "4.1.0"
45-
date = "2019-06-18"
44+
version = "4.1.01"
45+
date = "2020-07-26"
4646
)
4747

4848
////////////////////////////////////////////////////////////////////////////

template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ var egFuncMap = FuncMap{
101101

102102
// == my added functions
103103
"ENV": os.Getenv,
104+
"substr": Substr,
104105
"coalesce": coalesce,
105106
"quote4shell": quote4shell,
106107

tf-strings.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package easygen
22

33
import (
4+
"errors"
5+
"fmt"
46
"regexp"
57
"strings"
68
)
@@ -10,6 +12,73 @@ var (
1012
rec *regexp.Regexp
1113
)
1214

15+
////////////////////////////////////////////////////////////////////////////
16+
// Normal String Function Definitions
17+
18+
// Taken/adapted from hugo tpl/strings/strings.go
19+
20+
// Substr extracts parts of a string, beginning at the character at the specified
21+
// position, and returns the specified number of characters.
22+
//
23+
// It normally takes two parameters: start and length.
24+
// It can also take one parameter: start, i.e. length is omitted, in which case
25+
// the substring starting from start until the end of the string will be returned.
26+
//
27+
// To extract characters from the end of the string, use a negative start number.
28+
//
29+
// In addition, borrowing from the extended behavior described at http://php.net/substr,
30+
// if length is given and is negative, then that many characters will be omitted from
31+
// the end of string.
32+
func Substr(a string, nums ...interface{}) (string, error) {
33+
var start, length int
34+
35+
asRunes := []rune(a)
36+
37+
switch len(nums) {
38+
case 0:
39+
return "", errors.New("too less arguments")
40+
case 1:
41+
start = nums[0].(int)
42+
length = len(asRunes)
43+
case 2:
44+
start = nums[0].(int)
45+
length = nums[1].(int)
46+
default:
47+
return "", errors.New("too many arguments")
48+
}
49+
50+
if start < -len(asRunes) {
51+
start = 0
52+
}
53+
if start > len(asRunes) {
54+
return "", fmt.Errorf("start position out of bounds for %d-byte string", len(a))
55+
}
56+
57+
var s, e int
58+
if start >= 0 && length >= 0 {
59+
s = start
60+
e = start + length
61+
} else if start < 0 && length >= 0 {
62+
s = len(asRunes) + start
63+
e = len(asRunes) + start + length + 1
64+
} else if start >= 0 && length < 0 {
65+
s = start
66+
e = len(asRunes) + length
67+
} else {
68+
s = len(asRunes) + start
69+
e = len(asRunes) + length
70+
}
71+
72+
if s > e {
73+
return "", fmt.Errorf("calculated start position greater than end position: %d > %d", s, e)
74+
}
75+
if e > len(asRunes) {
76+
e = len(asRunes)
77+
}
78+
79+
return string(asRunes[s:e]), nil
80+
}
81+
1382
////////////////////////////////////////////////////////////////////////////
1483
// Regexp Function Definitions
1584

using_easygen.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ I.e., with the environment variable `EASYGEN_TS`, the `.tmpl` template file is n
1111

1212
$ EASYGEN_TS='{{.Name}}' easygen -ts '{{clk2uc .Name}}' /tmp/var
1313
SomeInitMethod
14+
15+
$ easygen -ts '{{substr (clk2uc .Name) 4}}' /tmp/var
16+
InitMethod
17+
18+
$ easygen -ts '{{substr (clk2uc .Name) 8 3}}' /tmp/var
19+
Met
20+
21+
$ easygen -ts '{{substr (clk2uc .Name) 4 -2}}' /tmp/var
22+
InitMeth
23+
24+
$ easygen -ts '{{substr (clk2uc .Name) -6}}' /tmp/var
25+
Method
26+
27+
$ easygen -ts '{{substr "abcde" -3}}' /tmp/var
28+
cde
1429

1530
I.e., command line value takes the highest priority, even overriding the environment variable `EASYGEN_TS`'s value.
1631

0 commit comments

Comments
 (0)