77)
88
99const argumentFormatSeparator = ":"
10+ const bytesPerArgDefault = 20
1011
1112// Format
1213/* Func that makes string formatting from template
@@ -35,7 +36,8 @@ func Format(template string, args ...any) string {
3536
3637 templateLen := len (template )
3738 formattedStr := & strings.Builder {}
38- formattedStr .Grow (templateLen + 22 * len (args ))
39+ argsLen := bytesPerArgDefault * len (args )
40+ formattedStr .Grow (templateLen + argsLen + 1 )
3941 j := - 1 //nolint:ineffassign
4042
4143 nestedBrackets := false
@@ -55,6 +57,13 @@ func Format(template string, args ...any) string {
5557 continue
5658 }
5759 // find end of placeholder
60+ // process empty pair - {}
61+ if template [i + 1 ] == '}' {
62+ i ++
63+ formattedStr .WriteString ("{}" )
64+ continue
65+ }
66+ // process non-empty placeholder
5867 j = i + 2
5968 for {
6069 if j >= templateLen {
@@ -117,13 +126,9 @@ func Format(template string, args ...any) string {
117126 strVal := getItemAsStr (& args [argNumber ], & argFormatOptions )
118127 formattedStr .WriteString (strVal )
119128 } else {
120- if argNumberStr != "" {
121- formattedStr .WriteByte ('{' )
122- formattedStr .WriteString (argNumberStr )
123- formattedStr .WriteByte ('}' )
124- } else {
125- // complicated case when we have brackets in line and open line at the end
126- formattedStr .WriteByte ('{' )
129+ formattedStr .WriteString (template [i :j ])
130+ if j < templateLen - 1 {
131+ formattedStr .WriteByte (template [j ])
127132 }
128133 }
129134 i = j
@@ -156,7 +161,8 @@ func FormatComplex(template string, args map[string]any) string {
156161
157162 templateLen := len (template )
158163 formattedStr := & strings.Builder {}
159- formattedStr .Grow (templateLen + 22 * len (args ))
164+ argsLen := bytesPerArgDefault * len (args )
165+ formattedStr .Grow (templateLen + argsLen + 1 )
160166 j := - 1 //nolint:ineffassign
161167 nestedBrackets := false
162168 formattedStr .WriteString (template [:start ])
@@ -170,10 +176,18 @@ func FormatComplex(template string, args map[string]any) string {
170176 break
171177 }
172178
173- if template [i + 1 ] == '{' { // todo: umv: this not considering {{0}}
179+ if template [i + 1 ] == '{' {
174180 formattedStr .WriteByte ('{' )
175181 continue
176182 }
183+ // find end of placeholder
184+ // process empty pair - {}
185+ if template [i + 1 ] == '}' {
186+ i ++
187+ formattedStr .WriteString ("{}" )
188+ continue
189+ }
190+ // process non-empty placeholder
177191
178192 // find end of placeholder
179193 j = i + 2
@@ -212,16 +226,20 @@ func FormatComplex(template string, args map[string]any) string {
212226 }
213227 if ok || (argFormatOptions != "" && ! nestedBrackets ) {
214228 // get number from placeholder
215- strVal := getItemAsStr (& arg , & argFormatOptions )
229+ strVal := ""
230+ if arg != nil {
231+ strVal = getItemAsStr (& arg , & argFormatOptions )
232+ } else {
233+ formattedStr .WriteString (template [i :j ])
234+ if j < templateLen - 1 {
235+ formattedStr .WriteByte (template [j ])
236+ }
237+ }
216238 formattedStr .WriteString (strVal )
217239 } else {
218- if argNumberStr != "" {
219- formattedStr .WriteByte ('{' )
220- formattedStr .WriteString (argNumberStr )
221- formattedStr .WriteByte ('}' )
222- } else {
223- // complicated case when we have brackets in line and open line at the end
224- formattedStr .WriteByte ('{' )
240+ formattedStr .WriteString (template [i :j ])
241+ if j < templateLen - 1 {
242+ formattedStr .WriteByte (template [j ])
225243 }
226244 }
227245 i = j
0 commit comments