-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add String#center method #8557
Add String#center method #8557
Conversation
src/string.cr
Outdated
|
||
unless left | ||
buffer.copy_from(to_unsafe, bytesize) | ||
Intrinsics.memcpy(buffer.as(Void*), to_unsafe.as(Void*), bytesize, false) |
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.
I think you might be able to use Pointer.copy_from
Intrinsics.memcpy(buffer.as(Void*), to_unsafe.as(Void*), bytesize, false) | |
buffer.copy_from(to_unsafe, bytesize) |
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.
I see now this is diff noise so this might be outside the scope of this PR.
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.
Is this "better" than memcpy ?
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.
It's the same as memcpy
, but shorter.
It's fine to leave this as-is for this PR.
This continues the previous PR #3924 (stalled). |
src/string.cr
Outdated
when .> 0 | ||
leftpadding, rightpadding = padding, 0 | ||
else | ||
leftpadding = padding >> 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.
// 2
, please
there's no need for manual pinhole optimizations the compiler is perfectly capable of.
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.
Ok, done !
src/string.cr
Outdated
|
||
unless left | ||
buffer.copy_from(to_unsafe, bytesize) | ||
Intrinsics.memcpy(buffer.as(Void*), to_unsafe.as(Void*), bytesize, false) |
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.
It's the same as memcpy
, but shorter.
It's fine to leave this as-is for this PR.
Thank you! |
String#center(width, char)
returns a new string of lengthwidth
with original string centered, and padded withchar
(default = space)