Commit e04e2dea authored by stolpmann's avatar stolpmann

Updates because of changes in Ocamlnet 0.96


git-svn-id: https://godirepo.camlcity.org/svn/wdialog/trunk@182 f54c9a64-0731-4a92-b797-30fd5898f27c
parent 2f15dd9a
......@@ -21,150 +21,20 @@
* </>
*)
(* $Id: wd_var_functions.ml,v 1.3 2003-02-16 01:07:03 stolpmann Exp $
(* $Id: wd_var_functions.ml,v 1.4 2003-06-21 12:09:08 stolpmann Exp $
* ----------------------------------------------------------------------
*
*)
open Wd_types
(* --------------- Unicode string functions ---------------------- *)
(* These should be moved to ocamlnet! *)
let uni_length_of_string enc ?(byte_pos = 0) ?byte_len s =
match enc with
`Enc_utf8 ->
let n = ref 0 in
let k = ref byte_pos in
let m =
match byte_len with
None -> String.length s
| Some l -> byte_pos + l
in
while !k < m do
incr n;
match s.[ !k ] with
('\000'..'\127') ->
incr k
| ('\128'..'\223') ->
k := !k + 2
| ('\224'..'\239') ->
k := !k + 3
| ('\240'..'\247') ->
k := !k + 3
| _ ->
assert false
done;
assert(!k = m);
!n
| (`Enc_utf16_le | `Enc_utf16_be | `Enc_utf16 ) ->
assert false (* UTF16 not supported by wdialog *)
| `Enc_java ->
assert false (* Not supported by Wdialog *)
| _ ->
assert(Netconversion.is_single_byte enc);
let m =
match byte_len with
None -> String.length s
| Some l -> l
in
m
;;
let uni_array_of_string enc s =
match enc with
`Enc_utf8 ->
let l = uni_length_of_string enc s in
let a = Array.make l 0 in
let k = ref 0 in
let dummy_buf = String.create 0 in
ignore(Netconversion.recode
~in_enc:enc
~in_buf:s
~in_pos:0
~in_len:(String.length s)
~out_enc:(`Enc_subset(enc, fun _ -> false))
~out_buf:dummy_buf
~out_pos:0
~out_len:0
~max_chars:l
~subst:(fun x -> a.( !k ) <- x; incr k; ""));
assert(!k = l);
a
| (`Enc_utf16_le | `Enc_utf16_be | `Enc_utf16 ) ->
assert false (* UTF16 not supported by wdialog *)
| `Enc_java ->
assert false (* Not supported by Wdialog *)
| (`Enc_usascii | `Enc_iso88591) ->
let l = String.length s in
let a = Array.make l 0 in
for k = 0 to l - 1 do a.(k) <- Char.code s.[k] done;
a
| _ ->
assert(Netconversion.is_single_byte enc);
let conv = Netconversion.to_unicode ~in_enc:enc in
let l = String.length s in
let a = Array.make l 0 in
for k = 0 to l - 1 do
let x = Char.code s.[k] in
let x' = conv x in
a.(k) <- x'
done;
a
;;
let string_of_uni_array enc a =
match enc with
`Enc_utf8 ->
let mk = Netconversion.makechar enc in
let a' = Array.map mk a in
let n = ref 0 in
for k = 0 to Array.length a' - 1 do
n := !n + String.length a'.(k)
done;
let s = String.create !n in
n := 0;
for k = 0 to Array.length a' - 1 do
let t = a'.(k) in
String.blit t 0 s !n (String.length t);
n := !n + String.length t
done;
s
| (`Enc_utf16_le | `Enc_utf16_be | `Enc_utf16 ) ->
assert false (* UTF16 not supported by wdialog *)
| `Enc_java ->
assert false (* Not supported by Wdialog *)
| (`Enc_usascii | `Enc_iso88591) ->
let l = Array.length a in
let s = String.create l in
for k = 0 to l - 1 do
s.[ k ] <- Char.chr a.(k)
done;
s
| _ ->
assert(Netconversion.is_single_byte enc);
let mk = Netconversion.makechar enc in
let l = Array.length a in
let s = String.create l in
for k = 0 to l - 1 do
let u = mk a.(k) in
s.[ k ] <- u.[ 0 ]
done;
s
;;
(* --------------------------------------------------------------- *)
let size (dlg : dialog_type) args =
match args with
[a1] ->
(match a1 with
String_value s ->
let enc = (dlg#application#charset :> Pxp_types.encoding) in
let n = uni_length_of_string enc s in
let n = Netconversion.ustring_length enc s in
String_value (string_of_int n)
| Enum_value e ->
String_value (string_of_int (List.length e))
......@@ -289,10 +159,8 @@ let substring (dlg : dialog_type) args =
let (k1,k2) =
if length >= 0 then (start,start+length) else (start+length,start) in
let n1 = max 0 k1 in
let n2 = min (uni_length_of_string enc s) k2 in
let a = uni_array_of_string enc s in
let a' = Array.sub a n1 (n2-n1) in
string_of_uni_array enc a'
let n2 = min (Netconversion.ustring_length enc s) k2 in
Netconversion.ustring_sub enc n1 (n2-n1) s
in
match args with
[a1;a2] ->
......@@ -370,13 +238,13 @@ let rec count_width enc s w k =
if next_cr_or_lf >= 0 then begin
match s.[next_cr_or_lf] with
'\010' ->
let n = uni_length_of_string
enc ~byte_pos:k ~byte_len:(next_cr_or_lf - k) s in
let n = Netconversion.ustring_length
enc ~range_pos:k ~range_len:(next_cr_or_lf - k) s in
let w' = max w n in
count_width enc s w' (next_cr_or_lf+1)
| '\013' ->
let n = uni_length_of_string
enc ~byte_pos:k ~byte_len:(next_cr_or_lf - k) s in
let n = Netconversion.ustring_length
enc ~range_pos:k ~range_len:(next_cr_or_lf - k) s in
let w' = max w n in
let l = String.length s in
if (next_cr_or_lf+1 < l && s.[next_cr_or_lf+1] = '\010') then
......@@ -448,7 +316,10 @@ let language =
* History:
*
* $Log: wd_var_functions.ml,v $
* Revision 1.3 2003-02-16 01:07:03 stolpmann
* Revision 1.4 2003-06-21 12:09:08 stolpmann
* Updates because of changes in Ocamlnet 0.96
*
* Revision 1.3 2003/02/16 01:07:03 stolpmann
* size, substring, width: string positions are measured as
* the number of characters, not as the number of bytes
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment