Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding strip function to remove leading and trailing whitespace from … #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions docs/query-guide/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ math, string manipulation or more sophisticated expressions to be expressed.

Returns true if ``b`` is a substring of ``a``

.. function:: strip(s[, leading, trailing])
reidgilman marked this conversation as resolved.
Show resolved Hide resolved

:param: s: The string that will be stripped
:param: leading: strip whitespace from the beginning of ``s``. Default is ``True``.
:param: trailing: strip whitespace from the end of ``s``. Default is ``True``.

Returns a string with whitespace removed from the beginning and end of input string ``s``.

.. function:: subtract(x, y)

Returns ``x - y``
Expand Down
23 changes: 23 additions & 0 deletions eql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,29 @@ def run(cls, source, substring):
return substring.lower() in source.lower()
return False

@register
class Strip(FunctionSignature):
reidgilman marked this conversation as resolved.
Show resolved Hide resolved
"""Strip leading & trailing whitespace from a string."""

name = "strip"
argument_types = [STRING, BOOLEAN, BOOLEAN]
return_value = STRING
minimum_args = 1

@classmethod
def run(cls, source, leading=True, trailing=True):
"""Strip whitespace from source."""

if not is_string(source):
return None

stripped = source
if leading:
stripped = stripped.lstrip()
if trailing:
stripped = stripped.rstrip()

return stripped

@register
class Substring(FunctionSignature):
Expand Down