Skip to content

Commit a5a6ca1

Browse files
committed
stash replace function somewhere else for now
1 parent 811b229 commit a5a6ca1

File tree

2 files changed

+94
-84
lines changed

2 files changed

+94
-84
lines changed

cty/function/stdlib/string.go

-84
Original file line numberDiff line numberDiff line change
@@ -343,76 +343,6 @@ var IndentFunc = function.New(&function.Spec{
343343
},
344344
})
345345

346-
// ReplaceFunc is a function that searches a given string for another given
347-
// substring, and replaces each occurence with a given replacement string.
348-
// The substr argument is a simple string.
349-
var ReplaceFunc = function.New(&function.Spec{
350-
Params: []function.Parameter{
351-
{
352-
Name: "str",
353-
Type: cty.String,
354-
},
355-
{
356-
Name: "substr",
357-
Type: cty.String,
358-
},
359-
{
360-
Name: "replace",
361-
Type: cty.String,
362-
},
363-
{
364-
Name: "n",
365-
Type: cty.Number,
366-
},
367-
},
368-
Type: function.StaticReturnType(cty.String),
369-
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
370-
str := args[0].AsString()
371-
substr := args[1].AsString()
372-
replace := args[2].AsString()
373-
var n int
374-
err := gocty.FromCtyValue(args[3], &n)
375-
if err != nil {
376-
return cty.NilVal, err
377-
}
378-
379-
return cty.StringVal(strings.Replace(str, substr, replace, n)), nil
380-
},
381-
})
382-
383-
// RegexpReplaceAllFunc is a function that searches a given string for another
384-
// given substring, and replaces each occurence with a given replacement
385-
// string. The substr argument must be a valid regular expression.
386-
var RegexpReplaceAllFunc = function.New(&function.Spec{
387-
Params: []function.Parameter{
388-
{
389-
Name: "str",
390-
Type: cty.String,
391-
},
392-
{
393-
Name: "substr",
394-
Type: cty.String,
395-
},
396-
{
397-
Name: "replace",
398-
Type: cty.String,
399-
},
400-
},
401-
Type: function.StaticReturnType(cty.String),
402-
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
403-
str := args[0].AsString()
404-
substr := args[1].AsString()
405-
replace := args[2].AsString()
406-
407-
re, err := regexp.Compile(substr[1 : len(substr)-1])
408-
if err != nil {
409-
return cty.UnknownVal(cty.String), err
410-
}
411-
412-
return cty.StringVal(re.ReplaceAllString(str, replace)), nil
413-
},
414-
})
415-
416346
// TitleFunc is a function that converts the first letter of each word in the
417347
// given string to uppercase.
418348
var TitleFunc = function.New(&function.Spec{
@@ -584,20 +514,6 @@ func Indent(spaces, str cty.Value) (cty.Value, error) {
584514
return IndentFunc.Call([]cty.Value{spaces, str})
585515
}
586516

587-
func Replace(str, substr, replace, n cty.Value) (cty.Value, error) {
588-
return ReplaceFunc.Call([]cty.Value{str, substr, replace, n})
589-
}
590-
591-
// ReplaceAll searches a given string for another given substring,
592-
// and replaces all occurrences with a given replacement string.
593-
func ReplaceAll(str, substr, replace cty.Value) (cty.Value, error) {
594-
return ReplaceFunc.Call([]cty.Value{str, substr, replace, cty.NumberIntVal(-1)})
595-
}
596-
597-
func RegexpReplaceAll(str, substr, replace cty.Value) (cty.Value, error) {
598-
return RegexpReplaceAllFunc.Call([]cty.Value{str, substr, replace})
599-
}
600-
601517
// Title converts the first letter of each word in the given string to uppercase.
602518
func Title(str cty.Value) (cty.Value, error) {
603519
return TitleFunc.Call([]cty.Value{str})

cty/function/stdlib/string_replace.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package stdlib
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"github.com/zclconf/go-cty/cty"
8+
"github.com/zclconf/go-cty/cty/function"
9+
"github.com/zclconf/go-cty/cty/gocty"
10+
)
11+
12+
// ReplaceFunc is a function that searches a given string for another given
13+
// substring, and replaces each occurence with a given replacement string.
14+
// The substr argument is a simple string.
15+
var ReplaceFunc = function.New(&function.Spec{
16+
Params: []function.Parameter{
17+
{
18+
Name: "str",
19+
Type: cty.String,
20+
},
21+
{
22+
Name: "substr",
23+
Type: cty.String,
24+
},
25+
{
26+
Name: "replace",
27+
Type: cty.String,
28+
},
29+
{
30+
Name: "n",
31+
Type: cty.Number,
32+
},
33+
},
34+
Type: function.StaticReturnType(cty.String),
35+
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
36+
str := args[0].AsString()
37+
substr := args[1].AsString()
38+
replace := args[2].AsString()
39+
var n int
40+
err := gocty.FromCtyValue(args[3], &n)
41+
if err != nil {
42+
return cty.NilVal, err
43+
}
44+
45+
return cty.StringVal(strings.Replace(str, substr, replace, n)), nil
46+
},
47+
})
48+
49+
// RegexpReplaceAllFunc is a function that searches a given string for another
50+
// given substring, and replaces each occurence with a given replacement
51+
// string. The substr argument must be a valid regular expression.
52+
var RegexpReplaceAllFunc = function.New(&function.Spec{
53+
Params: []function.Parameter{
54+
{
55+
Name: "str",
56+
Type: cty.String,
57+
},
58+
{
59+
Name: "substr",
60+
Type: cty.String,
61+
},
62+
{
63+
Name: "replace",
64+
Type: cty.String,
65+
},
66+
},
67+
Type: function.StaticReturnType(cty.String),
68+
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
69+
str := args[0].AsString()
70+
substr := args[1].AsString()
71+
replace := args[2].AsString()
72+
73+
re, err := regexp.Compile(substr[1 : len(substr)-1])
74+
if err != nil {
75+
return cty.UnknownVal(cty.String), err
76+
}
77+
78+
return cty.StringVal(re.ReplaceAllString(str, replace)), nil
79+
},
80+
})
81+
82+
func Replace(str, substr, replace, n cty.Value) (cty.Value, error) {
83+
return ReplaceFunc.Call([]cty.Value{str, substr, replace, n})
84+
}
85+
86+
// ReplaceAll searches a given string for another given substring,
87+
// and replaces all occurrences with a given replacement string.
88+
func ReplaceAll(str, substr, replace cty.Value) (cty.Value, error) {
89+
return ReplaceFunc.Call([]cty.Value{str, substr, replace, cty.NumberIntVal(-1)})
90+
}
91+
92+
func RegexpReplaceAll(str, substr, replace cty.Value) (cty.Value, error) {
93+
return RegexpReplaceAllFunc.Call([]cty.Value{str, substr, replace})
94+
}

0 commit comments

Comments
 (0)