Output_JavaSrc.java 1.6 KB

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