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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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.JGitText;
  59. import org.eclipse.jgit.diff.DiffEntry;
  60. import org.eclipse.jgit.diff.EditList;
  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 ");
  70. private static final byte[] NEW_MODE = encodeASCII("new mode ");
  71. static final byte[] DELETED_FILE_MODE = encodeASCII("deleted file mode ");
  72. static final byte[] NEW_FILE_MODE = encodeASCII("new file mode ");
  73. private static final byte[] COPY_FROM = encodeASCII("copy from ");
  74. private static final byte[] COPY_TO = encodeASCII("copy to ");
  75. private static final byte[] RENAME_OLD = encodeASCII("rename old ");
  76. private static final byte[] RENAME_NEW = encodeASCII("rename new ");
  77. private static final byte[] RENAME_FROM = encodeASCII("rename from ");
  78. private static final byte[] RENAME_TO = encodeASCII("rename to ");
  79. private static final byte[] SIMILARITY_INDEX = encodeASCII("similarity index ");
  80. private static final byte[] DISSIMILARITY_INDEX = encodeASCII("dissimilarity index ");
  81. static final byte[] INDEX = encodeASCII("index ");
  82. static final byte[] OLD_NAME = encodeASCII("--- ");
  83. static final byte[] NEW_NAME = encodeASCII("+++ ");
  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. ptr = 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(JGitText.get().expectedCharacterEncodingGuesses, (getParentCount() + 1)));
  183. if (trySimpleConversion(charsetGuess)) {
  184. Charset cs = charsetGuess != null ? charsetGuess[0] : null;
  185. if (cs == null)
  186. cs = Constants.CHARSET;
  187. try {
  188. return decodeNoFallback(cs, buf, startOffset, endOffset);
  189. } catch (CharacterCodingException cee) {
  190. // Try the much slower, more-memory intensive version which
  191. // can handle a character set conversion patch.
  192. }
  193. }
  194. final StringBuilder r = new StringBuilder(endOffset - startOffset);
  195. // Always treat the headers as US-ASCII; Git file names are encoded
  196. // in a C style escape if any character has the high-bit set.
  197. //
  198. final int hdrEnd = getHunks().get(0).getStartOffset();
  199. for (int ptr = startOffset; ptr < hdrEnd;) {
  200. final int eol = Math.min(hdrEnd, nextLF(buf, ptr));
  201. r.append(extractBinaryString(buf, ptr, eol));
  202. ptr = eol;
  203. }
  204. final String[] files = extractFileLines(charsetGuess);
  205. final int[] offsets = new int[files.length];
  206. for (final HunkHeader h : getHunks())
  207. h.extractFileLines(r, files, offsets);
  208. return r.toString();
  209. }
  210. private static boolean trySimpleConversion(final Charset[] charsetGuess) {
  211. if (charsetGuess == null)
  212. return true;
  213. for (int i = 1; i < charsetGuess.length; i++) {
  214. if (charsetGuess[i] != charsetGuess[0])
  215. return false;
  216. }
  217. return true;
  218. }
  219. private String[] extractFileLines(final Charset[] csGuess) {
  220. final TemporaryBuffer[] tmp = new TemporaryBuffer[getParentCount() + 1];
  221. try {
  222. for (int i = 0; i < tmp.length; i++)
  223. tmp[i] = new TemporaryBuffer.LocalFile();
  224. for (final HunkHeader h : getHunks())
  225. h.extractFileLines(tmp);
  226. final String[] r = new String[tmp.length];
  227. for (int i = 0; i < tmp.length; i++) {
  228. Charset cs = csGuess != null ? csGuess[i] : null;
  229. if (cs == null)
  230. cs = Constants.CHARSET;
  231. r[i] = RawParseUtils.decode(cs, tmp[i].toByteArray());
  232. }
  233. return r;
  234. } catch (IOException ioe) {
  235. throw new RuntimeException(JGitText.get().cannotConvertScriptToText, ioe);
  236. } finally {
  237. for (final TemporaryBuffer b : tmp) {
  238. if (b != null)
  239. b.destroy();
  240. }
  241. }
  242. }
  243. /** @return style of patch used to modify this file */
  244. public PatchType getPatchType() {
  245. return patchType;
  246. }
  247. /** @return true if this patch modifies metadata about a file */
  248. public boolean hasMetaDataChanges() {
  249. return changeType != ChangeType.MODIFY || newMode != oldMode;
  250. }
  251. /** @return hunks altering this file; in order of appearance in patch */
  252. public List<? extends HunkHeader> getHunks() {
  253. if (hunks == null)
  254. return Collections.emptyList();
  255. return hunks;
  256. }
  257. void addHunk(final HunkHeader h) {
  258. if (h.getFileHeader() != this)
  259. throw new IllegalArgumentException(JGitText.get().hunkBelongsToAnotherFile);
  260. if (hunks == null)
  261. hunks = new ArrayList<HunkHeader>();
  262. hunks.add(h);
  263. }
  264. HunkHeader newHunkHeader(final int offset) {
  265. return new HunkHeader(this, offset);
  266. }
  267. /** @return if a {@link PatchType#GIT_BINARY}, the new-image delta/literal */
  268. public BinaryHunk getForwardBinaryHunk() {
  269. return forwardBinaryHunk;
  270. }
  271. /** @return if a {@link PatchType#GIT_BINARY}, the old-image delta/literal */
  272. public BinaryHunk getReverseBinaryHunk() {
  273. return reverseBinaryHunk;
  274. }
  275. /** @return a list describing the content edits performed on this file. */
  276. public EditList toEditList() {
  277. final EditList r = new EditList();
  278. for (final HunkHeader hunk : hunks)
  279. r.addAll(hunk.toEditList());
  280. return r;
  281. }
  282. /**
  283. * Parse a "diff --git" or "diff --cc" line.
  284. *
  285. * @param ptr
  286. * first character after the "diff --git " or "diff --cc " part.
  287. * @param end
  288. * one past the last position to parse.
  289. * @return first character after the LF at the end of the line; -1 on error.
  290. */
  291. int parseGitFileName(int ptr, final int end) {
  292. final int eol = nextLF(buf, ptr);
  293. final int bol = ptr;
  294. if (eol >= end) {
  295. return -1;
  296. }
  297. // buffer[ptr..eol] looks like "a/foo b/foo\n". After the first
  298. // A regex to match this is "^[^/]+/(.*?) [^/+]+/\1\n$". There
  299. // is only one way to split the line such that text to the left
  300. // of the space matches the text to the right, excluding the part
  301. // before the first slash.
  302. //
  303. final int aStart = nextLF(buf, ptr, '/');
  304. if (aStart >= eol)
  305. return eol;
  306. while (ptr < eol) {
  307. final int sp = nextLF(buf, ptr, ' ');
  308. if (sp >= eol) {
  309. // We can't split the header, it isn't valid.
  310. // This may be OK if this is a rename patch.
  311. //
  312. return eol;
  313. }
  314. final int bStart = nextLF(buf, sp, '/');
  315. if (bStart >= eol)
  316. return eol;
  317. // If buffer[aStart..sp - 1] = buffer[bStart..eol - 1]
  318. // we have a valid split.
  319. //
  320. if (eq(aStart, sp - 1, bStart, eol - 1)) {
  321. if (buf[bol] == '"') {
  322. // We're a double quoted name. The region better end
  323. // in a double quote too, and we need to decode the
  324. // characters before reading the name.
  325. //
  326. if (buf[sp - 2] != '"') {
  327. return eol;
  328. }
  329. oldPath = QuotedString.GIT_PATH.dequote(buf, bol, sp - 1);
  330. oldPath = p1(oldPath);
  331. } else {
  332. oldPath = decode(Constants.CHARSET, buf, aStart, sp - 1);
  333. }
  334. newPath = oldPath;
  335. return eol;
  336. }
  337. // This split wasn't correct. Move past the space and try
  338. // another split as the space must be part of the file name.
  339. //
  340. ptr = sp;
  341. }
  342. return eol;
  343. }
  344. int parseGitHeaders(int ptr, final int end) {
  345. while (ptr < end) {
  346. final int eol = nextLF(buf, ptr);
  347. if (isHunkHdr(buf, ptr, eol) >= 1) {
  348. // First hunk header; break out and parse them later.
  349. break;
  350. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  351. parseOldName(ptr, eol);
  352. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  353. parseNewName(ptr, eol);
  354. } else if (match(buf, ptr, OLD_MODE) >= 0) {
  355. oldMode = parseFileMode(ptr + OLD_MODE.length, eol);
  356. } else if (match(buf, ptr, NEW_MODE) >= 0) {
  357. newMode = parseFileMode(ptr + NEW_MODE.length, eol);
  358. } else if (match(buf, ptr, DELETED_FILE_MODE) >= 0) {
  359. oldMode = parseFileMode(ptr + DELETED_FILE_MODE.length, eol);
  360. newMode = FileMode.MISSING;
  361. changeType = ChangeType.DELETE;
  362. } else if (match(buf, ptr, NEW_FILE_MODE) >= 0) {
  363. parseNewFileMode(ptr, eol);
  364. } else if (match(buf, ptr, COPY_FROM) >= 0) {
  365. oldPath = parseName(oldPath, ptr + COPY_FROM.length, eol);
  366. changeType = ChangeType.COPY;
  367. } else if (match(buf, ptr, COPY_TO) >= 0) {
  368. newPath = parseName(newPath, ptr + COPY_TO.length, eol);
  369. changeType = ChangeType.COPY;
  370. } else if (match(buf, ptr, RENAME_OLD) >= 0) {
  371. oldPath = parseName(oldPath, ptr + RENAME_OLD.length, eol);
  372. changeType = ChangeType.RENAME;
  373. } else if (match(buf, ptr, RENAME_NEW) >= 0) {
  374. newPath = parseName(newPath, ptr + RENAME_NEW.length, eol);
  375. changeType = ChangeType.RENAME;
  376. } else if (match(buf, ptr, RENAME_FROM) >= 0) {
  377. oldPath = parseName(oldPath, ptr + RENAME_FROM.length, eol);
  378. changeType = ChangeType.RENAME;
  379. } else if (match(buf, ptr, RENAME_TO) >= 0) {
  380. newPath = parseName(newPath, ptr + RENAME_TO.length, eol);
  381. changeType = ChangeType.RENAME;
  382. } else if (match(buf, ptr, SIMILARITY_INDEX) >= 0) {
  383. score = parseBase10(buf, ptr + SIMILARITY_INDEX.length, null);
  384. } else if (match(buf, ptr, DISSIMILARITY_INDEX) >= 0) {
  385. score = parseBase10(buf, ptr + DISSIMILARITY_INDEX.length, null);
  386. } else if (match(buf, ptr, INDEX) >= 0) {
  387. parseIndexLine(ptr + INDEX.length, eol);
  388. } else {
  389. // Probably an empty patch (stat dirty).
  390. break;
  391. }
  392. ptr = eol;
  393. }
  394. return ptr;
  395. }
  396. void parseOldName(int ptr, final int eol) {
  397. oldPath = p1(parseName(oldPath, ptr + OLD_NAME.length, eol));
  398. if (oldPath == DEV_NULL)
  399. changeType = ChangeType.ADD;
  400. }
  401. void parseNewName(int ptr, final int eol) {
  402. newPath = p1(parseName(newPath, ptr + NEW_NAME.length, eol));
  403. if (newPath == DEV_NULL)
  404. changeType = ChangeType.DELETE;
  405. }
  406. void parseNewFileMode(int ptr, final int eol) {
  407. oldMode = FileMode.MISSING;
  408. newMode = parseFileMode(ptr + NEW_FILE_MODE.length, eol);
  409. changeType = ChangeType.ADD;
  410. }
  411. int parseTraditionalHeaders(int ptr, final int end) {
  412. while (ptr < end) {
  413. final int eol = nextLF(buf, ptr);
  414. if (isHunkHdr(buf, ptr, eol) >= 1) {
  415. // First hunk header; break out and parse them later.
  416. break;
  417. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  418. parseOldName(ptr, eol);
  419. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  420. parseNewName(ptr, eol);
  421. } else {
  422. // Possibly an empty patch.
  423. break;
  424. }
  425. ptr = eol;
  426. }
  427. return ptr;
  428. }
  429. private String parseName(final String expect, int ptr, final int end) {
  430. if (ptr == end)
  431. return expect;
  432. String r;
  433. if (buf[ptr] == '"') {
  434. // New style GNU diff format
  435. //
  436. r = QuotedString.GIT_PATH.dequote(buf, ptr, end - 1);
  437. } else {
  438. // Older style GNU diff format, an optional tab ends the name.
  439. //
  440. int tab = end;
  441. while (ptr < tab && buf[tab - 1] != '\t')
  442. tab--;
  443. if (ptr == tab)
  444. tab = end;
  445. r = decode(Constants.CHARSET, buf, ptr, tab - 1);
  446. }
  447. if (r.equals(DEV_NULL))
  448. r = DEV_NULL;
  449. return r;
  450. }
  451. private static String p1(final String r) {
  452. final int s = r.indexOf('/');
  453. return s > 0 ? r.substring(s + 1) : r;
  454. }
  455. FileMode parseFileMode(int ptr, final int end) {
  456. int tmp = 0;
  457. while (ptr < end - 1) {
  458. tmp <<= 3;
  459. tmp += buf[ptr++] - '0';
  460. }
  461. return FileMode.fromBits(tmp);
  462. }
  463. void parseIndexLine(int ptr, final int end) {
  464. // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1
  465. // can be unique abbreviations
  466. //
  467. final int dot2 = nextLF(buf, ptr, '.');
  468. final int mode = nextLF(buf, dot2, ' ');
  469. oldId = AbbreviatedObjectId.fromString(buf, ptr, dot2 - 1);
  470. newId = AbbreviatedObjectId.fromString(buf, dot2 + 1, mode - 1);
  471. if (mode < end)
  472. newMode = oldMode = parseFileMode(mode, end);
  473. }
  474. private boolean eq(int aPtr, int aEnd, int bPtr, int bEnd) {
  475. if (aEnd - aPtr != bEnd - bPtr) {
  476. return false;
  477. }
  478. while (aPtr < aEnd) {
  479. if (buf[aPtr++] != buf[bPtr++])
  480. return false;
  481. }
  482. return true;
  483. }
  484. /**
  485. * Determine if this is a patch hunk header.
  486. *
  487. * @param buf
  488. * the buffer to scan
  489. * @param start
  490. * first position in the buffer to evaluate
  491. * @param end
  492. * last position to consider; usually the end of the buffer (
  493. * <code>buf.length</code>) or the first position on the next
  494. * line. This is only used to avoid very long runs of '@' from
  495. * killing the scan loop.
  496. * @return the number of "ancestor revisions" in the hunk header. A
  497. * traditional two-way diff ("@@ -...") returns 1; a combined diff
  498. * for a 3 way-merge returns 3. If this is not a hunk header, 0 is
  499. * returned instead.
  500. */
  501. static int isHunkHdr(final byte[] buf, final int start, final int end) {
  502. int ptr = start;
  503. while (ptr < end && buf[ptr] == '@')
  504. ptr++;
  505. if (ptr - start < 2)
  506. return 0;
  507. if (ptr == end || buf[ptr++] != ' ')
  508. return 0;
  509. if (ptr == end || buf[ptr++] != '-')
  510. return 0;
  511. return (ptr - 3) - start;
  512. }
  513. }