From c94dea7029caea6377f4e36213104afde0bd6642 Mon Sep 17 00:00:00 2001 From: Erik Bernhardson Date: Fri, 28 Sep 2018 10:58:00 -0700 Subject: [PATCH] Only strip newline in OrderedStreamingForkController The controller should pass through lines of input exactly as they were provided, only stripping the trailing newline that delimits items. The trim was making `door` and `door ` equivilant but for the use case in search the distinction is important. Additionally check that the line is actually empty, don't throw away inputs like '0' which are falsy. Change-Id: Ifac910543fdb46a27da021e831e3e18befefcfc5 --- includes/OrderedStreamingForkController.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/includes/OrderedStreamingForkController.php b/includes/OrderedStreamingForkController.php index ff29cb510d..11abc81c1f 100644 --- a/includes/OrderedStreamingForkController.php +++ b/includes/OrderedStreamingForkController.php @@ -134,9 +134,12 @@ class OrderedStreamingForkController extends ForkController { */ protected function consumeNoFork() { while ( !feof( $this->input ) ) { - $line = trim( fgets( $this->input ) ); - if ( $line ) { - $result = call_user_func( $this->workCallback, $line ); + $data = fgets( $this->input ); + if ( $data[ strlen( $data ) - 1 ] == "\n" ) { + $data = substr( $data, 0, -1 ); + } + if ( strlen( $data ) !== 0 ) { + $result = call_user_func( $this->workCallback, $data ); fwrite( $this->output, "$result\n" ); } } @@ -160,8 +163,12 @@ class OrderedStreamingForkController extends ForkController { $this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 ); } while ( !$sockets ); } - $data = trim( $data ); - if ( !$data ) { + // Strip the trailing \n. The last line of a file might not have a trailing + // \n though + if ( $data[ strlen( $data ) - 1 ] == "\n" ) { + $data = substr( $data, 0, -1 ); + } + if ( strlen( $data ) === 0 ) { continue; } $socket = array_pop( $sockets ); -- 2.20.1