aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/function/stdlib/format_fsm.go
blob: 86876ba9fe592dc64860778cfc082930f948ff66 (plain) (blame)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// line 1 "format_fsm.rl"
// This file is generated from format_fsm.rl. DO NOT EDIT.

// line 5 "format_fsm.rl"

package stdlib

import (
	"bytes"
	"fmt"
	"unicode/utf8"

	"github.com/zclconf/go-cty/cty"
)

// line 20 "format_fsm.go"
var _formatfsm_actions []byte = []byte{
	0, 1, 0, 1, 1, 1, 2, 1, 4,
	1, 5, 1, 6, 1, 7, 1, 8,
	1, 9, 1, 10, 1, 11, 1, 14,
	1, 16, 1, 17, 1, 18, 2, 3,
	4, 2, 12, 10, 2, 12, 16, 2,
	12, 18, 2, 13, 14, 2, 15, 10,
	2, 15, 18,
}

var _formatfsm_key_offsets []byte = []byte{
	0, 0, 14, 27, 34, 36, 39, 43,
	51,
}

var _formatfsm_trans_keys []byte = []byte{
	32, 35, 37, 43, 45, 46, 48, 91,
	49, 57, 65, 90, 97, 122, 32, 35,
	43, 45, 46, 48, 91, 49, 57, 65,
	90, 97, 122, 91, 48, 57, 65, 90,
	97, 122, 49, 57, 93, 48, 57, 65,
	90, 97, 122, 46, 91, 48, 57, 65,
	90, 97, 122, 37,
}

var _formatfsm_single_lengths []byte = []byte{
	0, 8, 7, 1, 0, 1, 0, 2,
	1,
}

var _formatfsm_range_lengths []byte = []byte{
	0, 3, 3, 3, 1, 1, 2, 3,
	0,
}

var _formatfsm_index_offsets []byte = []byte{
	0, 0, 12, 23, 28, 30, 33, 36,
	42,
}

var _formatfsm_indicies []byte = []byte{
	1, 2, 3, 4, 5, 6, 7, 10,
	8, 9, 9, 0, 1, 2, 4, 5,
	6, 7, 10, 8, 9, 9, 0, 13,
	11, 12, 12, 0, 14, 0, 15, 14,
	0, 9, 9, 0, 16, 19, 17, 18,
	18, 0, 20, 3,
}

var _formatfsm_trans_targs []byte = []byte{
	0, 2, 2, 8, 2, 2, 3, 2,
	7, 8, 4, 3, 8, 4, 5, 6,
	3, 7, 8, 4, 1,
}

var _formatfsm_trans_actions []byte = []byte{
	7, 17, 9, 3, 15, 13, 25, 11,
	43, 29, 19, 27, 49, 46, 21, 0,
	37, 23, 40, 34, 1,
}

var _formatfsm_eof_actions []byte = []byte{
	0, 31, 31, 31, 31, 31, 31, 31,
	5,
}

const formatfsm_start int = 8
const formatfsm_first_final int = 8
const formatfsm_error int = 0

const formatfsm_en_main int = 8

// line 19 "format_fsm.rl"

func formatFSM(format string, a []cty.Value) (string, error) {
	var buf bytes.Buffer
	data := format
	nextArg := 1 // arg numbers are 1-based
	var verb formatVerb

	// line 153 "format_fsm.rl"

	// Ragel state
	p := 0          // "Pointer" into data
	pe := len(data) // End-of-data "pointer"
	cs := 0         // current state (will be initialized by ragel-generated code)
	ts := 0
	te := 0
	eof := pe

	// Keep Go compiler happy even if generated code doesn't use these
	_ = ts
	_ = te
	_ = eof

	// line 121 "format_fsm.go"
	{
		cs = formatfsm_start
	}

	// line 126 "format_fsm.go"
	{
		var _klen int
		var _trans int
		var _acts int
		var _nacts uint
		var _keys int
		if p == pe {
			goto _test_eof
		}
		if cs == 0 {
			goto _out
		}
	_resume:
		_keys = int(_formatfsm_key_offsets[cs])
		_trans = int(_formatfsm_index_offsets[cs])

		_klen = int(_formatfsm_single_lengths[cs])
		if _klen > 0 {
			_lower := int(_keys)
			var _mid int
			_upper := int(_keys + _klen - 1)
			for {
				if _upper < _lower {
					break
				}

				_mid = _lower + ((_upper - _lower) >> 1)
				switch {
				case data[p] < _formatfsm_trans_keys[_mid]:
					_upper = _mid - 1
				case data[p] > _formatfsm_trans_keys[_mid]:
					_lower = _mid + 1
				default:
					_trans += int(_mid - int(_keys))
					goto _match
				}
			}
			_keys += _klen
			_trans += _klen
		}

		_klen = int(_formatfsm_range_lengths[cs])
		if _klen > 0 {
			_lower := int(_keys)
			var _mid int
			_upper := int(_keys + (_klen << 1) - 2)
			for {
				if _upper < _lower {
					break
				}

				_mid = _lower + (((_upper - _lower) >> 1) & ^1)
				switch {
				case data[p] < _formatfsm_trans_keys[_mid]:
					_upper = _mid - 2
				case data[p] > _formatfsm_trans_keys[_mid+1]:
					_lower = _mid + 2
				default:
					_trans += int((_mid - int(_keys)) >> 1)
					goto _match
				}
			}
			_trans += _klen
		}

	_match:
		_trans = int(_formatfsm_indicies[_trans])
		cs = int(_formatfsm_trans_targs[_trans])

		if _formatfsm_trans_actions[_trans] == 0 {
			goto _again
		}

		_acts = int(_formatfsm_trans_actions[_trans])
		_nacts = uint(_formatfsm_actions[_acts])
		_acts++
		for ; _nacts > 0; _nacts-- {
			_acts++
			switch _formatfsm_actions[_acts-1] {
			case 0:
				// line 29 "format_fsm.rl"

				verb = formatVerb{
					ArgNum: nextArg,
					Prec:   -1,
					Width:  -1,
				}
				ts = p

			case 1:
				// line 38 "format_fsm.rl"

				buf.WriteByte(data[p])

			case 4:
				// line 49 "format_fsm.rl"

				// We'll try to slurp a whole UTF-8 sequence here, to give the user
				// better feedback.
				r, _ := utf8.DecodeRuneInString(data[p:])
				return buf.String(), fmt.Errorf("unrecognized format character %q at offset %d", r, p)

			case 5:
				// line 56 "format_fsm.rl"

				verb.Sharp = true

			case 6:
				// line 59 "format_fsm.rl"

				verb.Zero = true

			case 7:
				// line 62 "format_fsm.rl"

				verb.Minus = true

			case 8:
				// line 65 "format_fsm.rl"

				verb.Plus = true

			case 9:
				// line 68 "format_fsm.rl"

				verb.Space = true

			case 10:
				// line 72 "format_fsm.rl"

				verb.ArgNum = 0

			case 11:
				// line 75 "format_fsm.rl"

				verb.ArgNum = (10 * verb.ArgNum) + (int(data[p]) - '0')

			case 12:
				// line 79 "format_fsm.rl"

				verb.HasWidth = true

			case 13:
				// line 82 "format_fsm.rl"

				verb.Width = 0

			case 14:
				// line 85 "format_fsm.rl"

				verb.Width = (10 * verb.Width) + (int(data[p]) - '0')

			case 15:
				// line 89 "format_fsm.rl"

				verb.HasPrec = true

			case 16:
				// line 92 "format_fsm.rl"

				verb.Prec = 0

			case 17:
				// line 95 "format_fsm.rl"

				verb.Prec = (10 * verb.Prec) + (int(data[p]) - '0')

			case 18:
				// line 99 "format_fsm.rl"

				verb.Mode = rune(data[p])
				te = p + 1
				verb.Raw = data[ts:te]
				verb.Offset = ts

				err := formatAppend(&verb, &buf, a)
				if err != nil {
					return buf.String(), err
				}
				nextArg = verb.ArgNum + 1

				// line 324 "format_fsm.go"
			}
		}

	_again:
		if cs == 0 {
			goto _out
		}
		p++
		if p != pe {
			goto _resume
		}
	_test_eof:
		{
		}
		if p == eof {
			__acts := _formatfsm_eof_actions[cs]
			__nacts := uint(_formatfsm_actions[__acts])
			__acts++
			for ; __nacts > 0; __nacts-- {
				__acts++
				switch _formatfsm_actions[__acts-1] {
				case 2:
					// line 42 "format_fsm.rl"

				case 3:
					// line 45 "format_fsm.rl"

					return buf.String(), fmt.Errorf("invalid format string starting at offset %d", p)

				case 4:
					// line 49 "format_fsm.rl"

					// We'll try to slurp a whole UTF-8 sequence here, to give the user
					// better feedback.
					r, _ := utf8.DecodeRuneInString(data[p:])
					return buf.String(), fmt.Errorf("unrecognized format character %q at offset %d", r, p)

					// line 363 "format_fsm.go"
				}
			}
		}

	_out:
		{
		}
	}

	// line 171 "format_fsm.rl"

	// If we fall out here without being in a final state then we've
	// encountered something that the scanner can't match, which should
	// be impossible (the scanner matches all bytes _somehow_) but we'll
	// flag it anyway rather than just losing data from the end.
	if cs < formatfsm_first_final {
		return buf.String(), fmt.Errorf("extraneous characters beginning at offset %i", p)
	}

	return buf.String(), nil
}