Skip to content

Commit

Permalink
rtlib: fb_LeftSelf()
Browse files Browse the repository at this point in the history
- fb_LeftSelf( s, 0 ) was failing to reduce the string to a length of zero
- declaration added to inc/fbc-int/string.bi
  • Loading branch information
jayrm committed Dec 23, 2023
1 parent de40b24 commit 0455a72
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Expand Up @@ -41,6 +41,7 @@ Version 1.20.0
- gas64: 'SI' word found in cvtsi2ss wrongly releasing RSI register (SARG)
- sf.net #997: gcc backend: handle EXTERN "rtlib": preserve the internally mangled name but emit an extra prototype to map the internal name to the external ASM name
- gfxlib2: Incorrect alpha blending in ALPHA put method - fix C implementation to avoid overflowing int type
- rtlib: internal fb_LeftSelf( s, 0 ) was failing to reduce the string to a length of zero.


Version 1.10.1
Expand Down
8 changes: 5 additions & 3 deletions inc/fbc-int/string.bi
Expand Up @@ -42,10 +42,12 @@ extern "rtlib"
size as integer '' size of allocated memory
end type

'' VAR-LEN STRING API (STRING)
'' declare sub fb_StrDelete( byref s as const string )
'' VAR-LEN STRING API (FBSTRING)
declare sub fb_StrDelete( byval s as const FBSTRING ptr )
declare sub fb_LEFTSELF( byval dst as FBSTRING ptr, byval length as const integer )

declare sub fb_StrDelete alias "fb_StrDelete" ( byval s as const FBSTRING ptr )
'' VAR-LEN STRING API (STRING)
declare sub LEFTSELF alias "fb_LEFTSELF" ( byref dst as string, byval length as const integer )

end extern

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/rtl-string.bas
Expand Up @@ -1897,7 +1897,7 @@
NULL, FB_RTL_OPT_OVER or FB_RTL_OPT_NOQB, _
2, _
{ _
( typeSetIsConst( FB_DATATYPE_STRING ), FB_PARAMMODE_BYREF, FALSE ), _
( FB_DATATYPE_STRING, FB_PARAMMODE_BYREF, FALSE ), _
( typeSetIsConst( FB_DATATYPE_INTEGER ), FB_PARAMMODE_BYVAL, FALSE ) _
} _
), _
Expand Down
10 changes: 5 additions & 5 deletions src/rtlib/str_left.c
Expand Up @@ -44,10 +44,10 @@ FBCALL FBSTRING *fb_LEFT( FBSTRING *src, ssize_t chars )
/*
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. Carefule, due the function declaration
does not check for fb_LEFTSELF( "lteral", n ) so if src is a temporary,
It just gets deleted.
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 )
{
Expand All @@ -59,7 +59,7 @@ FBCALL void fb_LEFTSELF( FBSTRING *src, ssize_t chars )
FB_STRLOCK();

src_len = FB_STRSIZE( src );
if( (src->data != NULL) && (chars > 0) && (src_len > 0) )
if( (src->data != NULL) && (chars >= 0) && (src_len >= 0) )
{
if( chars > src_len )
{
Expand Down
76 changes: 76 additions & 0 deletions tests/fbc-int/string.bas
Expand Up @@ -68,4 +68,80 @@ SUITE( fbc_tests.fbc_int.string_ )

END_TEST

TEST( leftSelf_ )
dim s as string
var p = cast(fbc.FBSTRING ptr, @s)
dim size as integer

s = "abcdef"
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 6 )
CU_ASSERT( p->size > 0 )
CU_ASSERT( s = "abcdef" )
size = p->size

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 5 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "abcde" )

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 4 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "abcd" )

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 3 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "abc" )

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 2 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "ab" )

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 1 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "a" )

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 0 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "" )

FBC.LeftSelf( s, len(s) - 1 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 0 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "" )

FBC.LeftSelf( s, 0 )
CU_ASSERT( p->data <> NULL )
CU_ASSERT( p->len = 0 )
CU_ASSERT( p->size = size )
CU_ASSERT( s = "" )

'' delete string
s = ""
CU_ASSERT( p->data = NULL )
CU_ASSERT( p->len = 0 )
CU_ASSERT( p->size = 0 )

FBC.LeftSelf( s, 0 )
CU_ASSERT( p->data = NULL )
CU_ASSERT( p->len = 0 )
CU_ASSERT( p->size = 0 )

END_TEST

TEST( concat_no_resize )
END_TEST

END_SUITE
2 changes: 1 addition & 1 deletion tests/warnings/rtl-prototypes.bas
Expand Up @@ -2939,7 +2939,7 @@

ID( sub fb_LeftSelf alias "fb_LEFTSELF" )
scope
dim chk as sub fbcall ( byref as const string, byval as const integer )
dim chk as sub fbcall ( byref as string, byval as const integer )
chk = procptr( fb_LeftSelf )
end scope

Expand Down

0 comments on commit 0455a72

Please sign in to comment.