Path: blob/dev/pkg/protocols/common/replacer/replacer.go
2072 views
package replacer12import (3"strings"45"github.com/projectdiscovery/fasttemplate"67"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/marker"8"github.com/projectdiscovery/nuclei/v3/pkg/types"9)1011// Replace replaces placeholders in template with values on the fly.12func Replace(template string, values map[string]interface{}) string {13valuesMap := make(map[string]interface{}, len(values))14for k, v := range values {15valuesMap[k] = types.ToString(v)16}17replaced := fasttemplate.ExecuteStringStd(template, marker.ParenthesisOpen, marker.ParenthesisClose, valuesMap)18final := fasttemplate.ExecuteStringStd(replaced, marker.General, marker.General, valuesMap)19return final20}2122// Replace replaces one placeholder in template with one value on the fly.23func ReplaceOne(template string, key string, value interface{}) string {24data := replaceOneWithMarkers(template, key, value, marker.ParenthesisOpen, marker.ParenthesisClose)25return replaceOneWithMarkers(data, key, value, marker.General, marker.General)26}2728// replaceOneWithMarkers is a helper function that perform one time replacement29func replaceOneWithMarkers(template, key string, value interface{}, openMarker, closeMarker string) string {30return strings.Replace(template, openMarker+key+closeMarker, types.ToString(value), 1)31}323334