Added a new public API `bytesUntilLimit` to GPBCodedInputStream.

This simply exposes preexisting logic. This read-only API is for convenience and doesn't reveal any information that was not already available. `[GPBCodedInputStream position]` is already a public API. The `limit` is known to the user because it's specified by them.

PiperOrigin-RevId: 635502694
pull/16883/head
Protobuf Team Bot 6 months ago committed by Copybara-Service
parent 47f4bc958b
commit 01b0b8e06e
  1. 5
      objectivec/GPBCodedInputStream.h
  2. 4
      objectivec/GPBCodedInputStream.m
  3. 14
      objectivec/Tests/GPBCodedInputStreamTests.m

@ -217,6 +217,11 @@ __attribute__((objc_subclassing_restricted))
*/ */
- (void)popLimit:(size_t)oldLimit; - (void)popLimit:(size_t)oldLimit;
/**
* @return The number of bytes from the current position to the current limit.
*/
- (size_t)bytesUntilLimit;
/** /**
* Verifies that the last call to -readTag returned the given tag value. This * Verifies that the last call to -readTag returned the given tag value. This
* is used to verify that a nested group ended with the correct end tag. * is used to verify that a nested group ended with the correct end tag.

@ -384,6 +384,10 @@ void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state, int32_t
GPBCodedInputStreamPopLimit(&state_, oldLimit); GPBCodedInputStreamPopLimit(&state_, oldLimit);
} }
- (size_t)bytesUntilLimit {
return GPBCodedInputStreamBytesUntilLimit(&state_);
}
- (double)readDouble { - (double)readDouble {
return GPBCodedInputStreamReadDouble(&state_); return GPBCodedInputStreamReadDouble(&state_);
} }

@ -283,6 +283,20 @@
} }
} }
- (void)testLimit {
TestAllTypes* message = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
NSData* rawBytes = message.data;
GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:rawBytes];
XCTAssertEqual([input bytesUntilLimit], rawBytes.length);
[input pushLimit:8];
XCTAssertEqual([input bytesUntilLimit], 8u);
[input popLimit:3];
XCTAssertEqual([input bytesUntilLimit], 3u);
[input readTag];
XCTAssertEqual([input position], 1u);
XCTAssertEqual([input bytesUntilLimit], 2u);
}
- (void)testReadHugeBlob { - (void)testReadHugeBlob {
// Allocate and initialize a 1MB blob. // Allocate and initialize a 1MB blob.
NSMutableData* blob = [NSMutableData dataWithLength:1 << 20]; NSMutableData* blob = [NSMutableData dataWithLength:1 << 20];

Loading…
Cancel
Save