Skip to content

Commit 9333088

Browse files
committed
Cleanup/fixes
1 parent 14a43cf commit 9333088

File tree

1 file changed

+13
-91
lines changed

1 file changed

+13
-91
lines changed

File.inc

Lines changed: 13 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -670,30 +670,30 @@ class DelimitedFile extends TextFile {
670670
}
671671

672672
/**
673+
* Unlike other functions in these classes this one has be optimized for speed.
673674
*
675+
* fgetcsv() is about twice as fast as this function but this supports multiple delimiters, allows for EOL characters
676+
* to not be considered delimters useful when dealing with mixed line endings.
674677
*
675-
* Unlike other functions in these classes this one has be optimized for speed. And is faster
676-
* than fgetcsv() in some cases.
677-
*
678-
* @param type $count
679-
* @return type
678+
* @param int $count
679+
* The number of fields to get.
680+
* @return array
681+
* The fields if found, FALSE otherwise.
680682
*/
681683
public function getFields($count) {
682684
$this->push();
683685
$read = '';
684686
$pos = NULL;
685687
$fields = array();
688+
$expected = $count + 1;
686689
while (!$this->EOF()) {
687690
$matches = array();
688-
$read .= $this->read(256);
689-
$expected = $count + 1;
691+
$read .= $this->read(1024);
690692
$matches = preg_split($this->pattern, $read, $expected, PREG_SPLIT_OFFSET_CAPTURE);
691693
$matched = count($matches);
692694
if ($matched == $expected) {
693-
$pos = $matches[$expected - 1][1];
694-
array_walk($matches, array($this, 'mapMatches'));
695-
array_pop($matches); // Start of next set.
696-
$fields = $matches;
695+
$pos = $matches[$count][1];
696+
array_pop($matches);
697697
break;
698698
}
699699
else if ($this->EOF() && $matched == $count) {
@@ -704,15 +704,8 @@ class DelimitedFile extends TextFile {
704704
$this->pop();
705705
if (isset($pos)) {
706706
$this->seek($pos, SEEK_CUR); // Move to the proper spot.
707-
}
708-
return empty($fields) ? FALSE : $fields;
709-
}
710-
711-
public function getFieldsSlow($count) {
712-
$fields = array();
713-
if (($string = $this->findFields($count)) != FALSE) {
714-
$string = $this->normalizeDelimiters($string, $this->delimiters[0]);
715-
$fields = explode($this->delimiters[0], $string);
707+
array_walk($matches, array($this, 'mapMatches'));
708+
$fields = $matches;
716709
}
717710
return empty($fields) ? FALSE : $fields;
718711
}
@@ -727,77 +720,6 @@ class DelimitedFile extends TextFile {
727720
$item = $item[0];
728721
}
729722

730-
/**
731-
* Normalizes the delimiters found in the given $string.
732-
*
733-
* @todo This could be moved to a string class, possibly would need to account for the EOF.
734-
*
735-
* @param string $string
736-
* The string to normalize
737-
*
738-
* @return string
739-
* The normalized $string.
740-
*/
741-
private function normalizeDelimiters($string, $normal) {
742-
$count = count($this->delimiters);
743-
for ($i = 0; $i < $count; $i++) {
744-
$string = str_replace($this->delimiters[$i], $normal, $string);
745-
}
746-
return $string;
747-
}
748-
749-
/**
750-
* Search file for string containing exactly $count fields.
751-
*
752-
753-
* @param int $count
754-
*
755-
* @return string
756-
*/
757-
private function findFields($count) {
758-
$this->push();
759-
$read = '';
760-
while (!$this->EOF()) {
761-
$matches = array();
762-
$read .= $this->read(256);
763-
if (($substr = $this->getSubStrWithFields($read, $count)) != FALSE) {
764-
$this->pop();
765-
$this->seek(strlen($substr), SEEK_CUR);
766-
return $substr;
767-
}
768-
}
769-
$this->pop();
770-
return FALSE;
771-
}
772-
773-
/**
774-
* Attempts to find a substring containing $count fields.
775-
*
776-
* @todo This could be moved to a string class, possibly would need to account for the EOF.
777-
*
778-
* @param string $read
779-
* The string to search for fields.
780-
* @param int $count
781-
* The number of fields to find.
782-
*
783-
* @return int
784-
* The substring containing $count fields if successful, FALSE otherwise.
785-
*/
786-
private function getSubStrWithFields($read, $count) {
787-
$matches = array();
788-
$matched = preg_match_all($this->pattern, $read, $matches, PREG_OFFSET_CAPTURE);
789-
if ($matched >= $count) { // There are enough delimiters matched to separate the requested number of fields.
790-
$last = $count - 1; // The index for the last delimiter for the requested fields.
791-
$last_delimiter = $matches[0][$last]; // Last delimiter for the requested fields.
792-
$length = strlen($last_delimiter[0]) + $last_delimiter[1];
793-
return substr($read, 0, $length);
794-
}
795-
else if ($this->EOF() && $matched == ($count - 1)) { // A special case were the EOF is expected to be a delimiter.
796-
return $read;
797-
}
798-
return FALSE;
799-
}
800-
801723
}
802724

803725
/**

0 commit comments

Comments
 (0)