#include "Lexer.h"

bool Lexer::matchPattern(const std::string& pattern, const std::string& checkString, char delimiter, std::vector<std::string>& hitSequence)
{
    if (checkString.empty())
    {
        return false;
    }

    if (pattern.empty())
    {
        return false;
    }

    bool found_pattern = true;
    unsigned check_idx = 0;
    unsigned pattern_idx = 0;

    std::vector<std::string> hits;
    std::string working_hit;
    while(check_idx < checkString.size())
    {
        if (pattern_idx == pattern.size())
        {
            break;
        }

        auto check_char = checkString[check_idx];
        auto pattern_char = pattern[pattern_idx];
        if (pattern_char == delimiter)
        {
            if (pattern_idx + 1 < pattern.size())
            {
                if (check_char == pattern[pattern_idx + 1])
                {
                    hits.push_back(working_hit);
                    working_hit.clear();
                    pattern_idx++;
                }
                else
                {
                    working_hit+=check_char;
                    check_idx++;
                }
            }
            else
            {
                working_hit+=check_char;
                check_idx++;
            }
        }
        else
        {
            if (check_char == pattern_char)
            {
                check_idx++;
                pattern_idx++;
            }
            else
            {
                found_pattern = false;
                break;
            }
        }
    }

    if (found_pattern)
    {
        hitSequence = hits;
    }
    return found_pattern;
}
