1 28 29 package com.caucho.util; 30 31 34 public class CharSegment implements CharSequence { 35 protected char []_buffer; 36 protected int _offset; 37 protected int _length; 38 39 public CharSegment() 40 { 41 } 42 43 46 public CharSegment(char []buffer, int offset, int length) 47 { 48 _buffer = buffer; 49 _offset = offset; 50 _length = length; 51 } 52 53 56 public void init(char []buffer, int offset, int length) 57 { 58 _buffer = buffer; 59 _offset = offset; 60 _length = length; 61 } 62 63 66 public int length() 67 { 68 return _length; 69 } 70 71 74 public int getLength() 75 { 76 return _length; 77 } 78 79 public int getOffset() 80 { 81 return _offset; 82 } 83 84 87 public char charAt(int i) 88 { 89 if (i < 0 || _length <= i) 90 throw new IndexOutOfBoundsException (); 91 92 return _buffer[i + _offset]; 93 } 94 95 100 public char getLastChar() 101 { 102 if (_length == 0) 103 throw new IndexOutOfBoundsException (); 104 105 return _buffer[_offset + _length - 1]; 106 } 107 108 111 public char []getBuffer() 112 { 113 return _buffer; 114 } 115 116 119 public void getChars(int srcBegin, int srcEnd, char []dst, int dstBegin) 120 { 121 srcBegin += _offset; 122 srcEnd += _offset; 123 124 char []buffer = _buffer; 125 while (srcBegin < srcEnd) 126 dst[dstBegin++] = buffer[srcBegin++]; 127 } 128 129 132 public String substring(int start) 133 { 134 if (_length < start || start < 0) 135 throw new StringIndexOutOfBoundsException (); 136 137 return new String (_buffer, _offset + start, _length - start); 138 } 139 140 143 public String substring(int start, int end) 144 { 145 if (_length < start || start < 0 || end < start) 146 throw new StringIndexOutOfBoundsException (); 147 148 return new String (_buffer, _offset + start, end - start); 149 } 150 151 154 public CharSequence subSequence(int start, int end) 155 { 156 if (_length < start || start < 0 || end < start) 157 throw new StringIndexOutOfBoundsException (); 158 159 return new String (_buffer, _offset + start, end - start); 160 } 161 162 165 public int indexOf(char ch) 166 { 167 return indexOf(ch, 0); 168 } 169 170 174 public final int indexOf(char ch, int start) 175 { 176 if (start < 0) 177 start = 0; 178 179 int end = _offset + _length; 180 start += _offset; 181 182 char []buffer = _buffer; 183 for (; start < end; start++) { 184 if (buffer[start] == ch) 185 return start - _offset; 186 } 187 188 return -1; 189 } 190 191 194 public final int lastIndexOf(char ch) 195 { 196 return lastIndexOf(ch, _length - 1); 197 } 198 202 203 public final int lastIndexOf(char ch, int start) 204 { 205 if (_length <= start) 206 start = _length - 1; 207 208 char []buffer = _buffer; 209 int offset = _offset; 210 for (; start >= 0; start--) { 211 if (buffer[start + offset] == ch) 212 return start; 213 } 214 215 return -1; 216 } 217 218 public int indexOf(String s) 219 { 220 return indexOf(s, 0); 221 } 222 223 public int indexOf(String s, int start) 224 { 225 int slen = s.length(); 226 227 if (start < 0) 228 start = 0; 229 230 int end = _offset + _length - slen + 1; 231 start += _offset; 232 233 char []buffer = _buffer; 234 for (; start < end; start++) { 235 int i = 0; 236 for (; i < slen; i++) { 237 if (buffer[start+i] != s.charAt(i)) 238 break; 239 } 240 if (i == slen) 241 return start - _offset; 242 } 243 244 return -1; 245 } 246 247 250 public int hashCode() 251 { 252 int hash = 0; 253 char []buffer = _buffer; 254 int begin = _offset; 255 int end = begin + _length; 256 257 for (; begin < end; begin++) 258 hash = 65521 * hash + buffer[begin] * 251 + 1021; 259 260 return hash; 261 } 262 263 266 public final boolean equals(Object a) 267 { 268 if (this == a) 269 return true; 270 271 else if (a instanceof CharSegment) { 272 CharSegment cb = (CharSegment) a; 273 274 int length = _length; 275 if (length != cb._length) 276 return false; 277 278 char []buffer = _buffer; 279 char []aBuffer = cb._buffer; 280 281 int offset = _offset; 282 int aOffset = cb._offset; 283 284 for (int i = length - 1; i >= 0; i--) 285 if (buffer[offset + i] != aBuffer[aOffset + i]) 286 return false; 287 288 return true; 289 } 290 else if (a instanceof CharSequence ) { 291 CharSequence seq = (CharSequence ) a; 292 293 int length = seq.length(); 294 295 if (_length != length) 296 return false; 297 298 for (int i = length - 1; i >= 0; i--) { 299 if (_buffer[i] != seq.charAt(i)) 300 return false; 301 } 302 303 return true; 304 } 305 else 306 return false; 307 } 308 309 312 public boolean equals(CharSegment cb) 313 { 314 int length = _length; 315 if (length != cb._length) 316 return false; 317 318 char []buffer = _buffer; 319 char []aBuffer = cb._buffer; 320 321 int offset = _offset; 322 int aOffset = cb._offset; 323 324 for (int i = length - 1; i >= 0; i--) 325 if (buffer[offset + i] != aBuffer[aOffset + i]) 326 return false; 327 328 return true; 329 } 330 331 334 public final boolean equals(char []cb, int length) 335 { 336 if (length != _length) 337 return false; 338 339 int offset = _offset; 340 char []buffer = _buffer; 341 342 for (int i = _length - 1; i >= 0; i--) 343 if (buffer[offset + i] != cb[i]) 344 return false; 345 346 return true; 347 } 348 349 352 public final boolean equalsIgnoreCase(String a) 353 { 354 int len = a.length(); 355 if (_length != len) 356 return false; 357 358 int offset = _offset; 359 char []buffer = _buffer; 360 361 for (int i = 0; i < len; i++) { 362 char ca = buffer[offset + i]; 363 char cb = a.charAt(i); 364 365 if (ca == cb) { 366 } 367 368 else if (Character.toLowerCase(ca) != Character.toLowerCase(cb)) 369 return false; 370 } 371 372 return true; 373 } 374 375 378 public final boolean equalsIgnoreCase(CharSegment b) 379 { 380 int length = _length; 381 if (length != b._length) 382 return false; 383 384 char []buffer = _buffer; 385 char []bBuffer = b._buffer; 386 387 int offset = _offset; 388 int bOffset = b._offset; 389 390 for (int i = length - 1; i >= 0; i--) { 391 char ca = buffer[offset + i]; 392 char cb = bBuffer[bOffset + i]; 393 394 if (ca != cb && 395 Character.toLowerCase(ca) != Character.toLowerCase(cb)) 396 return false; 397 } 398 return true; 399 } 400 401 404 public final boolean matches(Object a) 405 { 406 if (a instanceof CharSegment) { 407 CharSegment cb = (CharSegment) a; 408 if (_length != cb._length) 409 return false; 410 411 int offset = _offset; 412 int bOffset = cb._offset; 413 414 char []buffer = _buffer; 415 char []cbBuffer = cb._buffer; 416 417 for (int i = _length - 1; i >= 0; i--) 418 if (buffer[offset + i] != cbBuffer[bOffset + i]) 419 return false; 420 421 return true; 422 } else if (a instanceof String ) { 423 String sa = (String ) a; 424 425 if (_length != sa.length()) 426 return false; 427 428 int offset = _offset; 429 char []buffer = _buffer; 430 431 for (int i = _length - 1; i >= 0; i--) 432 if (buffer[i + offset] != sa.charAt(i)) 433 return false; 434 435 return true; 436 } else 437 return false; 438 } 439 440 443 public boolean matches(String sa) 444 { 445 if (_length != sa.length()) 446 return false; 447 448 char []buffer = _buffer; 449 int offset = _offset; 450 451 for (int i = _length - 1; i >= 0; i--) 452 if (_buffer[_offset + i] != sa.charAt(i)) 453 return false; 454 455 return true; 456 } 457 458 461 public boolean matchesIgnoreCase(String sa) 462 { 463 if (_length != sa.length()) 464 return false; 465 466 char []buffer = _buffer; 467 int offset = _offset; 468 for (int i = _length - 1; i >= 0; i--) { 469 char ca = buffer[offset + i]; 470 char cb = sa.charAt(i); 471 472 if (ca != cb && Character.toLowerCase(ca) != Character.toLowerCase(cb)) 473 return false; 474 } 475 476 return true; 477 } 478 479 public boolean regionMatches(int off1, CharSegment buf, int off2, int len) 480 { 481 if (_length < off1 + len || buf._length < off2 + len) 482 return false; 483 484 char []buffer = _buffer; 485 char []bufBuffer = buf._buffer; 486 for (int i = len - 1; i >= 0; i--) { 487 if (buffer[off1 + i] != bufBuffer[off2 + i]) 488 return false; 489 } 490 491 return true; 492 } 493 494 public boolean regionMatches(int off1, String buf, int off2, int len) 495 { 496 if (_length < off1 + len || buf.length() < off2 + len) 497 return false; 498 499 char []buffer = _buffer; 500 for (int i = 0; i < len; i++) { 501 if (buffer[off1 + i] != buf.charAt(off2 + i)) 502 return false; 503 } 504 505 return true; 506 } 507 508 public boolean regionMatchesIgnoreCase(int off1, CharSegment buf, 509 int off2, int len) 510 { 511 if (_length < off1 + len || buf._length < off2 + len) 512 return false; 513 514 char []buffer = _buffer; 515 char []bufBuffer = buf._buffer; 516 for (int i = len -1; i >= 0; i--) { 517 if (Character.toLowerCase(buffer[off1 + i]) != 518 Character.toLowerCase(bufBuffer[off2 + i])) 519 return false; 520 } 521 522 return true; 523 } 524 525 528 public boolean startsWith(String string) 529 { 530 if (string == null) 531 return false; 532 533 int strlen = string.length(); 534 if (_length < strlen) 535 return false; 536 537 char []buffer = _buffer; 538 int offset = _offset; 539 540 while (--strlen >= 0) { 541 if (buffer[offset + strlen] != string.charAt(strlen)) 542 return false; 543 } 544 545 return true; 546 } 547 548 551 public boolean endsWith(String string) 552 { 553 if (string == null) 554 return false; 555 556 int strlen = string.length(); 557 if (_length < strlen) 558 return false; 559 560 char []buffer = _buffer; 561 int offset = _offset + _length - strlen; 562 563 while (--strlen >= 0) { 564 if (buffer[offset + strlen] != string.charAt(strlen)) 565 return false; 566 } 567 568 return true; 569 } 570 571 574 public boolean endsWith(CharSegment cb) 575 { 576 if (cb == null) 577 return false; 578 579 int strlen = cb._length; 580 if (_length < strlen) 581 return false; 582 583 char []buffer = _buffer; 584 int offset = _offset + _length - strlen; 585 586 char []cbBuffer = cb._buffer; 587 int cbOffset = cb._offset; 588 589 while (--strlen >= 0) { 590 if (buffer[offset + strlen] != cbBuffer[cbOffset + strlen]) 591 return false; 592 } 593 594 return true; 595 } 596 597 600 public CharSegment toLowerCase() 601 { 602 char []buffer = _buffer; 603 int len = _length; 604 int offset = _offset; 605 606 while (--len >= 0) 607 buffer[offset + len] = Character.toLowerCase(buffer[offset + len]); 608 609 return this; 610 } 611 612 615 public String toString() 616 { 617 return new String (_buffer, _offset, _length); 618 } 619 } 620 | Popular Tags |