aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/lang/natCharacter.cc
blob: 14e0ebf2b7f877472bac2947d0463894b953c72d (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// natCharacter.cc - Native part of Character class.

/* Copyright (C) 1998, 1999  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>

#include <gcj/cni.h>
#include <jvm.h>
#include <java/lang/Character.h>

#include <java-chartables.h>



#define asize(x)  ((sizeof (x)) / sizeof (x[0]))

static jchar
to_lower_title (jchar ch)
{
  for (unsigned int i = 0; i < asize (title_to_upper_table); ++i)
    {
      // We can assume that the entries in the two tables are
      // parallel.  This is checked in the script.
      if (title_to_upper_table[i][1] == ch
	  || title_to_upper_table[i][0] == ch)
	return title_to_lower_table[i][1];
    }
  return ch;
}

static jchar
to_upper_title (jchar ch)
{
  for (unsigned int i = 0; i < asize (title_to_lower_table); ++i)
    {
      // We can assume that the entries in the two tables are
      // parallel.  This is checked in the script.
      if (title_to_lower_table[i][1] == ch
	  || title_to_lower_table[i][0] == ch)
	return title_to_upper_table[i][1];
    }
  return ch;
}

jboolean
java::lang::Character::isTitleCase (jchar ch)
{
  for (unsigned int i = 0; i < asize (title_to_lower_table); ++i)
    {
      if (title_to_lower_table[i][0] == ch)
	return true;
    }
  return false;
}

jchar
java::lang::Character::toTitleCase (jchar ch)
{
  // Both titlecase mapping tables have the same length.  This is
  // checked in the chartables script.
  for (unsigned int i = 0; i < asize (title_to_lower_table); ++i)
    {
      if (title_to_lower_table[i][0] == ch)
	return ch;
      if (title_to_lower_table[i][1] == ch)
	return title_to_lower_table[i][0];
      if (title_to_upper_table[i][1] == ch)
	return title_to_upper_table[i][0];
    }
  return toUpperCase (ch);
}

#ifdef COMPACT_CHARACTER

static int
table_search (const jchar table[][2], int table_len, jchar ch)
{
  int low, high, i, old;

  low = 0;
  high = table_len;
  i = high / 2;

  while (true)
    {
      if (ch < table[i][0])
	high = i;
      else if (ch > table[i][1])
	low = i;
      else
	return i;

      old = i;
      i = (high + low) / 2;
      if (i == old)
	break;
    }

  return -1;
}

jint
java::lang::Character::digit_value (jchar ch)
{
  int index = table_search (digit_table, asize (digit_table), ch);
  if (index == -1)
    return -1;

  jchar base = digit_table[index][0];
  // Tamil doesn't have a digit `0'.  So we special-case it here.
  if (base == TAMIL_DIGIT_ONE)
    return ch - base + 1;
  return ch - base;
}

jint
java::lang::Character::getNumericValue (jchar ch)
{
  jint d = digit (ch, 36);
  if (d != -1)
    return d;

  for (unsigned int i = 0; i < asize (numeric_table); ++i)
    {
      if (numeric_table[i] == ch)
	return numeric_value[i];
    }

  return -1;
}

jint
java::lang::Character::getType (jchar ch)
{
  int index = table_search (all_table, asize (all_table), ch);
  if (index != -1)
    return category_table[index];
  return UNASSIGNED;
}

jboolean
java::lang::Character::isLowerCase (jchar ch)
{
  if (ch >= 0x2000 && ch <= 0x2fff)
    return false;
  if (table_search (lower_case_table, asize (lower_case_table), ch) != -1)
    return true;

  int low, high, i, old;

  low = 0;
  high = asize (lower_anomalous_table);
  i = high / 2;

  while (true)
    {
      if (ch < lower_anomalous_table[i])
	high = i;
      else if (ch > lower_anomalous_table[i])
	low = i;
      else
	return true;

      old = i;
      i = (high + low) / 2;
      if (i == old)
	break;
    }

  return false;
}

jboolean
java::lang::Character::isSpaceChar (jchar ch)
{
  return table_search (space_table, asize (space_table), ch) != -1;
}

jboolean
java::lang::Character::isUpperCase (jchar ch)
{
  if (ch >= 0x2000 && ch <= 0x2fff)
    return false;
  return table_search (upper_case_table, asize (upper_case_table), ch) != -1;
}

jchar
java::lang::Character::toLowerCase (jchar ch)
{
  int index = table_search (upper_case_table, asize (upper_case_table), ch);
  if (index == -1)
    return to_lower_title (ch);
  return (jchar) (ch - upper_case_table[index][0]
		  + upper_case_map_table[index]);
}

jchar
java::lang::Character::toUpperCase (jchar ch)
{
  int index = table_search (lower_case_table, asize (lower_case_table), ch);
  if (index == -1)
    return to_upper_title (ch);
  return (jchar) (ch - lower_case_table[index][0]
		  + lower_case_map_table[index]);
}

#else /* COMPACT_CHARACTER */

jint
java::lang::Character::digit_value (jchar ch)
{
  if (type_table[ch] == DECIMAL_DIGIT_NUMBER)
    return attribute_table[ch];
  return -1;
}

jint
java::lang::Character::getNumericValue (jchar ch)
{
  jint d = digit (ch, 36);
  if (d != -1)
    return d;

  // Some characters require two attributes.  We special-case them here.
  if (ch >= ROMAN_START && ch <= ROMAN_END)
    return secondary_attribute_table[ch - ROMAN_START];
  if (type_table[ch] == LETTER_NUMBER || type_table[ch] == OTHER_NUMBER)
    return attribute_table[ch];
  return -1;
}

jint
java::lang::Character::getType (jchar ch)
{
  return type_table[ch];
}

jboolean
java::lang::Character::isLowerCase (jchar ch)
{
  if (ch >= 0x2000 && ch <= 0x2fff)
    return false;
  return type_table[ch] == LOWERCASE_LETTER;
}

jboolean
java::lang::Character::isSpaceChar (jchar ch)
{
  return (type_table[ch] == SPACE_SEPARATOR
	  || type_table[ch] == LINE_SEPARATOR
	  || type_table[ch] == PARAGRAPH_SEPARATOR);
}

jboolean
java::lang::Character::isUpperCase (jchar ch)
{
  if (ch >= 0x2000 && ch <= 0x2fff)
    return false;
  return type_table[ch] == UPPERCASE_LETTER;
}

jchar
java::lang::Character::toLowerCase (jchar ch)
{
  if (type_table[ch] == UPPERCASE_LETTER)
    return attribute_table[ch];
  return to_lower_title (ch);
}

jchar
java::lang::Character::toUpperCase (jchar ch)
{
  if (type_table[ch] == LOWERCASE_LETTER)
    return attribute_table[ch];
  return to_upper_title (ch);
}

#endif /* COMPACT_CHARACTER */