| 
									
										
										
										
											2018-09-21 20:36:33 -04:00
										 |  |  | package strings | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-10-14 20:06:27 +03:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 20:36:33 -04:00
										 |  |  | 	"github.com/MontFerret/ferret/pkg/runtime/core" | 
					
						
							|  |  |  | 	"github.com/MontFerret/ferret/pkg/runtime/values" | 
					
						
							| 
									
										
										
										
											2019-02-13 12:31:18 -05:00
										 |  |  | 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | 
					
						
							| 
									
										
										
										
											2018-09-21 20:36:33 -04:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-14 20:06:27 +03:00
										 |  |  | // Substitute replaces search values in the string value. | 
					
						
							|  |  |  | // @params text (String) - The string to modify | 
					
						
							|  |  |  | // @params search (String) - The string representing a search pattern | 
					
						
							|  |  |  | // @params replace (String) - The string representing a replace value | 
					
						
							|  |  |  | // @param limit (Int) - The cap the number of replacements to this value. | 
					
						
							|  |  |  | // @return (String) - Returns a string with replace substring. | 
					
						
							| 
									
										
										
										
											2018-09-21 20:36:33 -04:00
										 |  |  | func Substitute(_ context.Context, args ...core.Value) (core.Value, error) { | 
					
						
							|  |  |  | 	err := core.ValidateArgs(args, 2, 4) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return values.EmptyString, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	text := args[0].String() | 
					
						
							|  |  |  | 	search := args[1].String() | 
					
						
							|  |  |  | 	replace := "" | 
					
						
							|  |  |  | 	limit := -1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(args) > 2 { | 
					
						
							|  |  |  | 		replace = args[2].String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(args) > 3 { | 
					
						
							| 
									
										
										
										
											2019-02-13 12:31:18 -05:00
										 |  |  | 		if args[3].Type() == types.Int { | 
					
						
							| 
									
										
										
										
											2018-09-21 20:36:33 -04:00
										 |  |  | 			limit = int(args[3].(values.Int)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	out := strings.Replace(text, search, replace, limit) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return values.NewString(out), nil | 
					
						
							|  |  |  | } |