Skip to content

Commit

Permalink
Added a new public API bytesUntilLimit to GPBCodedInputStream.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
protobuf-github-bot authored and copybara-github committed May 20, 2024
1 parent 47f4bc9 commit 01b0b8e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions objectivec/GPBCodedInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ __attribute__((objc_subclassing_restricted))
*/
- (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
* is used to verify that a nested group ended with the correct end tag.
Expand Down
4 changes: 4 additions & 0 deletions objectivec/GPBCodedInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ - (void)popLimit:(size_t)oldLimit {
GPBCodedInputStreamPopLimit(&state_, oldLimit);
}

- (size_t)bytesUntilLimit {
return GPBCodedInputStreamBytesUntilLimit(&state_);
}

- (double)readDouble {
return GPBCodedInputStreamReadDouble(&state_);
}
Expand Down
14 changes: 14 additions & 0 deletions objectivec/Tests/GPBCodedInputStreamTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@ - (void)testSkipWholeMessage {
}
}

- (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 {
// Allocate and initialize a 1MB blob.
NSMutableData* blob = [NSMutableData dataWithLength:1 << 20];
Expand Down

0 comments on commit 01b0b8e

Please sign in to comment.