-
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.
Merge pull request #64 from squioc/fix/ValidatorErrorOnMisPlacedWhite…
…space Strip whitespace characters from leading characters
- Loading branch information
Showing
4 changed files
with
41 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import string | ||
|
||
|
||
def leading_characters(s, length): | ||
""" | ||
Returns non-whitespace leading characters | ||
: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 | ||
""" | ||
if s is None: | ||
return None | ||
|
||
stripped = [] | ||
for char in s: | ||
if char not in string.whitespace: | ||
stripped.append(char) | ||
|
||
upper_bound = min(length, len(stripped)) | ||
return ''.join(stripped[:upper_bound]) |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Test cases for stix2patterns/helpers.py. | ||
""" | ||
|
||
from stix2patterns.helpers import leading_characters | ||
|
||
|
||
def test_leading_characters(): | ||
|
||
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 |
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