您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IRCStringConverter.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import com.dmdirc.parser.interfaces.StringConverter;
  24. /**
  25. * IRC String Converter.
  26. *
  27. * @author Shane Mc Cormack
  28. */
  29. public class IRCStringConverter implements StringConverter {
  30. /** Characters to use when converting tolowercase. */
  31. private final char[] lowercase;
  32. /** Characters to use when converting touppercase. */
  33. private final char[] uppercase;
  34. /** limit */
  35. private final byte limit;
  36. /**
  37. * Create a new IRCStringConverter with rfc1459 encoding.
  38. */
  39. public IRCStringConverter() {
  40. this((byte)4);
  41. }
  42. /**
  43. * Create a new IRCStringConverter.
  44. * @param limit Number of post-alphabetical characters to convert
  45. * 0 = ascii encoding
  46. * 3 = strict-rfc1459 encoding
  47. * 4 = rfc1459 encoding
  48. */
  49. public IRCStringConverter(final byte limit) {
  50. // If limit is out side the boundries, use rfc1459
  51. if (limit > 4 || limit < 0 ) { this.limit = (byte)4; }
  52. else { this.limit = limit; }
  53. lowercase = new char[127];
  54. uppercase = new char[127];
  55. // Normal Chars
  56. for (char i = 0; i < lowercase.length; ++i) {
  57. lowercase[i] = i;
  58. uppercase[i] = i;
  59. }
  60. // Replace the uppercase chars with lowercase
  61. for (char i = 65; i <= (90 + this.limit); ++i) {
  62. lowercase[i] = (char)(i + 32);
  63. uppercase[i + 32] = i;
  64. }
  65. }
  66. /**
  67. * Get last used chararray limit.
  68. *
  69. * @return last used chararray limit
  70. */
  71. protected int getLimit() { return limit; }
  72. /** {@inheritDoc} */
  73. @Override
  74. public String toLowerCase(final String input) {
  75. final char[] result = input.toCharArray();
  76. for (int i = 0; i < input.length(); ++i) {
  77. if (result[i] >= 0 && result[i] < lowercase.length) {
  78. result[i] = lowercase[result[i]];
  79. } else {
  80. result[i] = result[i];
  81. }
  82. }
  83. return new String(result);
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public String toUpperCase(final String input) {
  88. final char[] result = input.toCharArray();
  89. for (int i = 0; i < input.length(); ++i) {
  90. if (result[i] >= 0 && result[i] < uppercase.length) {
  91. result[i] = uppercase[result[i]];
  92. } else {
  93. result[i] = result[i];
  94. }
  95. }
  96. return new String(result);
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public boolean equalsIgnoreCase(final String first, final String second) {
  101. if (first == null && second == null) { return true; }
  102. if (first == null || second == null) { return false; }
  103. boolean result = (first.length() == second.length());
  104. if (result) {
  105. final char[] firstChar = first.toCharArray();
  106. final char[] secondChar = second.toCharArray();
  107. for (int i = 0; i < first.length(); ++i) {
  108. if (firstChar[i] < lowercase.length && secondChar[i] < lowercase.length) {
  109. result = (lowercase[firstChar[i]] == lowercase[secondChar[i]]);
  110. } else {
  111. result = firstChar[i] == secondChar[i];
  112. }
  113. if (!result) { break; }
  114. }
  115. }
  116. return result;
  117. }
  118. }