
    <i*                         S r SSKrSSKrSSKJrJr  SSKJrJrJ	r	J
r
JrJrJrJrJrJrJrJr  Sr\\	\\\\4rS rS rS rS	 rS
 r\\\	\\\\\\\\\0rS rS rS rS rS rS r S r!g)z?
Comparison utilities for STIX pattern comparison expressions.
    N)generic_cmpiter_lex_cmp)AndBooleanExpressionBinaryConstantBooleanConstantFloatConstantHexConstantIntegerConstantListConstantListObjectPathComponentOrBooleanExpressionStringConstantTimestampConstant_ComparisonExpression)=z!=z<><z<=>z>=INLIKEMATCHESISSUBSET
ISSUPERSETc                 B    [        U R                  UR                  5      $ )aN  
Generic comparator for most _Constant instances.  They must have a "value"
attribute whose value supports the builtin comparison operators.

:param const1: The first _Constant instance
:param const2: The second _Constant instance
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
)r   value)const1const2s     g/home/james-whalen/.local/lib/python3.13/site-packages/stix2/equivalence/patterns/compare/comparison.pygeneric_constant_cmpr      s     v||V\\22    c                     U R                   n UR                   nU (       a  U(       d  U (       d  U(       d  SnU$ U (       a  SnU$ SnU$ )z
Compare two boolean constants.

:param value1: The first BooleanConstant instance
:param value2: The second BooleanConstant instance
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
r      )r   )value1value2results      r   bool_cmpr&   +   sH     \\F\\F66& M 

 M Mr   c                     [         R                  U R                  5      n[         R                  UR                  5      n[        X#5      $ )a;  
Compare two STIX "hex" values.  This decodes to bytes and compares that.
It does *not* do a string compare on the hex representations.

:param value1: The first HexConstant
:param value2: The second HexConstant
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
)bytesfromhexr   r   r#   r$   bytes1bytes2s       r   hex_cmpr-   F   s3     ]]6<<(F]]6<<(Fv&&r   c                     [         R                  " U R                  5      n[         R                  " UR                  5      n[        X#5      $ )aG  
Compare two STIX "binary" values.  This decodes to bytes and compares that.
It does *not* do a string compare on the base64 representations.

:param value1: The first BinaryConstant
:param value2: The second BinaryConstant
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
)base64standard_b64decoder   r   r*   s       r   bin_cmpr1   V   s7     &&v||4F&&v||4Fv&&r   c                     [        U R                  [        R                  " [        5      S9n[        UR                  [        R                  " [        5      S9n[        X#[        5      nU$ )z
Compare lists order-insensitively.

:param value1: The first ListConstant
:param value2: The second ListConstant
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
)key)sortedr   	functools
cmp_to_keyconstant_cmpr   )r#   r$   sorted_value1sorted_value2r%   s        r   list_cmpr:   f   sU     )..|<M )..|<M -EFMr   c                     [        U [        5      (       a  [        U[        5      (       d*  [        U [        5      (       a"  [        U[        5      (       a  [        X5      nU$ [        U [        5      (       a  SnU$ SnU$ )a  
Compare a string/int to another string/int; this induces an ordering over
all strings and ints.  It is used to perform a lexicographical sort on
object paths.

Ints and strings compare as usual to each other; ints compare less than
strings.

:param comp1: An object path component (string or int)
:param comp2: An object path component (string or int)
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
r!   r"   )
isinstanceintstrr   )comp1comp2r%   s      r   object_path_component_cmprA      sn      	5#:eS#9#95#&&:eS+A+AU* M 
E3		
 M Mr   c              #   D  #    U R                    H  n[        U[        5      (       af  UR                  v   UR                  S:X  d  [        UR                  [
        5      (       a  UR                  v   Me  [        UR                  5      v   M~  UR                  v   M     g7f)a:  
Converts the given ObjectPath instance to a list of strings and ints.
All property names become strings, regardless of whether they're *_ref
properties; "*" index steps become that string; and numeric index steps
become integers.

:param path: An ObjectPath instance
:return: A generator iterator over the values
*N)property_pathr<   r   property_nameindexr=   )pathcomps     r   object_path_to_raw_valuesrI      su      ""d344$$$zzS Jtzz3$?$?jj  $**o% $$$ #s   BB c                     U R                   UR                   :  a  SnU$ U R                   UR                   :  a  SnU$ [        U 5      n[        U5      n[        X4[        5      nU$ )z
Compare two object paths.

:param path1: The first ObjectPath instance
:param path2: The second ObjectPath instance
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
r!   r"   )object_type_namerI   r   rA   )path1path2r%   
path_vals1
path_vals2s        r   object_path_cmprP      ss      6 66  M 
		%"8"8	8 M /u5
.u5
$=
 Mr   c                 p    [         R                  U 5      n[         R                  U5      n[        X#5      nU$ )z
Compare two comparison operators.

:param op1: The first comparison operator (a string)
:param op2: The second comparison operator (a string)
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
)_COMPARISON_OP_ORDERrF   r   )op1op2op1_idxop2_idxr%   s        r   comparison_operator_cmprW      s3     #((-G"((-G*FMr   c                 "   [        U [        [        45      (       a(  [        U[        [        45      (       a  [        X5      nU$ [        U [        [        45      (       a  SnU$ [        U[        [        45      (       a  SnU$ [	        U 5      n[	        U5      n[
        R                  U5      n[
        R                  U5      n[        XV5      nUS:X  a<  [        R                  U5      nU(       d  [        SUR                  -   5      eU" X5      nU$ )z
Compare two constants.

:param value1: The first _Constant instance
:param value2: The second _Constant instance
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
r!   r"   r   zDon't know how to compare )r<   r
   r   r   type_CONSTANT_TYPE_ORDERrF   r   _CONSTANT_COMPARATORSget	TypeError__name__)r#   r$   r%   type1type2	type1_idx	type2_idxcmp_funcs           r   r7   r7      s     &?M:;;6O]#CDD%f52 M/ 
F_m<	=	=, M) 
F_m<	=	=& M VV(..u5	(..u5	Y2Q;,007H <u~~ MNNf-FMr   c                 |   [        U R                  UR                  5      nUS:X  a   [        U R                  UR                  5      nUS:X  aI  U R                  (       d  UR                  (       a  SnO$U R                  (       a  UR                  (       d  SnUS:X  a   [        U R                  UR                  5      nU$ )aG  
Compare "simple" comparison expressions: those which aren't AND/OR
combinations, just <path> <op> <value> comparisons.

:param expr1: first _ComparisonExpression instance
:param expr2: second _ComparisonExpression instance
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
r   r!   r"   )rP   lhsrW   operatornegatedr7   rhsexpr1expr2r%   s      r    simple_comparison_expression_cmprl     s     UYY		2F{(H{ }}F]]5==F{eii3Mr   c                    [        U [        5      (       a"  [        U[        5      (       a  [        X5      nU$ [        U [        5      (       a  SnU$ [        U[        5      (       a  SnU$ [        U [        5      (       a  [        U[        5      (       a  SnU$ [        U [        5      (       a  [        U[        5      (       a  SnU$ [        U R                  UR                  [        5      nU$ )a  
Compare two comparison expressions.  This is sensitive to the order of the
expressions' sub-components.  To achieve an order-insensitive comparison,
the ASTs must be canonically ordered first.

:param expr1: The first comparison expression
:param expr2: The second comparison expression
:return: <0, 0, or >0 depending on whether the first arg is less, equal or
    greater than the second
r!   r"   )r<   r   rl   r   r   r   operandscomparison_expression_cmpri   s      r   ro   ro   8  s     %.//5"7881%?4 M/ 
E0	1	1, M) 
E0	1	1& M! 
E/	0	05"566 M 
E.	/	/5"677 M	 NNENN,E
 Mr   )"__doc__r/   r5   "stix2.equivalence.patterns.comparer   r   stix2.patternsr   r   r   r   r	   r
   r   r   r   r   r   r   rR   rZ   r   r&   r-   r1   r:   r[   rA   rI   rP   rW   r7   rl   ro    r   r   <module>rt      s      H     O{NL 
36' ' 6 (X+G(	 <%4:"'T<'r   