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

Conversation

coco875
Copy link
Contributor

@coco875 coco875 commented Apr 4, 2024

the goal of this pr is to fix when the argument is smaller then 1 (where -1 < 1 -1 equals false so he don't enter in the loop). I just change the while for a for loop.

Copy link
Contributor

@sauraen sauraen left a comment

Choose a reason for hiding this comment

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

Good find!

Copy link
Contributor

@Dragorn421 Dragorn421 left a comment

Choose a reason for hiding this comment

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

(I thought I reviewed this already, sorry)

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

Copy link
Contributor

@sauraen sauraen left a comment

Choose a reason for hiding this comment

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

dragorn is right, you can't run the line end += 1 in a for loop iterating over end. I think the correct fix is to take the old code and change end = 0 at the beginning to end = -1.

Copy link
Contributor

@sauraen sauraen left a comment

Choose a reason for hiding this comment

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

I apologize, I was wrong, the PR is correct. dragorn, look at the logic again. The only time end is modified is on the last iteration.

@Dragorn421 Dragorn421 merged commit 4589b8b into Fast-64:main May 7, 2024
1 check passed
@Lilaa3 Lilaa3 modified the milestone: v2.3.0 Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants