Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the while with a for in parseMacroArgs #323

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions fast64_internal/f3d/f3d_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2204,13 +2204,11 @@ def parseMacroList(data: str):


def parseMacroArgs(data: str):
end = 0
start = 0
params: "list[str]" = []
parenthesesCount = 0

while end < len(data) - 1:
end += 1
for end in range(len(data)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to a for means the end += 1 below in the loop won't advance the loop as with the while

Overall this function should be basically unit tested, that would be a much easier way to tell correctness (doesn't need to be fancy, just a bunch of print(parseMacroArgs("...")) would do)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just create a simple programme (not in the PR)

def new_parseMacroArgs(data: str):
    start = 0
    params: "list[str]" = []
    parenthesesCount = 0

    for end in range(len(data)):
        if data[end] == "(":
            parenthesesCount += 1
        elif data[end] == ")":
            parenthesesCount -= 1

        if (data[end] == "," or end == len(data) - 1) and parenthesesCount == 0:
            if end == len(data) - 1:
                end += 1
            param = "".join(data[start:end].split())
            params.append(param)
            start = end + 1

    return params

def old_parseMacroArgs(data: str):
    end = 0
    start = 0
    params: "list[str]" = []
    parenthesesCount = 0

    while end < len(data) - 1:
        end += 1
        if data[end] == "(":
            parenthesesCount += 1
        elif data[end] == ")":
            parenthesesCount -= 1

        if (data[end] == "," or end == len(data) - 1) and parenthesesCount == 0:
            if end == len(data) - 1:
                end += 1
            param = "".join(data[start:end].split())
            params.append(param)
            start = end + 1

    return params

def test_new_parseMacroArgs():
    assert new_parseMacroArgs("a, b, c") == ["a", "b", "c"]
    assert new_parseMacroArgs("abc") == ["abc"]
    assert new_parseMacroArgs("a") == ["a"]

def test_old_parseMacroArgs():
    assert old_parseMacroArgs("a, b, c") == ["a", "b", "c"]
    assert old_parseMacroArgs("abc") == ["abc"]
    assert old_parseMacroArgs("a") == [] # This is the bug

if __name__ == "__main__":
    test_new_parseMacroArgs()
    test_old_parseMacroArgs()

and the new version work like the old one except for the bug who are fix

if data[end] == "(":
parenthesesCount += 1
elif data[end] == ")":
Expand Down
Loading