Skip to content

feat: add convenience methods to Token for direct equality checks#3266

Open
hippietrail wants to merge 4 commits into
Automattic:masterfrom
hippietrail:delegate_eq_str
Open

feat: add convenience methods to Token for direct equality checks#3266
hippietrail wants to merge 4 commits into
Automattic:masterfrom
hippietrail:delegate_eq_str

Conversation

@hippietrail
Copy link
Copy Markdown
Collaborator

Issues

N/A

Description

I noticed it's a very common pattern to get the content of a token and then check if it's the same as a string or char array literal. In fact there are multiple functions to get the token content, as a string or as a char array reference. It's almost always better to use the latter since it can do comparisons against other string types using only iterators and avoiding allocations and full type conversions.

In some places the change only saves a couple of characters, but in other places it saves one or two lines. I think it makes the code more expressive and easier to read and makes accidentally introducing unnecessary type conversions and allocations a little harder for new contributors copying existing code.

  • Add eq_ch() and eq_str() methods to Token to eliminate intermediate get_ch()/get_str() calls throughout the codebase.
  • Update all usage sites and remove unused CharStringExt imports.

How Has This Been Tested?

cargo test

Checklist

  • I have performed a self-review of my own code
  • I have added tests to cover my changes
  • I have considered splitting this into smaller pull requests.

- Add `eq_ch()` and `eq_str()` methods to `Token` to eliminate intermediate
`get_ch()`/`get_str()` calls throughout the codebase.
- Update all usage
sites and remove unused CharStringExt imports.
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.

1 participant