選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CharOperation.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /********************************************************************
  2. * Copyright (c) 2006 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - initial version
  10. *******************************************************************/
  11. package org.aspectj.asm.internal;
  12. /**
  13. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  14. *
  15. */
  16. public class CharOperation {
  17. public static final char[][] NO_CHAR_CHAR = new char[0][];
  18. public static final char[] NO_CHAR = new char[0];
  19. /**
  20. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  21. */
  22. public static final char[] subarray(char[] array, int start, int end) {
  23. if (end == -1)
  24. end = array.length;
  25. if (start > end)
  26. return null;
  27. if (start < 0)
  28. return null;
  29. if (end > array.length)
  30. return null;
  31. char[] result = new char[end - start];
  32. System.arraycopy(array, start, result, 0, end - start);
  33. return result;
  34. }
  35. public static final char[][] subarray(char[][] array, int start, int end) {
  36. if (end == -1)
  37. end = array.length;
  38. if (start > end)
  39. return null;
  40. if (start < 0)
  41. return null;
  42. if (end > array.length)
  43. return null;
  44. char[][] result = new char[end - start][];
  45. System.arraycopy(array, start, result, 0, end - start);
  46. return result;
  47. }
  48. public static final char[][] splitOn(char divider, char[] array) {
  49. int length = array == null ? 0 : array.length;
  50. if (length == 0)
  51. return NO_CHAR_CHAR;
  52. int wordCount = 1;
  53. for (int i = 0; i < length; i++)
  54. if (array[i] == divider)
  55. wordCount++;
  56. char[][] split = new char[wordCount][];
  57. int last = 0, currentWord = 0;
  58. for (int i = 0; i < length; i++) {
  59. if (array[i] == divider) {
  60. split[currentWord] = new char[i - last];
  61. System.arraycopy(array, last, split[currentWord++], 0, i - last);
  62. last = i + 1;
  63. }
  64. }
  65. split[currentWord] = new char[length - last];
  66. System.arraycopy(array, last, split[currentWord], 0, length - last);
  67. return split;
  68. }
  69. /**
  70. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  71. */
  72. public static final int lastIndexOf(char toBeFound, char[] array) {
  73. for (int i = array.length; --i >= 0;)
  74. if (toBeFound == array[i])
  75. return i;
  76. return -1;
  77. }
  78. /**
  79. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  80. */
  81. public static final int indexOf(char toBeFound, char[] array) {
  82. for (int i = 0; i < array.length; i++)
  83. if (toBeFound == array[i])
  84. return i;
  85. return -1;
  86. }
  87. /**
  88. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  89. */
  90. public static final char[] concat(char[] first, char[] second) {
  91. if (first == null)
  92. return second;
  93. if (second == null)
  94. return first;
  95. int length1 = first.length;
  96. int length2 = second.length;
  97. char[] result = new char[length1 + length2];
  98. System.arraycopy(first, 0, result, 0, length1);
  99. System.arraycopy(second, 0, result, length1, length2);
  100. return result;
  101. }
  102. /**
  103. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  104. */
  105. public static final boolean equals(char[] first, char[] second) {
  106. if (first == second)
  107. return true;
  108. if (first == null || second == null)
  109. return false;
  110. if (first.length != second.length)
  111. return false;
  112. for (int i = first.length; --i >= 0;)
  113. if (first[i] != second[i])
  114. return false;
  115. return true;
  116. }
  117. final static public String toString(char[][] array) {
  118. char[] result = concatWith(array, '.');
  119. return new String(result);
  120. }
  121. public static final char[] concatWith(char[][] array, char separator) {
  122. int length = array == null ? 0 : array.length;
  123. if (length == 0)
  124. return CharOperation.NO_CHAR;
  125. int size = length - 1;
  126. int index = length;
  127. while (--index >= 0) {
  128. if (array[index].length == 0)
  129. size--;
  130. else
  131. size += array[index].length;
  132. }
  133. if (size <= 0)
  134. return CharOperation.NO_CHAR;
  135. char[] result = new char[size];
  136. index = length;
  137. while (--index >= 0) {
  138. length = array[index].length;
  139. if (length > 0) {
  140. System.arraycopy(array[index], 0, result, (size -= length), length);
  141. if (--size >= 0)
  142. result[size] = separator;
  143. }
  144. }
  145. return result;
  146. }
  147. public static final int hashCode(char[] array) {
  148. int length = array.length;
  149. int hash = length == 0 ? 31 : array[0];
  150. if (length < 8) {
  151. for (int i = length; --i > 0;)
  152. hash = (hash * 31) + array[i];
  153. } else {
  154. // 8 characters is enough to compute a decent hash code, don't waste time examining every character
  155. for (int i = length - 1, last = i > 16 ? i - 16 : 0; i > last; i -= 2)
  156. hash = (hash * 31) + array[i];
  157. }
  158. return hash & 0x7FFFFFFF;
  159. }
  160. public static final boolean equals(char[][] first, char[][] second) {
  161. if (first == second)
  162. return true;
  163. if (first == null || second == null)
  164. return false;
  165. if (first.length != second.length)
  166. return false;
  167. for (int i = first.length; --i >= 0;)
  168. if (!equals(first[i], second[i]))
  169. return false;
  170. return true;
  171. }
  172. /**
  173. * Taken from org.aspectj.org.eclipse.jdt.core.compiler.CharOperation
  174. */
  175. public static final void replace(char[] array, char toBeReplaced, char replacementChar) {
  176. if (toBeReplaced != replacementChar) {
  177. for (int i = 0, max = array.length; i < max; i++) {
  178. if (array[i] == toBeReplaced)
  179. array[i] = replacementChar;
  180. }
  181. }
  182. }
  183. }