From 4c7e100910cb1b40fc3aed0a21abe0806645c02a Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 13 Oct 2010 19:05:52 -0700 Subject: [PATCH] Add getString utility functions to RawText These routines can be useful when debugging, because we can add an expression to the Eclipse "Expressions" panel to show the text that appears on a line. Gerrit Code Review also uses these in its own subclass of RawText in order to format patch files, so pulling it up to be part of core JGit may help other applications too. Change-Id: I20a6b112e3403ecfc1c2715ae75dcecc1a85b167 Signed-off-by: Shawn O. Pearce --- .../src/org/eclipse/jgit/diff/RawText.java | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java index 428183c94..06b01e9d0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java @@ -132,8 +132,8 @@ public int size() { */ public void writeLine(final OutputStream out, final int i) throws IOException { - final int start = lines.get(i + 1); - int end = lines.get(i + 2); + int start = getStart(i); + int end = getEnd(i); if (content[end - 1] == '\n') end--; out.write(content, start, end - start); @@ -151,6 +151,67 @@ public boolean isMissingNewlineAtEnd() { return content[end - 1] != '\n'; } + /** + * Get the text for a single line. + * + * @param i + * index of the line to extract. Note this is 0-based, so line + * number 1 is actually index 0. + * @return the text for the line, without a trailing LF. + */ + public String getString(int i) { + return getString(i, i + 1, true); + } + + /** + * Get the text for a region of lines. + * + * @param begin + * index of the first line to extract. Note this is 0-based, so + * line number 1 is actually index 0. + * @param end + * index of one past the last line to extract. + * @param dropLF + * if true the trailing LF ('\n') of the last returned line is + * dropped, if present. + * @return the text for lines {@code [begin, end)}. + */ + public String getString(int begin, int end, boolean dropLF) { + if (begin == end) + return ""; + + int s = getStart(begin); + int e = getEnd(end - 1); + if (dropLF && content[e - 1] == '\n') + e--; + return decode(s, e); + } + + /** + * Decode a region of the text into a String. + * + * The default implementation of this method tries to guess the character + * set by considering UTF-8, the platform default, and falling back on + * ISO-8859-1 if neither of those can correctly decode the region given. + * + * @param start + * first byte of the content to decode. + * @param end + * one past the last byte of the content to decode. + * @return the region {@code [start, end)} decoded as a String. + */ + protected String decode(int start, int end) { + return RawParseUtils.decode(content, start, end); + } + + private int getStart(final int i) { + return lines.get(i + 1); + } + + private int getEnd(final int i) { + return lines.get(i + 2); + } + /** * Determine heuristically whether a byte array represents binary (as * opposed to text) content.