This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
fmt.go
207 lines (177 loc) · 5.37 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package strings2
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/xgfone/cast"
)
// DefaultFormat is the default global formatter.
var DefaultFormat = NewFormat("{", "}")
// FmtString formats the string s by DefaultFormat, which is short for
// DefaultFormat.Format(s, kwargs...)
func FmtString(s string, kwargs ...interface{}) string {
return DefaultFormat.Format(s, kwargs...)
}
// FmtStringByMap formats the string s by DefaultFormat, which is short for
// DefaultFormat.FormatByMap(s, kwargs)
func FmtStringByMap(s string, kwargs map[string]interface{}) string {
return DefaultFormat.FormatByMap(s, kwargs)
}
// FmtStringByFunc formats the string s by DefaultFormat, which is short for
// DefaultFormat.FormatByFunc(s, getValue)
func FmtStringByFunc(s string, getValue func(string) (interface{}, bool)) string {
return DefaultFormat.FormatByFunc(s, getValue)
}
// FmtStringOutput formats the string s by DefaultFormat, which is short for
// DefaultFormat.FormatOutput(w, s, getValue)
func FmtStringOutput(w io.Writer, s string, getValue func(string) (interface{}, bool)) (int, error) {
return DefaultFormat.FormatOutput(w, s, getValue)
}
// Format is used to format a string based on the key placeholder
// that is replaced by the value.
type Format struct {
Left string
Right string
}
// NewFormat returns a new Format.
func NewFormat(left, right string) Format {
return Format{Left: left, Right: right}
}
func writeString(w io.Writer, n int, s string, args ...interface{}) (int, error) {
if len(args) == 0 {
m, err := io.WriteString(w, s)
n += m
return n, err
}
m, err := fmt.Fprintf(w, s, args...)
n += m
return n, err
}
// FormatOutput formats the string s into w, which will replaces the placeholder key
// with the value returned by getValue(key).
//
// If the placeholder key does not have a corresponding value, it will persist
// and not be replaced. However, if the value is a function, func() interface{},
// it will call it firstly to calculate the value.
//
// The placeholder key maybe contain the formatter, and the value will be
// formatted by fmt.Sprintf(formatter, value). They are separated by the colon
// and the % character is optional.
func (f Format) FormatOutput(w io.Writer, s string, getValue func(key string) (interface{}, bool)) (n int, err error) {
for {
leftIndex := strings.Index(s, f.Left)
if leftIndex == -1 {
break
}
if n, err = writeString(w, n, s[:leftIndex]); err != nil {
return
}
s = s[leftIndex+len(f.Left):]
rightIndex := strings.Index(s, f.Right)
if rightIndex == -1 {
break
}
valueEndIndex := rightIndex + len(f.Right)
var format string
key := s[:rightIndex]
if index := strings.IndexByte(key, ':'); index != -1 {
format = key[index+1:]
key = key[:index]
}
if key == "" {
continue
}
if value, ok := getValue(key); ok {
switch f := value.(type) {
case func() interface{}:
value = f()
}
if format != "" {
if format[0] != '%' {
format = "%" + format
}
if n, err = writeString(w, n, format, value); err != nil {
return
}
} else if v, err := cast.ToString(value); err == nil {
if n, err = writeString(w, n, v); err != nil {
return n, err
}
} else {
panic(fmt.Errorf("cannot convert '%v' to string: %s", value, err.Error()))
}
} else {
if n, err = writeString(w, n, f.Left); err != nil {
return
}
if n, err = writeString(w, n, s[:valueEndIndex]); err != nil {
return
}
}
s = s[valueEndIndex:]
}
if s != "" {
n, err = writeString(w, n, s)
}
return
}
// FormatByFunc is the same as FormatByFunc, but returns the result string.
func (f Format) FormatByFunc(s string, getValue func(key string) (interface{}, bool)) string {
_len := len(s)
if _len < 1024 {
_len *= 2
} else {
_len += _len / 4
}
buf := bytes.NewBuffer(nil)
buf.Grow(len(s) * 2)
f.FormatOutput(buf, s, getValue)
return buf.String()
}
// FormatByMap is the same as FormatByFunc, which will get the value from kwargs.
func (f Format) FormatByMap(s string, kwargs map[string]interface{}) string {
if len(kwargs) == 0 {
return s
}
return f.FormatByFunc(s, func(key string) (v interface{}, ok bool) {
v, ok = kwargs[key]
return
})
}
// Format formats the string s, which will convert kwargs to map[string]interface{}
// and call the method FormatByMap().
//
// Notice: the number of kwargs must be even, and the odd argument must be
// string.
func (f Format) Format(s string, kwargs ...interface{}) string {
_len := len(kwargs)
if _len%2 == 1 {
panic("the number of kwargs must be even")
}
ms := make(map[string]interface{}, _len)
for i := 0; i < _len; i += 2 {
switch s := kwargs[i].(type) {
case string:
ms[s] = kwargs[i+1]
case fmt.Stringer:
ms[s.String()] = kwargs[i+1]
default:
panic("the key must be string")
}
}
return f.FormatByMap(s, ms)
}