summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Smith <chris@jalakai.co.uk>2015-05-29 16:55:23 +0100
committerChristopher Smith <chris@jalakai.co.uk>2015-05-29 16:55:23 +0100
commite12c5ac781d560502d478775502df70cd80472de (patch)
tree01c29375b4cf46a737709e01bb39c801adfe45b1
parent3dfe7d64760baa568018c1d6d311c26d1a2da098 (diff)
downloadrpg-e12c5ac781d560502d478775502df70cd80472de.tar.gz
rpg-e12c5ac781d560502d478775502df70cd80472de.tar.bz2
Minor Refactoring
- put test comments in more appropriate spot - move appending replacement line alongside its search/delete code
-rw-r--r--_test/tests/inc/io_replaceinfile.test.php12
-rw-r--r--inc/io.php14
2 files changed, 14 insertions, 12 deletions
diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php
index de7301fa1..597138a20 100644
--- a/_test/tests/inc/io_replaceinfile.test.php
+++ b/_test/tests/inc/io_replaceinfile.test.php
@@ -59,35 +59,37 @@ class io_replaceinfile_test extends DokuWikiTest {
}
/**
- *
+ * Test for a non-regex replacement where $newline contains a backreference like construct - it shouldn't affect the replacement
*/
function test_edgecase1()
{
$file = TMP_DIR . '/test.txt';
- // Replace all, no regex, backreference like construct in replacement line
+
io_saveFile($file, $this->contents);
$this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\\00\012", false, -1));
$this->assertEquals("The\012Delete\\00\012Delete\\00\012Delete01\012Delete02\012Delete\\00\012DeleteX\012Test\012", io_readFile($file), "Edge case: backreference like construct in replacement line");
}
/**
+ * Test with replace all where replacement line == search line - must not timeout
+ *
* @small
*/
function test_edgecase2() {
$file = TMP_DIR.'/test.txt';
- // Replace all, no regex, replacement line == search line
+
io_saveFile($file, $this->contents);
$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");
}
/**
- *
+ * Test where $oldline exactly matches one line and also matches part of other lines - only the exact match should be replaced
*/
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 c559feb17..4c7fb094f 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -324,14 +324,14 @@ function io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0)
$lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched);
if ($matched) $count++;
}
- } else {
- $lines = ($maxlines == 0) ?
- preg_grep($pattern, $lines, PREG_GREP_INVERT) :
- preg_replace($pattern, $replace, $lines);
- }
+ } else if ($maxlines == 0) {
+ $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT);
- if($maxlines == 0 && ((string)$newline) !== '') {
- $lines[] = $newline;
+ if ((string)$newline !== ''){
+ $lines[] = $newline;
+ }
+ } else {
+ $lines = preg_replace($pattern, $replace, $lines);
}
if(count($lines)){