MCPcopy Create free account
hub / github.com/alibaba/graph-learn / _GetTextInside

Function _GetTextInside

dynamic_graph_service/ci/cpplint.py:4847–4900  ·  view source on GitHub ↗

r"""Retrieves all the text between matching open and close parentheses. Given a string of lines and a regular expression string, retrieve all the text following the expression and between opening punctuation symbols like (, [, or {, and the matching close-punctuation symbol. This properly nes

(text, start_pattern)

Source from the content-addressed store, hash-verified

4845
4846
4847def _GetTextInside(text, start_pattern):
4848 r"""Retrieves all the text between matching open and close parentheses.
4849
4850 Given a string of lines and a regular expression string, retrieve all the text
4851 following the expression and between opening punctuation symbols like
4852 (, [, or {, and the matching close-punctuation symbol. This properly nested
4853 occurrences of the punctuations, so for the text like
4854 printf(a(), b(c()));
4855 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4856 start_pattern must match string having an open punctuation symbol at the end.
4857
4858 Args:
4859 text: The lines to extract text. Its comments and strings must be elided.
4860 It can be single line and can span multiple lines.
4861 start_pattern: The regexp string indicating where to start extracting
4862 the text.
4863 Returns:
4864 The extracted text.
4865 None if either the opening string or ending punctuation could not be found.
4866 """
4867 # TODO(unknown): Audit cpplint.py to see what places could be profitably
4868 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4869
4870 # Give opening punctuations to get the matching close-punctuations.
4871 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4872 closing_punctuation = set(itervalues(matching_punctuation))
4873
4874 # Find the position to start extracting text.
4875 match = re.search(start_pattern, text, re.M)
4876 if not match: # start_pattern not found in text.
4877 return None
4878 start_position = match.end(0)
4879
4880 assert start_position > 0, (
4881 'start_pattern must ends with an opening punctuation.')
4882 assert text[start_position - 1] in matching_punctuation, (
4883 'start_pattern must ends with an opening punctuation.')
4884 # Stack of closing punctuations we expect to have in text after position.
4885 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4886 position = start_position
4887 while punctuation_stack and position < len(text):
4888 if text[position] == punctuation_stack[-1]:
4889 punctuation_stack.pop()
4890 elif text[position] in closing_punctuation:
4891 # A closing punctuation without matching opening punctuations.
4892 return None
4893 elif text[position] in matching_punctuation:
4894 punctuation_stack.append(matching_punctuation[text[position]])
4895 position += 1
4896 if punctuation_stack:
4897 # Opening punctuations left without matching close-punctuations.
4898 return None
4899 # punctuations match.
4900 return text[start_position:position - 1]
4901
4902
4903# Patterns for matching call-by-reference parameters.

Callers 1

CheckLanguageFunction · 0.85

Calls 4

popMethod · 0.80
searchMethod · 0.45
endMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected