-
Notifications
You must be signed in to change notification settings - Fork 17
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
Conversation
oh, I did not saw that; I've already commited 2584e86 . |
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 | ||
} |
There was a problem hiding this comment.
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?
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' |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
No description provided.