Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The release run heads these entries with the version and opens a fresh

## Unreleased

- Apple `Measure(string:)` is now failable. An invalid length returns `nil`
instead of letting a C++ exception escape into Swift.

- Python, Java and Apple file/image/font reads report stream failures instead
of returning partial data after a ZIP checksum or other read error.

Expand Down
6 changes: 3 additions & 3 deletions apple/include/OdrCoreObjC/ODRStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ NS_SWIFT_NAME(Measure)
/// Magnitude and unit as odrcore writes them, e.g. `12pt`.
@property(nonatomic, readonly, copy) NSString *stringValue;

/// A css length as odrcore writes one, e.g. `3mm`. A magnitude with no unit
/// reads as unitless.
- (instancetype)initWithString:(NSString *)string;
/// Parses a length such as `3mm` or a unitless magnitude; returns nil on
/// failure.
- (nullable instancetype)initWithString:(NSString *)string;
- (instancetype)initWithMagnitude:(double)magnitude unit:(NSString *)unit;

- (instancetype)init NS_UNAVAILABLE;
Expand Down
10 changes: 7 additions & 3 deletions apple/src/ODRStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ - (NSString *)stringValue {
return guarded_value([&] { return to_nsstring(_handle->to_string()); }, @"");
}

- (instancetype)initWithString:(NSString *)string {
- (nullable instancetype)initWithString:(NSString *)string {
if ((self = [super init]) == nil) {
return nil;
}
_handle = odr::Measure(to_string(string));
return self;
return guarded_value(
[&]() -> ODRMeasure * {
_handle = odr::Measure(to_string(string));
return self;
},
nil);
}

- (instancetype)initWithMagnitude:(double)magnitude unit:(NSString *)unit {
Expand Down
13 changes: 13 additions & 0 deletions apple/tests/OdrCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,19 @@ final class PdfAnnotationTests: XCTestCase {
}
}

final class MeasureTests: XCTestCase {
func testConstructionReturnsNilOnFailure() throws {
let parsed = try XCTUnwrap(Measure(string: "2.5cm"))
XCTAssertEqual(parsed.magnitude, 2.5)
XCTAssertEqual(parsed.unit, "cm")
XCTAssertEqual(Measure(magnitude: 2.5, unit: "cm").stringValue, "2.5cm")
XCTAssertEqual(Measure(string: "12")?.unit, "")
for invalid in ["", "invalid", "1e999px"] {
XCTAssertNil(Measure(string: invalid), invalid)
}
}
}

final class TableAddressTests: XCTestCase {
func testRoundTrips() throws {
XCTAssertEqual(TableAddress.columnNumber(from: "C"), 2)
Expand Down
Loading