-
-
Notifications
You must be signed in to change notification settings - Fork 640
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
group-pm: fix colorHashFromString #3991
Conversation
@ray-kraesig since this function is only used in GroupAvatar.js should I move it into that. |
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.
hi @Maskedman99 thanks for the PR.
Can you please link before and after screenshots here? It will help a lot in reviewing the PR.
@jainkuniya the difference isn't exactly visible in the UI,
Before:After: |
src/utils/color.js
Outdated
for (let i = 0; i < name.length; i++) { | ||
hash = hash * 31 + name.charCodeAt(1); | ||
for (let i = name.length - 1; i >= 0; i--) { | ||
hash = hash * 31 + name.codePointAt(i); |
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.
codePointAt
is normally what you want, for UTF-8 safety – but here charCodeAt
is fine. In fact, with this loop, it's required for correctness: name.length
gives the number of 16-byte code units, not the number of code points.
Also, why is the string now being iterated in reverse?
src/utils/color.js
Outdated
while (colorHash.length < 6) { | ||
colorHash += '0'; |
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.
adef1b3
to
c135d02
Compare
eb07b9c
to
63bc582
Compare
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.
The commit message seems to have been accidentally doubled.
This commit will also need some tests demonstrating that it succeeds where the previous code failed. (See src/utils/__tests__/color-test.js
for the existing tests.)
While it's unreasonable to find a string which produces exactly #ffffff
(and even less reasonable to require that any possible future implementation also produce #ffffff
for that string), it shouldn't be difficult to ensure that its other desirable properties are tested – e.g., that similar strings have different hashes, or that short and long strings generate valid values.
(I'd thought I'd mentioned this in a previous review, but apparently not. Apologies.)
src/utils/color.js
Outdated
let colorHash = hash.toString(16); | ||
colorHash = colorHash.padStart(6, '0'); | ||
|
||
return `#${colorHash}`; |
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.
This could all be a single line. (The first two lines in particular should be a single line.)
since this function is only used in GroupAvatar.js should I move it into that?
you meant the opposite right similar strings have similar hashes |
While that's the only place it's used now, it's conceptually generic and could reasonably be used elsewhere if there were any call for it. Unless that's going to change, it might as well stay where it is. (It probably wouldn't be worth the churn to move out, if it were already there. But it's not.)
Decidedly not! That's often exactly the property you want a hash function not to have; it makes finding collisions easier. This application doesn't exactly call for a cryptography-grade hash, but it's still desirable that (e.g.) |
4276aa1
to
5f89eb4
Compare
@ray-kraesig thanks for clarifying, It seems I interpreted 'similar strings' as |
Bumping in priority because it's apparent that the bug described in the issue is causing a P0 crash, #4038. I'll try for a simple fix that can ignore the #NaN output, unless @ray-kraesig if you happen to know if this is very close to mergeable (or you can merge with a few tweaks). |
As of the last pass, I think all issues have been resolved. I'll run a couple of additional tests, but it should be mergeable. |
5f89eb4
to
fc00fab
Compare
colorHashFromString() now considers all characters in the string. It can also now return colors with no red component in it. Also fixed the cases where the function returned #NaN for very long or very short strings. [ray: Minor copyedits to commit message.] Fixes: zulip#3985
fc00fab
to
b08a014
Compare
Thanks @Maskedman99 and @ray-kraesig! |
colorHashFromString() now considers all charecters in the string it can also now return colors with red component in it. Also fixed the cases where the function returned #NaN for very long or empty strings.
Fixes: #3985