MCPcopy Create free account
hub / github.com/alibaba/GraphScope / _GetTextInside

Function _GetTextInside

analytical_engine/misc/cpplint.py:5141–5194  ·  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

5139
5140
5141def _GetTextInside(text, start_pattern):
5142 r"""Retrieves all the text between matching open and close parentheses.
5143
5144 Given a string of lines and a regular expression string, retrieve all the text
5145 following the expression and between opening punctuation symbols like
5146 (, [, or {, and the matching close-punctuation symbol. This properly nested
5147 occurrences of the punctuations, so for the text like
5148 printf(a(), b(c()));
5149 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
5150 start_pattern must match string having an open punctuation symbol at the end.
5151
5152 Args:
5153 text: The lines to extract text. Its comments and strings must be elided.
5154 It can be single line and can span multiple lines.
5155 start_pattern: The regexp string indicating where to start extracting
5156 the text.
5157 Returns:
5158 The extracted text.
5159 None if either the opening string or ending punctuation could not be found.
5160 """
5161 # TODO(unknown): Audit cpplint.py to see what places could be profitably
5162 # rewritten to use _GetTextInside (and use inferior regexp matching today).
5163
5164 # Give opening punctuations to get the matching close-punctuations.
5165 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
5166 closing_punctuation = set(itervalues(matching_punctuation))
5167
5168 # Find the position to start extracting text.
5169 match = re.search(start_pattern, text, re.M)
5170 if not match: # start_pattern not found in text.
5171 return None
5172 start_position = match.end(0)
5173
5174 assert start_position > 0, (
5175 'start_pattern must ends with an opening punctuation.')
5176 assert text[start_position - 1] in matching_punctuation, (
5177 'start_pattern must ends with an opening punctuation.')
5178 # Stack of closing punctuations we expect to have in text after position.
5179 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
5180 position = start_position
5181 while punctuation_stack and position < len(text):
5182 if text[position] == punctuation_stack[-1]:
5183 punctuation_stack.pop()
5184 elif text[position] in closing_punctuation:
5185 # A closing punctuation without matching opening punctuations.
5186 return None
5187 elif text[position] in matching_punctuation:
5188 punctuation_stack.append(matching_punctuation[text[position]])
5189 position += 1
5190 if punctuation_stack:
5191 # Opening punctuations left without matching close-punctuations.
5192 return None
5193 # punctuations match.
5194 return text[start_position:position - 1]
5195
5196
5197# Patterns for matching call-by-reference parameters.

Callers 1

CheckLanguageFunction · 0.85

Calls 3

endMethod · 0.65
appendMethod · 0.65
popMethod · 0.45

Tested by

no test coverage detected