1313 */
1414final class SequenceMatcher
1515{
16- // people may prefer this for debugging
17- const OP_EQ = ' eq ' ;
18- const OP_DEL = ' del ' ;
19- const OP_INS = ' ins ' ;
20- const OP_REP = ' rep ' ;
21-
22- // people may prefer this for bit operations
23- const OP_INT_EQ = 1 << 0 ;
24- const OP_INT_DEL = 1 << 1 ;
25- const OP_INT_INS = 1 << 2 ;
26- const OP_INT_REP = 1 << 3 ;
16+ const OP_EQ = 1 << 0 ;
17+ const OP_DEL = 1 << 1 ;
18+ const OP_INS = 1 << 2 ;
19+ const OP_REP = 1 << 3 ;
20+
21+ const OP_INT_TO_STR_MAP = [
22+ self :: OP_EQ => ' eq ' ,
23+ self :: OP_DEL => ' del ' ,
24+ self :: OP_INS => ' ins ' ,
25+ self :: OP_REP => ' rep ' ,
26+ ] ;
2727
2828 /**
2929 * @var null|callable either a string or an array containing a callback function to determine if a line is "junk" or not
@@ -61,17 +61,6 @@ final class SequenceMatcher
6161 private static $ defaultOptions = [
6262 'ignoreWhitespace ' => false ,
6363 'ignoreCase ' => false ,
64- 'useIntOpcodes ' => false ,
65- ];
66-
67- /**
68- * @var array opcode constants
69- */
70- private $ ops = [
71- // 'eq' => ?,
72- // 'del' => ?,
73- // 'ins' => ?,
74- // 'rep' => ?,
7564 ];
7665
7766 /**
@@ -117,7 +106,6 @@ public function setOptions(array $options): self
117106 {
118107 $ this ->options = $ options + self ::$ defaultOptions ;
119108
120- $ this ->setOps ($ this ->options ['useIntOpcodes ' ]);
121109 $ this ->resetCachedResults ();
122110
123111 return $ this ;
@@ -190,16 +178,6 @@ public function setSeq2(array $b): self
190178 return $ this ;
191179 }
192180
193- /**
194- * Get the ops.
195- *
196- * @return array the ops
197- */
198- public function getOps (): array
199- {
200- return $ this ->ops ;
201- }
202-
203181 /**
204182 * Find the longest matching block in the two sequences, as defined by the
205183 * lower and upper constraints for each sequence. (for the first sequence,
@@ -418,24 +396,24 @@ public function getOpcodes(): array
418396
419397 foreach ($ this ->getMatchingBlocks () as [$ ai , $ bj , $ size ]) {
420398 if ($ i < $ ai && $ j < $ bj ) {
421- $ tag = $ this -> ops [ ' rep ' ] ;
399+ $ tag = self :: OP_REP ;
422400 } elseif ($ i < $ ai ) {
423- $ tag = $ this -> ops [ ' del ' ] ;
401+ $ tag = self :: OP_DEL ;
424402 } elseif ($ j < $ bj ) {
425- $ tag = $ this -> ops [ ' ins ' ] ;
403+ $ tag = self :: OP_INS ;
426404 } else {
427- $ tag = null ;
405+ $ tag = 0 ;
428406 }
429407
430- if (isset ( $ tag) ) {
408+ if ($ tag ) {
431409 $ this ->opcodes [] = [$ tag , $ i , $ ai , $ j , $ bj ];
432410 }
433411
434412 $ i = $ ai + $ size ;
435413 $ j = $ bj + $ size ;
436414
437415 if ($ size ) {
438- $ this ->opcodes [] = [$ this -> ops [ ' eq ' ] , $ ai , $ i , $ bj , $ j ];
416+ $ this ->opcodes [] = [self :: OP_EQ , $ ai , $ i , $ bj , $ j ];
439417 }
440418 }
441419
@@ -463,11 +441,11 @@ public function getGroupedOpcodes(int $context = 3): array
463441
464442 if (empty ($ opcodes )) {
465443 $ opcodes = [
466- [$ this -> ops [ ' eq ' ] , 0 , 1 , 0 , 1 ],
444+ [self :: OP_EQ , 0 , 1 , 0 , 1 ],
467445 ];
468446 }
469447
470- if ($ opcodes [0 ][0 ] === $ this -> ops [ ' eq ' ] ) {
448+ if ($ opcodes [0 ][0 ] === self :: OP_EQ ) {
471449 $ opcodes [0 ] = [
472450 $ opcodes [0 ][0 ],
473451 \max ($ opcodes [0 ][1 ], $ opcodes [0 ][2 ] - $ context ),
@@ -478,7 +456,7 @@ public function getGroupedOpcodes(int $context = 3): array
478456 }
479457
480458 $ lastItem = \count ($ opcodes ) - 1 ;
481- if ($ opcodes [$ lastItem ][0 ] === $ this -> ops [ ' eq ' ] ) {
459+ if ($ opcodes [$ lastItem ][0 ] === self :: OP_EQ ) {
482460 [$ tag , $ i1 , $ i2 , $ j1 , $ j2 ] = $ opcodes [$ lastItem ];
483461 $ opcodes [$ lastItem ] = [
484462 $ tag ,
@@ -492,7 +470,7 @@ public function getGroupedOpcodes(int $context = 3): array
492470 $ maxRange = $ context << 1 ;
493471 $ groups = $ group = [];
494472 foreach ($ opcodes as [$ tag , $ i1 , $ i2 , $ j1 , $ j2 ]) {
495- if ($ tag === $ this -> ops [ ' eq ' ] && $ i2 - $ i1 > $ maxRange ) {
473+ if ($ tag === self :: OP_EQ && $ i2 - $ i1 > $ maxRange ) {
496474 $ group [] = [
497475 $ tag ,
498476 $ i1 ,
@@ -511,7 +489,7 @@ public function getGroupedOpcodes(int $context = 3): array
511489
512490 if (
513491 !empty ($ group ) &&
514- (\count ($ group ) !== 1 || $ group [0 ][0 ] !== $ this -> ops [ ' eq ' ] )
492+ (\count ($ group ) !== 1 || $ group [0 ][0 ] !== self :: OP_EQ )
515493 ) {
516494 $ groups [] = $ group ;
517495 }
@@ -520,33 +498,21 @@ public function getGroupedOpcodes(int $context = 3): array
520498 }
521499
522500 /**
523- * Set the ops .
501+ * Convert an operation code into string for human reading .
524502 *
525- * @param bool $useIntOpcodes to use int opcodes or not
503+ * @param int $op the operation code
526504 *
527- * @return self
505+ * @throws \InvalidArgumentException
506+ *
507+ * @return string the string representation of the operation code
528508 */
529- private function setOps ( bool $ useIntOpcodes ): self
509+ public static function opIntToStr ( int $ op ): string
530510 {
531- if ($ useIntOpcodes ) {
532- $ this ->ops = [
533- 'del ' => self ::OP_INT_DEL ,
534- 'eq ' => self ::OP_INT_EQ ,
535- 'ins ' => self ::OP_INT_INS ,
536- 'rep ' => self ::OP_INT_REP ,
537- ];
538- } else {
539- $ this ->ops = [
540- 'del ' => self ::OP_DEL ,
541- 'eq ' => self ::OP_EQ ,
542- 'ins ' => self ::OP_INS ,
543- 'rep ' => self ::OP_REP ,
544- ];
511+ if (!isset (self ::OP_INT_TO_STR_MAP [$ op ])) {
512+ throw new \InvalidArgumentException ("Invalid OP: {$ op }" );
545513 }
546514
547- $ this ->resetCachedResults ();
548-
549- return $ this ;
515+ return self ::OP_INT_TO_STR_MAP [$ op ];
550516 }
551517
552518 /**
0 commit comments