You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCStringConverter.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  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. public class IRCStringConverter implements StringConverter {
  28. /** Characters to use when converting to lowercase. */
  29. private final char[] lowercase;
  30. /** Characters to use when converting to uppercase. */
  31. private final char[] uppercase;
  32. /** Encoding to use. */
  33. private final IRCEncoding encoding;
  34. /**
  35. * Create a new IRCStringConverter with rfc1459 encoding.
  36. */
  37. public IRCStringConverter() {
  38. this(IRCEncoding.RFC1459);
  39. }
  40. /**
  41. * Create a new IRCStringConverter.
  42. *
  43. * @param encoding The encoding to use.
  44. */
  45. public IRCStringConverter(final IRCEncoding encoding) {
  46. this.encoding = encoding;
  47. lowercase = new char[127];
  48. uppercase = new char[127];
  49. // Normal Chars
  50. for (char i = 0; i < lowercase.length; ++i) {
  51. lowercase[i] = i;
  52. uppercase[i] = i;
  53. }
  54. // Replace the uppercase chars with lowercase
  55. for (char i = 65; i <= (90 + encoding.getLimit()); ++i) {
  56. lowercase[i] = (char) (i + 32);
  57. uppercase[i + 32] = i;
  58. }
  59. }
  60. /**
  61. * Retrieves the encoding used by this converter.
  62. *
  63. * @return This converter's current encoding
  64. */
  65. public IRCEncoding getEncoding() {
  66. return encoding;
  67. }
  68. @Override
  69. public String toLowerCase(final String input) {
  70. final char[] result = input.toCharArray();
  71. for (int i = 0; i < input.length(); ++i) {
  72. if (result[i] >= 0 && result[i] < lowercase.length) {
  73. result[i] = lowercase[result[i]];
  74. } else {
  75. result[i] = result[i];
  76. }
  77. }
  78. return new String(result);
  79. }
  80. @Override
  81. public String toUpperCase(final String input) {
  82. final char[] result = input.toCharArray();
  83. for (int i = 0; i < input.length(); ++i) {
  84. if (result[i] >= 0 && result[i] < uppercase.length) {
  85. result[i] = uppercase[result[i]];
  86. } else {
  87. result[i] = result[i];
  88. }
  89. }
  90. return new String(result);
  91. }
  92. @Override
  93. public boolean equalsIgnoreCase(final String first, final String second) {
  94. if (first == null && second == null) {
  95. return true;
  96. }
  97. if (first == null || second == null) {
  98. return false;
  99. }
  100. boolean result = first.length() == second.length();
  101. if (result) {
  102. final char[] firstChar = first.toCharArray();
  103. final char[] secondChar = second.toCharArray();
  104. for (int i = 0; i < first.length(); ++i) {
  105. if (firstChar[i] < lowercase.length && secondChar[i] < lowercase.length) {
  106. result = lowercase[firstChar[i]] == lowercase[secondChar[i]];
  107. } else {
  108. result = firstChar[i] == secondChar[i];
  109. }
  110. if (!result) {
  111. break;
  112. }
  113. }
  114. }
  115. return result;
  116. }
  117. }