-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchableFile.phpclass
executable file
·1375 lines (1029 loc) · 48.3 KB
/
SearchableFile.phpclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**************************************************************************************************************
NAME
SearchableFile.phpclass
DESCRIPTION
A class to perform string search operations on big files that do not fit into memory.
AUTHOR
Christian Vigh, 03/2016.
HISTORY
[Version : 1.0] [Date : 2016/03/29] [Author : CV]
Initial version.
[Version : 1.1] [Date : 2016/04/24] [Author : CV]
. Implemented some kind of naive buffer cache to avoid unnecessary disk IO.
[Version : 1.1.1] [Date : 2016/09/04] [Author : CV]
. Added the get_contents() method.
[Version : 1.1.2] [Date : 2016/09/20] [Author : CV]
. The substr() method was returning an empty string when the 3rd parameter ($length) was not specified.
[Version : 1.1.3] [Date : 2016/09/20] [Author : CV]
. Fixed an inconsistency in method do_strpos() which caused warnings to be issued when searching for a
string in cached data.
**************************************************************************************************************/
/*==============================================================================================================
SearchableFile class -
Allows to perform string search operations on big files that do not fit into memory.
==============================================================================================================*/
class SearchableFile implements \ArrayAccess, \Countable, \Iterator
{
// Name of the file whose contents are to be searched
public $Filename ;
// File size
public $Filesize ;
// Buffer cache and cache size
public $Cache ;
private $CacheSize ;
// Input record size
public $RecordSize ;
// File descriptor
public $fp ;
// Number of cache hits/misses
public $CacheHits ;
public $CacheMisses ;
/*--------------------------------------------------------------------------------------------------------------
CONSTRUCTOR
Instantiates a SearchableFile object.
PROTOTYPE
$sf = new SearchableFile ( $record_size = 16384, $cache_size = 8 ) ;
PARAMETERS
$record_size (integer) -
Size of a read buffer, in bytes.
$cache_size (integer) -
Buffer cache size. A cache of $cache_size items, each having a record size of $record_size, will
be allocated into memory.
NOTES
Both record size and cache size can be changed after instatiation but they will only be applied during
the next call to Open().
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( $record_size = 16384, $cache_size = 8 )
{
$this -> RecordSize = $record_size ;
$this -> CacheSize = $cache_size ;
$this -> __initialize ( ) ;
}
private function __initialize ( )
{
$this -> Filename = false ;
$this -> Filesize = false ;
$this -> Cache = false ;
$this -> fp = false ;
$this -> CacheMisses = 0 ;
$this -> CacheHits = 0 ;
}
/*--------------------------------------------------------------------------------------------------------------
DESTRUCTOR
Closes the searched file, if opened.
*-------------------------------------------------------------------------------------------------------------*/
public function __destruct ( )
{ $this -> Close ( ) ; }
/**************************************************************************************************************
**************************************************************************************************************
**************************************************************************************************************
****** ******
****** ******
****** PUBLIC FUNCTIONS ******
****** ******
****** ******
**************************************************************************************************************
**************************************************************************************************************
**************************************************************************************************************/
/*--------------------------------------------------------------------------------------------------------------
NAME
Open - Opens a file for searching.
PROTOTYPE
$sf -> Open ( $filename ) ;
DESCRIPTION
Opens the specified file for searching.
PARAMETERS
$filename (string) -
File to be opened.
*-------------------------------------------------------------------------------------------------------------*/
public function Open ( $filename )
{
$this -> Close ( ) ;
if ( ! ( $this -> fp = @fopen ( $filename, "r" ) ) )
throw ( new \RuntimeException ( "Cannot open file \"$filename\"." ) ) ;
$info = fstat ( $this -> fp ) ;
$this -> Filename = $filename ;
$this -> Filesize = $info [ 'size' ] ;
$this -> Cache = new SearchableFileCache ( $this, $this -> CacheSize ) ;
$this -> CacheMisses = 0 ;
$this -> CacheHits = 0 ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
Close - Closes the searchable file.
PROTOTYPE
$sf -> Close ( ) ;
DESCRIPTION
Closes the searchable file, if already opened.
NOTES
Nothing happens if the file is already closed.
*-------------------------------------------------------------------------------------------------------------*/
public function Close ( )
{
if ( $this -> fp )
{
fclose ( $this -> fp ) ;
$this -> __initialize ( ) ;
}
}
/*--------------------------------------------------------------------------------------------------------------
NAME
Write - write file data between two offsets.
PROTOTYPE
$sf -> Write ( $output, $start, $end = false ) ;
DESCRIPTION
Writes all file data comprised between offsets $start and $end.
PARAMETERS
$output -
Either a file or stream resource, or a callback which has the following prototype, the $data
parameter containing the next block of data :
function callback ( $data ) ;
$start (integer) -
Start offset.
$end (integer) -
End offset. If this parameter is not specified or set to false, the whole contents of the file
will be written, starting at offset $start.
*-------------------------------------------------------------------------------------------------------------*/
public function Write ( $output, $start, $end = false )
{
if ( is_callable ( $output ) )
$callable = true ;
else if ( is_resource ( $output ) )
$callable = false ;
else
throw ( new \InvalidArgumentException ( "The '\$ouput' parameter of the Write() method must either be a valid file resource or a callback." ) ) ;
if ( $end !== false && $end < $start )
throw ( new \InvalidArgumentException ( "The '\$end' parameter of the Write() method must be greater than or equal to " .
"the '\$start' parameter." ) ) ;
if ( $end === false )
$end = $this -> Filesize - 1 ;
if ( ! ( $buffer = $this -> Cache -> Get ( $start ) ) )
return ;
$rel_offset = $start % $this -> RecordSize ;
$count = $end - $start + 1 ;
$length = min ( $count, $this -> RecordSize - $rel_offset ) ;
$data = substr ( $buffer, $rel_offset, $length ) ;
$count -= strlen ( $data ) ;
if ( $callable )
$output ( $data ) ;
else
fwrite ( $output, $data ) ;
while ( $count > 0 )
{
if ( ! ( $buffer = $this -> Cache -> Next ( ) ) )
return ;
$data = substr ( $buffer -> Data, 0, $count ) ;
if ( $callable )
$output ( $data ) ;
else
fwrite ( $output, $data ) ;
$count -= strlen ( $data ) ;
}
}
/*--------------------------------------------------------------------------------------------------------------
NAME
get_contents - Returns file contents as a string.
*-------------------------------------------------------------------------------------------------------------*/
public function get_contents ( )
{
return ( file_get_contents ( $this -> Filename ) ) ;
}
/**************************************************************************************************************
**************************************************************************************************************
**************************************************************************************************************
****** ******
****** ******
****** SEARCH FUNCTIONS ******
****** ******
****** ******
**************************************************************************************************************
**************************************************************************************************************
**************************************************************************************************************/
/*--------------------------------------------------------------------------------------------------------------
NAME
strpos, stripos - Searches for a string.
PROTOTYPE
$pos = $sf -> strpos ( $searched_string, $offset = 0 ) ;
$pos = $sf -> stripos ( $searched_string, $offset = 0 ) ;
DESCRIPTION
Behave like the PHP standard strpos() and stripos() functions.
PARAMETERS
$searched_string (string) -
The string to be searched.
$offset (integer) -
Byte offset where the search should start.
RETURN VALUE
Returns either the byte offset of a found occurrence of $searched_string, or false if the string was not
found in the file.
*-------------------------------------------------------------------------------------------------------------*/
public function strpos ( $searched_string, $offset = 0 )
{ return ( $this -> do_strpos ( $searched_string, $offset, 'strpos' ) ) ; }
public function stripos ( $searched_string, $offset = 0 )
{ return ( $this -> do_strpos ( $searched_string, $offset, 'stripos' ) ) ; }
private function do_strpos ( $searched_string, $offset = 0, $func )
{
$searched_length = strlen ( $searched_string ) ;
// Read the data block that contains the specified offset
$buffer = $this -> Cache -> Get ( $offset ) ;
if ( ! $buffer )
return ( false ) ;
// Compute the relative offset within this data block
$relative_offset = $offset - $buffer -> Offset ;
// Search the string ; if found, we're done
$pos = $func ( $buffer -> Data, $searched_string, $relative_offset ) ;
if ( $pos !== false )
return ( $buffer -> Offset + $pos ) ;
// Otherwise, continue reading blocks until we find something or EOF
while ( $buffer )
{
// Don't forget that the searched string could span two data blocks, so we will do a search on the
// last $searched_length-1 characters of the current buffer plus the next data block
$tail = substr ( $buffer -> Data, $buffer -> Size - $searched_length + 1 ) ;
$old_size = $buffer -> Size ;
$old_offset = $buffer -> Offset ;
$buffer = $this -> Cache -> Next ( ) ;
// There is a next block
if ( $buffer )
{
$data = $tail . $buffer -> Data ; // Catenate the tail of the previous block with the current one
$pos = $func ( $data, $searched_string, 0 ) ; // Do a search on this
// Of cours, since we did a search on a whole block plus some characters from the previous one, we will
// have to perform some adjustments to the value return by the strpos/stripos function.
if ( $pos !== false )
return ( $old_offset + $old_size - $searched_length + 1 + $pos ) ;
}
}
// Everything has been read, the searched string was not found
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
strchr - Searches for a character within a set.
PROTOTYPE
$pos = $sf -> strchr ( $cset, $offset = 0 ) ;
DESCRIPTION
Finds the offset of the first character belonging to $cset.
PARAMETERS
$cset (string) -
The set of characters to be searched.
$offset (integer) -
Byte offset where the search should start.
RETURN VALUE
Returns either the byte offset of the first character found belonging to $cset, or false if no more
characters from $cset are present in the file.
NOTES
Unlike the useless PHP strchr() function, which returns a substring starting with the searched
character or string, but much more like the C strchr() function, which returns a pointer to the found
character, strchr() returns the offset in the file of the searched character(s).
*-------------------------------------------------------------------------------------------------------------*/
public function strchr ( $cset, $offset = 0 )
{
// Read the data block that contains the specified offset
$buffer = $this -> Cache -> Get ( $offset ) ;
if ( ! $buffer )
return ( false ) ;
// Compute the relative offset within this data block
$relative_offset = $offset - $buffer -> Offset ;
// Search for the first character in the specified cset
$length = strcspn ( $buffer -> Data, $cset, $relative_offset ) ;
if ( $length + $relative_offset < $buffer -> Size &&
strpos ( $cset, $buffer -> Data [ $relative_offset + $length ] ) !== false )
return ( $buffer -> Offset + $relative_offset + $length ) ;
// Otherwise, continue reading blocks until we find something or EOF
while ( $buffer )
{
$buffer = $this -> Cache -> Next ( ) ;
if ( $buffer )
{
$length = strcspn ( $buffer -> Data, $cset, 0 ) ;
if ( $length < $buffer -> Size &&
strpos ( $cset, $buffer -> Data [ $length ] ) !== false )
return ( $buffer -> Offset + $length ) ;
}
}
// Everything has been read, no character within the cset was found
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
multistrpos, multistripos - Searches for a string within a set of strings.
PROTOTYPE
$pos = $sf -> multistrpos ( $searched_strings, $offset = 0, &$found_index = null, &$found_string = null ) ;
$pos = $sf -> multistripos ( $searched_strings, $offset = 0, &$found_index = null, &$found_string = null ) ;
DESCRIPTION
Behaves like the PHP standard strpos() and stripos() functions, but can be used to find the first occurrence
of a string within a set of searched strings.
PARAMETERS
$searched_strings (array of strings) -
Strings to be searched.
$offset (integer) -
Byte offset where the search should start.
&$found_index (integer) -
Will receive the index, in the $searched_string array, of the found string.
&$found_string (string) -
Will receive the string contents that have been found in the input file, which may differ from
the searched strings when the case-insensitive version is used.
RETURN VALUE
Returns either the byte offset of a found occurrence in $searched_strings, or false if the string was not
found in the file.
*-------------------------------------------------------------------------------------------------------------*/
public function multistrpos ( $searched_strings, $offset = 0, & $found_index = null, & $found_string = null )
{
return ( $this -> domultistrpos ( false, $searched_strings, $offset, $found_index, $found_string ) ) ;
}
public function multistripos ( $searched_strings, $offset = 0, & $found_index = null, & $found_string = null )
{
return ( $this -> domultistrpos ( true, $searched_strings, $offset, $found_index, $found_string ) ) ;
}
protected function domultistrpos ( $case_insensitive, $searched_strings, $offset = 0, & $found_index = null, & $found_string = null )
{
// Let's assume that the $searched_strings array will be the same from one call to another.
// This is why we will store this static data and reuse it until a different $searched_strings
// array is passed
static $previous_searched_strings = [] ; // Serves to compare $searched_strings with the previous one
static $searched_lengths ; // Store string lengths (they will be computed only once)
static $cset ; // A charset built from the first letter of every search string
// (we will be using the strchr() method to locate the first
// potential candidate for a match)
static $search_count ; // Number of searched strings
static $min_string_length ; // When strchr() succeeded but the string is not the desired one,
// we will go forward of that number of characters
// 1st time call or parameters changed, rebuild our static data
if ( $searched_strings !== $previous_searched_strings )
{
$previous_searched_strings = $searched_strings ;
$searched_lengths = [] ;
$cset = '' ;
$search_count = count ( $searched_strings ) ;
$min_string_length = PHP_INT_MAX ;
// For each string, add its first character to $cset (if not already here), compute string length
// and adjust min string length.
for ( $i = 0 ; $i < $search_count ; $i ++ )
{
$string = $searched_strings [$i] ;
$length = strlen ( $string ) ;
if ( ! $length )
throw ( new \RuntimeException ( "Empty search string found at index #$i." ) ) ;
$searched_lengths [] = $length ;
$ch = $string [0] ;
if ( strpos ( $cset, $ch ) === false )
$cset .= $ch ;
if ( $min_string_length > $length )
$min_string_length = $length ;
}
}
// Loop through file contents until a match is found
$pos = $offset ;
while ( ( $pos = $this -> strchr ( $cset, $pos ) ) !== false )
{
$relpos = $pos - $this -> BufferOffset ; // Relative position in the current buffer
$ch = $this -> Buffer [ $relpos ] ;
// Loop through searched strings to find a match
for ( $i = 0 ; $i < $search_count ; $i ++ )
{
$string = $searched_strings [$i] ;
// Match found
if ( $string [0] == $ch &&
! substr_compare ( $this -> Buffer, $string, $relpos, $searched_lengths [$i], $case_insensitive ) )
{
$found_index = $i ;
$found_string = substr ( $this -> Buffer, $relpos, $searched_lengths [$i] ) ;
return ( $pos ) ;
}
}
// Match not found : skip by then length of the shortest searched string
$pos += $min_string_length ;
}
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
substr - Extracts a substring from the file
PROTOTYPE
$result = $sf -> substr ( $start, $length ) ;
DESCRIPTION
Returns a substring from the searchable file. The $start and $length parameters have the same meaning
that for the php substr() function.
PARAMETERS
The explanation of the following parameters is taken from PHP5.6 documentation ; the word "string",
when designating the 1st parameter of the PHP substr() function, should be read here as "file" :
$start (integer) -
If start is non-negative, the returned string will start at the start'th position in string,
counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a',
the character at position 2 is 'c', and so forth.
If start is negative, the returned string will start at the start'th character from the end of
string.
If string is less than start characters long, FALSE will be returned.
$length (integer) -
If length is given and is positive, the string returned will contain at most length characters
beginning from start (depending on the length of string).
If length is given and is negative, then that many characters will be omitted from the end of
string (after the start position has been calculated when a start is negative).
If start denotes the position of this truncation or beyond, FALSE will be returned.
If length is given and is 0, FALSE or NULL, an empty string will be returned.
If length is omitted, the substring starting from start until the end of the string will be
returned.
RETURN VALUE
Returns the specified substring or false if one of the following conditions occur :
- The file is less than $start bytes
- $length is negative, and goes past $start backwards
An empty string is returned if $length has been specified and is zero (ie, 0, false or null)
*-------------------------------------------------------------------------------------------------------------*/
public function substr ( /* $start, $length */ )
{
$argv = func_get_args ( ) ;
$argc = count ( $argv ) ;
$start = 0 ;
$length = $this -> Filesize ;
// Compute the real start offset and the real length, according to the specifications above
switch ( $argc )
{
case 0 :
throw ( new \RuntimeException ( "substr() expects at least 1 parameter." ) ) ;
case 2 :
$length2 = $argv [1] ;
if ( ! $length2 )
return ( false ) ;
if ( $length2 < 0 )
$length += $length2 ;
else
$length = $length2 ;
case 1 :
$start = $argv [0] ;
if ( $start >= $this -> Filesize )
return ( FALSE ) ;
if ( $start < 0 )
$start = $this -> Filesize + $start ;
break ;
default :
throw ( new \RuntimeException ( "substr() expects at most 2 parameters." ) ) ;
}
if ( $length <= 0 )
return ( false ) ;
// Read the first block that contains the start character
$buffer = $this -> Cache -> Get ( $start ) ;
if ( ! $buffer )
return ( false ) ;
$relpos = $start - $buffer -> Offset ; // Compute relative position in current buffer
$substring = '' ; // Value to be returned
// Loop until $length characters have been extracted
while ( $length > 0 )
{
// Compute the number of bytes we can extract from the current buffer
$remaining = min ( $buffer -> Size - $relpos, $length ) ;
// Get the substring
$substring .= substr ( $buffer -> Data, $relpos, $remaining ) ;
// Subtract the number of characters extracted from the remaining number of
// characters to process
$length -= $remaining ;
$relpos = 0 ;
// If we have still more characters to be extracted, read the next block
if ( $length )
{
$buffer = $this -> Cache -> Next ( ) ;
if ( ! $buffer )
break ;
}
}
// All done, return
return ( $substring ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
pcre_match - Searches for a pattern.
PROTOTYPE
$status = pcre_match ( $pattern, &$matches = null, $flags = 0, $start_offset = 0 ) ;
DESCRIPTION
pcre_match() tries to behave like preg_match(), but operates on a file rather than in memory.
For achieving that, it uses the pcregrep linux command to extract match offset using the --file-offsets
parameter.
PARAMETERS
$pattern (string) -
Pcre pattern to be matched.
$matches (array) -
Array that will receive the match values.
$flags (integer) -
Any PREG_* flags recognized by the PHP preg_match() builtin function.
$start_offset (integer) -
Offset in the file where the search is to be started.
RETURN VALUE
Returns false if some error occurred (the starting offset is beyond the end of the file, or the search
pattern is incorrect) ; otherwise the number of matches is returned (0 or 1).
PERFORMANCES
The performances have been tested on a quad-core system running at 1.8GHz, with 8Gb of memory and a
sample data file of 380Mb, enough to be considered as a "big file", but not too big to allow the
file_get_contents() function to load it into memory.
The file contains Rtf data, and the string '\pict' has been searched all over it. The test has been
performed on a Windows platform.
A single string search gives the following :
- Using the SearchableFile class : 2.08s
- Using file_get_contents() + preg_match() : 0.448s
The overhead between both methods is due to the execution of the pcregrep command, which takes around
1.5s.
Finding all the occurrences of the same pattern gives the following :
- Using the SearchableFile class : 2.155s
- Using file_get_contents() + preg_match() : 0.886s
Running the same test with a 780Mb (which still fits in memory on my system) file gives the following :
- Using the SearchableFile class : 4.36s
- Using file_get_contents() + preg_match() : 1.83s
As a conclusion, pcre_match() will always take longer than preg_match() ; however, it allows to handle
files whose size does not fit into memory.
However, performance delta between both methods should significantly decrease on Linux systems, since
file IO is faster.
NOTES
. pcre_match() first uses the pcregrep command to find the strings matching the specified pattern ; then
it uses the SearchableFile::substr() method to extract that data ; and finally it applies the
preg_match() function to generate an appropriate $matches array. For these reasons, anchor characters
("^" and "$") should be avoided : if you have to cope with huge files, you have to make some
concessions.
. pcregrep command results are cached ; so a next call to the pcre_match() function with the same
pattern will extract the results from the cache, instead of running the pcregrep command again.
*-------------------------------------------------------------------------------------------------------------*/
function pcre_match ( $pattern, &$matches = null, $flags = 0, $start_offset = 0 )
{
static $match_data = [] ;
// Handle extreme cases immediately
if ( $start_offset >= $this -> Filesize )
return ( false ) ;
// Cache the match results if we have a new pattern
if ( ! isset ( $match_data [ $pattern ] ) )
{
$pcre = $this -> transform_regex ( $pattern ) ;
$re = $pcre [0] ;
$re_flags = $pcre [1] ;
$command = "pcregrep --file-offsets $re_flags -e $re {$this -> Filename}" ;
exec ( $command, $output, $status ) ;
if ( $status )
throw ( new \RuntimeException ( "An error occurred during the execution of the pcregrep command :\n" .
implode ( "\n", $output ) ) ) ;
$match_data [ $pattern ] = [] ;
foreach ( $output as $line )
{
list ( $offset, $length ) = explode ( ',', $line ) ;
$match_data [ $pattern ] [] = [ $offset, $length ] ;
}
}
$ranges = $match_data [ $pattern ] ;
// Instead of sequentially searching within the array of matches the first entry that has an offset
// strictly greater than zero, we will first perform some kind of dichotomic search.
// In most cases, the found entry will not be equal to the searched offset, so we will have to
// perform some adjustments after.
// For an array of x matches, it will require log2(x)+a comparisons, where a is the number of
// adjustments needed (between 0 and 2)
$min = 0 ;
$max = count ( $ranges ) - 1 ;
$index = ( integer ) ( ( $max - $min ) / 2 ) ;
while ( $min <= $max )
{
$offset = $ranges [ $index ] [0] ;
if ( $offset < $start_offset )
$min = $index + 1 ;
else if ( $offset > $start_offset )
$max = $index + 1 ;
else
break ;
$old_index = $index ;
$index = $min + ( integer ) ( ( $max - $min ) / 2 ) ;
if ( $old_index == $index )
break ;
}
$adjusted = 0 ;
while ( $index >= 0 && $ranges [ $index ] [0] > $start_offset )
{
$index -- ;
$adjusted ++ ;
}
if ( $adjusted )
$index ++ ;
// Get the matched substring from the searchable file
$match_offset = $ranges [ $index ] [0] ;
$match_length = $ranges [ $index ] [1] ;
$data = $this -> substr ( $match_offset, $match_length ) ;
// Now execute preg_match to get our match array
$status = preg_match ( $pattern, $data, $matches, $flags ) ;
if ( $status && ( $flags & PREG_OFFSET_CAPTURE ) )
{
foreach ( $matches as &$match )
$match [1] += $match_offset ;
}
// All done, return
return ( $status ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
pcre_match_all - Searches for all occurrences of a pattern.
PROTOTYPE
$status = pcre_match_all ( $pattern, &$matches = null, $flags = 0, $start_offset = 0 ) ;
DESCRIPTION
pcre_match_all() tries to behave like preg_match_all(), but operates on a file rather than in memory.
For achieving that, it uses the pcregrep linux command to extract match offset using the --file-offsets
parameter.
PARAMETERS
$pattern (string) -
Pcre pattern to be matched.
$matches (array) -
Array that will receive the match values.
$flags (integer) -
Any PREG_* flags recognized by the PHP preg_match() builtin function.
$start_offset (integer) -
Offset in the file where the search is to be started.
RETURN VALUE
Returns false if some error occurred (the starting offset is beyond the end of the file, or the search
pattern is incorrect, or an individual preg_match() on one of the sub-results failed for some reason) ;
otherwise the number of matches is returned.
PERFORMANCES
The performances have been tested with the same conditions as for the pcre_match() method ; operating
on a 780Mb file gave the following results :
- Using the SearchableFile class : 4.2s
- Using file_get_contents() + preg_match() : 0.811s
*-------------------------------------------------------------------------------------------------------------*/
function pcre_match_all ( $pattern, &$matches = null, $flags = 0, $start_offset = 0 )
{
// Handle extreme cases immediately
if ( $start_offset >= $this -> Filesize )
return ( false ) ;
// Execute the pcregrep command
$pcre = $this -> transform_regex ( $pattern ) ;
$re = $pcre [0] ;
$re_flags = $pcre [1] ;
$command = "pcregrep --file-offsets $re_flags -e $re {$this -> Filename}" ;
exec ( $command, $output, $status ) ;
if ( $status )
throw ( new \RuntimeException ( "An error occurred during the execution of the pcregrep command :\n" .
implode ( "\n", $output ) ) ) ;
// Collect the results
$matches = [] ;
foreach ( $output as $line )
{
list ( $offset, $length ) = explode ( ',', $line ) ;
$data = $this -> substr ( $offset, $length ) ;
$status = preg_match ( $pattern, $data, $data_matches, $flags ) ;
if ( $status && ( $flags & PREG_OFFSET_CAPTURE ) )
{
foreach ( $data_matches as &$match )
$match [1] += $offset ;
$matches [] = $data_matches ;
}
else
return ( $status ) ;
}
// All done, return
return ( count ( $matches ) ) ;
}
/**************************************************************************************************************
**************************************************************************************************************
**************************************************************************************************************
****** ******
****** ******
****** PRIVATE METHODS ******
****** ******
****** ******
**************************************************************************************************************
**************************************************************************************************************
**************************************************************************************************************/
// transform_regex -
// Transforms a regular expression into a form suitable to the pcregrep command, including command-line
// options determined by the regex modifiers.
private function transform_regex ( $re )
{
static $modifiers = 'imsxeADSUXJu' ; // Allowed modifiers
$length = strlen ( $re ) ;
// A regex must contain at list two delimiters and a character within - hence the 3-characters minimal expression
if ( $length < 3 )
throw ( new \RuntimeException ( "Invalid regular expression \"$re\"." ) ) ;
// First character is the opening delimiter
$start = $re [0] ;
// And we must match the ending delimiter ; <>, [], {} and () are authorized as beginning and ending delimiters !
switch ( $start )
{
case '<' : $end = '>' ; break ;
case '[' : $end = ']' ; break ;
case '(' : $end = ')' ; break ;
case '{' : $end = '}' ; break ;
default : $end = $start ;
}
// Scan starting from the last character until we find a closing delimiter
$length -- ;
$found = false ;
$pcregrep_options = [] ;
$extended = false ;
while ( $length > 1 )
{
$ch = $re [ $length ] ;
// Closing delimiter found : we're done
if ( $ch == $end )
{
$found = true ;
break ;
}
// Check that the specified modifiers are authorized
else if ( strpos ( $modifiers, $ch ) === false )
throw ( new \RuntimeException ( "Invalid modifier '$ch' in regular expression." ) ) ;
// If yes, try to emulate what we can
else
{
switch ( $ch )
{
// 'm' modifier :
// Matches multiline patterns.
case 'm' :
$pcregrep_options [] = '-M' ;
break ;
// 'i' modifier :
// Case-insensitive match.
case 'i' :
$pcregrep_options [] = '-i' ;
break ;
// 'x' modifiers :
// Spaces are not significant in a regular expression.
case 'x' :
$extended = true ;
// Other modifiers have no counterpart - silently ignore them
default :
}
}
$length -- ;
}
// Check that a closing delimiter has been found
if ( ! $found )
throw ( new \RuntimeException ( "Missing ending delimiter in regular expression." ) ) ;
// Extract the regular expression, without delimiters and modifiers