From a26c94ea6a7ed1d62eccb2170a17e3cbb86c759c Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 23 Sep 2002 10:37:31 +0000 Subject: [PATCH] Make sure doesn't add a newline char at the end of the file if byline="true" either. PR: 12407 Submitted by: Stefan Moebius git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273332 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 + .../ant/taskdefs/optional/ReplaceRegExp.java | 81 ++++++++++++++++--- .../taskdefs/optional/ReplaceRegExpTest.java | 6 -- 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 5dbdd650f..e88a6097c 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -29,6 +29,9 @@ Fixed bugs: be a single empty argument. Use if you need the quotes literally. +* could append a newline character at the end of the + file. + Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java index bfcf81c65..3eb2836f1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java @@ -59,7 +59,6 @@ import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.LineNumberReader; import java.io.PrintWriter; import java.util.Vector; import org.apache.tools.ant.BuildException; @@ -287,7 +286,8 @@ public class ReplaceRegExp extends Task { Regexp regexp = r.getRegexp(getProject()); if (regexp.matches(input, options)) { - res = regexp.substitute(input, s.getExpression(getProject()), options); + res = regexp.substitute(input, s.getExpression(getProject()), + options); } return res; @@ -322,18 +322,77 @@ public class ReplaceRegExp extends Task { Project.MSG_VERBOSE); if (byline) { - LineNumberReader lnr = new LineNumberReader(br); + StringBuffer linebuf = new StringBuffer(); String line = null; - - while ((line = lnr.readLine()) != null) { - String res = doReplace(regex, subs, line, options); - - if (!res.equals(line)) { - changes = true; + String res = null; + int c; + boolean hasCR = false; + + do { + c = br.read(); + + if (c == '\r') { + if (hasCR) { + // second CR -> EOL + possibly empty line + line = linebuf.toString(); + res = doReplace(regex, subs, line, options); + + if (!res.equals(line)) { + changes = true; + } + + pw.print(res); + pw.print('\r'); + + linebuf.setLength(0); + // hasCR is still true (for the second one) + } else { + // first CR in this line + hasCR = true; + } } + else if (c == '\n') { + // LF -> EOL + line = linebuf.toString(); + res = doReplace(regex, subs, line, options); + + if (!res.equals(line)) { + changes = true; + } + + pw.print(res); + if (hasCR) { + pw.print('\r'); + hasCR = false; + } + pw.print('\n'); + + linebuf.setLength(0); + } else { // any other char + if ((hasCR) || (c < 0)) { + // Mac-style linebreak or EOF (or both) + line = linebuf.toString(); + res = doReplace(regex, subs, line, options); + + if (!res.equals(line)) { + changes = true; + } + + pw.print(res); + if (hasCR) { + pw.print('\r'); + hasCR = false; + } + + linebuf.setLength(0); + } + + if (c >= 0) { + linebuf.append((char) c); + } + } + } while (c >= 0); - pw.println(res); - } pw.flush(); } else { int flen = (int) f.length(); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java index 101bac3f8..0a3958542 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java @@ -121,11 +121,6 @@ public class ReplaceRegExpTest extends BuildFileTest { new File("src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties"))); } - /** - * FIXME - * - * will be fixed this week, just running out of time and - * committing a partly fixed version now -- Stefan public void testDontAddNewline2() throws IOException { executeTarget("testDontAddNewline2"); assertTrue("Files match", @@ -133,6 +128,5 @@ public class ReplaceRegExpTest extends BuildFileTest { .contentEquals(new File("src/etc/testcases/taskdefs/optional/test.properties"), new File("src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties"))); } - */ }// ReplaceRegExpTest