-
Notifications
You must be signed in to change notification settings - Fork 252
Add STRNCPYTAIL(), and use it instead of its pattern #1304
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
base: master
Are you sure you want to change the base?
Add STRNCPYTAIL(), and use it instead of its pattern #1304
Conversation
9acb3e0 to
0747fec
Compare
|
Nice. |
|
For the records. inline char *
strncpytail(char *restrict dst, const char *restrict src, size_t dsize)
{
size_t src_len;
src_len = strlen(src);
if (dsize > src_len)
return memcpy (dst, src, src_len + 1);
return memcpy (dst, src + (src_len - dsize), dsize);
}Large strings / buffers can be processed much faster. But if performance is not an issue, the code in the PR will work fine. Note: the binary is still a few bytes larger. |
Indeed, since this API is designed for short strings (ut_id), I'll optimize for readability for now. |
0747fec to
25ed13d
Compare
25ed13d to
22ce0a4
Compare
|
@hallyn The term nonstring comes from https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute. (To avoid you having to read the lengthy discussion in the other thread.) |
Thanks |
This comment was marked as off-topic.
This comment was marked as off-topic.
Hm, do we need to define a new term? |
Probably. I any case, I think both terms "non-string" and "zero-padded array" are confusing and not fully describe this kind of entities. |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
Please, do not continue the discussion unthreaded. That makes it impossible to follow the page. If you want to have a lengthy discussion, open a comment on any line of code, which can be hidden easily. |
| size_t dsize); | ||
|
|
||
|
|
||
| // nonstring copy tail-of-string |
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'm standing by this term. For the mnemonic comment explaining the name, nonstring is the most appropriate thing to use. You can think of strn as "string-non".
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.
Also need to mention that GCC means by this term "do not use strlen() and other string-based function because the data MAY be not NUL-terminated or could be binary data", but here, in this project, it means different: "it is a text data in fixed size buffer/array, which terminates at the end of the array without NUL-termination or terminated early by first NUL".
Hm, do we need to define a new term?
We already have "fixed-width null-padded character array", which is what utmp(5) members are.
nonstring is a more generic thing, which englobes this and other creatures. But for this mnemonic use case, nonstring is easier to remember.
@Karlson2k Please do not respond here. I already know your opinion, and am not going to agree with it. Anyone interested in it, can read the lengthy thread above.
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.
@hallyn Please merge when you're happy with the name. Feel free to close all conversations, if you need that to be able to merge.
22ce0a4 to
6b0a0ac
Compare
6b0a0ac to
df8b604
Compare
df8b604 to
95ab3cb
Compare
This works similar to strncpy(3), except that it truncates from the start of the string if the string doesn't fit. It is useful for utmp(5) ut_id, where the tail of the string is more useful and distinctive. Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
95ab3cb to
cd8af17
Compare
Cc: @Karlson2k
Revisions:
v1b
v1c
v1d
v1e
v1f
v1g