@@ -96,14 +96,12 @@ func (s *String) format(args []Object) Object {
96
96
return & String {Value : value }
97
97
}
98
98
99
- // Format the string like "Hello {0}", jina ...
100
99
func formatStr (format string , options []Object ) (string , error ) {
101
100
var str strings.Builder
102
101
var val strings.Builder
103
102
var check_val bool
104
103
var opts_len int = len (options )
105
104
106
- // This is to enable escaping the {} braces if you want them included
107
105
var escapeChar bool
108
106
109
107
type optM struct {
@@ -113,13 +111,10 @@ func formatStr(format string, options []Object) (string, error) {
113
111
114
112
var optionsMap = make (map [int ]optM , opts_len )
115
113
116
- // convert the options into a map (may not be the most efficient but we are not going fast)
117
114
for i , optm := range options {
118
115
optionsMap [i ] = optM {val : false , obj : optm }
119
116
}
120
117
121
- // Now go through the format string and do the replacement(s)
122
- // this has approx time complexity of O(n) (bestest case)
123
118
for _ , opt := range format {
124
119
125
120
if ! escapeChar && opt == '\\' {
@@ -140,12 +135,9 @@ func formatStr(format string, options []Object) (string, error) {
140
135
}
141
136
142
137
if check_val && opt == '}' {
143
- vstr := strings .TrimSpace (val .String ()) // remove accidental spaces
144
-
145
- // check the value and try to convert to int
138
+ vstr := strings .TrimSpace (val .String ())
146
139
arrv , err := strconv .Atoi (vstr )
147
140
if err != nil {
148
- // return non-cryptic go errors
149
141
return "" , fmt .Errorf (fmt .Sprintf ("Ulichopeana si NAMBA, jaribu tena: `%s'" , vstr ))
150
142
}
151
143
@@ -156,7 +148,6 @@ func formatStr(format string, options []Object) (string, error) {
156
148
}
157
149
158
150
str .WriteString (oVal .obj .Inspect ())
159
- // may be innefficient
160
151
optionsMap [arrv ] = optM {val : true , obj : oVal .obj }
161
152
162
153
check_val = false
@@ -172,25 +163,15 @@ func formatStr(format string, options []Object) (string, error) {
172
163
str .WriteRune (opt )
173
164
}
174
165
175
- // A check if they never closed the formatting braces e.g '{0'
176
166
if check_val {
177
167
return "" , fmt .Errorf (fmt .Sprintf ("Haukufunga '{', tuliokota kabla ya kufika mwisho `%s'" , val .String ()))
178
168
}
179
169
180
- // Another innefficient loop
181
170
for _ , v := range optionsMap {
182
171
if ! v .val {
183
172
return "" , fmt .Errorf (fmt .Sprintf ("Ulipeana hili chaguo (%s) {%s} lakini haukutumia" , v .obj .Inspect (), v .obj .Type ()))
184
173
}
185
174
}
186
175
187
- // !start
188
- // Here we can talk about wtf just happened
189
- // 3 loops to do just formatting, formatting for codes sake.
190
- // it can be done in 2 loops, the last one is just to confirm that you didn't forget anything.
191
- // this is not required but a nice syntatic sugar, we are not here for speed, so ergonomics matter instead of speed
192
- // finally we can say we are slower than python!, what an achievement
193
- // done!
194
-
195
176
return str .String (), nil
196
177
}
0 commit comments