Coverage Report

Created: 2025-07-04 06:57

/src/swift-protobuf/Sources/SwiftProtobuf/TextFormatDecoder.swift
Line
Count
Source (jump to first uncovered line)
1
// Sources/SwiftProtobuf/TextFormatDecoder.swift - Text format decoding
2
//
3
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
4
// Licensed under Apache License v2.0 with Runtime Library Exception
5
//
6
// See LICENSE.txt for license information:
7
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
8
//
9
// -----------------------------------------------------------------------------
10
///
11
/// Test format decoding engine.
12
///
13
// -----------------------------------------------------------------------------
14
15
import Foundation
16
17
///
18
/// Provides a higher-level interface to the token stream coming
19
/// from a TextFormatScanner.  In particular, this provides
20
/// single-token pushback and convenience functions for iterating
21
/// over complex structures.
22
///
23
internal struct TextFormatDecoder: Decoder {
24
    internal var scanner: TextFormatScanner
25
21.9M
    private var fieldCount = 0
26
    private let terminator: UInt8?
27
    private let fieldNameMap: _NameMap
28
    private let messageType: any Message.Type
29
30
266k
    internal var options: TextFormatDecodingOptions {
31
266k
        scanner.options
32
266k
    }
33
34
48.3k
    internal var complete: Bool { scanner.complete }
35
36
    internal init(
37
        messageType: any Message.Type,
38
        utf8Pointer: UnsafeRawPointer,
39
        count: Int,
40
        options: TextFormatDecodingOptions,
41
        extensions: (any ExtensionMap)?
42
96.9k
    ) throws {
43
96.9k
        scanner = TextFormatScanner(utf8Pointer: utf8Pointer, count: count, options: options, extensions: extensions)
44
96.9k
        guard let nameProviding = (messageType as? any _ProtoNameProviding.Type) else {
45
0
            throw TextFormatDecodingError.missingFieldNames
46
96.9k
        }
47
96.9k
        fieldNameMap = nameProviding._protobuf_nameMap
48
96.9k
        self.messageType = messageType
49
96.9k
        self.terminator = nil
50
96.9k
    }
51
52
5.26M
    internal init(messageType: any Message.Type, scanner: TextFormatScanner, terminator: UInt8?) throws {
53
5.26M
        self.scanner = scanner
54
5.26M
        self.terminator = terminator
55
5.26M
        guard let nameProviding = (messageType as? any _ProtoNameProviding.Type) else {
56
0
            throw TextFormatDecodingError.missingFieldNames
57
5.26M
        }
58
5.26M
        fieldNameMap = nameProviding._protobuf_nameMap
59
5.26M
        self.messageType = messageType
60
5.26M
    }
61
62
94
    mutating func handleConflictingOneOf() throws {
63
94
        throw TextFormatDecodingError.conflictingOneOf
64
94
    }
65
66
43.2M
    mutating func nextFieldNumber() throws -> Int? {
67
43.2M
        if fieldCount > 0 {
68
31.2M
            scanner.skipOptionalSeparator()
69
43.2M
        }
70
43.2M
        if let fieldNumber = try scanner.nextFieldNumber(
71
43.2M
            names: fieldNameMap,
72
43.2M
            messageType: messageType,
73
43.2M
            terminator: terminator
74
43.2M
        ) {
75
31.4M
            fieldCount += 1
76
31.4M
            return fieldNumber
77
31.4M
        } else {
78
11.8M
            return nil
79
11.8M
        }
80
11.8M
    }
81
82
516
    mutating func decodeSingularFloatField(value: inout Float) throws {
83
516
        try scanner.skipRequiredColon()
84
516
        value = try scanner.nextFloat()
85
516
    }
86
22.7k
    mutating func decodeSingularFloatField(value: inout Float?) throws {
87
22.7k
        try scanner.skipRequiredColon()
88
22.7k
        value = try scanner.nextFloat()
89
22.7k
    }
90
68.4k
    mutating func decodeRepeatedFloatField(value: inout [Float]) throws {
91
68.4k
        try scanner.skipRequiredColon()
92
68.4k
        if scanner.skipOptionalBeginArray() {
93
4.84k
            var firstItem = true
94
13.8k
            while true {
95
13.6k
                if scanner.skipOptionalEndArray() {
96
4.63k
                    return
97
9.01k
                }
98
9.01k
                if firstItem {
99
4.49k
                    firstItem = false
100
9.01k
                } else {
101
4.52k
                    try scanner.skipRequiredComma()
102
9.01k
                }
103
9.01k
                let n = try scanner.nextFloat()
104
9.01k
                value.append(n)
105
9.01k
            }
106
63.8k
        } else {
107
63.5k
            let n = try scanner.nextFloat()
108
63.5k
            value.append(n)
109
63.8k
        }
110
63.8k
    }
111
707
    mutating func decodeSingularDoubleField(value: inout Double) throws {
112
707
        try scanner.skipRequiredColon()
113
707
        value = try scanner.nextDouble()
114
707
    }
115
41.5k
    mutating func decodeSingularDoubleField(value: inout Double?) throws {
116
41.5k
        try scanner.skipRequiredColon()
117
41.5k
        value = try scanner.nextDouble()
118
41.5k
    }
119
336k
    mutating func decodeRepeatedDoubleField(value: inout [Double]) throws {
120
336k
        try scanner.skipRequiredColon()
121
336k
        if scanner.skipOptionalBeginArray() {
122
127k
            var firstItem = true
123
584k
            while true {
124
584k
                if scanner.skipOptionalEndArray() {
125
127k
                    return
126
457k
                }
127
457k
                if firstItem {
128
126k
                    firstItem = false
129
457k
                } else {
130
330k
                    try scanner.skipRequiredComma()
131
457k
                }
132
457k
                let n = try scanner.nextDouble()
133
457k
                value.append(n)
134
457k
            }
135
208k
        } else {
136
208k
            let n = try scanner.nextDouble()
137
208k
            value.append(n)
138
208k
        }
139
208k
    }
140
62.4k
    mutating func decodeSingularInt32Field(value: inout Int32) throws {
141
62.4k
        try scanner.skipRequiredColon()
142
62.4k
        let n = try scanner.nextSInt()
143
77.6k
        if n > Int64(Int32.max) || n < Int64(Int32.min) {
144
61
            throw TextFormatDecodingError.malformedNumber
145
62.4k
        }
146
62.4k
        value = Int32(truncatingIfNeeded: n)
147
62.4k
    }
148
125k
    mutating func decodeSingularInt32Field(value: inout Int32?) throws {
149
125k
        try scanner.skipRequiredColon()
150
125k
        let n = try scanner.nextSInt()
151
206k
        if n > Int64(Int32.max) || n < Int64(Int32.min) {
152
81
            throw TextFormatDecodingError.malformedNumber
153
125k
        }
154
125k
        value = Int32(truncatingIfNeeded: n)
155
125k
    }
156
89.6k
    mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
157
89.6k
        try scanner.skipRequiredColon()
158
89.6k
        if scanner.skipOptionalBeginArray() {
159
28.2k
            var firstItem = true
160
105k
            while true {
161
105k
                if scanner.skipOptionalEndArray() {
162
27.8k
                    return
163
77.1k
                }
164
77.1k
                if firstItem {
165
28.1k
                    firstItem = false
166
77.1k
                } else {
167
48.9k
                    try scanner.skipRequiredComma()
168
77.1k
                }
169
77.1k
                let n = try scanner.nextSInt()
170
78.0k
                if n > Int64(Int32.max) || n < Int64(Int32.min) {
171
82
                    throw TextFormatDecodingError.malformedNumber
172
77.0k
                }
173
77.0k
                value.append(Int32(truncatingIfNeeded: n))
174
77.0k
            }
175
61.6k
        } else {
176
61.3k
            let n = try scanner.nextSInt()
177
78.3k
            if n > Int64(Int32.max) || n < Int64(Int32.min) {
178
74
                throw TextFormatDecodingError.malformedNumber
179
61.2k
            }
180
61.2k
            value.append(Int32(truncatingIfNeeded: n))
181
61.5k
        }
182
61.5k
    }
183
1.45k
    mutating func decodeSingularInt64Field(value: inout Int64) throws {
184
1.45k
        try scanner.skipRequiredColon()
185
1.45k
        value = try scanner.nextSInt()
186
1.45k
    }
187
197k
    mutating func decodeSingularInt64Field(value: inout Int64?) throws {
188
197k
        try scanner.skipRequiredColon()
189
197k
        value = try scanner.nextSInt()
190
197k
    }
191
165k
    mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws {
192
165k
        try scanner.skipRequiredColon()
193
165k
        if scanner.skipOptionalBeginArray() {
194
19.3k
            var firstItem = true
195
83.1k
            while true {
196
82.8k
                if scanner.skipOptionalEndArray() {
197
19.0k
                    return
198
63.7k
                }
199
63.7k
                if firstItem {
200
17.4k
                    firstItem = false
201
63.7k
                } else {
202
46.2k
                    try scanner.skipRequiredComma()
203
63.7k
                }
204
63.7k
                let n = try scanner.nextSInt()
205
63.7k
                value.append(n)
206
63.7k
            }
207
146k
        } else {
208
145k
            let n = try scanner.nextSInt()
209
145k
            value.append(n)
210
146k
        }
211
146k
    }
212
157
    mutating func decodeSingularUInt32Field(value: inout UInt32) throws {
213
157
        try scanner.skipRequiredColon()
214
157
        let n = try scanner.nextUInt()
215
157
        if n > UInt64(UInt32.max) {
216
1
            throw TextFormatDecodingError.malformedNumber
217
156
        }
218
156
        value = UInt32(truncatingIfNeeded: n)
219
156
    }
220
77.4k
    mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
221
77.4k
        try scanner.skipRequiredColon()
222
77.4k
        let n = try scanner.nextUInt()
223
77.4k
        if n > UInt64(UInt32.max) {
224
52
            throw TextFormatDecodingError.malformedNumber
225
77.4k
        }
226
77.4k
        value = UInt32(truncatingIfNeeded: n)
227
77.4k
    }
228
61.0k
    mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
229
61.0k
        try scanner.skipRequiredColon()
230
61.0k
        if scanner.skipOptionalBeginArray() {
231
36.1k
            var firstItem = true
232
212k
            while true {
233
212k
                if scanner.skipOptionalEndArray() {
234
35.9k
                    return
235
176k
                }
236
176k
                if firstItem {
237
36.1k
                    firstItem = false
238
176k
                } else {
239
140k
                    try scanner.skipRequiredComma()
240
176k
                }
241
176k
                let n = try scanner.nextUInt()
242
176k
                if n > UInt64(UInt32.max) {
243
3
                    throw TextFormatDecodingError.malformedNumber
244
176k
                }
245
176k
                value.append(UInt32(truncatingIfNeeded: n))
246
176k
            }
247
25.1k
        } else {
248
24.9k
            let n = try scanner.nextUInt()
249
24.9k
            if n > UInt64(UInt32.max) {
250
44
                throw TextFormatDecodingError.malformedNumber
251
24.9k
            }
252
24.9k
            value.append(UInt32(truncatingIfNeeded: n))
253
25.0k
        }
254
25.0k
    }
255
1.01k
    mutating func decodeSingularUInt64Field(value: inout UInt64) throws {
256
1.01k
        try scanner.skipRequiredColon()
257
1.01k
        value = try scanner.nextUInt()
258
1.01k
    }
259
172k
    mutating func decodeSingularUInt64Field(value: inout UInt64?) throws {
260
172k
        try scanner.skipRequiredColon()
261
172k
        value = try scanner.nextUInt()
262
172k
    }
263
85.9k
    mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws {
264
85.9k
        try scanner.skipRequiredColon()
265
85.9k
        if scanner.skipOptionalBeginArray() {
266
10.4k
            var firstItem = true
267
22.0k
            while true {
268
21.8k
                if scanner.skipOptionalEndArray() {
269
10.3k
                    return
270
11.5k
                }
271
11.5k
                if firstItem {
272
10.4k
                    firstItem = false
273
11.5k
                } else {
274
1.13k
                    try scanner.skipRequiredComma()
275
11.5k
                }
276
11.5k
                let n = try scanner.nextUInt()
277
11.5k
                value.append(n)
278
11.5k
            }
279
75.6k
        } else {
280
75.4k
            let n = try scanner.nextUInt()
281
75.4k
            value.append(n)
282
75.6k
        }
283
75.6k
    }
284
0
    mutating func decodeSingularSInt32Field(value: inout Int32) throws {
285
0
        try decodeSingularInt32Field(value: &value)
286
0
    }
287
34.1k
    mutating func decodeSingularSInt32Field(value: inout Int32?) throws {
288
34.1k
        try decodeSingularInt32Field(value: &value)
289
34.1k
    }
290
35.3k
    mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws {
291
35.3k
        try decodeRepeatedInt32Field(value: &value)
292
35.3k
    }
293
0
    mutating func decodeSingularSInt64Field(value: inout Int64) throws {
294
0
        try decodeSingularInt64Field(value: &value)
295
0
    }
296
146k
    mutating func decodeSingularSInt64Field(value: inout Int64?) throws {
297
146k
        try decodeSingularInt64Field(value: &value)
298
146k
    }
299
80.0k
    mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws {
300
80.0k
        try decodeRepeatedInt64Field(value: &value)
301
80.0k
    }
302
0
    mutating func decodeSingularFixed32Field(value: inout UInt32) throws {
303
0
        try decodeSingularUInt32Field(value: &value)
304
0
    }
305
43.8k
    mutating func decodeSingularFixed32Field(value: inout UInt32?) throws {
306
43.8k
        try decodeSingularUInt32Field(value: &value)
307
43.8k
    }
308
32.7k
    mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws {
309
32.7k
        try decodeRepeatedUInt32Field(value: &value)
310
32.7k
    }
311
0
    mutating func decodeSingularFixed64Field(value: inout UInt64) throws {
312
0
        try decodeSingularUInt64Field(value: &value)
313
0
    }
314
145k
    mutating func decodeSingularFixed64Field(value: inout UInt64?) throws {
315
145k
        try decodeSingularUInt64Field(value: &value)
316
145k
    }
317
26.9k
    mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws {
318
26.9k
        try decodeRepeatedUInt64Field(value: &value)
319
26.9k
    }
320
0
    mutating func decodeSingularSFixed32Field(value: inout Int32) throws {
321
0
        try decodeSingularInt32Field(value: &value)
322
0
    }
323
25.2k
    mutating func decodeSingularSFixed32Field(value: inout Int32?) throws {
324
25.2k
        try decodeSingularInt32Field(value: &value)
325
25.2k
    }
326
8.82k
    mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws {
327
8.82k
        try decodeRepeatedInt32Field(value: &value)
328
8.82k
    }
329
0
    mutating func decodeSingularSFixed64Field(value: inout Int64) throws {
330
0
        try decodeSingularInt64Field(value: &value)
331
0
    }
332
20.3k
    mutating func decodeSingularSFixed64Field(value: inout Int64?) throws {
333
20.3k
        try decodeSingularInt64Field(value: &value)
334
20.3k
    }
335
45.3k
    mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws {
336
45.3k
        try decodeRepeatedInt64Field(value: &value)
337
45.3k
    }
338
2.06k
    mutating func decodeSingularBoolField(value: inout Bool) throws {
339
2.06k
        try scanner.skipRequiredColon()
340
2.06k
        value = try scanner.nextBool()
341
2.06k
    }
342
18.0k
    mutating func decodeSingularBoolField(value: inout Bool?) throws {
343
18.0k
        try scanner.skipRequiredColon()
344
18.0k
        value = try scanner.nextBool()
345
18.0k
    }
346
159k
    mutating func decodeRepeatedBoolField(value: inout [Bool]) throws {
347
159k
        try scanner.skipRequiredColon()
348
159k
        if scanner.skipOptionalBeginArray() {
349
11.5k
            var firstItem = true
350
58.7k
            while true {
351
58.5k
                if scanner.skipOptionalEndArray() {
352
11.3k
                    return
353
47.2k
                }
354
47.2k
                if firstItem {
355
11.5k
                    firstItem = false
356
47.2k
                } else {
357
35.7k
                    try scanner.skipRequiredComma()
358
47.2k
                }
359
47.2k
                let n = try scanner.nextBool()
360
47.2k
                value.append(n)
361
47.2k
            }
362
148k
        } else {
363
147k
            let n = try scanner.nextBool()
364
147k
            value.append(n)
365
148k
        }
366
148k
    }
367
1.03M
    mutating func decodeSingularStringField(value: inout String) throws {
368
1.03M
        try scanner.skipRequiredColon()
369
1.03M
        value = try scanner.nextStringValue()
370
1.03M
    }
371
18.4k
    mutating func decodeSingularStringField(value: inout String?) throws {
372
18.4k
        try scanner.skipRequiredColon()
373
18.4k
        value = try scanner.nextStringValue()
374
18.4k
    }
375
41.3k
    mutating func decodeRepeatedStringField(value: inout [String]) throws {
376
41.3k
        try scanner.skipRequiredColon()
377
41.3k
        if scanner.skipOptionalBeginArray() {
378
835
            var firstItem = true
379
2.56k
            while true {
380
2.40k
                if scanner.skipOptionalEndArray() {
381
680
                    return
382
1.72k
                }
383
1.72k
                if firstItem {
384
798
                    firstItem = false
385
1.72k
                } else {
386
927
                    try scanner.skipRequiredComma()
387
1.72k
                }
388
1.72k
                let n = try scanner.nextStringValue()
389
1.72k
                value.append(n)
390
1.72k
            }
391
40.6k
        } else {
392
40.5k
            let n = try scanner.nextStringValue()
393
40.5k
            value.append(n)
394
40.6k
        }
395
40.6k
    }
396
654k
    mutating func decodeSingularBytesField(value: inout Data) throws {
397
654k
        try scanner.skipRequiredColon()
398
654k
        value = try scanner.nextBytesValue()
399
654k
    }
400
6.48k
    mutating func decodeSingularBytesField(value: inout Data?) throws {
401
6.48k
        try scanner.skipRequiredColon()
402
6.48k
        value = try scanner.nextBytesValue()
403
6.48k
    }
404
44.7k
    mutating func decodeRepeatedBytesField(value: inout [Data]) throws {
405
44.7k
        try scanner.skipRequiredColon()
406
44.7k
        if scanner.skipOptionalBeginArray() {
407
981
            var firstItem = true
408
2.85k
            while true {
409
2.79k
                if scanner.skipOptionalEndArray() {
410
923
                    return
411
1.87k
                }
412
1.87k
                if firstItem {
413
905
                    firstItem = false
414
1.87k
                } else {
415
970
                    try scanner.skipRequiredComma()
416
1.87k
                }
417
1.87k
                let n = try scanner.nextBytesValue()
418
1.87k
                value.append(n)
419
1.87k
            }
420
43.7k
        } else {
421
43.7k
            let n = try scanner.nextBytesValue()
422
43.7k
            value.append(n)
423
43.7k
        }
424
43.7k
    }
425
426
104k
    private mutating func decodeEnum<E: Enum>() throws -> E where E.RawValue == Int {
427
104k
        if let name = try scanner.nextOptionalEnumName() {
428
512
            if let b = E(rawUTF8: name) {
429
413
                return b
430
413
            } else {
431
99
                throw TextFormatDecodingError.unrecognizedEnumValue
432
99
            }
433
103k
        }
434
103k
        let number = try scanner.nextSInt()
435
103k
        if number >= Int64(Int32.min) && number <= Int64(Int32.max) {
436
103k
            let n = Int32(truncatingIfNeeded: number)
437
103k
            if let e = E(rawValue: Int(n)) {
438
103k
                return e
439
103k
            } else {
440
170
                throw TextFormatDecodingError.unrecognizedEnumValue
441
170
            }
442
284
        }
443
284
        throw TextFormatDecodingError.malformedText
444
103k
445
103k
    }
446
447
23.5k
    mutating func decodeSingularEnumField<E: Enum>(value: inout E?) throws where E.RawValue == Int {
448
23.5k
        try scanner.skipRequiredColon()
449
23.5k
        let e: E = try decodeEnum()
450
23.5k
        value = e
451
23.5k
    }
452
453
5.75k
    mutating func decodeSingularEnumField<E: Enum>(value: inout E) throws where E.RawValue == Int {
454
5.75k
        try scanner.skipRequiredColon()
455
5.75k
        let e: E = try decodeEnum()
456
5.75k
        value = e
457
5.75k
    }
458
459
31.9k
    mutating func decodeRepeatedEnumField<E: Enum>(value: inout [E]) throws where E.RawValue == Int {
460
31.9k
        try scanner.skipRequiredColon()
461
31.9k
        if scanner.skipOptionalBeginArray() {
462
1.67k
            var firstItem = true
463
11.3k
            while true {
464
11.2k
                if scanner.skipOptionalEndArray() {
465
1.51k
                    return
466
9.71k
                }
467
9.71k
                if firstItem {
468
1.25k
                    firstItem = false
469
9.71k
                } else {
470
8.45k
                    try scanner.skipRequiredComma()
471
9.71k
                }
472
9.71k
                let e: E = try decodeEnum()
473
9.71k
                value.append(e)
474
9.71k
            }
475
30.4k
        } else {
476
30.2k
            let e: E = try decodeEnum()
477
30.2k
            value.append(e)
478
30.4k
        }
479
30.4k
    }
480
481
693k
    mutating func decodeSingularMessageField<M: Message>(value: inout M?) throws {
482
693k
        _ = scanner.skipOptionalColon()
483
693k
        if value == nil {
484
348k
            value = M()
485
693k
        }
486
693k
        let terminator = try scanner.skipObjectStart()
487
693k
        var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
488
693k
        if M.self == Google_Protobuf_Any.self {
489
183k
            var any = value as! Google_Protobuf_Any?
490
183k
            try any!.decodeTextFormat(decoder: &subDecoder)
491
183k
            value = any as! M?
492
693k
        } else {
493
509k
            try value!.decodeMessage(decoder: &subDecoder)
494
693k
        }
495
693k
        assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
496
693k
        scanner = subDecoder.scanner
497
693k
    }
498
499
620k
    mutating func decodeRepeatedMessageField<M: Message>(value: inout [M]) throws {
500
620k
        _ = scanner.skipOptionalColon()
501
620k
        if scanner.skipOptionalBeginArray() {
502
4.35k
            var firstItem = true
503
5.34k
            while true {
504
4.73k
                if scanner.skipOptionalEndArray() {
505
3.75k
                    return
506
3.75k
                }
507
986
                if firstItem {
508
613
                    firstItem = false
509
986
                } else {
510
373
                    try scanner.skipRequiredComma()
511
986
                }
512
986
                let terminator = try scanner.skipObjectStart()
513
986
                var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
514
986
                if M.self == Google_Protobuf_Any.self {
515
0
                    var message = Google_Protobuf_Any()
516
0
                    try message.decodeTextFormat(decoder: &subDecoder)
517
0
                    value.append(message as! M)
518
986
                } else {
519
986
                    var message = M()
520
986
                    try message.decodeMessage(decoder: &subDecoder)
521
986
                    value.append(message)
522
986
                }
523
986
                assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
524
986
                scanner = subDecoder.scanner
525
986
            }
526
616k
        } else {
527
615k
            let terminator = try scanner.skipObjectStart()
528
615k
            var subDecoder = try TextFormatDecoder(messageType: M.self, scanner: scanner, terminator: terminator)
529
615k
            if M.self == Google_Protobuf_Any.self {
530
0
                var message = Google_Protobuf_Any()
531
0
                try message.decodeTextFormat(decoder: &subDecoder)
532
0
                value.append(message as! M)
533
615k
            } else {
534
615k
                var message = M()
535
615k
                try message.decodeMessage(decoder: &subDecoder)
536
615k
                value.append(message)
537
615k
            }
538
615k
            assert((scanner.recursionBudget + 1) == subDecoder.scanner.recursionBudget)
539
615k
            scanner = subDecoder.scanner
540
616k
        }
541
616k
    }
542
543
39.2k
    mutating func decodeSingularGroupField<G: Message>(value: inout G?) throws {
544
39.2k
        try decodeSingularMessageField(value: &value)
545
39.2k
    }
546
547
26.4k
    mutating func decodeRepeatedGroupField<G: Message>(value: inout [G]) throws {
548
26.4k
        try decodeRepeatedMessageField(value: &value)
549
26.4k
    }
550
551
    private mutating func decodeMapEntry<KeyType, ValueType: MapValueType>(
552
        mapType: _ProtobufMap<KeyType, ValueType>.Type,
553
        value: inout _ProtobufMap<KeyType, ValueType>.BaseType
554
338k
    ) throws {
555
338k
        var keyField: KeyType.BaseType?
556
338k
        var valueField: ValueType.BaseType?
557
338k
        let terminator = try scanner.skipObjectStart()
558
338k
        let ignoreExtensionFields = options.ignoreUnknownExtensionFields
559
1.03M
        while true {
560
1.03M
            if scanner.skipOptionalObjectEnd(terminator) {
561
336k
                if let keyField = keyField, let valueField = valueField {
562
336k
                    value[keyField] = valueField
563
336k
                    return
564
336k
                } else {
565
43
                    throw TextFormatDecodingError.malformedText
566
43
                }
567
696k
            }
568
696k
            if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
569
695k
                switch key {
570
695k
                case "key", "1":
571
346k
                    try KeyType.decodeSingular(value: &keyField, from: &self)
572
695k
                case "value", "2":
573
340k
                    try ValueType.decodeSingular(value: &valueField, from: &self)
574
695k
                default:
575
8.41k
                    if ignoreExtensionFields && key.hasPrefix("[") {
576
1.35k
                        try scanner.skipUnknownFieldValue()
577
8.41k
                    } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
578
6.78k
                        try scanner.skipUnknownFieldValue()
579
8.41k
                    } else {
580
1.62k
                        throw TextFormatDecodingError.unknownField
581
695k
                    }
582
695k
                }
583
694k
                scanner.skipOptionalSeparator()
584
694k
            } else {
585
359
                throw TextFormatDecodingError.malformedText
586
694k
            }
587
694k
        }
588
578
    }
589
590
    mutating func decodeMapField<KeyType, ValueType: MapValueType>(
591
        fieldType: _ProtobufMap<KeyType, ValueType>.Type,
592
        value: inout _ProtobufMap<KeyType, ValueType>.BaseType
593
358k
    ) throws {
594
358k
        _ = scanner.skipOptionalColon()
595
358k
        if scanner.skipOptionalBeginArray() {
596
139k
            var firstItem = true
597
149k
            while true {
598
149k
                if scanner.skipOptionalEndArray() {
599
139k
                    return
600
139k
                }
601
9.39k
                if firstItem {
602
3.12k
                    firstItem = false
603
9.39k
                } else {
604
6.27k
                    try scanner.skipRequiredComma()
605
9.39k
                }
606
9.39k
                try decodeMapEntry(mapType: fieldType, value: &value)
607
9.39k
            }
608
219k
        } else {
609
218k
            try decodeMapEntry(mapType: fieldType, value: &value)
610
219k
        }
611
219k
    }
612
613
    private mutating func decodeMapEntry<KeyType, ValueType>(
614
        mapType: _ProtobufEnumMap<KeyType, ValueType>.Type,
615
        value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
616
21.3k
    ) throws where ValueType.RawValue == Int {
617
21.3k
        var keyField: KeyType.BaseType?
618
21.3k
        var valueField: ValueType?
619
21.3k
        let terminator = try scanner.skipObjectStart()
620
21.3k
        let ignoreExtensionFields = options.ignoreUnknownExtensionFields
621
70.5k
        while true {
622
70.5k
            if scanner.skipOptionalObjectEnd(terminator) {
623
20.4k
                if let keyField = keyField, let valueField = valueField {
624
20.4k
                    value[keyField] = valueField
625
20.4k
                    return
626
20.4k
                } else {
627
20
                    throw TextFormatDecodingError.malformedText
628
20
                }
629
50.0k
            }
630
50.0k
            if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
631
49.8k
                switch key {
632
49.8k
                case "key", "1":
633
23.0k
                    try KeyType.decodeSingular(value: &keyField, from: &self)
634
49.8k
                case "value", "2":
635
21.1k
                    try decodeSingularEnumField(value: &valueField)
636
49.8k
                default:
637
5.61k
                    if ignoreExtensionFields && key.hasPrefix("[") {
638
1.02k
                        try scanner.skipUnknownFieldValue()
639
5.61k
                    } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
640
4.40k
                        try scanner.skipUnknownFieldValue()
641
5.61k
                    } else {
642
1.20k
                        throw TextFormatDecodingError.unknownField
643
49.8k
                    }
644
49.8k
                }
645
48.6k
                scanner.skipOptionalSeparator()
646
48.8k
            } else {
647
237
                throw TextFormatDecodingError.malformedText
648
48.6k
            }
649
18.4E
        }
650
18.4E
    }
651
652
    mutating func decodeMapField<KeyType, ValueType>(
653
        fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
654
        value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
655
33.6k
    ) throws where ValueType.RawValue == Int {
656
33.6k
        _ = scanner.skipOptionalColon()
657
33.6k
        if scanner.skipOptionalBeginArray() {
658
18.5k
            var firstItem = true
659
21.4k
            while true {
660
21.3k
                if scanner.skipOptionalEndArray() {
661
18.5k
                    return
662
18.5k
                }
663
2.81k
                if firstItem {
664
946
                    firstItem = false
665
2.81k
                } else {
666
1.87k
                    try scanner.skipRequiredComma()
667
2.81k
                }
668
2.81k
                try decodeMapEntry(mapType: fieldType, value: &value)
669
2.81k
            }
670
15.1k
        } else {
671
15.0k
            try decodeMapEntry(mapType: fieldType, value: &value)
672
15.1k
        }
673
15.1k
    }
674
675
    private mutating func decodeMapEntry<KeyType, ValueType>(
676
        mapType: _ProtobufMessageMap<KeyType, ValueType>.Type,
677
        value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
678
29.1k
    ) throws {
679
29.1k
        var keyField: KeyType.BaseType?
680
29.1k
        var valueField: ValueType?
681
29.1k
        let terminator = try scanner.skipObjectStart()
682
29.1k
        let ignoreExtensionFields = options.ignoreUnknownExtensionFields
683
93.0k
        while true {
684
91.8k
            if scanner.skipOptionalObjectEnd(terminator) {
685
26.6k
                if let keyField = keyField, let valueField = valueField {
686
26.6k
                    value[keyField] = valueField
687
26.6k
                    return
688
26.6k
                } else {
689
30
                    throw TextFormatDecodingError.malformedText
690
30
                }
691
65.1k
            }
692
65.1k
            if let key = try scanner.nextKey(allowExtensions: ignoreExtensionFields) {
693
64.9k
                switch key {
694
64.9k
                case "key", "1":
695
27.2k
                    try KeyType.decodeSingular(value: &keyField, from: &self)
696
64.9k
                case "value", "2":
697
28.8k
                    try decodeSingularMessageField(value: &valueField)
698
64.9k
                default:
699
8.76k
                    if ignoreExtensionFields && key.hasPrefix("[") {
700
766
                        try scanner.skipUnknownFieldValue()
701
8.76k
                    } else if options.ignoreUnknownFields && !key.hasPrefix("[") {
702
7.79k
                        try scanner.skipUnknownFieldValue()
703
8.76k
                    } else {
704
965
                        throw TextFormatDecodingError.unknownField
705
64.9k
                    }
706
64.9k
                }
707
63.9k
                scanner.skipOptionalSeparator()
708
64.2k
            } else {
709
237
                throw TextFormatDecodingError.malformedText
710
63.9k
            }
711
63.9k
        }
712
1.26k
    }
713
714
    mutating func decodeMapField<KeyType, ValueType>(
715
        fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
716
        value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
717
15.6k
    ) throws {
718
15.6k
        _ = scanner.skipOptionalColon()
719
15.6k
        if scanner.skipOptionalBeginArray() {
720
7.48k
            var firstItem = true
721
7.85k
            while true {
722
7.80k
                if scanner.skipOptionalEndArray() {
723
7.43k
                    return
724
7.43k
                }
725
372
                if firstItem {
726
350
                    firstItem = false
727
372
                } else {
728
22
                    try scanner.skipRequiredComma()
729
372
                }
730
372
                try decodeMapEntry(mapType: fieldType, value: &value)
731
372
            }
732
8.24k
        } else {
733
8.18k
            try decodeMapEntry(mapType: fieldType, value: &value)
734
8.24k
        }
735
8.24k
    }
736
737
    mutating func decodeExtensionField(
738
        values: inout ExtensionFieldValueSet,
739
        messageType: any Message.Type,
740
        fieldNumber: Int
741
399k
    ) throws {
742
399k
        if let ext = scanner.extensions?[messageType, fieldNumber] {
743
440k
            try values.modify(index: fieldNumber) { fieldValue in
744
440k
                if fieldValue != nil {
745
168k
                    try fieldValue!.decodeExtensionField(decoder: &self)
746
440k
                } else {
747
271k
                    fieldValue = try ext._protobuf_newField(decoder: &self)
748
440k
                }
749
440k
                if fieldValue == nil {
750
0
                    // Really things should never get here, for TextFormat, decoding
751
0
                    // the value should always work or throw an error.  This specific
752
0
                    // error result is to allow this to be more detectable.
753
0
                    throw TextFormatDecodingError.internalExtensionError
754
440k
                }
755
440k
            }
756
399k
        }
757
399k
    }
758
}