Skip to content

Commit b95a045

Browse files
committed
Add SequenceMatcher::opStrToInt()
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent bed369c commit b95a045

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/SequenceMatcher.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ final class SequenceMatcher
2727
self::OP_REP => 'rep',
2828
];
2929

30+
const OP_STR_TO_INT_MAP = [
31+
'nop' => self::OP_NOP,
32+
'eq' => self::OP_EQ,
33+
'del' => self::OP_DEL,
34+
'ins' => self::OP_INS,
35+
'rep' => self::OP_REP,
36+
];
37+
3038
/**
3139
* @var null|callable either a string or an array containing a callback function to determine if a line is "junk" or not
3240
*/
@@ -485,7 +493,7 @@ public function getGroupedOpcodes(int $context = 3): array
485493
}
486494

487495
/**
488-
* Convert an operation code into string for human reading.
496+
* Convert an operation code from int into its string form.
489497
*
490498
* @param int $op the operation code
491499
*
@@ -502,6 +510,24 @@ public static function opIntToStr(int $op): string
502510
return self::OP_INT_TO_STR_MAP[$op];
503511
}
504512

513+
/**
514+
* Convert an operation code from string into its int form.
515+
*
516+
* @param string $op the operation code
517+
*
518+
* @throws \InvalidArgumentException
519+
*
520+
* @return int the int representation of the operation code
521+
*/
522+
public static function opStrToInt(string $op): int
523+
{
524+
if (!isset(self::OP_STR_TO_INT_MAP[$op])) {
525+
throw new \InvalidArgumentException("Invalid OP: {$op}");
526+
}
527+
528+
return self::OP_STR_TO_INT_MAP[$op];
529+
}
530+
505531
/**
506532
* Check if the two lines at the given indexes are different or not.
507533
*

0 commit comments

Comments
 (0)