package strutil

// MatchSurround returns the matching character of a rune that
// is either a bracket/brace/parenthesis, or a single/double quote.
func ( rune) (,  rune) {
	 = 
	 = 

	switch  {
	case '{':
		 = '}'
	case '(':
		 = ')'
	case '[':
		 = ']'
	case '<':
		 = '>'
	case '}':
		 = '{'
		 = '}'
	case ')':
		 = '('
		 = ')'
	case ']':
		 = '['
		 = ']'
	case '>':
		 = '<'
		 = '>'
	case '"':
		 = '"'
		 = '"'
	case '\'':
		 = '\''
		 = '\''
	}

	return , 
}

// IsSurround returns true if the character is a quote or a bracket/brace, etc.
func (,  rune) bool {
	switch  {
	case '{':
		return  == '}'
	case '(':
		return  == ')'
	case '[':
		return  == ']'
	case '<':
		return  == '>'
	case '"':
		return  == '"'
	case '\'':
		return  == '\''
	}

	return  == 
}

// SurroundType says if the character is a pairing one (first boolean),
// and if the character is the closing part of the pair (second boolean).
func ( rune) (,  bool) {
	switch  {
	case '{':
		return true, false
	case '}':
		return true, true
	case '(':
		return true, false
	case ')':
		return true, true
	case '[':
		return true, false
	case ']':
		return true, true
	case '<':
		return true, false
	case '>':
	case '"':
		return true, true
	case '\'':
		return true, true
	}

	return false, false
}

// AdjustSurroundQuotes returns the correct mark and cursor positions when
// we want to know where a shell word enclosed with quotes (and potentially
// having inner ones) starts and ends.
func (, , ,  int) (,  int) {
	 = -1
	 = -1

	if ( == -1 ||  == -1) && ( == -1 ||  == -1) {
		return
	}

	 := ( <  && // Outermost
		 >= 0 && // Double found
		 >= 0 && // compared with a found single
		 > ) // ensuring that we are not comparing unfound

	 := ( <  &&
		 >= 0 &&
		 >= 0 &&
		 > )

	if ( == -1 ||  == -1) ||  {
		 = 
		 = 
	} else if ( == -1 ||  == -1) ||  {
		 = 
		 = 
	}

	return
}

// IsBracket returns true if the character is an opening/closing bracket/brace/parenthesis.
func ( rune) bool {
	if  == '(' ||
		 == ')' ||
		 == '{' ||
		 == '}' ||
		 == '[' ||
		 == ']' {
		return true
	}

	return false
}

// GetQuotedWordStart returns the position of the outmost containing quote
// of the word (going backward from the end of the provided line), if the
// current word is a shell word that is not closed yet.
// Ex: `this 'quote contains "surrounded" words`. the outermost quote is the single one.
func ( []rune) ( bool,  int) {
	var (
		,  bool
		,      = -1, -1
	)

	for ,  := range  {
		switch  {
		case '\'':
			 = !
			 = 
		case '"':
			 = !
			 = 
		default:
			continue
		}
	}

	if  &&  {
		 = true

		if  <  {
			 = 
		} else {
			 = 
		}

		return
	}

	if  {
		 = true
		 = 
	} else if  {
		 = true
		 = 
	}

	return , 
}