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ů.

RawTextComparator.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * Copyright (C) 2008-2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.diff;
  45. import static org.eclipse.jgit.util.RawCharUtil.isWhitespace;
  46. import static org.eclipse.jgit.util.RawCharUtil.trimLeadingWhitespace;
  47. import static org.eclipse.jgit.util.RawCharUtil.trimTrailingWhitespace;
  48. /** Equivalence function for {@link RawText}. */
  49. public abstract class RawTextComparator extends SequenceComparator<RawText> {
  50. /** No special treatment. */
  51. public static final RawTextComparator DEFAULT = new RawTextComparator() {
  52. @Override
  53. public boolean equals(RawText a, int ai, RawText b, int bi) {
  54. ai++;
  55. bi++;
  56. if (a.hashes[ai] != b.hashes[bi])
  57. return false;
  58. int as = a.lines.get(ai);
  59. int bs = b.lines.get(bi);
  60. final int ae = a.lines.get(ai + 1);
  61. final int be = b.lines.get(bi + 1);
  62. if (ae - as != be - bs)
  63. return false;
  64. while (as < ae) {
  65. if (a.content[as++] != b.content[bs++])
  66. return false;
  67. }
  68. return true;
  69. }
  70. @Override
  71. public int hashRegion(final byte[] raw, int ptr, final int end) {
  72. int hash = 5381;
  73. for (; ptr < end; ptr++)
  74. hash = (hash << 5) ^ (raw[ptr] & 0xff);
  75. return hash;
  76. }
  77. };
  78. /** Ignores all whitespace. */
  79. public static final RawTextComparator WS_IGNORE_ALL = new RawTextComparator() {
  80. @Override
  81. public boolean equals(RawText a, int ai, RawText b, int bi) {
  82. ai++;
  83. bi++;
  84. if (a.hashes[ai] != b.hashes[bi])
  85. return false;
  86. int as = a.lines.get(ai);
  87. int bs = b.lines.get(bi);
  88. int ae = a.lines.get(ai + 1);
  89. int be = b.lines.get(bi + 1);
  90. ae = trimTrailingWhitespace(a.content, as, ae);
  91. be = trimTrailingWhitespace(b.content, bs, be);
  92. while (as < ae && bs < be) {
  93. byte ac = a.content[as];
  94. byte bc = b.content[bs];
  95. while (as < ae - 1 && isWhitespace(ac)) {
  96. as++;
  97. ac = a.content[as];
  98. }
  99. while (bs < be - 1 && isWhitespace(bc)) {
  100. bs++;
  101. bc = b.content[bs];
  102. }
  103. if (ac != bc)
  104. return false;
  105. as++;
  106. bs++;
  107. }
  108. return as == ae && bs == be;
  109. }
  110. @Override
  111. public int hashRegion(byte[] raw, int ptr, int end) {
  112. int hash = 5381;
  113. for (; ptr < end; ptr++) {
  114. byte c = raw[ptr];
  115. if (!isWhitespace(c))
  116. hash = (hash << 5) ^ (c & 0xff);
  117. }
  118. return hash;
  119. }
  120. };
  121. /** Ignores leading whitespace. */
  122. public static final RawTextComparator WS_IGNORE_LEADING = new RawTextComparator() {
  123. @Override
  124. public boolean equals(RawText a, int ai, RawText b, int bi) {
  125. ai++;
  126. bi++;
  127. if (a.hashes[ai] != b.hashes[bi])
  128. return false;
  129. int as = a.lines.get(ai);
  130. int bs = b.lines.get(bi);
  131. int ae = a.lines.get(ai + 1);
  132. int be = b.lines.get(bi + 1);
  133. as = trimLeadingWhitespace(a.content, as, ae);
  134. bs = trimLeadingWhitespace(b.content, bs, be);
  135. if (ae - as != be - bs)
  136. return false;
  137. while (as < ae) {
  138. if (a.content[as++] != b.content[bs++])
  139. return false;
  140. }
  141. return true;
  142. }
  143. @Override
  144. public int hashRegion(final byte[] raw, int ptr, int end) {
  145. int hash = 5381;
  146. ptr = trimLeadingWhitespace(raw, ptr, end);
  147. for (; ptr < end; ptr++) {
  148. hash = (hash << 5) ^ (raw[ptr] & 0xff);
  149. }
  150. return hash;
  151. }
  152. };
  153. /** Ignores trailing whitespace. */
  154. public static final RawTextComparator WS_IGNORE_TRAILING = new RawTextComparator() {
  155. @Override
  156. public boolean equals(RawText a, int ai, RawText b, int bi) {
  157. ai++;
  158. bi++;
  159. if (a.hashes[ai] != b.hashes[bi])
  160. return false;
  161. int as = a.lines.get(ai);
  162. int bs = b.lines.get(bi);
  163. int ae = a.lines.get(ai + 1);
  164. int be = b.lines.get(bi + 1);
  165. ae = trimTrailingWhitespace(a.content, as, ae);
  166. be = trimTrailingWhitespace(b.content, bs, be);
  167. if (ae - as != be - bs)
  168. return false;
  169. while (as < ae) {
  170. if (a.content[as++] != b.content[bs++])
  171. return false;
  172. }
  173. return true;
  174. }
  175. @Override
  176. public int hashRegion(final byte[] raw, int ptr, int end) {
  177. int hash = 5381;
  178. end = trimTrailingWhitespace(raw, ptr, end);
  179. for (; ptr < end; ptr++) {
  180. hash = (hash << 5) ^ (raw[ptr] & 0xff);
  181. }
  182. return hash;
  183. }
  184. };
  185. /** Ignores whitespace occurring between non-whitespace characters. */
  186. public static final RawTextComparator WS_IGNORE_CHANGE = new RawTextComparator() {
  187. @Override
  188. public boolean equals(RawText a, int ai, RawText b, int bi) {
  189. ai++;
  190. bi++;
  191. if (a.hashes[ai] != b.hashes[bi])
  192. return false;
  193. int as = a.lines.get(ai);
  194. int bs = b.lines.get(bi);
  195. int ae = a.lines.get(ai + 1);
  196. int be = b.lines.get(bi + 1);
  197. ae = trimTrailingWhitespace(a.content, as, ae);
  198. be = trimTrailingWhitespace(b.content, bs, be);
  199. while (as < ae && bs < be) {
  200. byte ac = a.content[as];
  201. byte bc = b.content[bs];
  202. if (ac != bc)
  203. return false;
  204. if (isWhitespace(ac))
  205. as = trimLeadingWhitespace(a.content, as, ae);
  206. else
  207. as++;
  208. if (isWhitespace(bc))
  209. bs = trimLeadingWhitespace(b.content, bs, be);
  210. else
  211. bs++;
  212. }
  213. return as == ae && bs == be;
  214. }
  215. @Override
  216. public int hashRegion(final byte[] raw, int ptr, int end) {
  217. int hash = 5381;
  218. end = trimTrailingWhitespace(raw, ptr, end);
  219. while (ptr < end) {
  220. byte c = raw[ptr];
  221. hash = (hash << 5) ^ (c & 0xff);
  222. if (isWhitespace(c))
  223. ptr = trimLeadingWhitespace(raw, ptr, end);
  224. else
  225. ptr++;
  226. }
  227. return hash;
  228. }
  229. };
  230. @Override
  231. public int hash(RawText seq, int ptr) {
  232. return seq.hashes[ptr + 1];
  233. }
  234. /**
  235. * Compute a hash code for a region.
  236. *
  237. * @param raw
  238. * the raw file content.
  239. * @param ptr
  240. * first byte of the region to hash.
  241. * @param end
  242. * 1 past the last byte of the region.
  243. * @return hash code for the region <code>[ptr, end)</code> of raw.
  244. */
  245. public abstract int hashRegion(byte[] raw, int ptr, int end);
  246. }