summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Smith <chris@jalakai.co.uk>2015-05-29 16:52:05 +0100
committerChristopher Smith <chris@jalakai.co.uk>2015-05-29 16:52:05 +0100
commit3dfe7d64760baa568018c1d6d311c26d1a2da098 (patch)
tree60bdca1cd70a14b4ec8fec689c2b9771683ff70a
parent4b8290f563ad5ac84dd92fb8ca97ba5970c472b3 (diff)
downloadrpg-3dfe7d64760baa568018c1d6d311c26d1a2da098.tar.gz
rpg-3dfe7d64760baa568018c1d6d311c26d1a2da098.tar.bz2
add anchors when constructing pattern from a non-regex oldline
-rw-r--r--_test/tests/inc/io_replaceinfile.test.php13
-rw-r--r--inc/io.php2
2 files changed, 14 insertions, 1 deletions
diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php
index c2dbc0dd4..de7301fa1 100644
--- a/_test/tests/inc/io_replaceinfile.test.php
+++ b/_test/tests/inc/io_replaceinfile.test.php
@@ -79,4 +79,17 @@ class io_replaceinfile_test extends DokuWikiTest {
$this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1));
$this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line");
}
+
+ /**
+ *
+ */
+ function test_edgecase3()
+ {
+ $file = TMP_DIR . '/test.txt';
+ $contents = "The\012Delete\01201Delete\01202Delete\012Test\012";
+ // Replace all, no regex, oldline exactly matches one line; matches part of other lines - only the exact match should be replaced
+ io_saveFile($file, $contents);
+ $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1));
+ $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines");
+ }
}
diff --git a/inc/io.php b/inc/io.php
index 51ca2ea14..c559feb17 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -312,7 +312,7 @@ function io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0)
}
// make non-regexes into regexes
- $pattern = $regex ? $oldline : '/'.preg_quote($oldline,'/').'/';
+ $pattern = $regex ? $oldline : '/^'.preg_quote($oldline,'/').'$/';
$replace = $regex ? $newline : addcslashes($newline, '\$');
// remove matching lines