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.

FileHeader.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.patch;
  44. import static org.eclipse.jgit.lib.Constants.encodeASCII;
  45. import static org.eclipse.jgit.util.RawParseUtils.decode;
  46. import static org.eclipse.jgit.util.RawParseUtils.decodeNoFallback;
  47. import static org.eclipse.jgit.util.RawParseUtils.extractBinaryString;
  48. import static org.eclipse.jgit.util.RawParseUtils.match;
  49. import static org.eclipse.jgit.util.RawParseUtils.nextLF;
  50. import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
  51. import java.io.IOException;
  52. import java.nio.charset.CharacterCodingException;
  53. import java.nio.charset.Charset;
  54. import java.text.MessageFormat;
  55. import java.util.ArrayList;
  56. import java.util.Collections;
  57. import java.util.List;
  58. import org.eclipse.jgit.diff.DiffEntry;
  59. import org.eclipse.jgit.diff.EditList;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.FileMode;
  64. import org.eclipse.jgit.util.QuotedString;
  65. import org.eclipse.jgit.util.RawParseUtils;
  66. import org.eclipse.jgit.util.TemporaryBuffer;
  67. /** Patch header describing an action for a single file path. */
  68. public class FileHeader extends DiffEntry {
  69. private static final byte[] OLD_MODE = encodeASCII("old mode "); //$NON-NLS-1$
  70. private static final byte[] NEW_MODE = encodeASCII("new mode "); //$NON-NLS-1$
  71. static final byte[] DELETED_FILE_MODE = encodeASCII("deleted file mode "); //$NON-NLS-1$
  72. static final byte[] NEW_FILE_MODE = encodeASCII("new file mode "); //$NON-NLS-1$
  73. private static final byte[] COPY_FROM = encodeASCII("copy from "); //$NON-NLS-1$
  74. private static final byte[] COPY_TO = encodeASCII("copy to "); //$NON-NLS-1$
  75. private static final byte[] RENAME_OLD = encodeASCII("rename old "); //$NON-NLS-1$
  76. private static final byte[] RENAME_NEW = encodeASCII("rename new "); //$NON-NLS-1$
  77. private static final byte[] RENAME_FROM = encodeASCII("rename from "); //$NON-NLS-1$
  78. private static final byte[] RENAME_TO = encodeASCII("rename to "); //$NON-NLS-1$
  79. private static final byte[] SIMILARITY_INDEX = encodeASCII("similarity index "); //$NON-NLS-1$
  80. private static final byte[] DISSIMILARITY_INDEX = encodeASCII("dissimilarity index "); //$NON-NLS-1$
  81. static final byte[] INDEX = encodeASCII("index "); //$NON-NLS-1$
  82. static final byte[] OLD_NAME = encodeASCII("--- "); //$NON-NLS-1$
  83. static final byte[] NEW_NAME = encodeASCII("+++ "); //$NON-NLS-1$
  84. /** Type of patch used by this file. */
  85. public static enum PatchType {
  86. /** A traditional unified diff style patch of a text file. */
  87. UNIFIED,
  88. /** An empty patch with a message "Binary files ... differ" */
  89. BINARY,
  90. /** A Git binary patch, holding pre and post image deltas */
  91. GIT_BINARY;
  92. }
  93. /** Buffer holding the patch data for this file. */
  94. final byte[] buf;
  95. /** Offset within {@link #buf} to the "diff ..." line. */
  96. final int startOffset;
  97. /** Position 1 past the end of this file within {@link #buf}. */
  98. int endOffset;
  99. /** Type of patch used to modify this file */
  100. PatchType patchType;
  101. /** The hunks of this file */
  102. private List<HunkHeader> hunks;
  103. /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the new image */
  104. BinaryHunk forwardBinaryHunk;
  105. /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the old image */
  106. BinaryHunk reverseBinaryHunk;
  107. /**
  108. * Constructs a new FileHeader
  109. *
  110. * @param headerLines
  111. * buffer holding the diff header for this file
  112. * @param edits
  113. * the edits for this file
  114. * @param type
  115. * the type of patch used to modify this file
  116. */
  117. public FileHeader(final byte[] headerLines, EditList edits, PatchType type) {
  118. this(headerLines, 0);
  119. endOffset = headerLines.length;
  120. int ptr = parseGitFileName(Patch.DIFF_GIT.length, headerLines.length);
  121. parseGitHeaders(ptr, headerLines.length);
  122. this.patchType = type;
  123. addHunk(new HunkHeader(this, edits));
  124. }
  125. FileHeader(final byte[] b, final int offset) {
  126. buf = b;
  127. startOffset = offset;
  128. changeType = ChangeType.MODIFY; // unless otherwise designated
  129. patchType = PatchType.UNIFIED;
  130. }
  131. int getParentCount() {
  132. return 1;
  133. }
  134. /** @return the byte array holding this file's patch script. */
  135. public byte[] getBuffer() {
  136. return buf;
  137. }
  138. /** @return offset the start of this file's script in {@link #getBuffer()}. */
  139. public int getStartOffset() {
  140. return startOffset;
  141. }
  142. /** @return offset one past the end of the file script. */
  143. public int getEndOffset() {
  144. return endOffset;
  145. }
  146. /**
  147. * Convert the patch script for this file into a string.
  148. * <p>
  149. * The default character encoding ({@link Constants#CHARSET}) is assumed for
  150. * both the old and new files.
  151. *
  152. * @return the patch script, as a Unicode string.
  153. */
  154. public String getScriptText() {
  155. return getScriptText(null, null);
  156. }
  157. /**
  158. * Convert the patch script for this file into a string.
  159. *
  160. * @param oldCharset
  161. * hint character set to decode the old lines with.
  162. * @param newCharset
  163. * hint character set to decode the new lines with.
  164. * @return the patch script, as a Unicode string.
  165. */
  166. public String getScriptText(Charset oldCharset, Charset newCharset) {
  167. return getScriptText(new Charset[] { oldCharset, newCharset });
  168. }
  169. String getScriptText(Charset[] charsetGuess) {
  170. if (getHunks().isEmpty()) {
  171. // If we have no hunks then we can safely assume the entire
  172. // patch is a binary style patch, or a meta-data only style
  173. // patch. Either way the encoding of the headers should be
  174. // strictly 7-bit US-ASCII and the body is either 7-bit ASCII
  175. // (due to the base 85 encoding used for a BinaryHunk) or is
  176. // arbitrary noise we have chosen to ignore and not understand
  177. // (e.g. the message "Binary files ... differ").
  178. //
  179. return extractBinaryString(buf, startOffset, endOffset);
  180. }
  181. if (charsetGuess != null && charsetGuess.length != getParentCount() + 1)
  182. throw new IllegalArgumentException(MessageFormat.format(
  183. JGitText.get().expectedCharacterEncodingGuesses,
  184. Integer.valueOf(getParentCount() + 1)));
  185. if (trySimpleConversion(charsetGuess)) {
  186. Charset cs = charsetGuess != null ? charsetGuess[0] : null;
  187. if (cs == null)
  188. cs = Constants.CHARSET;
  189. try {
  190. return decodeNoFallback(cs, buf, startOffset, endOffset);
  191. } catch (CharacterCodingException cee) {
  192. // Try the much slower, more-memory intensive version which
  193. // can handle a character set conversion patch.
  194. }
  195. }
  196. final StringBuilder r = new StringBuilder(endOffset - startOffset);
  197. // Always treat the headers as US-ASCII; Git file names are encoded
  198. // in a C style escape if any character has the high-bit set.
  199. //
  200. final int hdrEnd = getHunks().get(0).getStartOffset();
  201. for (int ptr = startOffset; ptr < hdrEnd;) {
  202. final int eol = Math.min(hdrEnd, nextLF(buf, ptr));
  203. r.append(extractBinaryString(buf, ptr, eol));
  204. ptr = eol;
  205. }
  206. final String[] files = extractFileLines(charsetGuess);
  207. final int[] offsets = new int[files.length];
  208. for (final HunkHeader h : getHunks())
  209. h.extractFileLines(r, files, offsets);
  210. return r.toString();
  211. }
  212. private static boolean trySimpleConversion(final Charset[] charsetGuess) {
  213. if (charsetGuess == null)
  214. return true;
  215. for (int i = 1; i < charsetGuess.length; i++) {
  216. if (charsetGuess[i] != charsetGuess[0])
  217. return false;
  218. }
  219. return true;
  220. }
  221. private String[] extractFileLines(final Charset[] csGuess) {
  222. final TemporaryBuffer[] tmp = new TemporaryBuffer[getParentCount() + 1];
  223. try {
  224. for (int i = 0; i < tmp.length; i++)
  225. tmp[i] = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  226. for (final HunkHeader h : getHunks())
  227. h.extractFileLines(tmp);
  228. final String[] r = new String[tmp.length];
  229. for (int i = 0; i < tmp.length; i++) {
  230. Charset cs = csGuess != null ? csGuess[i] : null;
  231. if (cs == null)
  232. cs = Constants.CHARSET;
  233. r[i] = RawParseUtils.decode(cs, tmp[i].toByteArray());
  234. }
  235. return r;
  236. } catch (IOException ioe) {
  237. throw new RuntimeException(JGitText.get().cannotConvertScriptToText, ioe);
  238. }
  239. }
  240. /** @return style of patch used to modify this file */
  241. public PatchType getPatchType() {
  242. return patchType;
  243. }
  244. /** @return true if this patch modifies metadata about a file */
  245. public boolean hasMetaDataChanges() {
  246. return changeType != ChangeType.MODIFY || newMode != oldMode;
  247. }
  248. /** @return hunks altering this file; in order of appearance in patch */
  249. public List<? extends HunkHeader> getHunks() {
  250. if (hunks == null)
  251. return Collections.emptyList();
  252. return hunks;
  253. }
  254. void addHunk(final HunkHeader h) {
  255. if (h.getFileHeader() != this)
  256. throw new IllegalArgumentException(JGitText.get().hunkBelongsToAnotherFile);
  257. if (hunks == null)
  258. hunks = new ArrayList<HunkHeader>();
  259. hunks.add(h);
  260. }
  261. HunkHeader newHunkHeader(final int offset) {
  262. return new HunkHeader(this, offset);
  263. }
  264. /** @return if a {@link PatchType#GIT_BINARY}, the new-image delta/literal */
  265. public BinaryHunk getForwardBinaryHunk() {
  266. return forwardBinaryHunk;
  267. }
  268. /** @return if a {@link PatchType#GIT_BINARY}, the old-image delta/literal */
  269. public BinaryHunk getReverseBinaryHunk() {
  270. return reverseBinaryHunk;
  271. }
  272. /** @return a list describing the content edits performed on this file. */
  273. public EditList toEditList() {
  274. final EditList r = new EditList();
  275. for (final HunkHeader hunk : hunks)
  276. r.addAll(hunk.toEditList());
  277. return r;
  278. }
  279. /**
  280. * Parse a "diff --git" or "diff --cc" line.
  281. *
  282. * @param ptr
  283. * first character after the "diff --git " or "diff --cc " part.
  284. * @param end
  285. * one past the last position to parse.
  286. * @return first character after the LF at the end of the line; -1 on error.
  287. */
  288. int parseGitFileName(int ptr, final int end) {
  289. final int eol = nextLF(buf, ptr);
  290. final int bol = ptr;
  291. if (eol >= end) {
  292. return -1;
  293. }
  294. // buffer[ptr..eol] looks like "a/foo b/foo\n". After the first
  295. // A regex to match this is "^[^/]+/(.*?) [^/+]+/\1\n$". There
  296. // is only one way to split the line such that text to the left
  297. // of the space matches the text to the right, excluding the part
  298. // before the first slash.
  299. //
  300. final int aStart = nextLF(buf, ptr, '/');
  301. if (aStart >= eol)
  302. return eol;
  303. while (ptr < eol) {
  304. final int sp = nextLF(buf, ptr, ' ');
  305. if (sp >= eol) {
  306. // We can't split the header, it isn't valid.
  307. // This may be OK if this is a rename patch.
  308. //
  309. return eol;
  310. }
  311. final int bStart = nextLF(buf, sp, '/');
  312. if (bStart >= eol)
  313. return eol;
  314. // If buffer[aStart..sp - 1] = buffer[bStart..eol - 1]
  315. // we have a valid split.
  316. //
  317. if (eq(aStart, sp - 1, bStart, eol - 1)) {
  318. if (buf[bol] == '"') {
  319. // We're a double quoted name. The region better end
  320. // in a double quote too, and we need to decode the
  321. // characters before reading the name.
  322. //
  323. if (buf[sp - 2] != '"') {
  324. return eol;
  325. }
  326. oldPath = QuotedString.GIT_PATH.dequote(buf, bol, sp - 1);
  327. oldPath = p1(oldPath);
  328. } else {
  329. oldPath = decode(Constants.CHARSET, buf, aStart, sp - 1);
  330. }
  331. newPath = oldPath;
  332. return eol;
  333. }
  334. // This split wasn't correct. Move past the space and try
  335. // another split as the space must be part of the file name.
  336. //
  337. ptr = sp;
  338. }
  339. return eol;
  340. }
  341. int parseGitHeaders(int ptr, final int end) {
  342. while (ptr < end) {
  343. final int eol = nextLF(buf, ptr);
  344. if (isHunkHdr(buf, ptr, eol) >= 1) {
  345. // First hunk header; break out and parse them later.
  346. break;
  347. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  348. parseOldName(ptr, eol);
  349. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  350. parseNewName(ptr, eol);
  351. } else if (match(buf, ptr, OLD_MODE) >= 0) {
  352. oldMode = parseFileMode(ptr + OLD_MODE.length, eol);
  353. } else if (match(buf, ptr, NEW_MODE) >= 0) {
  354. newMode = parseFileMode(ptr + NEW_MODE.length, eol);
  355. } else if (match(buf, ptr, DELETED_FILE_MODE) >= 0) {
  356. oldMode = parseFileMode(ptr + DELETED_FILE_MODE.length, eol);
  357. newMode = FileMode.MISSING;
  358. changeType = ChangeType.DELETE;
  359. } else if (match(buf, ptr, NEW_FILE_MODE) >= 0) {
  360. parseNewFileMode(ptr, eol);
  361. } else if (match(buf, ptr, COPY_FROM) >= 0) {
  362. oldPath = parseName(oldPath, ptr + COPY_FROM.length, eol);
  363. changeType = ChangeType.COPY;
  364. } else if (match(buf, ptr, COPY_TO) >= 0) {
  365. newPath = parseName(newPath, ptr + COPY_TO.length, eol);
  366. changeType = ChangeType.COPY;
  367. } else if (match(buf, ptr, RENAME_OLD) >= 0) {
  368. oldPath = parseName(oldPath, ptr + RENAME_OLD.length, eol);
  369. changeType = ChangeType.RENAME;
  370. } else if (match(buf, ptr, RENAME_NEW) >= 0) {
  371. newPath = parseName(newPath, ptr + RENAME_NEW.length, eol);
  372. changeType = ChangeType.RENAME;
  373. } else if (match(buf, ptr, RENAME_FROM) >= 0) {
  374. oldPath = parseName(oldPath, ptr + RENAME_FROM.length, eol);
  375. changeType = ChangeType.RENAME;
  376. } else if (match(buf, ptr, RENAME_TO) >= 0) {
  377. newPath = parseName(newPath, ptr + RENAME_TO.length, eol);
  378. changeType = ChangeType.RENAME;
  379. } else if (match(buf, ptr, SIMILARITY_INDEX) >= 0) {
  380. score = parseBase10(buf, ptr + SIMILARITY_INDEX.length, null);
  381. } else if (match(buf, ptr, DISSIMILARITY_INDEX) >= 0) {
  382. score = parseBase10(buf, ptr + DISSIMILARITY_INDEX.length, null);
  383. } else if (match(buf, ptr, INDEX) >= 0) {
  384. parseIndexLine(ptr + INDEX.length, eol);
  385. } else {
  386. // Probably an empty patch (stat dirty).
  387. break;
  388. }
  389. ptr = eol;
  390. }
  391. return ptr;
  392. }
  393. void parseOldName(int ptr, final int eol) {
  394. oldPath = p1(parseName(oldPath, ptr + OLD_NAME.length, eol));
  395. if (oldPath == DEV_NULL)
  396. changeType = ChangeType.ADD;
  397. }
  398. void parseNewName(int ptr, final int eol) {
  399. newPath = p1(parseName(newPath, ptr + NEW_NAME.length, eol));
  400. if (newPath == DEV_NULL)
  401. changeType = ChangeType.DELETE;
  402. }
  403. void parseNewFileMode(int ptr, final int eol) {
  404. oldMode = FileMode.MISSING;
  405. newMode = parseFileMode(ptr + NEW_FILE_MODE.length, eol);
  406. changeType = ChangeType.ADD;
  407. }
  408. int parseTraditionalHeaders(int ptr, final int end) {
  409. while (ptr < end) {
  410. final int eol = nextLF(buf, ptr);
  411. if (isHunkHdr(buf, ptr, eol) >= 1) {
  412. // First hunk header; break out and parse them later.
  413. break;
  414. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  415. parseOldName(ptr, eol);
  416. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  417. parseNewName(ptr, eol);
  418. } else {
  419. // Possibly an empty patch.
  420. break;
  421. }
  422. ptr = eol;
  423. }
  424. return ptr;
  425. }
  426. private String parseName(final String expect, int ptr, final int end) {
  427. if (ptr == end)
  428. return expect;
  429. String r;
  430. if (buf[ptr] == '"') {
  431. // New style GNU diff format
  432. //
  433. r = QuotedString.GIT_PATH.dequote(buf, ptr, end - 1);
  434. } else {
  435. // Older style GNU diff format, an optional tab ends the name.
  436. //
  437. int tab = end;
  438. while (ptr < tab && buf[tab - 1] != '\t')
  439. tab--;
  440. if (ptr == tab)
  441. tab = end;
  442. r = decode(Constants.CHARSET, buf, ptr, tab - 1);
  443. }
  444. if (r.equals(DEV_NULL))
  445. r = DEV_NULL;
  446. return r;
  447. }
  448. private static String p1(final String r) {
  449. final int s = r.indexOf('/');
  450. return s > 0 ? r.substring(s + 1) : r;
  451. }
  452. FileMode parseFileMode(int ptr, final int end) {
  453. int tmp = 0;
  454. while (ptr < end - 1) {
  455. tmp <<= 3;
  456. tmp += buf[ptr++] - '0';
  457. }
  458. return FileMode.fromBits(tmp);
  459. }
  460. void parseIndexLine(int ptr, final int end) {
  461. // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1
  462. // can be unique abbreviations
  463. //
  464. final int dot2 = nextLF(buf, ptr, '.');
  465. final int mode = nextLF(buf, dot2, ' ');
  466. oldId = AbbreviatedObjectId.fromString(buf, ptr, dot2 - 1);
  467. newId = AbbreviatedObjectId.fromString(buf, dot2 + 1, mode - 1);
  468. if (mode < end)
  469. newMode = oldMode = parseFileMode(mode, end);
  470. }
  471. private boolean eq(int aPtr, int aEnd, int bPtr, int bEnd) {
  472. if (aEnd - aPtr != bEnd - bPtr) {
  473. return false;
  474. }
  475. while (aPtr < aEnd) {
  476. if (buf[aPtr++] != buf[bPtr++])
  477. return false;
  478. }
  479. return true;
  480. }
  481. /**
  482. * Determine if this is a patch hunk header.
  483. *
  484. * @param buf
  485. * the buffer to scan
  486. * @param start
  487. * first position in the buffer to evaluate
  488. * @param end
  489. * last position to consider; usually the end of the buffer (
  490. * <code>buf.length</code>) or the first position on the next
  491. * line. This is only used to avoid very long runs of '@' from
  492. * killing the scan loop.
  493. * @return the number of "ancestor revisions" in the hunk header. A
  494. * traditional two-way diff ("@@ -...") returns 1; a combined diff
  495. * for a 3 way-merge returns 3. If this is not a hunk header, 0 is
  496. * returned instead.
  497. */
  498. static int isHunkHdr(final byte[] buf, final int start, final int end) {
  499. int ptr = start;
  500. while (ptr < end && buf[ptr] == '@')
  501. ptr++;
  502. if (ptr - start < 2)
  503. return 0;
  504. if (ptr == end || buf[ptr++] != ' ')
  505. return 0;
  506. if (ptr == end || buf[ptr++] != '-')
  507. return 0;
  508. return (ptr - 3) - start;
  509. }
  510. }