@@ -670,30 +670,30 @@ class DelimitedFile extends TextFile {
670
670
}
671
671
672
672
/**
673
+ * Unlike other functions in these classes this one has be optimized for speed.
673
674
*
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.
674
677
*
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.
680
682
*/
681
683
public function getFields ($ count ) {
682
684
$ this ->push ();
683
685
$ read = '' ;
684
686
$ pos = NULL ;
685
687
$ fields = array ();
688
+ $ expected = $ count + 1 ;
686
689
while (!$ this ->EOF ()) {
687
690
$ matches = array ();
688
- $ read .= $ this ->read (256 );
689
- $ expected = $ count + 1 ;
691
+ $ read .= $ this ->read (1024 );
690
692
$ matches = preg_split ($ this ->pattern , $ read , $ expected , PREG_SPLIT_OFFSET_CAPTURE );
691
693
$ matched = count ($ matches );
692
694
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 );
697
697
break ;
698
698
}
699
699
else if ($ this ->EOF () && $ matched == $ count ) {
@@ -704,15 +704,8 @@ class DelimitedFile extends TextFile {
704
704
$ this ->pop ();
705
705
if (isset ($ pos )) {
706
706
$ 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 ;
716
709
}
717
710
return empty ($ fields ) ? FALSE : $ fields ;
718
711
}
@@ -727,77 +720,6 @@ class DelimitedFile extends TextFile {
727
720
$ item = $ item [0 ];
728
721
}
729
722
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
-
801
723
}
802
724
803
725
/**
0 commit comments