natCharacter.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* java.lang.Character -- Wrapper class for char, and Unicode subsets
  2. Copyright (C) 1998, 1999, 2001, 2002, 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <config.h>
  32. #include <gcj/cni.h>
  33. #include <jvm.h>
  34. #include <java/lang/Character.h>
  35. #include <java-chartables.h>
  36. // These constants define the return values for characters that are unassigned
  37. // or reserved for private use.
  38. #define UNASSIGNED_TYPE 0
  39. #define UNASSIGNED_DIGIT -1
  40. #define UNASSIGNED_DIRECTION -1
  41. #define UNASSIGNED_NUMERIC_VALUE -1
  42. #define PRIVATE_TYPE 18
  43. #define PRIVATE_DIRECTION 0
  44. // The methods that take a char as an argument all have counterparts that
  45. // take ints. The ones that take chars only work for the BMP or plane 0 of the
  46. // Unicode standard but the ones that take ints work for all Unicode code
  47. // points. However, the ones that take chars don't simply redirect the calls
  48. // because the BMP is by far the most used plane so saving a little time on
  49. // each call makes sense.
  50. jchar
  51. java::lang::Character::readChar(jchar ch)
  52. {
  53. // Perform 16-bit addition to find the correct entry in data.
  54. return data[0][(jchar) (blocks[0][ch >> shift[0]] + ch)];
  55. }
  56. jchar
  57. java::lang::Character::readCodePoint(jint codePoint)
  58. {
  59. jint plane = codePoint >> 16;
  60. jchar offset = (jchar)(codePoint & 0xffff);
  61. // Be careful not to call this method with an unassigned character. The only
  62. // characters assigned as of Unicode 4.0.0 belong to planes 0, 1, 2, and 14.
  63. return data[plane][(jchar) (blocks[plane][offset >> shift[plane]] + offset)];
  64. }
  65. jint
  66. java::lang::Character::getType(jchar ch)
  67. {
  68. // Perform 16-bit addition to find the correct entry in data.
  69. return (jint) (data[0][(jchar) (blocks[0][ch >> shift[0]] + ch)] & TYPE_MASK);
  70. }
  71. jint
  72. java::lang::Character::getType(jint codePoint)
  73. {
  74. jint plane = codePoint >> 16;
  75. if (plane < 0 || (plane > 2 && plane != 14))
  76. {
  77. if (plane > 14 && ((codePoint & 0xffff) < 0xfffe))
  78. return (jint) PRIVATE_TYPE;
  79. return (jint) UNASSIGNED_TYPE;
  80. }
  81. jint offset = codePoint & 0xffff;
  82. return (jint)
  83. (data[plane]
  84. [(jchar) (blocks[plane][offset >> shift[plane]] + offset)] & TYPE_MASK);
  85. }
  86. jchar
  87. java::lang::Character::toLowerCase(jchar ch)
  88. {
  89. return (jchar) (ch + lower[0][readChar(ch) >> 7]);
  90. }
  91. jint
  92. java::lang::Character::toLowerCase(jint codePoint)
  93. {
  94. jint plane = codePoint >> 16;
  95. if (plane < 0 || (plane > 2 && plane != 14))
  96. return codePoint;
  97. return (lower[plane][readCodePoint(codePoint) >> 7]) + codePoint;
  98. }
  99. jchar
  100. java::lang::Character::toUpperCase(jchar ch)
  101. {
  102. return (jchar) (ch + upper[0][readChar(ch) >> 7]);
  103. }
  104. jint
  105. java::lang::Character::toUpperCase(jint codePoint)
  106. {
  107. jint plane = codePoint >> 16;
  108. if (plane < 0 || (plane > 2 && plane != 14))
  109. return codePoint;
  110. return (upper[plane][readCodePoint(codePoint) >> 7]) + codePoint;
  111. }
  112. jchar
  113. java::lang::Character::toTitleCase(jchar ch)
  114. {
  115. // As title is short, it doesn't hurt to exhaustively iterate over it.
  116. for (int i = title_length - 2; i >= 0; i -= 2)
  117. if (title[i] == ch)
  118. return title[i + 1];
  119. return toUpperCase(ch);
  120. }
  121. jint
  122. java::lang::Character::toTitleCase(jint codePoint)
  123. {
  124. // As of Unicode 4.0.0 no characters outside of plane 0 have titlecase
  125. // mappings that are different from their uppercase mapping.
  126. if (codePoint >= 0 && codePoint < 0x10000)
  127. return toTitleCase((jchar)codePoint);
  128. return toUpperCase(codePoint);
  129. }
  130. jint
  131. java::lang::Character::digit(jchar ch, jint radix)
  132. {
  133. if (radix < MIN_RADIX || radix > MAX_RADIX)
  134. return (jint) -1;
  135. jchar attr = readChar(ch);
  136. if (((1 << (attr & TYPE_MASK))
  137. & ((1 << UPPERCASE_LETTER)
  138. | (1 << LOWERCASE_LETTER)
  139. | (1 << DECIMAL_DIGIT_NUMBER))))
  140. {
  141. // Signedness doesn't matter; 0xffff vs. -1 are both rejected.
  142. jint digit = (jint) numValue[0][attr >> 7];
  143. return (digit >= 0 && digit < radix) ? digit : (jint) -1;
  144. }
  145. return (jint) -1;
  146. }
  147. jint
  148. java::lang::Character::digit(jint codePoint, jint radix)
  149. {
  150. if (radix < MIN_RADIX || radix > MAX_RADIX)
  151. return (jint) -1;
  152. jint plane = codePoint >> 16;
  153. if (plane < 0 || (plane > 2 && plane != 14))
  154. return UNASSIGNED_DIGIT;
  155. jchar attr = readCodePoint(codePoint);
  156. if (((1 << (attr & TYPE_MASK))
  157. & ((1 << UPPERCASE_LETTER)
  158. | (1 << LOWERCASE_LETTER)
  159. | (1 << DECIMAL_DIGIT_NUMBER))))
  160. {
  161. // Signedness doesn't matter; 0xffff vs. -1 are both rejected.
  162. jint digit = (jint) numValue[plane][attr >> 7];
  163. if (digit <= -3)
  164. digit = largenums[-digit -3];
  165. return (digit >= 0 && digit < radix) ? digit : (jint) -1;
  166. }
  167. return (jint) -1;
  168. }
  169. jint
  170. java::lang::Character::getNumericValue(jchar ch)
  171. {
  172. // numValue is stored as an array of jshort, since 10000 is the maximum.
  173. return (jint) numValue[0][readChar(ch) >> 7];
  174. }
  175. jint
  176. java::lang::Character::getNumericValue(jint codePoint)
  177. {
  178. jint plane = codePoint >> 16;
  179. if (plane < 0 || (plane > 2 && plane != 14))
  180. return UNASSIGNED_NUMERIC_VALUE;
  181. jshort num = numValue[plane][readCodePoint(codePoint) >> 7];
  182. if (num <= -3)
  183. return largenums[-num - 3];
  184. return num;
  185. }
  186. jbyte
  187. java::lang::Character::getDirectionality(jchar ch)
  188. {
  189. return direction[0][readChar(ch) >> 7];
  190. }
  191. jbyte
  192. java::lang::Character::getDirectionality(jint codePoint)
  193. {
  194. jint plane = codePoint >> 16;
  195. if (plane < 0 || (plane > 2 && plane != 14))
  196. {
  197. if (plane > 14 && ((codePoint & 0xffff) < 0xfffe))
  198. return (jint) PRIVATE_DIRECTION;
  199. return (jint) UNASSIGNED_DIRECTION;
  200. }
  201. return direction[plane][readCodePoint(codePoint) >> 7];
  202. }