Skip to content

Commit

Permalink
stash replace function somewhere else for now
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Feb 3, 2020
1 parent 811b229 commit 8e00637
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 135 deletions.
84 changes: 0 additions & 84 deletions cty/function/stdlib/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,76 +343,6 @@ var IndentFunc = function.New(&function.Spec{
},
})

// ReplaceFunc is a function that searches a given string for another given
// substring, and replaces each occurence with a given replacement string.
// The substr argument is a simple string.
var ReplaceFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "str",
Type: cty.String,
},
{
Name: "substr",
Type: cty.String,
},
{
Name: "replace",
Type: cty.String,
},
{
Name: "n",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
str := args[0].AsString()
substr := args[1].AsString()
replace := args[2].AsString()
var n int
err := gocty.FromCtyValue(args[3], &n)
if err != nil {
return cty.NilVal, err
}

return cty.StringVal(strings.Replace(str, substr, replace, n)), nil
},
})

// RegexpReplaceAllFunc is a function that searches a given string for another
// given substring, and replaces each occurence with a given replacement
// string. The substr argument must be a valid regular expression.
var RegexpReplaceAllFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "str",
Type: cty.String,
},
{
Name: "substr",
Type: cty.String,
},
{
Name: "replace",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
str := args[0].AsString()
substr := args[1].AsString()
replace := args[2].AsString()

re, err := regexp.Compile(substr[1 : len(substr)-1])
if err != nil {
return cty.UnknownVal(cty.String), err
}

return cty.StringVal(re.ReplaceAllString(str, replace)), nil
},
})

// TitleFunc is a function that converts the first letter of each word in the
// given string to uppercase.
var TitleFunc = function.New(&function.Spec{
Expand Down Expand Up @@ -584,20 +514,6 @@ func Indent(spaces, str cty.Value) (cty.Value, error) {
return IndentFunc.Call([]cty.Value{spaces, str})
}

func Replace(str, substr, replace, n cty.Value) (cty.Value, error) {
return ReplaceFunc.Call([]cty.Value{str, substr, replace, n})
}

// ReplaceAll searches a given string for another given substring,
// and replaces all occurrences with a given replacement string.
func ReplaceAll(str, substr, replace cty.Value) (cty.Value, error) {
return ReplaceFunc.Call([]cty.Value{str, substr, replace, cty.NumberIntVal(-1)})
}

func RegexpReplaceAll(str, substr, replace cty.Value) (cty.Value, error) {
return RegexpReplaceAllFunc.Call([]cty.Value{str, substr, replace})
}

// Title converts the first letter of each word in the given string to uppercase.
func Title(str cty.Value) (cty.Value, error) {
return TitleFunc.Call([]cty.Value{str})
Expand Down
51 changes: 0 additions & 51 deletions cty/function/stdlib/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,54 +387,3 @@ func TestSubstr(t *testing.T) {
})
}
}

func TestReplace(t *testing.T) {
tests := []struct {
Input cty.Value
Substr, Replace, N cty.Value
Want cty.Value
}{
{
cty.StringVal("hello"),
cty.StringVal("l"),
cty.StringVal(""),
cty.NumberIntVal(1),
cty.StringVal("helo"),
},
{
cty.StringVal("hello"),
cty.StringVal("l"),
cty.StringVal(""),
cty.NumberIntVal(-1),
cty.StringVal("heo"),
},
{
cty.StringVal("😸😸😸😾😾😾"),
cty.StringVal("😾"),
cty.StringVal("😸"),
cty.NumberIntVal(1),
cty.StringVal("😸😸😸😸😾😾"),
},
{
cty.StringVal("😸😸😸😾😾😾"),
cty.StringVal("😾"),
cty.StringVal("😸"),
cty.NumberIntVal(-1),
cty.StringVal("😸😸😸😸😸😸"),
},
}

for _, test := range tests {
t.Run(test.Input.GoString(), func(t *testing.T) {
got, err := Replace(test.Input, test.Substr, test.Replace, test.N)

if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}

0 comments on commit 8e00637

Please sign in to comment.