Skip to content

Commit

Permalink
rtlib: ./inc/fbc-int/string.bi - for string descriptor and low level …
Browse files Browse the repository at this point in the history
…string functions
  • Loading branch information
jayrm committed Dec 3, 2023
1 parent 36650b9 commit 59b05ee
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Expand Up @@ -19,6 +19,7 @@ Version 1.20.0
- gas64: optimizations: Replace some CMP instructions with TEST instruction (SARG)
- gas64: optimizations: multiplication for small constants -1,-2,3,5,6,10 (SARG)
- fbc headers: add ./inc/fberror.bi for runtime library error constants
- rtlib: ./inc/fbc-int/string.bi - for string descriptor and low level string functions

[fixed]
- github #410: give consistent floating point comparisons results involving NaNs.
Expand Down
54 changes: 54 additions & 0 deletions inc/fbc-int/string.bi
@@ -0,0 +1,54 @@
#ifndef __FBC_INT_STRING_BI__
#define __FBC_INT_STRING_BI__

# if __FB_LANG__ = "qb"
# error not supported in qb dialect
# endif

'' DISCLAIMER!!!
''
'' 1) this header documents runtime library internals and is
'' subject to change without notice
''
'' declarations must follow ./src/rtlib/fb_string.h

'' fbc won't allow redefinition of these functions in to a namespace due to the
'' internal name lookups and caching in the rtlLookupTB() table - so instead we
'' can keep the global ones and also define our own in a namespace. The global
'' ones will override the ones in our namespace unless we explicitly specify
'' the namespace
''
'' #undef fb_StrDelete
''

namespace FBC

extern "rtlib"

type FB_CHAR as ZSTRING
type FB_WCHAR as WSTRING

'' var-len string descriptor
type FBSTRING
data as FB_CHAR ptr '' pointer to the start of characters
len as integer '' length
size as integer '' size of allocated memory
end type

'' var-len wstring descriptor (future)
type FBWSTRING
data as FB_WCHAR ptr '' pointer to the start of characters
len as integer '' length
size as integer '' size of allocated memory
end type

'' VAR-LEN STRING API (STRING)
'' declare sub fb_StrDelete( byref s as const string )

declare sub fb_StrDelete alias "fb_StrDelete" ( byval s as const FBSTRING ptr )

end extern

end namespace

#endif
71 changes: 71 additions & 0 deletions tests/fbc-int/string.bas
@@ -0,0 +1,71 @@
#include "fbcunit.bi"
#include "fbc-int/string.bi"

'' tests for string descriptor internals and low level string functions

#ifndef NULL
#define NULL cast( const any ptr, 0 )
#endif

SUITE( fbc_tests.fbc_int.string_ )


TEST( struct )
dim sd as fbc.FBSTRING
CU_ASSERT( sizeof(sd.data) = sizeof(any ptr) )
CU_ASSERT( sizeof(sd.len) = sizeof(integer) )
CU_ASSERT( sizeof(sd.size) = sizeof(integer) )
CU_ASSERT( sizeof(fbc.FBSTRING) = sizeof(any ptr) + 2*sizeof(integer) )
END_TEST

#macro check_string( s, hasdata, expected_len )
if( s ) then
if( hasdata ) then
CU_ASSERT( s->data <> 0 )
CU_ASSERT( s->size > 0 )
CU_ASSERT( s->len = expected_len )
else
CU_ASSERT( s->data = 0 )
CU_ASSERT( s->size = 0 )
CU_ASSERT( s->len = 0 )
end if
CU_ASSERT( s->size >= s->len )
else
CU_FAIL()
end if
#endmacro

TEST( init )
dim s as string
var s1 = @s
var s2 = cast( fbc.FBSTRING ptr, @s )

CU_ASSERT( s1 = s2 )

CU_ASSERT( s2->data = NULL )
CU_ASSERT( s2->len = 0 )
CU_ASSERT( s2->size = 0 )
END_TEST

TEST( dim_ )
dim s as string
check_string( cast(fbc.FBSTRING ptr, @s), FALSE, 0 )

s = ""
check_string( cast(fbc.FBSTRING ptr, @s), FALSE, 0 )

s = "hello"
check_string( cast(fbc.FBSTRING ptr, @s), TRUE, 5 )

s = ""
check_string( cast(fbc.FBSTRING ptr, @s), FALSE, 0 )

s = "hello"
check_string( cast(fbc.FBSTRING ptr, @s), TRUE, 5 )

fbc.fb_strDelete( cast(fbc.FBSTRING ptr, @s) )
check_string( cast(fbc.FBSTRING ptr, @s), FALSE, 0 )

END_TEST

END_SUITE

0 comments on commit 59b05ee

Please sign in to comment.