MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / _get_substring_expression

Function _get_substring_expression

src/packagedcode/maven.py:923–960  ·  view source on GitHub ↗

Return a tuple of (text, start/end) such that: - if there is a substring() expression in text, the returned text has been stripped from it and start/end is a tuple representing slice indexes for the substring expression. - if there is no substring() expression in text, text is

(text)

Source from the content-addressed store, hash-verified

921
922
923def _get_substring_expression(text):
924 """
925 Return a tuple of (text, start/end) such that:
926
927 - if there is a substring() expression in text, the returned text
928 has been stripped from it and start/end is a tuple representing
929 slice indexes for the substring expression.
930
931 - if there is no substring() expression in text, text is returned
932 as-is and start/end is None.
933
934 For example:
935 >>> assert ('pom.artifactId', (8, None)) == _get_substring_expression('pom.artifactId.substring(8)')
936 >>> assert ('pom.artifactId', None) == _get_substring_expression('pom.artifactId')
937 """
938 key, _, start_end = text.partition('.substring(')
939 if not start_end:
940 return text, None
941
942 start_end = start_end.rstrip(')')
943 start_end = [se.strip() for se in start_end.split(',')]
944
945 # we cannot parse less than 1 and more than 2 slice indexes
946 if len(start_end) not in (1, 2):
947 return text, None
948
949 # we cannot parse slice indexes that are not numbers
950 if not all(se.isdigit() for se in start_end):
951 return text, None
952 start_end = [int(se) for se in start_end]
953
954 if len(start_end) == 1:
955 start = start_end[0]
956 end = None
957 else:
958 start, end = start_end
959
960 return key, (start, end)
961
962
963def substring(s, start, end):

Callers 1

subfuncMethod · 0.85

Calls 1

splitMethod · 0.45

Tested by

no test coverage detected