19 |
19 |
20 // Number represents the field number. |
20 // Number represents the field number. |
21 type Number int32 |
21 type Number int32 |
22 |
22 |
23 const ( |
23 const ( |
24 MinValidNumber Number = 1 |
24 MinValidNumber Number = 1 |
25 FirstReservedNumber Number = 19000 |
25 FirstReservedNumber Number = 19000 |
26 LastReservedNumber Number = 19999 |
26 LastReservedNumber Number = 19999 |
27 MaxValidNumber Number = 1<<29 - 1 |
27 MaxValidNumber Number = 1<<29 - 1 |
|
28 DefaultRecursionLimit = 10000 |
28 ) |
29 ) |
29 |
30 |
30 // IsValid reports whether the field number is semantically valid. |
31 // IsValid reports whether the field number is semantically valid. |
31 // |
32 // |
32 // Note that while numbers within the reserved range are semantically invalid, |
33 // Note that while numbers within the reserved range are semantically invalid, |
110 // This returns a negative length upon an error (see ParseError). |
112 // This returns a negative length upon an error (see ParseError). |
111 // |
113 // |
112 // When parsing a group, the length includes the end group marker and |
114 // When parsing a group, the length includes the end group marker and |
113 // the end group is verified to match the starting field number. |
115 // the end group is verified to match the starting field number. |
114 func ConsumeFieldValue(num Number, typ Type, b []byte) (n int) { |
116 func ConsumeFieldValue(num Number, typ Type, b []byte) (n int) { |
|
117 return consumeFieldValueD(num, typ, b, DefaultRecursionLimit) |
|
118 } |
|
119 |
|
120 func consumeFieldValueD(num Number, typ Type, b []byte, depth int) (n int) { |
115 switch typ { |
121 switch typ { |
116 case VarintType: |
122 case VarintType: |
117 _, n = ConsumeVarint(b) |
123 _, n = ConsumeVarint(b) |
118 return n |
124 return n |
119 case Fixed32Type: |
125 case Fixed32Type: |
505 func EncodeTag(num Number, typ Type) uint64 { |
514 func EncodeTag(num Number, typ Type) uint64 { |
506 return uint64(num)<<3 | uint64(typ&7) |
515 return uint64(num)<<3 | uint64(typ&7) |
507 } |
516 } |
508 |
517 |
509 // DecodeZigZag decodes a zig-zag-encoded uint64 as an int64. |
518 // DecodeZigZag decodes a zig-zag-encoded uint64 as an int64. |
|
519 // |
510 // Input: {…, 5, 3, 1, 0, 2, 4, 6, …} |
520 // Input: {…, 5, 3, 1, 0, 2, 4, 6, …} |
511 // Output: {…, -3, -2, -1, 0, +1, +2, +3, …} |
521 // Output: {…, -3, -2, -1, 0, +1, +2, +3, …} |
512 func DecodeZigZag(x uint64) int64 { |
522 func DecodeZigZag(x uint64) int64 { |
513 return int64(x>>1) ^ int64(x)<<63>>63 |
523 return int64(x>>1) ^ int64(x)<<63>>63 |
514 } |
524 } |
515 |
525 |
516 // EncodeZigZag encodes an int64 as a zig-zag-encoded uint64. |
526 // EncodeZigZag encodes an int64 as a zig-zag-encoded uint64. |
|
527 // |
517 // Input: {…, -3, -2, -1, 0, +1, +2, +3, …} |
528 // Input: {…, -3, -2, -1, 0, +1, +2, +3, …} |
518 // Output: {…, 5, 3, 1, 0, 2, 4, 6, …} |
529 // Output: {…, 5, 3, 1, 0, 2, 4, 6, …} |
519 func EncodeZigZag(x int64) uint64 { |
530 func EncodeZigZag(x int64) uint64 { |
520 return uint64(x<<1) ^ uint64(x>>63) |
531 return uint64(x<<1) ^ uint64(x>>63) |
521 } |
532 } |
522 |
533 |
523 // DecodeBool decodes a uint64 as a bool. |
534 // DecodeBool decodes a uint64 as a bool. |
|
535 // |
524 // Input: { 0, 1, 2, …} |
536 // Input: { 0, 1, 2, …} |
525 // Output: {false, true, true, …} |
537 // Output: {false, true, true, …} |
526 func DecodeBool(x uint64) bool { |
538 func DecodeBool(x uint64) bool { |
527 return x != 0 |
539 return x != 0 |
528 } |
540 } |
529 |
541 |
530 // EncodeBool encodes a bool as a uint64. |
542 // EncodeBool encodes a bool as a uint64. |
|
543 // |
531 // Input: {false, true} |
544 // Input: {false, true} |
532 // Output: { 0, 1} |
545 // Output: { 0, 1} |
533 func EncodeBool(x bool) uint64 { |
546 func EncodeBool(x bool) uint64 { |
534 if x { |
547 if x { |
535 return 1 |
548 return 1 |