Browse Source

Make sure <replaceregexp> doesn't add a newline char at the end of the

file if byline="true" either.

PR: 12407
Submitted by:	Stefan Moebius <stmoebius at yahoo.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273332 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
a26c94ea6a
3 changed files with 73 additions and 17 deletions
  1. +3
    -0
      WHATSNEW
  2. +70
    -11
      src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
  3. +0
    -6
      src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java

+ 3
- 0
WHATSNEW View File

@@ -29,6 +29,9 @@ Fixed bugs:
be a single empty argument. Use <arg value="''"/> if you need the
quotes literally.

* <replaceregexp> could append a newline character at the end of the
file.

Other changes:
--------------



+ 70
- 11
src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java View File

@@ -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();


+ 0
- 6
src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java View File

@@ -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

Loading…
Cancel
Save