Skip to content

Commit 5be23dd

Browse files
khwilliamsondemerphq
authored andcommitted
Inline strlcat(), strlcpy()
These short functions are reasonably frequently used.
1 parent 90db462 commit 5be23dd

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

embed.fnc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,11 +3780,11 @@ FXpoT |I32 |xs_handshake |const U32 key|NN void * v_my_perl\
37803780
|NN const char * file| ...
37813781
Xp |void |xs_boot_epilog |const I32 ax
37823782
#ifndef HAS_STRLCAT
3783-
ApTd |Size_t |my_strlcat |NULLOK char *dst|NULLOK const char *src|Size_t size
3783+
AsTd |Size_t |my_strlcat |NULLOK char *dst|NULLOK const char *src|Size_t size
37843784
#endif
37853785

37863786
#ifndef HAS_STRLCPY
3787-
ApTd |Size_t |my_strlcpy |NULLOK char *dst|NULLOK const char *src|Size_t size
3787+
AsTd |Size_t |my_strlcpy |NULLOK char *dst|NULLOK const char *src|Size_t size
37883788
#endif
37893789

37903790
#ifndef HAS_STRNLEN

inline.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,80 @@ Perl_savesharedsvpv(pTHX_ SV *sv)
36243624
return savesharedpvn(pv, len);
36253625
}
36263626

3627+
/*
3628+
=for apidoc my_strlcat
3629+
3630+
The C library C<strlcat> if available, or a Perl implementation of it.
3631+
This operates on C C<NUL>-terminated strings.
3632+
3633+
C<my_strlcat()> appends string C<src> to the end of C<dst>. It will append at
3634+
most S<C<size - strlen(dst) - 1>> characters. It will then C<NUL>-terminate,
3635+
unless C<size> is 0 or the original C<dst> string was longer than C<size> (in
3636+
practice this should not happen as it means that either C<size> is incorrect or
3637+
that C<dst> is not a proper C<NUL>-terminated string).
3638+
3639+
Note that C<size> is the full size of the destination buffer and
3640+
the result is guaranteed to be C<NUL>-terminated if there is room. Note that
3641+
room for the C<NUL> should be included in C<size>.
3642+
3643+
The return value is the total length that C<dst> would have if C<size> is
3644+
sufficiently large. Thus it is the initial length of C<dst> plus the length of
3645+
C<src>. If C<size> is smaller than the return, the excess was not appended.
3646+
3647+
=cut
3648+
3649+
Description stolen from http://man.openbsd.org/strlcat.3
3650+
*/
3651+
#ifndef HAS_STRLCAT
3652+
PERL_STATIC_INLINE Size_t
3653+
Perl_my_strlcat(char *dst, const char *src, Size_t size)
3654+
{
3655+
Size_t used, length, copy;
3656+
3657+
used = strlen(dst);
3658+
length = strlen(src);
3659+
if (size > 0 && used < size - 1) {
3660+
copy = (length >= size - used) ? size - used - 1 : length;
3661+
memcpy(dst + used, src, copy);
3662+
dst[used + copy] = '\0';
3663+
}
3664+
return used + length;
3665+
}
3666+
#endif
3667+
3668+
3669+
/*
3670+
=for apidoc my_strlcpy
3671+
3672+
The C library C<strlcpy> if available, or a Perl implementation of it.
3673+
This operates on C C<NUL>-terminated strings.
3674+
3675+
C<my_strlcpy()> copies up to S<C<size - 1>> characters from the string C<src>
3676+
to C<dst>, C<NUL>-terminating the result if C<size> is not 0.
3677+
3678+
The return value is the total length C<src> would be if the copy completely
3679+
succeeded. If it is larger than C<size>, the excess was not copied.
3680+
3681+
=cut
3682+
3683+
Description stolen from http://man.openbsd.org/strlcpy.3
3684+
*/
3685+
#ifndef HAS_STRLCPY
3686+
PERL_STATIC_INLINE Size_t
3687+
Perl_my_strlcpy(char *dst, const char *src, Size_t size)
3688+
{
3689+
Size_t length, copy;
3690+
3691+
length = strlen(src);
3692+
if (size > 0) {
3693+
copy = (length >= size) ? size - 1 : length;
3694+
memcpy(dst, src, copy);
3695+
dst[copy] = '\0';
3696+
}
3697+
return length;
3698+
}
3699+
#endif
3700+
36273701
/*
36283702
* ex: set ts=8 sts=4 sw=4 et:
36293703
*/

proto.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util.c

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5738,80 +5738,6 @@ S_xs_version_bootcheck(pTHX_ U32 items, U32 ax, const char *xs_p,
57385738
}
57395739
}
57405740

5741-
/*
5742-
=for apidoc my_strlcat
5743-
5744-
The C library C<strlcat> if available, or a Perl implementation of it.
5745-
This operates on C C<NUL>-terminated strings.
5746-
5747-
C<my_strlcat()> appends string C<src> to the end of C<dst>. It will append at
5748-
most S<C<size - strlen(dst) - 1>> characters. It will then C<NUL>-terminate,
5749-
unless C<size> is 0 or the original C<dst> string was longer than C<size> (in
5750-
practice this should not happen as it means that either C<size> is incorrect or
5751-
that C<dst> is not a proper C<NUL>-terminated string).
5752-
5753-
Note that C<size> is the full size of the destination buffer and
5754-
the result is guaranteed to be C<NUL>-terminated if there is room. Note that
5755-
room for the C<NUL> should be included in C<size>.
5756-
5757-
The return value is the total length that C<dst> would have if C<size> is
5758-
sufficiently large. Thus it is the initial length of C<dst> plus the length of
5759-
C<src>. If C<size> is smaller than the return, the excess was not appended.
5760-
5761-
=cut
5762-
5763-
Description stolen from http://man.openbsd.org/strlcat.3
5764-
*/
5765-
#ifndef HAS_STRLCAT
5766-
Size_t
5767-
Perl_my_strlcat(char *dst, const char *src, Size_t size)
5768-
{
5769-
Size_t used, length, copy;
5770-
5771-
used = strlen(dst);
5772-
length = strlen(src);
5773-
if (size > 0 && used < size - 1) {
5774-
copy = (length >= size - used) ? size - used - 1 : length;
5775-
memcpy(dst + used, src, copy);
5776-
dst[used + copy] = '\0';
5777-
}
5778-
return used + length;
5779-
}
5780-
#endif
5781-
5782-
5783-
/*
5784-
=for apidoc my_strlcpy
5785-
5786-
The C library C<strlcpy> if available, or a Perl implementation of it.
5787-
This operates on C C<NUL>-terminated strings.
5788-
5789-
C<my_strlcpy()> copies up to S<C<size - 1>> characters from the string C<src>
5790-
to C<dst>, C<NUL>-terminating the result if C<size> is not 0.
5791-
5792-
The return value is the total length C<src> would be if the copy completely
5793-
succeeded. If it is larger than C<size>, the excess was not copied.
5794-
5795-
=cut
5796-
5797-
Description stolen from http://man.openbsd.org/strlcpy.3
5798-
*/
5799-
#ifndef HAS_STRLCPY
5800-
Size_t
5801-
Perl_my_strlcpy(char *dst, const char *src, Size_t size)
5802-
{
5803-
Size_t length, copy;
5804-
5805-
length = strlen(src);
5806-
if (size > 0) {
5807-
copy = (length >= size) ? size - 1 : length;
5808-
memcpy(dst, src, copy);
5809-
dst[copy] = '\0';
5810-
}
5811-
return length;
5812-
}
5813-
#endif
5814-
58155741
PERL_STATIC_INLINE bool
58165742
S_gv_has_usable_name(pTHX_ GV *gv)
58175743
{

0 commit comments

Comments
 (0)