Skip to content

Conversation

@fla-t
Copy link
Contributor

@fla-t fla-t commented Jun 28, 2024

The dotstrings parser has been updated to handle multi-line entries in the format

"key" = "value
that spans
multiple lines"; 

This allows for more flexibility in writing and parsing dotstrings files

fla-t added 2 commits June 28, 2024 11:17
The dotstrings parser has been updated to handle multi-line entries in the format "key = value;". This allows for more flexibility in writing and parsing dotstrings files.
comment = re.compile(r"(\'(?:[^\'\\]|\\[\s\S])*\')|//.*|/\*(?:[^*]|\*(?!/))*\*/", re.MULTILINE)
whitespace = re.compile(r"\s*", re.MULTILINE)
entry = re.compile(r'"(.*)"\s*=\s*"(.*)";')
entry = re.compile(r'"([^"]*?)"\s*=\s*"((?:[^";]|"(?!\s*;))*?)";', re.DOTALL)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unfortunately this will break on strings that contain quotes. e.g. NSLocalizedString("Hello \"World\"", "Some Comment") gets turned into "Hello \"World\"" = "Hello \"World\"";.

I think the second change on this line will also stop the string containing ;.

Copy link
Contributor Author

@fla-t fla-t Jun 28, 2024

Choose a reason for hiding this comment

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

The first change is there so that the first match group doesn't end up matching the whole string (even matching the "value" part).

But yeah it wouldn't work when there are more than two quotes, in the "key" part of the string (the two quotes around the key itself). This was intentional because I didn't thought there would be any "key" that would have more quotes than two.

If we really want to cater keys that have quotes inside of them, we can replicate the regex that we have for value part, but without the semicolon

entry = re.compile(r'"((?:[^";]|"(?!\s*;))*?)"\s*=\s*"((?:[^";]|"(?!\s*;))*?)";')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The second change is finding a ";" to match the value part of the string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you change the test file I have here to contain edge cases?

@fla-t fla-t requested a review from dalemyers June 28, 2024 10:22
@fla-t
Copy link
Contributor Author

fla-t commented Jun 28, 2024

Also there is a line that I hate everytime I take a look at it.

comment = comment[::-1].replace("/*", "", 1)[::-1]

# can easily be this
comment = comment.removeprefix("/*")
comment = comment.removesuffix("*/")

Or maybe I am missing something? :)
(I would love if we can add this change in this PR)

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.

2 participants