diff options
| author | 2025-12-23 10:22:06 -0500 | |
|---|---|---|
| committer | 2025-12-23 10:23:17 -0500 | |
| commit | 041afd58de8525dc0b6f6a9e2b493031dbf4bbee (patch) | |
| tree | 1e46fb22f97c569c72244d0236d87abd365b5eb0 /src | |
| parent | Bump version refs (diff) | |
| download | zg-041afd58de8525dc0b6f6a9e2b493031dbf4bbee.tar.gz zg-041afd58de8525dc0b6f6a9e2b493031dbf4bbee.tar.xz zg-041afd58de8525dc0b6f6a9e2b493031dbf4bbee.zip | |
Fix #74: Check for characters before popping in wrapv0.15.3
Diffstat (limited to 'src')
| -rw-r--r-- | src/DisplayWidth.zig | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/DisplayWidth.zig b/src/DisplayWidth.zig index 82b2649..d15dbae 100644 --- a/src/DisplayWidth.zig +++ b/src/DisplayWidth.zig | |||
| @@ -420,8 +420,10 @@ pub fn wrap( | |||
| 420 | } | 420 | } |
| 421 | 421 | ||
| 422 | // Remove trailing space and newline. | 422 | // Remove trailing space and newline. |
| 423 | _ = result.pop(); | 423 | if (result.items[result.items.len - 1] == '\n') |
| 424 | _ = result.pop(); | 424 | _ = result.pop(); |
| 425 | if (result.items[result.items.len - 1] == ' ') | ||
| 426 | _ = result.pop(); | ||
| 425 | 427 | ||
| 426 | return try result.toOwnedSlice(); | 428 | return try result.toOwnedSlice(); |
| 427 | } | 429 | } |
| @@ -438,6 +440,18 @@ test "wrap" { | |||
| 438 | try testing.expectEqualStrings(want, got); | 440 | try testing.expectEqualStrings(want, got); |
| 439 | } | 441 | } |
| 440 | 442 | ||
| 443 | test "zg/74" { | ||
| 444 | var debug_alloc = std.heap.DebugAllocator(.{}).init; | ||
| 445 | const allocator = debug_alloc.allocator(); | ||
| 446 | defer _ = debug_alloc.deinit(); | ||
| 447 | const dw = try DisplayWidth.init(allocator); | ||
| 448 | defer dw.deinit(allocator); | ||
| 449 | const wrapped = try dw.wrap(allocator, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pellentesque pulvinar felis, sit amet commodo ligula feugiat sed. Sed quis malesuada elit, nec eleifend lectus. Sed tincidunt finibus aliquet. Praesent consectetur nibh libero, tempus imperdiet lorem congue eget.", 16, 1); | ||
| 450 | defer allocator.free(wrapped); | ||
| 451 | const expected_wrap = "Lorem ipsum dolor \nsit amet, consectetur \nadipiscing elit. \nNullam pellentesque \npulvinar felis, \nsit amet commodo \nligula feugiat \nsed. Sed quis malesuada \nelit, nec eleifend \nlectus. Sed tincidunt \nfinibus aliquet. \nPraesent consectetur \nnibh libero, tempus \nimperdiet lorem \ncongue eget."; | ||
| 452 | try std.testing.expectEqualStrings(expected_wrap, wrapped); | ||
| 453 | } | ||
| 454 | |||
| 441 | fn testAllocation(allocator: Allocator) !void { | 455 | fn testAllocation(allocator: Allocator) !void { |
| 442 | { | 456 | { |
| 443 | var dw = try DisplayWidth.init(allocator); | 457 | var dw = try DisplayWidth.init(allocator); |