Skip to content

Commit

Permalink
Fix issue with whitespace and negative operators
Browse files Browse the repository at this point in the history
  • Loading branch information
JiFish committed May 17, 2022
1 parent b736491 commit 373170b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ezdice.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace ezdice;

class EZDice {
// Magic dice & modifier matching regex
private $re = '(?<operator>[\+-])?\s*(?<number>\d+)(?:[dD](?<sides>(?:\d+|%))(?:-(?<variant>[LlHh]))?)?';

// Stores information on last roll
private $total = 0;
private $states = [];
private $modifier = 0;
Expand All @@ -22,8 +26,7 @@ public function roll(string $diceStr)
}

// Search for dice groups and modifiers
$re = '/(?<operator>[\+-])?(?<number>\d+)(?:[dD](?<sides>(?:\d+|%))(?:-(?<variant>[LlHh]))?)?/m';
preg_match_all($re, $diceStr, $matches, PREG_SET_ORDER, 0);
preg_match_all("/{$this->re}/", $diceStr, $matches, PREG_SET_ORDER, 0);

// Returning false if no matches found
if (sizeof($matches) == 0) return false;
Expand Down

0 comments on commit 373170b

Please sign in to comment.