#[must_use]
#[allow(dead_code)]
pub fn tokenize_with_equals_and_quotes(input: &str) -> Vec<(String, bool, bool)> {
    let mut tokens = Vec::new();
    let mut current_token = String::new();
    let mut in_quotes = false;
    let mut quote_started_at_beginning = false;
    let mut value_quoted = false;
    let mut chars = input.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '\\' if in_quotes => {
                // Handle escape sequences inside quotes
                if let Some(&next_ch) = chars.peek() {
                    match next_ch {
                        '"' => {
                            // Escaped quote - add literal quote to token
                            current_token.push('"');
                            chars.next(); // consume the quote
                        }
                        '\\' => {
                            // Escaped backslash - add literal backslash to token
                            current_token.push('\\');
                            chars.next(); // consume the backslash
                        }
                        _ => {
                            // Other escaped characters - keep the backslash and the character
                            current_token.push('\\');
                            current_token.push(chars.next().unwrap());
                        }
                    }
                } else {
                    // Backslash at end of input - add it as-is
                    current_token.push('\\');
                }
            }
            '"' => {
                if in_quotes {
                    // Closing quote
                    if quote_started_at_beginning {
                        // Quote started at beginning of token - check if we should continue
                        // Look ahead to see if there's an equals sign (indicating key=value)
                        let should_continue = chars.peek() == Some(&'=');

                        if should_continue {
                            // Continue building the token (don't finish it yet)
                            quote_started_at_beginning = false;
                        } else {
                            // Finish the token
                            tokens.push((current_token.clone(), true, value_quoted)); // was quoted
                            current_token.clear();
                            quote_started_at_beginning = false;
                            value_quoted = false;
                        }
                    }
                    // Otherwise, quote in middle - ignore it
                    in_quotes = false;
                } else {
                    // Opening quote
                    if current_token.is_empty() {
                        // Quote at beginning of token
                        quote_started_at_beginning = true;
                        in_quotes = true;
                    } else if current_token.ends_with('=') {
                        // Quote immediately after equals - this is a quoted value
                        value_quoted = true;
                        in_quotes = true;
                    }
                    // Otherwise, quote in middle - ignore it
                }
            }
            ' ' | '\t' if !in_quotes => {
                if !current_token.is_empty() {
                    tokens.push((
                        current_token.clone(),
                        quote_started_at_beginning,
                        value_quoted,
                    ));
                    current_token.clear();
                }
                quote_started_at_beginning = false;
                value_quoted = false;
                // Skip whitespace
                while let Some(&next_ch) = chars.peek() {
                    if next_ch == ' ' || next_ch == '\t' {
                        chars.next();
                    } else {
                        break;
                    }
                }
            }
            _ => {
                current_token.push(ch);
            }
        }
    }

    // Add final token (including empty tokens from unclosed quotes)
    if !current_token.is_empty() || in_quotes {
        tokens.push((current_token, quote_started_at_beginning, value_quoted));
    }

    tokens
}

#[must_use]
pub fn tokenize_with_equals(input: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let mut current_token = String::new();
    let mut in_quotes = false;
    let mut quote_started_at_beginning = false;
    let mut chars = input.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '\\' if in_quotes => {
                // Handle escape sequences inside quotes
                if let Some(&next_ch) = chars.peek() {
                    match next_ch {
                        '"' => {
                            // Escaped quote - add literal quote to token
                            current_token.push('"');
                            chars.next(); // consume the quote
                        }
                        '\\' => {
                            // Escaped backslash - add literal backslash to token
                            current_token.push('\\');
                            chars.next(); // consume the backslash
                        }
                        _ => {
                            // Other escaped characters - keep the backslash and the character
                            current_token.push('\\');
                            current_token.push(chars.next().unwrap());
                        }
                    }
                } else {
                    // Backslash at end of input - add it as-is
                    current_token.push('\\');
                }
            }
            '"' => {
                if in_quotes {
                    // Closing quote
                    if quote_started_at_beginning {
                        // Quote started at beginning of token - check if we should continue
                        // Look ahead to see if there's an equals sign (indicating key=value)
                        let should_continue = chars.peek() == Some(&'=');

                        if should_continue {
                            // Continue building the token (don't finish it yet)
                            quote_started_at_beginning = false;
                        } else {
                            // Finish the token
                            tokens.push(current_token.clone());
                            current_token.clear();
                            quote_started_at_beginning = false;
                        }
                    }
                    // Otherwise, quote in middle - ignore it
                    in_quotes = false;
                } else {
                    // Opening quote
                    if current_token.is_empty() {
                        // Quote at beginning of token
                        quote_started_at_beginning = true;
                        in_quotes = true;
                    } else if current_token.ends_with('=') {
                        // Quote immediately after equals - this is a quoted value
                        quote_started_at_beginning = true;
                        in_quotes = true;
                    }
                    // Otherwise, quote in middle - ignore it
                }
            }
            ' ' | '\t' if !in_quotes => {
                if !current_token.is_empty() {
                    tokens.push(current_token.clone());
                    current_token.clear();
                }
                quote_started_at_beginning = false;
                // Skip whitespace
                while let Some(&next_ch) = chars.peek() {
                    if next_ch == ' ' || next_ch == '\t' {
                        chars.next();
                    } else {
                        break;
                    }
                }
            }
            _ => {
                current_token.push(ch);
            }
        }
    }

    // Add final token (including empty tokens from unclosed quotes)
    if !current_token.is_empty() || in_quotes {
        tokens.push(current_token);
    }

    tokens
}

#[must_use]
pub fn find_first_equals_outside_quotes(token: &str) -> Option<usize> {
    let mut in_quotes = false;
    let mut quote_started_at_beginning = false;
    let mut char_indices = token.char_indices().peekable();

    while let Some((byte_pos, ch)) = char_indices.next() {
        match ch {
            '\\' if in_quotes => {
                // Skip escaped characters
                if let Some(&(_, next_ch)) = char_indices.peek() {
                    if next_ch == '"' || next_ch == '\\' {
                        char_indices.next(); // consume the escaped character
                    }
                }
            }
            '"' => {
                if byte_pos == 0 {
                    // Quote at the beginning starts quote mode
                    in_quotes = true;
                    quote_started_at_beginning = true;
                } else if in_quotes && quote_started_at_beginning {
                    // Closing quote that started at beginning
                    in_quotes = false;
                    quote_started_at_beginning = false;
                }
                // Quotes in middle of token are ignored
            }
            '=' if !in_quotes => {
                return Some(byte_pos);
            }
            _ => {}
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tokenize_with_equals_and_quotes_empty() {
        let result = tokenize_with_equals_and_quotes("");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_simple() {
        let result = tokenize_with_equals_and_quotes("hello world");
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], ("hello".to_string(), false, false));
        assert_eq!(result[1], ("world".to_string(), false, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_quoted() {
        let result = tokenize_with_equals_and_quotes("\"hello world\"");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("hello world".to_string(), true, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_key_value() {
        let result = tokenize_with_equals_and_quotes("key=value");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("key=value".to_string(), false, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_quoted_key() {
        let result = tokenize_with_equals_and_quotes("\"quoted key\"=value");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("quoted key=value".to_string(), false, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_quoted_value() {
        let result = tokenize_with_equals_and_quotes("key=\"quoted value\"");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("key=quoted value".to_string(), false, true));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_escaped_quote() {
        let result = tokenize_with_equals_and_quotes("\"hello \\\"world\\\"\"");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("hello \"world\"".to_string(), true, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_escaped_backslash() {
        let result = tokenize_with_equals_and_quotes("\"hello\\\\world\"");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("hello\\world".to_string(), true, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_unclosed_quote() {
        let result = tokenize_with_equals_and_quotes("\"unclosed");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("unclosed".to_string(), true, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_whitespace() {
        let result = tokenize_with_equals_and_quotes("  hello   world  ");
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], ("hello".to_string(), false, false));
        assert_eq!(result[1], ("world".to_string(), false, false));
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_tabs() {
        let result = tokenize_with_equals_and_quotes("\thello\t\tworld\t");
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], ("hello".to_string(), false, false));
        assert_eq!(result[1], ("world".to_string(), false, false));
    }

    #[test]
    fn test_tokenize_with_equals_simple() {
        let result = tokenize_with_equals("hello world test");
        assert_eq!(result, vec!["hello", "world", "test"]);
    }

    #[test]
    fn test_tokenize_with_equals_quoted() {
        let result = tokenize_with_equals("\"hello world\"");
        assert_eq!(result, vec!["hello world"]);
    }

    #[test]
    fn test_tokenize_with_equals_key_value() {
        let result = tokenize_with_equals("key=value");
        assert_eq!(result, vec!["key=value"]);
    }

    #[test]
    fn test_tokenize_with_equals_quoted_key_value() {
        let result = tokenize_with_equals("\"my key\"=value");
        assert_eq!(result, vec!["my key=value"]);
    }

    #[test]
    fn test_tokenize_with_equals_quoted_value() {
        let result = tokenize_with_equals("key=\"my value\"");
        assert_eq!(result, vec!["key=my value"]);
    }

    #[test]
    fn test_tokenize_with_equals_escaped_quote() {
        let result = tokenize_with_equals("\"hello \\\"world\\\"\"");
        assert_eq!(result, vec!["hello \"world\""]);
    }

    #[test]
    fn test_tokenize_with_equals_escaped_backslash() {
        let result = tokenize_with_equals("\"path\\\\to\\\\file\"");
        assert_eq!(result, vec!["path\\to\\file"]);
    }

    #[test]
    fn test_tokenize_with_equals_other_escape() {
        let result = tokenize_with_equals("\"hello\\nworld\"");
        assert_eq!(result, vec!["hello\\nworld"]);
    }

    #[test]
    fn test_tokenize_with_equals_trailing_backslash() {
        let result = tokenize_with_equals("\"hello\\\"");
        assert_eq!(result, vec!["hello\""]);
    }

    #[test]
    fn test_tokenize_with_equals_unclosed_quote() {
        let result = tokenize_with_equals("\"unclosed");
        assert_eq!(result, vec!["unclosed"]);
    }

    #[test]
    fn test_tokenize_with_equals_empty() {
        let result = tokenize_with_equals("");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_tokenize_with_equals_only_whitespace() {
        let result = tokenize_with_equals("   \t  ");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_tokenize_with_equals_mixed_quotes() {
        let result = tokenize_with_equals("hello \"quoted text\" world");
        assert_eq!(result, vec!["hello", "quoted text", "world"]);
    }

    #[test]
    fn test_find_first_equals_outside_quotes_simple() {
        assert_eq!(find_first_equals_outside_quotes("key=value"), Some(3));
    }

    #[test]
    fn test_find_first_equals_outside_quotes_no_equals() {
        assert_eq!(find_first_equals_outside_quotes("hello"), None);
    }

    #[test]
    fn test_find_first_equals_outside_quotes_quoted_key() {
        assert_eq!(
            find_first_equals_outside_quotes("\"my key\"=value"),
            Some(8)
        );
    }

    #[test]
    fn test_find_first_equals_outside_quotes_equals_in_quotes() {
        assert_eq!(find_first_equals_outside_quotes("\"key=value\""), None);
    }

    #[test]
    fn test_find_first_equals_outside_quotes_multiple_equals() {
        assert_eq!(find_first_equals_outside_quotes("key=value=extra"), Some(3));
    }

    #[test]
    fn test_find_first_equals_outside_quotes_escaped_quote() {
        assert_eq!(
            find_first_equals_outside_quotes("\"key\\\"test\"=value"),
            Some(11)
        );
    }

    #[test]
    fn test_find_first_equals_outside_quotes_escaped_backslash() {
        assert_eq!(
            find_first_equals_outside_quotes("\"key\\\\test\"=value"),
            Some(11)
        );
    }

    #[test]
    fn test_find_first_equals_outside_quotes_empty() {
        assert_eq!(find_first_equals_outside_quotes(""), None);
    }

    #[test]
    fn test_find_first_equals_outside_quotes_only_equals() {
        assert_eq!(find_first_equals_outside_quotes("="), Some(0));
    }

    #[test]
    fn test_find_first_equals_outside_quotes_quote_at_start() {
        assert_eq!(
            find_first_equals_outside_quotes("\"quoted\"=value"),
            Some(8)
        );
    }

    #[test]
    fn test_find_first_equals_outside_quotes_quote_not_at_start() {
        // Quotes not at the beginning are ignored
        assert_eq!(
            find_first_equals_outside_quotes("key\"quoted\"=value"),
            Some(11)
        );
    }

    #[test]
    fn test_complex_tokenization_scenarios() {
        // Test complex real-world scenarios
        let result =
            tokenize_with_equals("arg1 \"spaced arg\" key=value \"quoted key\"=\"quoted value\"");
        assert_eq!(
            result,
            vec!["arg1", "spaced arg", "key=value", "quoted key=quoted value"]
        );
    }

    #[test]
    fn test_edge_case_empty_quoted_string() {
        let result = tokenize_with_equals("\"\"");
        assert_eq!(result, vec![""]);
    }

    #[test]
    fn test_edge_case_only_escaped_characters() {
        let result = tokenize_with_equals("\"\\\"\\\\\"");
        assert_eq!(result, vec!["\"\\"]);
    }

    /// Test coverage exclusions for trivial code
    #[cfg(test)]
    mod coverage_exclusions {
        // The following patterns are excluded from coverage requirements:

        // 1. Iterator peekable operations - standard library functionality
        // 2. Character iteration and enumeration - standard library
        // 3. String operations (push, clear, is_empty) - standard library
        // 4. Vector operations (push, len) - standard library
        // 5. Some edge cases in escape handling that would require malformed input

        // These are either standard library functions or would require
        // artificially constructed invalid inputs to trigger.

        use super::*;

        #[test]
        fn test_function_attributes() {
            // Test that #[must_use] attribute is applied correctly
            // This is handled by the compiler, but we verify the functions work
            let result1 = tokenize_with_equals_and_quotes("test");
            let result2 = tokenize_with_equals("test");
            let result3 = find_first_equals_outside_quotes("test=value");

            // Functions successfully return values when used
            assert!(!result1.is_empty());
            assert!(!result2.is_empty());
            assert!(result3.is_some());
        }
    }
}
