Source for gnu.gcj.convert.Input_JavaSrc

   1: /* Copyright (C) 1999  Free Software Foundation
   2: 
   3:    This file is part of libgcj.
   4: 
   5: This software is copyrighted work licensed under the terms of the
   6: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   7: details.  */
   8: 
   9: package gnu.gcj.convert; 
  10:  
  11: /**
  12:  * Convert Ascii with \ u XXXX-escapes to Unicode.
  13:  * @author Per Bothner <bothner@cygnus.com>
  14:  * @date April 1999.
  15:  */
  16: 
  17: public class Input_JavaSrc extends BytesToUnicode
  18: {
  19:   public String getName() { return "JavaSrc"; }
  20: 
  21:   // 0: normal
  22:   // 1: seen '\\'
  23:   // 2: seen '\\' and 'u'
  24:   // 3: seen '\\' and need to emit value.
  25:   // 4, 5, 6, 7:  seen '\\u', 'u' and (state-3) hex digits.
  26:   int state = 0;
  27: 
  28:   int value;
  29: 
  30:   public int read (char[] outbuffer, int outpos, int count)
  31:   {
  32:     int origpos = outpos;
  33:     for (;;)
  34:       {
  35:     if (inpos >= inlength)
  36:       break;
  37:     if (outpos - origpos >= count)
  38:       break;
  39:     char b = (char) (inbuffer[inpos++] & 0xFF);
  40:     switch (state)
  41:       {
  42:       case 0:
  43:         if (b == '\\')
  44:           {
  45:         state = 1;
  46:         continue;
  47:           }
  48:         break;
  49:       case 1:
  50:         if (b == 'u')
  51:           {
  52:         state = 2;
  53:         continue;
  54:           }
  55:         if (b != '\\')
  56:           {
  57:         value = b;
  58:         b = '\\';
  59:         state = 3;
  60:           }
  61:         break;
  62:       case 3:
  63:         b = (char) value;
  64:         break;
  65:       default:  //  case 4:  case 5:  case 6:  case 7:
  66:         int digit = Character.digit(b, 16);
  67:         if (digit < 0)
  68:           {
  69:         b = '\uFFFD';
  70:         state = 0;
  71:           }
  72:         else
  73:           {
  74:         value = value * 16 + digit;
  75:         if (state < 7)
  76:           {
  77:             state++;
  78:             continue;
  79:           }
  80:         b = (char) value;
  81:           }
  82:         state = 0;
  83:       }
  84:     outbuffer[outpos++] = b;
  85:       }
  86:     return outpos - origpos;
  87:   }
  88: }