1 7 8 package com.ibm.icu.lang; 9 10 import com.ibm.icu.util.ValueIterator; 11 import com.ibm.icu.impl.UCharacterName; 12 import com.ibm.icu.impl.UCharacterNameChoice; 13 14 22 class UCharacterNameIterator implements ValueIterator 23 { 24 26 36 public boolean next(ValueIterator.Element element) 37 { 38 if (m_current_ >= m_limit_) { 39 return false; 40 } 41 42 if (m_choice_ != UCharacterNameChoice.UNICODE_10_CHAR_NAME) { 43 int length = m_name_.getAlgorithmLength(); 44 if (m_algorithmIndex_ < length) { 45 while (m_algorithmIndex_ < length) { 46 if (m_algorithmIndex_ < 0 || 48 m_name_.getAlgorithmEnd(m_algorithmIndex_) < 49 m_current_) { 50 m_algorithmIndex_ ++; 51 } 52 else { 53 break; 54 } 55 } 56 57 if (m_algorithmIndex_ < length) { 58 int start = m_name_.getAlgorithmStart(m_algorithmIndex_); 62 if (m_current_ < start) { 63 int end = start; 66 if (m_limit_ <= start) { 67 end = m_limit_; 68 } 69 if (!iterateGroup(element, end)) { 70 m_current_ ++; 71 return true; 72 } 73 } 74 75 if (m_current_ >= m_limit_) { 76 return false; 79 } 80 81 element.integer = m_current_; 82 element.value = m_name_.getAlgorithmName(m_algorithmIndex_, 83 m_current_); 84 m_groupIndex_ = -1; 86 m_current_ ++; 87 return true; 88 } 89 } 90 } 91 if (!iterateGroup(element, m_limit_)) { 93 m_current_ ++; 94 return true; 95 } 96 else if (m_choice_ == UCharacterNameChoice.EXTENDED_CHAR_NAME) { 97 if (!iterateExtended(element, m_limit_)) { 98 m_current_ ++; 99 return true; 100 } 101 } 102 103 return false; 104 } 105 106 112 public void reset() 113 { 114 m_current_ = m_start_; 115 m_groupIndex_ = -1; 116 m_algorithmIndex_ = -1; 117 } 118 119 135 public void setRange(int start, int limit) 136 { 137 if (start >= limit) { 138 throw new IllegalArgumentException ( 139 "start or limit has to be valid Unicode codepoints and start < limit"); 140 } 141 if (start < UCharacter.MIN_VALUE) { 142 m_start_ = UCharacter.MIN_VALUE; 143 } 144 else { 145 m_start_ = start; 146 } 147 148 if (limit > UCharacter.MAX_VALUE + 1) { 149 m_limit_ = UCharacter.MAX_VALUE + 1; 150 } 151 else { 152 m_limit_ = limit; 153 } 154 m_current_ = m_start_; 155 } 156 157 159 166 protected UCharacterNameIterator(UCharacterName name, int choice) 167 { 168 if(name==null){ 169 throw new IllegalArgumentException ("UCharacterName name argument cannot be null. Missing unames.icu?"); 170 } 171 m_name_ = name; 172 m_choice_ = choice; 174 m_start_ = UCharacter.MIN_VALUE; 175 m_limit_ = UCharacter.MAX_VALUE + 1; 176 m_current_ = m_start_; 177 } 178 179 181 184 private UCharacterName m_name_; 185 188 private int m_choice_; 189 192 private int m_start_; 193 196 private int m_limit_; 197 200 private int m_current_; 201 204 private int m_groupIndex_ = -1; 205 208 private int m_algorithmIndex_ = -1; 209 212 private static char GROUP_OFFSETS_[] = 213 new char[UCharacterName.LINES_PER_GROUP_ + 1]; 214 private static char GROUP_LENGTHS_[] = 215 new char[UCharacterName.LINES_PER_GROUP_ + 1]; 216 217 219 228 private boolean iterateSingleGroup(ValueIterator.Element result, int limit) 229 { 230 synchronized(GROUP_OFFSETS_) { 231 synchronized(GROUP_LENGTHS_) { 232 int index = m_name_.getGroupLengths(m_groupIndex_, GROUP_OFFSETS_, 233 GROUP_LENGTHS_); 234 while (m_current_ < limit) { 235 int offset = UCharacterName.getGroupOffset(m_current_); 236 String name = m_name_.getGroupName( 237 index + GROUP_OFFSETS_[offset], 238 GROUP_LENGTHS_[offset], m_choice_); 239 if ((name == null || name.length() == 0) && 240 m_choice_ == UCharacterNameChoice.EXTENDED_CHAR_NAME) { 241 name = m_name_.getExtendedName(m_current_); 242 } 243 if (name != null && name.length() > 0) { 244 result.integer = m_current_; 245 result.value = name; 246 return false; 247 } 248 ++ m_current_; 249 } 250 } 251 } 252 return true; 253 } 254 255 264 private boolean iterateGroup(ValueIterator.Element result, int limit) 265 { 266 if (m_groupIndex_ < 0) { 267 m_groupIndex_ = m_name_.getGroup(m_current_); 268 } 269 270 while (m_groupIndex_ < m_name_.m_groupcount_ && 271 m_current_ < limit) { 272 int startMSB = UCharacterName.getCodepointMSB(m_current_); 274 int gMSB = m_name_.getGroupMSB(m_groupIndex_); if (startMSB == gMSB) { 276 if (startMSB == UCharacterName.getCodepointMSB(limit - 1)) { 277 return iterateSingleGroup(result, limit); 280 } 281 if (!iterateSingleGroup(result, 284 UCharacterName.getGroupLimit(gMSB))) { 285 return false; 286 } 287 ++ m_groupIndex_; } 289 else if (startMSB > gMSB) { 290 m_groupIndex_ ++; 293 } 294 else { 295 int gMIN = UCharacterName.getGroupMin(gMSB); 296 if (gMIN > limit) { 297 gMIN = limit; 298 } 299 if (m_choice_ == UCharacterNameChoice.EXTENDED_CHAR_NAME) { 300 if (!iterateExtended(result, gMIN)) { 301 return false; 302 } 303 } 304 m_current_ = gMIN; 305 } 306 } 307 308 return true; 309 } 310 311 319 private boolean iterateExtended(ValueIterator.Element result, 320 int limit) 321 { 322 while (m_current_ < limit) { 323 String name = m_name_.getExtendedOr10Name(m_current_); 324 if (name != null && name.length() > 0) { 325 result.integer = m_current_; 326 result.value = name; 327 return false; 328 } 329 ++ m_current_; 330 } 331 return true; 332 } 333 } 334 | Popular Tags |