Skip to content

Commit

Permalink
Fix whitespace processing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin committed Dec 21, 2024
1 parent acbd993 commit e5c6193
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
.vscode
_opam/
_build/
**/.merlin
*.xml
*.xlsx
!test/files
23 changes: 11 additions & 12 deletions src/xml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,7 @@ module SAX = struct
true
| _ -> false

let squish_into raw final acc =
String.fold raw ~init:acc ~f:(fun acc c ->
match acc with
| after_first, _ when is_ws c -> after_first, true
| true, true ->
Buffer.add_char final ' ';
Buffer.add_char final c;
true, false
| _ ->
Buffer.add_char final c;
true, false )
let find_not_ws (_ : int) c = not (is_ws c)

let render attrs text =
match Queue.length text with
Expand All @@ -114,7 +104,16 @@ module SAX = struct
if after_first && not prev_ws then Buffer.add_char final ' ';
Buffer.add_string final raw;
true, is_ws raw.[String.length raw - 1]
| { raw; _ } -> squish_into raw final (if after_first then true, true else acc) )
| { raw; _ } -> (
match String.lfindi raw ~f:find_not_ws with
| None -> acc
| Some start -> (
match String.rfindi raw ~f:find_not_ws with
| None -> raise (Sys_error "Impossible case in Xml.render. Please report this bug.")
| Some stop ->
if after_first && not prev_ws then Buffer.add_char final ' ';
Buffer.add_substring final raw ~pos:start ~len:(stop - start + 1);
true, false ) ) )
in
Buffer.contents final

Expand Down
16 changes: 16 additions & 0 deletions test/files/spaces.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [
[
null,
null
],
[
"Shen LI",
null,
null,
null,
null,
null
]
]
}
Binary file added test/files/spaces.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions test/xlsx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ let () =
"zip64.xlsx", `Quick, xlsx "zip64";
"formatting.xlsx", `Quick, xlsx "formatting";
"inline.xlsx", `Quick, xlsx "inline";
"spaces.xlsx", `Quick, xlsx "spaces";
"Readme example 1", `Quick, readme_example1 "financial";
"Readme example 2", `Quick, readme_example2 "financial";
"Unbuffed stream", `Quick, stream_rows "financial";
Expand Down
4 changes: 2 additions & 2 deletions test/xml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ comment -->
{
"tag": "t",
"attrs": [],
"text": "hello world y",
"text": "hello world y",
"children": [
{ "tag": "x", "attrs": [], "text": "", "children": [] }
]
Expand Down Expand Up @@ -344,7 +344,7 @@ module Test4 = struct
{
"tag": "head",
"attrs": [],
"text": "clear head text X some lost text back to the top",
"text": "clear head text X some lost text back to the top",
"children": [
{
"tag": "meta",
Expand Down

0 comments on commit e5c6193

Please sign in to comment.