Skip to content

Why are operators limited to "two" characters ? #27

Closed
@kasajian

Description

@kasajian

Method name "EvaluateTwoCharsOperators" implies it.

If one were to add "And" and "Or" operators as aliases for && and ||, the current implementation wouldn't work because "And" has three characters.

Making this change seems to work, but not sure it's the best way:

BEFORE

    private bool EvaluateTwoCharsOperators(string expr, Stack<object> stack, ref int i)
    {
        if (i < expr.Length - 1)
        {
            string op = expr.Substring(i, 2);
            if (operatorsDictionary.ContainsKey(op))
            {
                stack.Push(operatorsDictionary[op]);
                i++;
                return true;
            }
        }

        return false;
    }

AFTER

    private bool EvaluateTwoCharsOperators(string expr, Stack<object> stack, ref int i)
    {
        if (i < expr.Length - 1)
        {
            string op = expr.Substring(i, 2);
            var containsKey = operatorsDictionary.ContainsKey(op);
            if (!containsKey && expr.Length >= i + 3)
            {
                op = expr.Substring(i, 3);
                containsKey = operatorsDictionary.ContainsKey(op);
            }
            if (containsKey)
            {
                stack.Push(operatorsDictionary[op]);
                i += op.Length - 1;
                return true;
            }
        }

        return false;
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions