Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // do template string substitution. All template variables have to start with `prefix_byte`
- func SearchAndJoin(template string, prefix_byte byte, template_params []string, template_values []string) string {
- ss := make([]string, 0, 16)
- remaining := template
- for {
- index := strings.IndexByte(remaining, prefix_byte)
- if index == -1 {
- ss = append(ss, remaining)
- break
- }
- ss = append(ss, remaining[:index])
- remaining = remaining[index:]
- substituted := false
- for i, placeholder := range(template_params) {
- if strings.HasPrefix(remaining, placeholder) {
- replacement := (template_values[i])
- ss = append(ss, replacement)
- remaining = remaining[len(placeholder):]
- substituted = true
- break
- }
- }
- if !substituted {
- ss = append(ss, remaining[:1])
- remaining = remaining[1:]
- }
- }
- return strings.Join(ss, "")
- }
Add Comment
Please, Sign In to add comment