bodewig 2002/09/23 03:37:31
Modified: . WHATSNEW
src/main/org/apache/tools/ant/taskdefs/optional
ReplaceRegExp.java
src/testcases/org/apache/tools/ant/taskdefs/optional
ReplaceRegExpTest.java
Log:
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>
Revision Changes Path
1.287 +3 -0 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.286
retrieving revision 1.287
diff -u -r1.286 -r1.287
--- WHATSNEW 13 Sep 2002 10:03:10 -0000 1.286
+++ WHATSNEW 23 Sep 2002 10:37:31 -0000 1.287
@@ -29,6 +29,9 @@
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:
--------------
1.15 +70 -11
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
Index: ReplaceRegExp.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- ReplaceRegExp.java 16 Sep 2002 16:19:29 -0000 1.14
+++ ReplaceRegExp.java 23 Sep 2002 10:37:31 -0000 1.15
@@ -59,7 +59,6 @@
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 @@
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 @@
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();
1.4 +1 -7
jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
Index: ReplaceRegExpTest.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ReplaceRegExpTest.java 16 Sep 2002 16:19:30 -0000 1.3
+++ ReplaceRegExpTest.java 23 Sep 2002 10:37:31 -0000 1.4
@@ -121,11 +121,6 @@
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 @@
.contentEquals(new
File("src/etc/testcases/taskdefs/optional/test.properties"),
new
File("src/etc/testcases/taskdefs/optional/replaceregexp2.result.properties")));
}
- */
}// ReplaceRegExpTest
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>