Add strncpytail_a(), and use it instead of its pattern#1304
Add strncpytail_a(), and use it instead of its pattern#1304alejandro-colomar wants to merge 3 commits intoshadow-maint:masterfrom
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. |
22ce0a4 to
6b0a0ac
Compare
6b0a0ac to
df8b604
Compare
df8b604 to
95ab3cb
Compare
cd8af17 to
36c4c50
Compare
982f944 to
7d68001
Compare
7d68001 to
e780271
Compare
e780271 to
4ffb5bc
Compare
|
Besides used terms, the code is OK. |
Thanks! |
2f4793f to
22b1199
Compare
22b1199 to
c33c3d5
Compare
e950aa7 to
6a97a41
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 <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
6a97a41 to
760f6b1
Compare
|
Cc: @kees |
Cc: @Karlson2k
Revisions:
v1b
v1c
v1d
v1e
v1f
v1g
v2
s/STRNCPYTAIL/strncpytail_a/.v2b
v2c
v2d
v2e
v2f
v2g
v2h
v2i
v2j
v3
v3b
v3c