Return the start and end position of pairs of brackets.

        https://stackoverflow.com/questions/29991917/
        indices-of-matching-parentheses-in-python
9068
def _find_bracket_position(self, text, bracket_left, bracket_right,
                               pos_quote):
        
        pos = {}
        pstack = []

        for idx, character in enumerate(text):
            if character == bracket_left and \
                    not self.is_char_in_pairs(idx, pos_quote):
                pstack.append(idx)
            elif character == bracket_right and \
                    not self.is_char_in_pairs(idx, pos_quote):
                if len(pstack) == 0:
                    raise IndexError(
                        "No matching closing parens at: " + str(idx))
                pos[pstack.pop()] = idx

        if len(pstack) > 0:
            raise IndexError(
                "No matching opening parens at: " + str(pstack.pop()))

        return pos
780043
public Pair<Integer, Integer> getStartPosition() {
        return Pair.makePair(iterationNumber.get(), epochNumber.get());
    }
339289
def find_enclosing_bracket_right(self, left_ch, right_ch, end_pos=None):
        
        if self.current_char == right_ch:
            return 0

        if end_pos is None:
            end_pos = len(self.text)
        else:
            end_pos = min(len(self.text), end_pos)

        stack = 1

        # Look forward.
        for i in range(self.cursor_position + 1, end_pos):
            c = self.text[i]

            if c == left_ch:
                stack += 1
            elif c == right_ch:
                stack -= 1

            if stack == 0:
                return i - self.cursor_position
1040206
function getMatchingParenthesis(string, startPos) {
    var length = string.length;
    var bracket = 1;
    for (var i=startPos+1; i<=length; i++) {
        if (string[i] == '(') {
            bracket += 1;
        } else if (string[i] == ')') {
            bracket -= 1;
        }
        if (bracket == 0) {
            return i;
        }
    }
}
139848
function getAllNearPositions(position) {
    return [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1] // Below positions
    ].map(function (toAdd) {
        return {
            x: position.x + toAdd[0],
            y: position.y + toAdd[1]
        };
    });
}
560304
def find_line_beginning(strings: Sequence[str],
                        linestart: Optional[str]) -> int:
    
    if linestart is None:  # match an empty line
        for i in range(len(strings)):
            if is_empty_string(strings[i]):
                return i
        return -1
    for i in range(len(strings)):
        if strings[i].find(linestart) == 0:
            return i
    return -1
930531
function(input, startIndex) {
		var begin;
		var end = startIndex;

		//First find the curly bracket that opens this block.
		while(end-- && input.charAt(end) !== '{') {}

		//The end is now fixed to the right of the selector.
		//Now start there to find the begin of the selector.
		begin = end;

		//Now walk farther backwards until we grabbed the whole selector.
		//This either ends at beginning of string or at end of next block.
		while(begin-- && input.charAt(begin - 1) !== '}') {}

		//Return the cleaned selector.
		return input.substring(begin, end).replace(/[\n\r\t]/g, '');
	}
233827
def source_range_tuple(start, end, nr_var_dict):
    

    starts = np.array([0 for nr_var in SOURCE_VAR_TYPES.itervalues()])
    ends = np.array([nr_var_dict[nr_var] if nr_var in nr_var_dict else 0
        for nr_var in SOURCE_VAR_TYPES.itervalues()])
    sum_counts = np.cumsum(ends)
    idx = np.arange(len(starts))

    # Find the intervals containing the
    # start and ending indices
    start_idx, end_idx = np.searchsorted(
        sum_counts, [start, end], side='right')

    # Handle edge cases
    if end >= sum_counts[-1]:
        end = sum_counts[-1]
        end_idx = len(sum_counts) - 1

    # Find out which variable counts fall within the range
    # of the supplied indices and zero those outside this range
    invalid = np.logical_not(np.logical_and(start_idx <= idx, idx <= end_idx))
    starts[invalid] = ends[invalid] = 0

    # Modify the associated starting and ending positions
    starts[start_idx] = start
    ends[end_idx] = end

    if start >= sum_counts[0]:
        starts[start_idx] -= sum_counts[start_idx-1]

    if end >= sum_counts[0]:
        ends[end_idx] -= sum_counts[end_idx-1]

    return OrderedDict((n, (starts[i], ends[i]))
        for i, n in enumerate(SOURCE_VAR_TYPES.values()))
745369
def end_pos
      node_begin_pos = @node.loc.expression.begin_pos
      node_begin_pos += @node.loc.expression.source.index "do"
      while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@'
      end
      node_begin_pos
    end
436640
private boolean goToNextStartPosition() throws IOException {
    int nextSpans1StartPosition;
    int nextSpans1EndPosition;
    int nextSpans2StartPosition;
    int nextSpans2EndPosition;
    // loop over span1
    while ((nextSpans1StartPosition = spans1.spans
        .nextStartPosition()) != NO_MORE_POSITIONS) {
      nextSpans1EndPosition = spans1.spans.endPosition();
      if (noMorePositionsSpan2
          && nextSpans1StartPosition > lastSpans2StartPosition) {
        noMorePositions = true;
        return false;
        // check if start/en span1 matches start/end span2 from last or previous
      } else if ((nextSpans1StartPosition == lastSpans2StartPosition
          && nextSpans1EndPosition == lastSpans2EndPosition)
          || (nextSpans1StartPosition == previousSpans2StartPosition
              && previousSpans2EndPositions.contains(nextSpans1EndPosition))) {
        return true;
      } else {
        // try to find matching span2
        while (!noMorePositionsSpan2
            && nextSpans1StartPosition >= lastSpans2StartPosition) {
          // get new span2
          nextSpans2StartPosition = spans2.spans.nextStartPosition();
          // check for finished span2
          if (nextSpans2StartPosition == NO_MORE_POSITIONS) {
            noMorePositionsSpan2 = true;
          } else {
            // get end for new span2
            nextSpans2EndPosition = spans2.spans.endPosition();
            // check for registering last span2 as previous
            if (nextSpans1StartPosition <= lastSpans2StartPosition) {
              if (previousSpans2StartPosition != lastSpans2StartPosition) {
                previousSpans2StartPosition = lastSpans2StartPosition;
                previousSpans2EndPositions.clear();
              }
              previousSpans2EndPositions.add(lastSpans2EndPosition);
            }
            // register span2 as last
            lastSpans2StartPosition = nextSpans2StartPosition;
            lastSpans2EndPosition = nextSpans2EndPosition;
            // check for match
            if (nextSpans1StartPosition == nextSpans2StartPosition
                && nextSpans1EndPosition == nextSpans2EndPosition) {
              return true;
            }
          }
        }

      }
    }
    noMorePositions = true;
    return false;
  }
448447
function getNthIndex(node)
{
	var child,i;
	var count = 0;
	var parentsChildren = node.parentNode.childNodes;
	var numParentsChildren = parentsChildren.length;
	
	for (i=0; i<numParentsChildren; i++)
	{
		child = parentsChildren[i];
		
		if (child !== node)
		{
			// Exclude text and comments nodes
			if (child.nodeName[0] !== "#")
			{
				count++;
			}
		}
		else
		{
			break;
		}
	}
	
	// `:nth-child()` indices don't start at 0
	return count+1;
}
86047
def _closing_bracket_index(self, text, bpair=('(', ')')):
        """"""
        level = 1
        for i, char in enumerate(text[1:]):
            if char == bpair[0]:
                level += 1
            elif char == bpair[1]:
                level -= 1
            if level == 0:
                return i + 1
115062
def _get_start_end(parts, index=7):
    
    start = parts[1]
    end = [x.split("=")[-1] for x in parts[index].split(";") if x.startswith("END=")]
    if end:
        end = end[0]
        return start, end
    return None, None
534408
def get_answer_begin_end(data):
    '''
    
    '''
    begin = []
    end = []
    for qa_pair in data:
        tokens = qa_pair['passage_tokens']
        char_begin = qa_pair['answer_begin']
        char_end = qa_pair['answer_end']
        word_begin = get_word_index(tokens, char_begin)
        word_end = get_word_index(tokens, char_end)
        begin.append(word_begin)
        end.append(word_end)
    return np.asarray(begin), np.asarray(end)
909367
def range_matches(self, other):
        
        return (
            self.begin == other.begin and 
            self.end == other.end
        )
446237
def bounds(filename, start_re, end_re, encoding='utf8'):
    
    start_re, end_re = re.compile(start_re), re.compile(end_re)
    mo, line_start, line_end, byte_start, byte_end = [None]*5
    offset = 0

    with open(filename, 'rb') as f:
        for index, line in enumerate(f):
            line_text = line.decode(encoding)
            start_match = re.match(start_re, line_text)
            if start_match:
                mo, line_start, byte_start = start_match, index, offset

            offset += len(line)

            end_match = re.match(end_re, line_text)
            if end_match:
                yield mo, Bounds(line_start, index, byte_start, offset)
                mo, line_start, line_end, byte_start, byte_end = [None]*5
1248025
def variant_indices(self):
        
        variant_start_index = len(self.prefix)
        variant_len = len(self.alt)
        variant_end_index = variant_start_index + variant_len
        return variant_start_index, variant_end_index