-(* This would be so much simpler with ChriS's delimited
- * overloading macro XXX
- *)
-let i32_of_string_le v =
- let b0 = int_of_char (String.unsafe_get v 0) in
- let b1 = int_of_char (String.unsafe_get v 1) in
- let b2 = int_of_char (String.unsafe_get v 2) in
- let b3 = Int32.of_int (int_of_char (String.unsafe_get v 3)) in
- Int32.logor
- (Int32.of_int (b0 lor (b1 lsl 8) lor (b2 lsl 16)))
- (Int32.shift_left b3 24)
-
-let i32_of_string_be v =
- let b0 = Int32.of_int (int_of_char (String.unsafe_get v 0)) in
- let b1 = int_of_char (String.unsafe_get v 1) in
- let b2 = int_of_char (String.unsafe_get v 2) in
- let b3 = int_of_char (String.unsafe_get v 3) in
- Int32.logor
- (Int32.of_int (b3 lor (b2 lsl 8) lor (b1 lsl 16)))
- (Int32.shift_left b0 24)
-
-let i64_of_string_le v =
- let b0 = int_of_char (String.unsafe_get v 0) in
- let b1 = int_of_char (String.unsafe_get v 1) in
- let b2 = int_of_char (String.unsafe_get v 2) in
- let b3 = Int64.of_int (int_of_char (String.unsafe_get v 3)) in
- let b4 = Int64.of_int (int_of_char (String.unsafe_get v 4)) in
- let b5 = Int64.of_int (int_of_char (String.unsafe_get v 5)) in
- let b6 = Int64.of_int (int_of_char (String.unsafe_get v 6)) in
- let b7 = Int64.of_int (int_of_char (String.unsafe_get v 7)) in
- Int64.logor
- (Int64.logor
- (Int64.logor
- (Int64.logor
- (Int64.logor
- (Int64.of_int (b0 lor (b1 lsl 8) lor (b2 lsl 16)))
- (Int64.shift_left b3 24))
- (Int64.shift_left b4 32))
- (Int64.shift_left b5 40))
- (Int64.shift_left b6 48))
- (Int64.shift_left b7 56)
+let utf16le = CharEncoding.utf16le
+let utf8 = CharEncoding.utf8
+let recode = CharEncoding.recode_string ~in_enc:utf16le ~out_enc:utf8
+
+let windows_string_to_utf8 str =
+ let str = recode str in
+
+ (* Windows strings include the final \0 so remove this if present. *)
+ let len = UTF8.length str in
+ if len > 0 && UChar.code (UTF8.get str (len-1)) = 0 then
+ String.sub str 0 (UTF8.last str)
+ else
+ str