-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix square bracket checking to account for arbitrary numbers of
open parens.
- Loading branch information
Showing
7 changed files
with
58 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import string | ||
import six | ||
|
||
|
||
def leading_characters(s, length): | ||
def brackets_check(pattern): | ||
""" | ||
Returns non-whitespace leading characters | ||
Check whether the pattern is missing square brackets, in a way which does | ||
not require the usual parsing. This is a light hack to provide an improved | ||
error message in this particular case. | ||
:param str s: The string to process | ||
:param int length: The number of characters to return | ||
:return: The non-whitespace leading characters | ||
:rtype: str or None | ||
:param pattern: A STIX pattern string | ||
:return: True if the pattern had its brackets; False if not | ||
""" | ||
if s is None: | ||
return None | ||
if isinstance(pattern, six.string_types): | ||
# the non-whitespace chars | ||
non_ws_chars = (c for c in pattern if not c.isspace()) | ||
|
||
stripped = [] | ||
for char in s: | ||
if char not in string.whitespace: | ||
stripped.append(char) | ||
# There can be an arbitrary number of open parens first... skip over those | ||
c = next(non_ws_chars, None) | ||
while c == "(": | ||
c = next(non_ws_chars, None) | ||
|
||
upper_bound = min(length, len(stripped)) | ||
return ''.join(stripped[:upper_bound]) | ||
if c == "[": | ||
result = True | ||
else: | ||
result = False | ||
|
||
else: | ||
result = False | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
""" | ||
Test cases for stix2patterns/helpers.py. | ||
""" | ||
import pytest | ||
|
||
from stix2patterns.helpers import leading_characters | ||
from stix2patterns.helpers import brackets_check | ||
|
||
|
||
def test_leading_characters(): | ||
@pytest.mark.parametrize( | ||
"value", [ | ||
'[file:size = 1280]', | ||
' [file:size = 1280]', | ||
'( [file:size = 1280])', | ||
'( ( [file:size = 1280]) )', | ||
'(( ( ( [file:size = 1280])) ))', | ||
'[', | ||
], | ||
) | ||
def test_brackets_check(value): | ||
assert brackets_check(value) | ||
|
||
assert leading_characters('[file:size = 1280]', 2) == '[f' | ||
assert leading_characters(' [file:size = 1280]', 2) == '[f' | ||
assert leading_characters('( [file:size = 1280])', 2) == '([' | ||
assert leading_characters('[', 2) == '[' | ||
assert leading_characters(None, 2) is None | ||
|
||
def test_brackets_check_fail(): | ||
assert not brackets_check(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import re | ||
|
||
import stix2patterns.inspector | ||
|
||
HASHES_REGEX = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import re | ||
|
||
import stix2patterns.inspector | ||
|
||
HASHES_REGEX = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters