summaryrefslogtreecommitdiff
path: root/src/lab.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/lab.zig')
-rw-r--r--src/lab.zig30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/lab.zig b/src/lab.zig
index 3750d80..84ab9d1 100644
--- a/src/lab.zig
+++ b/src/lab.zig
@@ -2,6 +2,7 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const mem = std.mem;
+const doc = @import("doc.zig");
const Json = std.json.Value;
pub const Pass = enum {
@@ -159,9 +160,11 @@ const Sentences = struct {
};
/// Pair with pass.prompt(). The same UTF-8 snapshot must be used for parse().
-/// Input is plain text with paragraphs on separate lines and Markdown # headings.
-pub fn question(arena: Allocator, pass: Pass, body: []const u8) ![]const u8 {
- if (!std.unicode.utf8ValidateSlice(body)) return error.InvalidUtf8;
+/// Input is plain text with paragraphs on separate lines and Markdown # headings. Its
+/// comments are nobody else's to read.
+pub fn question(arena: Allocator, pass: Pass, draft: []const u8) ![]const u8 {
+ if (!std.unicode.utf8ValidateSlice(draft)) return error.InvalidUtf8;
+ const body = try doc.withoutComments(arena, draft);
var buf: std.ArrayList(u8) = .empty;
var blocks: Blocks = .{ .lines = .{ .text = body } };
var n: usize = 0;
@@ -183,12 +186,14 @@ pub fn question(arena: Allocator, pass: Pass, body: []const u8) ![]const u8 {
return buf.toOwnedSlice(arena);
}
-/// `text` borrows body. The array and replacements live in arena, independent of answer.
-/// Invalid JSON is an error; invalid individual entries are ignored.
-pub fn parse(arena: Allocator, pass: Pass, body: []const u8, answer: []const u8) ![]Finding {
- if (!std.unicode.utf8ValidateSlice(body) or !std.unicode.utf8ValidateSlice(answer)) return error.InvalidUtf8;
+/// Everything returned lives in arena, independent of answer. A finding can run over a
+/// comment, which is blank in its text. Invalid JSON is an error; invalid individual entries
+/// are ignored.
+pub fn parse(arena: Allocator, pass: Pass, draft: []const u8, answer: []const u8) ![]Finding {
+ if (!std.unicode.utf8ValidateSlice(draft) or !std.unicode.utf8ValidateSlice(answer)) return error.InvalidUtf8;
const root = try std.json.parseFromSliceLeaky(Json, arena, answer, .{ .allocate = .alloc_always });
if (root != .array) return error.InvalidAnswer;
+ const body = try doc.withoutComments(arena, draft);
return if (pass.isTrim()) trims(arena, pass, body, root.array.items) else parseMarks(arena, pass, body, root.array.items);
}
@@ -581,6 +586,17 @@ test "Lab numbers the snapshot consistently and anchors only unique exact quotes
try std.testing.expect(findings[0].replacement == null and findings[1].replacement == null);
}
+test "Lab reads around comments, and its offsets still address the draft" {
+ var a = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer a.deinit();
+ const arena = a.allocator();
+ const body = "One <!-- sécret --> thing.\n\n<!--\nPrivate.\n-->\nLast fuzzy.";
+ try std.testing.expectEqualStrings("[1] One" ++ " " ** 18 ++ "thing.\n[2] Last fuzzy.\n", try question(arena, .hedges, body));
+ const found = try parse(arena, .hedges, body, "[{\"paragraph\":2,\"text\":\"fuzzy\"},{\"paragraph\":1,\"text\":\"cret\"}]");
+ try std.testing.expectEqual(@as(usize, 1), found.len);
+ try std.testing.expectEqual(mem.indexOf(u8, body, "fuzzy").?, found[0].at);
+}
+
test "Lab typo fixes reject rewrites, conflicts and line separators" {
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
defer a.deinit();