8000 Check that `Zed.next_error` index value is within the string length by tmattio · Pull Request #442 · ocaml-community/utop · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Check that Zed.next_error index value is within the string length #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2023
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
42 changes: 24 additions & 18 deletions src/lib/uTop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -849,23 +849,29 @@ let prompt_comment = ref (S.const [| |])
module Private = struct
let fix_string str =
let len = String.length str in
let ofs, _, _ = Zed_utf8.next_error str 0 in
if ofs = len then
if len = 0 then
str
else begin
let buf = Buffer.create (len + 128) in
if ofs > 0 then Buffer.add_substring buf str 0 ofs;
let rec loop ofs =
Zed_utf8.add buf (Uchar.of_char str.[ofs]);
let ofs1 = ofs + 1 in
let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
if ofs1 < ofs2 then
Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
if ofs2 < len then
loop ofs2
else
Buffer.contents buf
in
loop ofs
end
else
let ofs, _, _ = Zed_utf8.next_error str 0 in
if ofs = len then
str
else begin
let buf = Buffer.create (len + 128) in
if ofs > 0 then Buffer.add_substring buf str 0 ofs;
let rec loop ofs =
Zed_utf8.add buf (Uchar.of_char str.[ofs]);
let ofs1 = ofs + 1 in
if ofs1 < len then
let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
if ofs1 < ofs2 then
Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
if ofs2 < len then
loop ofs2
else
Buffer.contents buf
else
Buffer.contents buf
in
loop ofs
end
end
1 change: 1 addition & 0 deletions test/test_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let test_fix_string =
in
( "fix_string"
, [ test ~name:"small" "x" ~expected:"x"
; test ~name:"empty" "" ~expected:""
]
)

Expand Down
0