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

analyzer: update pascal_case_to_snake_case util function #109

Closed
wants to merge 2 commits into from

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Jun 14, 2024

No description provided.

@spytheman
Copy link
Member

oh, I did not saw that; I've already commited 2584e86 .

Comment on lines 3 to 16
pub fn pascal_case_to_snake_case(s string) string {
mut res := ''
for index, c in s {
if c.ascii_str().is_upper() {
if index > 0 {
res += '_'
}
res += c.ascii_str().to_lower()
} else {
res += c.ascii_str()
if s == '' {
return ''
}
mut res := s[0].ascii_str().to_lower()
for i in 1 .. s.len {
c := s[i]
if (c != `_` && s[i - 1] != `_`) && (c.ascii_str().is_upper() || c.is_digit()) {
res += '_'
}
res += c.ascii_str().to_lower()
}
return res
}
Copy link
Member

Choose a reason for hiding this comment

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

That seems more complex than before; is there a particular problem that it solves, beside the one revealed in the other PR?

@ttytm
Copy link
Member Author

ttytm commented Jun 14, 2024

No problem. Maybe it can be put on top, as the simpler fix would fail with the added test.

@@ -5,6 +5,7 @@ fn test_pascal_case_to_snake_case() {
assert pascal_case_to_snake_case('SomeValue') == 'some_value'
assert pascal_case_to_snake_case('SomeValue') == 'some_value'
assert pascal_case_to_snake_case('SomeValue1') == 'some_value_1'
assert pascal_case_to_snake_case('some_value_1') == 'some_value_1'
Copy link
Member

Choose a reason for hiding this comment

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

some_value_1 is not a pascal case input 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. It just makes sure it's not corrupted.

E.g. the current would turn it to

some__value___1

Copy link
Member

Choose a reason for hiding this comment

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

Good point.

@ttytm ttytm closed this Jun 14, 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.

2 participants