SHARE
TWEET

Untitled

a guest Jan 24th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // do template string substitution. All template variables have to start with `prefix_byte`
  2. func SearchAndJoin(template string, prefix_byte byte, template_params []string, template_values []string) string {
  3.     ss := make([]string, 0, 16)
  4.     remaining := template
  5.     for {
  6.         index := strings.IndexByte(remaining, prefix_byte)
  7.         if index == -1 {
  8.             ss = append(ss, remaining)
  9.             break
  10.         }
  11.         ss = append(ss, remaining[:index])
  12.         remaining = remaining[index:]
  13.  
  14.         substituted := false
  15.         for i, placeholder := range(template_params) {
  16.             if strings.HasPrefix(remaining, placeholder) {
  17.                 replacement := (template_values[i])
  18.                 ss = append(ss, replacement)
  19.                 remaining = remaining[len(placeholder):]
  20.                 substituted = true
  21.                 break
  22.             }
  23.         }
  24.  
  25.         if !substituted {
  26.             ss = append(ss, remaining[:1])
  27.             remaining = remaining[1:]
  28.         }
  29.     }
  30.  
  31.     return strings.Join(ss, "")
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top