Skip to content

Commit 6324de2

Browse files
committed
Merge branch 'release/v1.2.2'
2 parents a0a85aa + 74fe596 commit 6324de2

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A set of a ***high performance string tools*** that helps to build strings from
66
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/wissance/stringFormatter?style=plastic)
77
![GitHub issues](https://img.shields.io/github/issues/wissance/stringFormatter?style=plastic)
88
![GitHub Release Date](https://img.shields.io/github/release-date/wissance/stringFormatter)
9-
![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.2.1/total?style=plastic)
9+
![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.2.2/total?style=plastic)
1010

1111
![String Formatter: a convenient string formatting tool](/img/sf_cover.png)
1212

formatter.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,14 @@ func Format(template string, args ...any) string {
117117
strVal := getItemAsStr(&args[argNumber], &argFormatOptions)
118118
formattedStr.WriteString(strVal)
119119
} else {
120-
formattedStr.WriteByte('{')
121-
formattedStr.WriteString(argNumberStr)
122-
formattedStr.WriteByte('}')
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('{')
127+
}
123128
}
124129
i = j
125130
}
@@ -210,9 +215,14 @@ func FormatComplex(template string, args map[string]any) string {
210215
strVal := getItemAsStr(&arg, &argFormatOptions)
211216
formattedStr.WriteString(strVal)
212217
} else {
213-
formattedStr.WriteByte('{')
214-
formattedStr.WriteString(argNumberStr)
215-
formattedStr.WriteByte('}')
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('{')
225+
}
216226
}
217227
i = j
218228
}

formatter_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,22 @@ func TestFormat(t *testing.T) {
113113
},
114114
expected: "Example is: {123 This is a test str, nothing more special -1.098743 main question error, is 42}",
115115
},
116-
"open bracket at the end of line of go file": {
116+
"open bracket at the end of line of go line": {
117117
template: "type serviceHealth struct {",
118118
args: []any{},
119119
expected: "type serviceHealth struct {",
120120
},
121+
"open bracket at the end of line of go line with {} inside": {
122+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
123+
args: []any{},
124+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
125+
},
126+
127+
"close bracket at the end of line of go line with {} inside": {
128+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
129+
args: []any{},
130+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
131+
},
121132
} {
122133
t.Run(name, func(t *testing.T) {
123134
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
@@ -202,6 +213,17 @@ func TestFormatComplex(t *testing.T) {
202213
args: map[string]any{},
203214
expected: " \"server\": {",
204215
},
216+
"open bracket at the end of line of go line with {} inside": {
217+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
218+
args: map[string]any{},
219+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
220+
},
221+
222+
"close bracket at the end of line of go line with {} inside": {
223+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
224+
args: map[string]any{},
225+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
226+
},
205227
} {
206228
t.Run(name, func(t *testing.T) {
207229
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))

0 commit comments

Comments
 (0)