Java IRC bot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCStringConverter.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  24. * IRC String Converter.
  25. *
  26. * @author Shane Mc Cormack
  27. */
  28. public class IRCStringConverter {
  29. /** Characters to use when converting tolowercase. */
  30. private final char[] lowercase;
  31. /** Characters to use when converting touppercase. */
  32. private final char[] uppercase;
  33. /** limit */
  34. private final byte limit;
  35. /**
  36. * Create a new IRCStringConverter with rfc1459 encoding.
  37. */
  38. public IRCStringConverter() {
  39. this((byte)4);
  40. }
  41. /**
  42. * Create a new IRCStringConverter.
  43. * @param limit Number of post-alphabetical characters to convert
  44. * 0 = ascii encoding
  45. * 3 = strict-rfc1459 encoding
  46. * 4 = rfc1459 encoding
  47. */
  48. public IRCStringConverter(final byte limit) {
  49. // If limit is out side the boundries, use rfc1459
  50. if (limit > 4 || limit < 0 ) { this.limit = (byte)4; }
  51. else { this.limit = limit; }
  52. lowercase = new char[127];
  53. uppercase = new char[127];
  54. // Normal Chars
  55. for (char i = 0; i < lowercase.length; ++i) {
  56. lowercase[i] = i;
  57. uppercase[i] = i;
  58. }
  59. // Replace the uppercase chars with lowercase
  60. for (char i = 65; i <= (90 + this.limit); ++i) {
  61. lowercase[i] = (char)(i + 32);
  62. uppercase[i + 32] = i;
  63. }
  64. }
  65. /**
  66. * Get last used chararray limit.
  67. *
  68. * @return last used chararray limit
  69. */
  70. protected int getLimit() { return limit; }
  71. /**
  72. * Get the lowercase version of a String for this Server.
  73. *
  74. * @param input String to convert lowercase
  75. * @return input String converterd to lowercase
  76. */
  77. public String toLowerCase(final String input) {
  78. final char[] result = input.toCharArray();
  79. for (int i = 0; i < input.length(); ++i) {
  80. if (result[i] >= 0 && result[i] < lowercase.length) {
  81. result[i] = lowercase[result[i]];
  82. } else {
  83. result[i] = result[i];
  84. }
  85. }
  86. return new String(result);
  87. }
  88. /**
  89. * Get the uppercase version of a String for this Server.
  90. *
  91. * @param input String to convert uppercase
  92. * @return input String converterd to uppercase
  93. */
  94. public String toUpperCase(final String input) {
  95. final char[] result = input.toCharArray();
  96. for (int i = 0; i < input.length(); ++i) {
  97. if (result[i] >= 0 && result[i] < uppercase.length) {
  98. result[i] = uppercase[result[i]];
  99. } else {
  100. result[i] = result[i];
  101. }
  102. }
  103. return new String(result);
  104. }
  105. /**
  106. * Check if 2 strings are equal to each other ignoring case.
  107. *
  108. * @param first First string to check
  109. * @param second Second string to check
  110. * @return True if both strings are equal after being lowercased
  111. */
  112. public boolean equalsIgnoreCase(final String first, final String second) {
  113. if (first == null && second == null) { return true; }
  114. if (first == null || second == null) { return false; }
  115. boolean result = (first.length() == second.length());
  116. if (result) {
  117. final char[] firstChar = first.toCharArray();
  118. final char[] secondChar = second.toCharArray();
  119. for (int i = 0; i < first.length(); ++i) {
  120. if (firstChar[i] < lowercase.length && secondChar[i] < lowercase.length) {
  121. result = (lowercase[firstChar[i]] == lowercase[secondChar[i]]);
  122. } else {
  123. result = firstChar[i] == secondChar[i];
  124. }
  125. if (!result) { break; }
  126. }
  127. }
  128. return result;
  129. }
  130. }