OLD | NEW |
1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 // Package stringtemplate implements Python string.Template-like substitution. | 5 // Package stringtemplate implements Python string.Template-like substitution. |
6 package stringtemplate | 6 package stringtemplate |
7 | 7 |
8 import ( | 8 import ( |
9 "regexp" | 9 "regexp" |
10 "strings" | 10 "strings" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 var ( | 53 var ( |
54 parts = make([]string, 0, (len(smi)*2)+1) | 54 parts = make([]string, 0, (len(smi)*2)+1) |
55 pos = 0 | 55 pos = 0 |
56 ) | 56 ) |
57 | 57 |
58 for _, match := range smi { | 58 for _, match := range smi { |
59 key := "" | 59 key := "" |
60 switch { | 60 switch { |
61 case match[8] >= 0: | 61 case match[8] >= 0: |
62 // Invalid. | 62 // Invalid. |
63 » » » return "", errors.Reason("invalid template: %(template)q
"). | 63 » » » return "", errors.Reason("invalid template: %q", v).Err(
) |
64 » » » » D("template", v). | |
65 » » » » Err() | |
66 | 64 |
67 case match[2] >= 0: | 65 case match[2] >= 0: |
68 // Escaped. | 66 // Escaped. |
69 parts = append(parts, v[pos:match[2]]) | 67 parts = append(parts, v[pos:match[2]]) |
70 pos = match[3] | 68 pos = match[3] |
71 continue | 69 continue |
72 | 70 |
73 case match[4] >= 0: | 71 case match[4] >= 0: |
74 // Key (without braces) | 72 // Key (without braces) |
75 key = v[match[4]:match[5]] | 73 key = v[match[4]:match[5]] |
76 case match[6] >= 0: | 74 case match[6] >= 0: |
77 // Key (with braces) | 75 // Key (with braces) |
78 key = v[match[6]:match[7]] | 76 key = v[match[6]:match[7]] |
79 | 77 |
80 default: | 78 default: |
81 panic("impossible") | 79 panic("impossible") |
82 } | 80 } |
83 | 81 |
84 // Add anything in between the previous match and the current. I
f our match | 82 // Add anything in between the previous match and the current. I
f our match |
85 // includes a non-escape character, add that too. | 83 // includes a non-escape character, add that too. |
86 if pos < match[0] { | 84 if pos < match[0] { |
87 parts = append(parts, v[pos:match[0]]) | 85 parts = append(parts, v[pos:match[0]]) |
88 } | 86 } |
89 pos = match[1] | 87 pos = match[1] |
90 | 88 |
91 subst, ok := subst[key] | 89 subst, ok := subst[key] |
92 if !ok { | 90 if !ok { |
93 » » » return "", errors.Reason("no substitution for %(key)q"). | 91 » » » return "", errors.Reason("no substitution for %q", key).
Err() |
94 » » » » D("key", key). | |
95 » » » » Err() | |
96 } | 92 } |
97 parts = append(parts, subst) | 93 parts = append(parts, subst) |
98 } | 94 } |
99 | 95 |
100 // Append any trailing string. | 96 // Append any trailing string. |
101 parts = append(parts, v[pos:]) | 97 parts = append(parts, v[pos:]) |
102 | 98 |
103 // Join the parts. | 99 // Join the parts. |
104 return strings.Join(parts, ""), nil | 100 return strings.Join(parts, ""), nil |
105 } | 101 } |
OLD | NEW |