2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / gcj / convert / Output_JavaSrc.java
blobdc99218cf9b0088a5b5f80ad291d9c74d223dbeb
1 /* Copyright (C) 1999 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.gcj.convert;
11 /**
12 * Convert Unicode to Ascii with \ u XXXX-escapes.
13 * @author Per Bothner <bothner@cygnus.com>
14 * @date April 1999.
17 public class Output_JavaSrc extends UnicodeToBytes
19 public String getName() { return "JavaSrc"; }
21 // Number of bytes remaining before pending_char has been written.
22 int todo;
23 int pending_char;
25 public int write (char[] inbuffer, int inpos, int inlength)
27 int start_pos = inpos;
28 int avail = buf.length - count;
29 for (;;)
31 if (avail == 0)
32 break;
33 switch (todo)
35 case 1:
36 if (pending_char == '\\')
38 buf[count++] = (byte) '\\';
39 avail--;
40 todo = 0;
41 continue;
43 /* ... else fall through ... */
44 case 2:
45 case 3:
46 case 4:
47 todo--;
48 int digit = ((int) pending_char >> (todo * 4)) & 0xF;
49 buf[count++] = (byte) Character.forDigit(digit, 16);
50 avail--;
51 continue;
52 case 5:
53 buf[count++] = (byte) 'u';
54 avail--;
55 todo = 4;
56 continue;
57 default:
60 if (inlength == 0)
61 break;
62 char ch = inbuffer[inpos++];
63 inlength--;
64 if (ch == '\\')
66 buf[count++] = (byte) '\\';
67 pending_char = ch;
68 todo = 1;
69 avail--;
71 else if (ch < 127)
73 buf[count++] = (byte) ch;
74 avail--;
76 else
78 buf[count++] = (byte) '\\';
79 pending_char = ch;
80 todo = 5;
81 avail--;
84 return inpos - start_pos;