-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose UTF-16/WTF-8 conversion functions
- Loading branch information
Showing
11 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
(* This file is part of Luv, released under the MIT license. See LICENSE.md for | ||
details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *) | ||
|
||
|
||
|
||
let utf16_length_as_wtf8 s = | ||
let result = | ||
C.Functions.String_.utf16_length_as_wtf8 | ||
s | ||
(PosixTypes.Ssize.of_int (String.length s / 2)) | ||
|> Unsigned.Size_t.to_int | ||
in | ||
if result < 0 then | ||
Printf.ksprintf | ||
failwith "Luv internal error: utf16_length_as_wtf8 error code %i;%s" | ||
result "\nCheck your libuv version; 1.47.0 or higher required."; | ||
result | ||
|
||
let utf16_to_wtf8 s = | ||
let wtf8 = Ctypes.(allocate (ptr char) (coerce (ptr void) (ptr char) null)) in | ||
let wtf8_length = Ctypes.(allocate size_t) Unsigned.Size_t.zero in | ||
let error_code = | ||
C.Functions.String_.utf16_to_wtf8 | ||
s | ||
(PosixTypes.Ssize.of_int (String.length s / 2)) | ||
wtf8 | ||
wtf8_length | ||
in | ||
if error_code <> 0 then | ||
Printf.ksprintf | ||
failwith "Luv internal error: utf16_to_wtf8 error code %i;%s" error_code | ||
"\nCheck your libuv version; 1.47.0 or higher required."; | ||
let wtf8 = Ctypes.(!@ wtf8) in | ||
let wtf8_length = Ctypes.(!@ wtf8_length) |> Unsigned.Size_t.to_int in | ||
let s = Ctypes.string_from_ptr wtf8 ~length:wtf8_length in | ||
C.Functions.String_.free Ctypes.(coerce (ptr char) (ptr void) wtf8); | ||
s | ||
|
||
let wtf8_length_as_utf16 s = | ||
let result = | ||
C.Functions.String_.wtf8_length_as_utf16 s | ||
|> PosixTypes.Ssize.to_int | ||
in | ||
if result < 0 then | ||
Printf.ksprintf | ||
failwith "Luv internal error: wtf8_length_as_utf16 error code %i;%s" | ||
result "\nCheck your libuv version; 1.47.0 or higher required."; | ||
result | ||
|
||
let wtf8_to_utf16 s = | ||
let utf16_length = wtf8_length_as_utf16 s in | ||
let utf16 = Ctypes.(allocate_n uint16_t) ~count:utf16_length in | ||
C.Functions.String_.wtf8_to_utf16 | ||
s utf16 (Unsigned.Size_t.of_int utf16_length); | ||
utf16 | ||
|> Ctypes.(coerce (ptr uint16_t) (ptr char)) | ||
|> Ctypes.string_from_ptr ~length:((utf16_length - 1) * 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(* This file is part of Luv, released under the MIT license. See LICENSE.md for | ||
details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *) | ||
|
||
|
||
|
||
val utf16_length_as_wtf8 : string -> int | ||
(** Binds {{:https://docs.libuv.org/en/v1.x/misc.html#c.uv_utf16_length_as_wtf8} | ||
[uv_utf16_length_as_wtf8]}. | ||
Requires Luv 0.5.13 and libuv 1.47.0. | ||
{{!Luv.Require} Feature check}: [Luv.Require.(has utf_16)] *) | ||
|
||
val utf16_to_wtf8 : string -> string | ||
(** Binds {{:https://docs.libuv.org/en/v1.x/misc.html#c.uv_utf16_to_wtf8} | ||
[uv_utf16_to_wtf8]}. | ||
Requires Luv 0.5.13 and libuv 1.47.0. | ||
{{!Luv.Require} Feature check}: [Luv.Require.(has utf_16)] *) | ||
|
||
val wtf8_length_as_utf16 : string -> int | ||
(** Binds {{:https://docs.libuv.org/en/v1.x/misc.html#c.uv_wtf8_length_as_utf16} | ||
[uv_wtf8_length_as_utf16]}. | ||
Requires Luv 0.5.13 and libuv 1.47.0. | ||
{{!Luv.Require} Feature check}: [Luv.Require.(has utf_16)] *) | ||
|
||
val wtf8_to_utf16 : string -> string | ||
(** Binds {{:https://docs.libuv.org/en/v1.x/misc.html#c.uv_wtf8_to_utf16} | ||
[uv_wtf8_to_utf16]}. | ||
Requires Luv 0.5.13 and libuv 1.47.0. | ||
{{!Luv.Require} Feature check}: [Luv.Require.(has utf_16)] *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(* This file is part of Luv, released under the MIT license. See LICENSE.md for | ||
details, or visit https://github.com/aantron/luv/blob/master/LICENSE.md. *) | ||
|
||
|
||
|
||
let tests = [ | ||
"string", [ | ||
"utf16_length_as_wtf8", `Quick, begin fun () -> | ||
Luv.String.utf16_length_as_wtf8 "f\x00\xBB\x03" | ||
|> Alcotest.(check int) "length" 3 | ||
end; | ||
|
||
"utf16_to_wtf8", `Quick, begin fun () -> | ||
Luv.String.utf16_to_wtf8 "f\x00\xBB\x03" | ||
|> Alcotest.(check string) "value" "fλ" | ||
end; | ||
|
||
"wtf8_length_as_utf16", `Quick, begin fun () -> | ||
Luv.String.wtf8_length_as_utf16 "fλ" | ||
|> Alcotest.(check int) "length" 3 | ||
end; | ||
|
||
"wtf8_to_utf16", `Quick, begin fun () -> | ||
Luv.String.wtf8_to_utf16 "fλ" | ||
|> Alcotest.(check string) "value" "f\x00\xBB\x03" | ||
end; | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ let () = | |
DNS.tests; | ||
Thread_.tests; | ||
Misc.tests; | ||
String_.tests; | ||
]) |