Avoid recursive call to `Insert` in the flat case.
[google-protobuf.git] / objectivec / GPBWireFormat.m
blob021ea756f32c037c812837f12b0d9522c200ff19
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
8 #import "GPBWireFormat.h"
10 #import "GPBUtilities.h"
11 #import "GPBUtilities_PackagePrivate.h"
13 enum {
14   GPBWireFormatTagTypeBits = 3,
15   GPBWireFormatTagTypeMask = 7 /* = (1 << GPBWireFormatTagTypeBits) - 1 */,
18 uint32_t GPBWireFormatMakeTag(uint32_t fieldNumber, GPBWireFormat wireType) {
19   return (fieldNumber << GPBWireFormatTagTypeBits) | wireType;
22 GPBWireFormat GPBWireFormatGetTagWireType(uint32_t tag) {
23   return (GPBWireFormat)(tag & GPBWireFormatTagTypeMask);
26 uint32_t GPBWireFormatGetTagFieldNumber(uint32_t tag) {
27   return GPBLogicalRightShift32(tag, GPBWireFormatTagTypeBits);
30 BOOL GPBWireFormatIsValidTag(uint32_t tag) {
31   uint32_t formatBits = (tag & GPBWireFormatTagTypeMask);
32   // The valid GPBWireFormat* values are 0-5, anything else is not a valid tag.
33   BOOL result = (formatBits <= 5);
34   return result;
37 GPBWireFormat GPBWireFormatForType(GPBDataType type, BOOL isPacked) {
38   if (isPacked) {
39     return GPBWireFormatLengthDelimited;
40   }
42   static const GPBWireFormat format[GPBDataType_Count] = {
43       GPBWireFormatVarint,           // GPBDataTypeBool
44       GPBWireFormatFixed32,          // GPBDataTypeFixed32
45       GPBWireFormatFixed32,          // GPBDataTypeSFixed32
46       GPBWireFormatFixed32,          // GPBDataTypeFloat
47       GPBWireFormatFixed64,          // GPBDataTypeFixed64
48       GPBWireFormatFixed64,          // GPBDataTypeSFixed64
49       GPBWireFormatFixed64,          // GPBDataTypeDouble
50       GPBWireFormatVarint,           // GPBDataTypeInt32
51       GPBWireFormatVarint,           // GPBDataTypeInt64
52       GPBWireFormatVarint,           // GPBDataTypeSInt32
53       GPBWireFormatVarint,           // GPBDataTypeSInt64
54       GPBWireFormatVarint,           // GPBDataTypeUInt32
55       GPBWireFormatVarint,           // GPBDataTypeUInt64
56       GPBWireFormatLengthDelimited,  // GPBDataTypeBytes
57       GPBWireFormatLengthDelimited,  // GPBDataTypeString
58       GPBWireFormatLengthDelimited,  // GPBDataTypeMessage
59       GPBWireFormatStartGroup,       // GPBDataTypeGroup
60       GPBWireFormatVarint            // GPBDataTypeEnum
61   };
62   return format[type];