Skip to content

Commit

Permalink
Added Text::Patterns::HEX_BYTE (issue #553).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Dec 11, 2024
1 parent 46936f2 commit 4672b3a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/ronin/support/text/patterns/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ module Patterns
# @since 0.4.0
DECIMAL_OCTET = DECIMAL_BYTE

# Regular expression for finding hexadecimal bytes (00 - ff).
#
# @since 1.2.0
HEX_BYTE = /(?:0x)?[0-9a-fA-F]{2}/

# Regular expression for finding all hexadecimal numbers in text.
#
# @since 1.0.0
Expand Down
34 changes: 34 additions & 0 deletions spec/text/patterns/numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@
end
end

describe "HEX_BYTE" do
subject { described_class::HEX_BYTE }

it "must match 00 - ff" do
hex_bytes = (0..255).map { |byte| "%.2x" % byte }

expect(hex_bytes).to all(match(subject))
end

it "must match 00 - FF" do
hex_bytes = (0..255).map { |byte| "%.2X" % byte }

expect(hex_bytes).to all(match(subject))
end

it "must only match two hexadecimal digits" do
string = "a1b2"

expect(string[subject]).to eq("a1")
end

it "must match 0x00 - 0xff" do
hex_bytes = (0..255).map { |byte| "0x%.2x" % byte }

expect(hex_bytes).to all(match(subject))
end

it "must match 0x00 - 0xFF" do
hex_bytes = (0..255).map { |byte| "0x%.2X" % byte }

expect(hex_bytes).to all(match(subject))
end
end

describe "HEX_NUMBER" do
subject { described_class::HEX_NUMBER }

Expand Down

0 comments on commit 4672b3a

Please sign in to comment.