55// license that can be found in the LICENSE file or at
66// https://developers.google.com/open-source/licenses/bsd
77
8- #import " GPBUtilities_PackagePrivate .h"
8+ #import " GPBUtilities .h"
99
1010#import < objc/runtime.h>
1111
1515#import " GPBMessage_PackagePrivate.h"
1616#import " GPBUnknownField.h"
1717#import " GPBUnknownFieldSet.h"
18+ #import " GPBUnknownField_PackagePrivate.h"
19+ #import " GPBUnknownFields.h"
20+ #import " GPBUtilities_PackagePrivate.h"
1821
1922// Direct access is use for speed, to avoid even internally declaring things
2023// read/write, etc. The warning is enabled in the project to ensure code calling
@@ -1929,6 +1932,78 @@ static void AppendTextFormatForMessageExtensionRange(GPBMessage *message, NSArra
19291932 } // for..in(activeExtensions)
19301933}
19311934
1935+ static void AppendTextFormatForUnknownFields (GPBUnknownFields *ufs, NSMutableString *toStr,
1936+ NSString *lineIndent) {
1937+ #if defined(DEBUG) && DEBUG
1938+ NSCAssert (!ufs.empty, @" Internal Error: No unknown fields to format." );
1939+ #endif
1940+ // Extract the fields and sort them by field number. Use a stable sort and sort just by the field
1941+ // numbers, that way the output will still show the order the different types were added as well
1942+ // as maintaining the order within a give number/type pair (i.e. - repeated fields say in order).
1943+ NSMutableArray *sortedFields = [[NSMutableArray alloc ] initWithCapacity: ufs.count];
1944+ for (GPBUnknownField *field in ufs) {
1945+ [sortedFields addObject: field];
1946+ }
1947+ [sortedFields
1948+ sortWithOptions: NSSortStable
1949+ usingComparator: ^NSComparisonResult (GPBUnknownField *field1, GPBUnknownField *field2) {
1950+ int32_t fieldNumber1 = field1->number_ ;
1951+ int32_t fieldNumber2 = field2->number_ ;
1952+ if (fieldNumber1 < fieldNumber2) {
1953+ return NSOrderedAscending;
1954+ } else if (fieldNumber1 > fieldNumber2) {
1955+ return NSOrderedDescending;
1956+ } else {
1957+ return NSOrderedSame;
1958+ }
1959+ }];
1960+
1961+ NSString *subIndent = nil ;
1962+
1963+ for (GPBUnknownField *field in sortedFields) {
1964+ int32_t fieldNumber = field->number_ ;
1965+ switch (field->type_ ) {
1966+ case GPBUnknownFieldTypeVarint:
1967+ [toStr appendFormat: @" %@ %d : %llu \n " , lineIndent, fieldNumber, field->storage_.intValue];
1968+ break ;
1969+ case GPBUnknownFieldTypeFixed32:
1970+ [toStr appendFormat: @" %@ %d : 0x%X \n " , lineIndent, fieldNumber,
1971+ (uint32_t )field->storage_.intValue];
1972+ break ;
1973+ case GPBUnknownFieldTypeFixed64:
1974+ [toStr appendFormat: @" %@ %d : 0x%llX \n " , lineIndent, fieldNumber, field->storage_.intValue];
1975+ break ;
1976+ case GPBUnknownFieldTypeLengthDelimited:
1977+ [toStr appendFormat: @" %@ %d : " , lineIndent, fieldNumber];
1978+ AppendBufferAsString (field->storage_ .lengthDelimited , toStr);
1979+ [toStr appendString: @" \n " ];
1980+ break ;
1981+ case GPBUnknownFieldTypeGroup: {
1982+ GPBUnknownFields *group = field->storage_ .group ;
1983+ if (group.empty ) {
1984+ [toStr appendFormat: @" %@ %d : {}\n " , lineIndent, fieldNumber];
1985+ } else {
1986+ [toStr appendFormat: @" %@ %d : {\n " , lineIndent, fieldNumber];
1987+ if (subIndent == nil ) {
1988+ subIndent = [lineIndent stringByAppendingString: @" " ];
1989+ }
1990+ AppendTextFormatForUnknownFields (group, toStr, subIndent);
1991+ [toStr appendFormat: @" %@ }\n " , lineIndent];
1992+ }
1993+ } break ;
1994+ case GPBUnknownFieldTypeLegacy:
1995+ #if defined(DEBUG) && DEBUG
1996+ NSCAssert (
1997+ NO ,
1998+ @" Internal error: Shouldn't have gotten a legacy field type in the unknown fields." );
1999+ #endif
2000+ break ;
2001+ }
2002+ }
2003+ [subIndent release ];
2004+ [sortedFields release ];
2005+ }
2006+
19322007static void AppendTextFormatForMessage (GPBMessage *message, NSMutableString *toStr,
19332008 NSString *lineIndent) {
19342009 GPBDescriptor *descriptor = [message descriptor ];
@@ -1951,11 +2026,12 @@ static void AppendTextFormatForMessage(GPBMessage *message, NSMutableString *toS
19512026 }
19522027 }
19532028
1954- NSString *unknownFieldsStr = GPBTextFormatForUnknownFieldSet (message. unknownFields , lineIndent) ;
1955- if ([unknownFieldsStr length ] > 0 ) {
2029+ GPBUnknownFields *ufs = [[GPBUnknownFields alloc ] initFromMessage: message] ;
2030+ if (ufs. count > 0 ) {
19562031 [toStr appendFormat: @" %@ # --- Unknown fields ---\n " , lineIndent];
1957- [ toStr appendString: unknownFieldsStr] ;
2032+ AppendTextFormatForUnknownFields (ufs, toStr, lineIndent) ;
19582033 }
2034+ [ufs release ];
19592035}
19602036
19612037NSString *GPBTextFormatForMessage (GPBMessage *message, NSString *lineIndent) {
0 commit comments