MCPcopy Create free account
hub / github.com/OpenPTrack/open_ptrack_v2 / _GetTextInside

Function _GetTextInside

rtpose_wrapper/scripts/cpp_lint.py:3752–3805  ·  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

3750
3751
3752def _GetTextInside(text, start_pattern):
3753 r"""Retrieves all the text between matching open and close parentheses.
3754
3755 Given a string of lines and a regular expression string, retrieve all the text
3756 following the expression and between opening punctuation symbols like
3757 (, [, or {, and the matching close-punctuation symbol. This properly nested
3758 occurrences of the punctuations, so for the text like
3759 printf(a(), b(c()));
3760 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
3761 start_pattern must match string having an open punctuation symbol at the end.
3762
3763 Args:
3764 text: The lines to extract text. Its comments and strings must be elided.
3765 It can be single line and can span multiple lines.
3766 start_pattern: The regexp string indicating where to start extracting
3767 the text.
3768 Returns:
3769 The extracted text.
3770 None if either the opening string or ending punctuation could not be found.
3771 """
3772 # TODO(sugawarayu): Audit cpplint.py to see what places could be profitably
3773 # rewritten to use _GetTextInside (and use inferior regexp matching today).
3774
3775 # Give opening punctuations to get the matching close-punctuations.
3776 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
3777 closing_punctuation = set(matching_punctuation.itervalues())
3778
3779 # Find the position to start extracting text.
3780 match = re.search(start_pattern, text, re.M)
3781 if not match: # start_pattern not found in text.
3782 return None
3783 start_position = match.end(0)
3784
3785 assert start_position > 0, (
3786 'start_pattern must ends with an opening punctuation.')
3787 assert text[start_position - 1] in matching_punctuation, (
3788 'start_pattern must ends with an opening punctuation.')
3789 # Stack of closing punctuations we expect to have in text after position.
3790 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
3791 position = start_position
3792 while punctuation_stack and position < len(text):
3793 if text[position] == punctuation_stack[-1]:
3794 punctuation_stack.pop()
3795 elif text[position] in closing_punctuation:
3796 # A closing punctuation without matching opening punctuations.
3797 return None
3798 elif text[position] in matching_punctuation:
3799 punctuation_stack.append(matching_punctuation[text[position]])
3800 position += 1
3801 if punctuation_stack:
3802 # Opening punctuations left without matching close-punctuations.
3803 return None
3804 # punctuations match.
3805 return text[start_position:position - 1]
3806
3807
3808# Patterns for matching call-by-reference parameters.

Callers 1

CheckLanguageFunction · 0.85

Calls 2

popMethod · 0.80
endMethod · 0.45

Tested by

no test coverage detected