File: eucjp.ml

package info (click to toggle)
lablgtk2 2.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,804 kB
  • ctags: 5,871
  • sloc: ml: 32,939; ansic: 8,488; makefile: 679; sh: 85
file content (83 lines) | stat: -rw-r--r-- 2,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(**************************************************************************)
(*     Lablgtk - Camlirc                                                  *)
(*                                                                        *)
(*    * You are free to do anything you want with this code as long       *)
(*      as it is for personal use.                                        *)
(*                                                                        *)
(*    * Redistribution can only be "as is".  Binary distribution          *)
(*      and bug fixes are allowed, but you cannot extensively             *)
(*      modify the code without asking the authors.                       *)
(*                                                                        *)
(*    The authors may choose to remove any of the above                   *)
(*    restrictions on a per request basis.                                *)
(*                                                                        *)
(*    Authors:                                                            *)
(*      Nobuaki Yoshida  <nyoshi@dd.iij4u.or.jp>                          *)
(*      Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>                  *)
(*                                                                        *)
(**************************************************************************)

(* $Id: eucjp.ml 1354 2007-07-20 04:18:38Z garrigue $ *)

open String
open Char

exception Conversion_error

let ntol s =
  let is_ascii = ref true
  and dest = Buffer.create 0
  and str = Stream.of_string s
  in
  try
    while true do
      let c = Stream.next str in
      match c with
	'\027' ->
	  begin
	    let c1 = Stream.next str
	    and c2 = Stream.next str
	    in
	    match (c1,c2) with
	      ('(', 'B') -> is_ascii := true
	    | ('$', 'B') -> is_ascii := false
	    | _ -> raise Conversion_error
	  end
      | _ ->
	  Buffer.add_char dest 
	    (if !is_ascii then c else (chr ((code c) + 128)))
    done; Buffer.contents dest
  with Stream.Failure -> Buffer.contents dest

let lton s =
  let is_ascii = ref true
  and dest = Buffer.create 0
  and str = Stream.of_string s
  in
  try
    while true do
      let c = Stream.next str in
      if (code c) > 127 then
	if !is_ascii then
	  begin
	    is_ascii := false;
	    Buffer.add_string dest "\027$B";
	    Buffer.add_char dest (chr ((code c) - 128))
	  end
	else Buffer.add_char dest (chr ((code c) - 128))
      else
	if !is_ascii then
	  Buffer.add_char dest c
	else 
	  begin
	    is_ascii := true;
	    Buffer.add_string dest "\027(B";
	    Buffer.add_char dest c
	  end
    done; 
    if not !is_ascii then Buffer.add_string dest "\027(B";
    Buffer.contents dest
  with Stream.Failure -> 
    if not !is_ascii then Buffer.add_string dest "\027(B";
    Buffer.contents dest