Skip to content

Commit

Permalink
rtlib: split LEFT() and LEFTSELF() to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrm committed Mar 24, 2024
1 parent bc61d9d commit 2fb10c9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
41 changes: 0 additions & 41 deletions src/rtlib/str_left.c
Expand Up @@ -39,44 +39,3 @@ FBCALL FBSTRING *fb_LEFT( FBSTRING *src, ssize_t chars )

return dst;
}


/*
Special case of a = left( a, n )
The string 'a' is not reallocated, only the string length field is adjusted
and a NUL terminator written. fbc does not optimize for this so to use,
it must be a direct call by the user. Careful, due the function declaration, it
does not check for fb_LEFTSELF( "literal", n ) so if src is a temporary,
it just gets deleted.
*/
FBCALL void fb_LEFTSELF( FBSTRING *src, ssize_t chars )
{
ssize_t src_len;

if( src == NULL )
return;

FB_STRLOCK();

src_len = FB_STRSIZE( src );
if( (src->data != NULL) && (chars >= 0) && (src_len >= 0) )
{
if( chars > src_len )
{
fb_hStrSetLength( src, src_len );
/* add a NUL character */
src->data[src_len] = '\0';
}
else
{
fb_hStrSetLength( src, chars );
/* add a NUL character */
src->data[chars] = '\0';
}
}

/* del if temp */
fb_hStrDelTemp_NoLock( src );

FB_STRUNLOCK();
}
43 changes: 43 additions & 0 deletions src/rtlib/str_leftself.c
@@ -0,0 +1,43 @@
/* leftself statement */

#include "fb.h"

/*
Special case of a = left( a, n )
The string 'a' is not reallocated, only the string length field is adjusted
and a NUL terminator written. fbc does not optimize for this so to use,
it must be a direct call by the user. Careful, due the function declaration, it
does not check for fb_LEFTSELF( "literal", n ) so if src is a temporary,
it just gets deleted.
*/
FBCALL void fb_LEFTSELF( FBSTRING *src, ssize_t chars )
{
ssize_t src_len;

if( src == NULL )
return;

FB_STRLOCK();

src_len = FB_STRSIZE( src );
if( (src->data != NULL) && (chars >= 0) && (src_len >= 0) )
{
if( chars > src_len )
{
fb_hStrSetLength( src, src_len );
/* add a NUL character */
src->data[src_len] = '\0';
}
else
{
fb_hStrSetLength( src, chars );
/* add a NUL character */
src->data[chars] = '\0';
}
}

/* del if temp */
fb_hStrDelTemp_NoLock( src );

FB_STRUNLOCK();
}

0 comments on commit 2fb10c9

Please sign in to comment.