Skip to content

proposal: strings: add isEnclosed and extractEnclosed #78358

Description

@mazzoccantelorenzo

Proposal Details

Description
Added isEnclosed and extractEnclosed helpers to handle string parsing. These functions allow detecting and extracting content between specific delimiters (like quotes or brackets) by calculating the correct offset, avoiding issues when start and end symbols are identical.

Why
Needed for cleaner string manipulation and to handle quoted arguments or environment variables without pulling in heavy external dependencies.

// isEnclosed checks if a string is enclosed by the given start and end symbols
func isEnclosed(text, start, end string) bool {
	return strings.HasPrefix(text, start) && strings.HasSuffix(text, end)
}
// extractEnclosed returns the content between the start and end delimiters
func extractEnclosed(text, start, end string) string {
	sIdx := strings.Index(text, start)
	if sIdx == -1 { return "" }
	cutStart := sIdx + len(start)
	eIdx := strings.Index(text[cutStart:], end)
	if eIdx == -1 { return "" }
	return text[cutStart : cutStart+eIdx]
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions