Skip to content

Commit d9a09d7

Browse files
committed
udt-wstring: SWAP statement will accept UDT as Z|WSTRING
1 parent f2fbc61 commit d9a09d7

File tree

5 files changed

+316
-0
lines changed

5 files changed

+316
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Version 1.07.0
2121
- ASC function will accept UDT as Z|WSTRING
2222
- STR/WSTR function will accept UDT as Z|WSTRING to return a Z|WSTRING
2323
- SELECT statement will accept UDT as Z|WSTRING to return a Z|WSTRING
24+
- SWAP statement will accept UDT as Z|WSTRING
2425

2526
[fixed]
2627
- sf.net #881: C backend: support for varadic function parameters in gcc using __builtin_va_list type and related macros

src/compiler/parser-quirk-array.bas

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,37 @@ private function hScopedSwap( ) as integer
8484
dim as integer ldtype = astGetDataType( l )
8585
dim as integer rdtype = astGetDataType( r )
8686

87+
'' Maybe UDT extends Z|WSTRING? Check for string conversions...
88+
if( ldtype <> rdtype ) then
89+
if( ldtype = FB_DATATYPE_STRUCT ) then
90+
var sym = astGetSubType( l )
91+
if( symbGetUdtIsZstring( sym ) ) then
92+
if( rdtype = FB_DATATYPE_CHAR ) then
93+
astTryOvlStringCONV( l )
94+
ldtype = astGetDataType( l )
95+
end if
96+
elseif( symbGetUdtIsWstring( sym ) ) then
97+
if( rdtype = FB_DATATYPE_WCHAR ) then
98+
astTryOvlStringCONV( l )
99+
ldtype = astGetDataType( l )
100+
end if
101+
end if
102+
elseif( rdtype = FB_DATATYPE_STRUCT ) then
103+
var sym = astGetSubType( r )
104+
if( symbGetUdtIsZstring( sym ) ) then
105+
if( ldtype = FB_DATATYPE_CHAR ) then
106+
astTryOvlStringCONV( r )
107+
rdtype = astGetDataType( r )
108+
end if
109+
elseif( symbGetUdtIsWstring( sym ) ) then
110+
if( ldtype = FB_DATATYPE_WCHAR ) then
111+
astTryOvlStringCONV( r )
112+
rdtype = astGetDataType( r )
113+
end if
114+
end if
115+
end if
116+
end if
117+
87118
select case( ldtype )
88119
case FB_DATATYPE_STRING, FB_DATATYPE_FIXSTR, FB_DATATYPE_CHAR
89120
select case rdtype

tests/udt-wstring/swap.bas

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "fbcunit.bi"
2+
#include once "uwstring-fixed.bi"
3+
#include once "chk-wstring.bi"
4+
5+
type UWSTRING_HAS_ID extends UWSTRING_FIXED_MUTABLE
6+
public:
7+
id as integer
8+
declare constructor()
9+
declare constructor( byval rhs as const wstring const ptr )
10+
declare constructor( byval rhs as const zstring const ptr )
11+
12+
declare operator cast() byref as wstring
13+
end type
14+
15+
constructor UWSTRING_HAS_ID()
16+
end constructor
17+
18+
constructor UWSTRING_HAS_ID( byval s as const wstring const ptr )
19+
base( s )
20+
end constructor
21+
22+
constructor UWSTRING_HAS_ID( byval s as const zstring const ptr )
23+
base( s )
24+
end constructor
25+
26+
operator UWSTRING_HAS_ID.Cast() byref as wstring
27+
operator = *cast(wstring ptr, @_data)
28+
end operator
29+
30+
#define ustring UWSTRING_HAS_ID
31+
32+
SUITE( fbc_tests.udt_wstring_.swap_ )
33+
34+
#macro check( literal1, literal2 )
35+
scope
36+
dim t1 as wstring * 50 = literal1
37+
dim t2 as wstring * 50 = literal2
38+
39+
dim u1 as ustring = literal1
40+
u1.id = 1
41+
dim u2 as ustring = literal2
42+
u2.id = 2
43+
44+
'' initial condition check
45+
scope
46+
dim r1 as wstring * 50 = u1
47+
dim r2 as wstring * 50 = u2
48+
CU_ASSERT( u1.id = 1 )
49+
CU_ASSERT( u2.id = 2 )
50+
CU_ASSERT_WSTRING_EQUAL( r1, t1 )
51+
CU_ASSERT_WSTRING_EQUAL( r2, t2 )
52+
end scope
53+
54+
'' swap string only
55+
scope
56+
swap wstr( u1 ), wstr( u2 )
57+
58+
dim r1 as wstring * 50 = u1
59+
dim r2 as wstring * 50 = u2
60+
61+
CU_ASSERT( u1.id = 1 )
62+
CU_ASSERT( u2.id = 2 )
63+
CU_ASSERT_WSTRING_EQUAL( r1, t2 )
64+
CU_ASSERT_WSTRING_EQUAL( r2, t1 )
65+
end scope
66+
67+
'' swap string only
68+
scope
69+
swap wstr( u1 ), u2
70+
71+
dim r1 as wstring * 50 = u1
72+
dim r2 as wstring * 50 = u2
73+
74+
CU_ASSERT( u1.id = 1 )
75+
CU_ASSERT( u2.id = 2 )
76+
CU_ASSERT_WSTRING_EQUAL( r1, t1 )
77+
CU_ASSERT_WSTRING_EQUAL( r2, t2 )
78+
end scope
79+
80+
'' swap string only
81+
scope
82+
swap u1, wstr( u2 )
83+
84+
dim r1 as wstring * 50 = u1
85+
dim r2 as wstring * 50 = u2
86+
87+
CU_ASSERT( u1.id = 1 )
88+
CU_ASSERT( u2.id = 2 )
89+
CU_ASSERT_WSTRING_EQUAL( r1, t2 )
90+
CU_ASSERT_WSTRING_EQUAL( r2, t1 )
91+
end scope
92+
93+
'' swap complete UDT
94+
scope
95+
swap u1, u2
96+
97+
dim r1 as wstring * 50 = u1
98+
dim r2 as wstring * 50 = u2
99+
100+
CU_ASSERT( u1.id = 2 )
101+
CU_ASSERT( u2.id = 1 )
102+
CU_ASSERT_WSTRING_EQUAL( r1, t1 )
103+
CU_ASSERT_WSTRING_EQUAL( r2, t2 )
104+
end scope
105+
end scope
106+
#endmacro
107+
108+
TEST( default )
109+
check( "", "" )
110+
111+
check( "", "a" )
112+
check( "a", "a" )
113+
check( "a", "abcdefghi" )
114+
115+
check( "", !"\u3041\u3043\u3045\u3047\u3049" )
116+
check( "abc", !"\u3041\u3043\u3045\u3047\u3049" )
117+
check( !"\u3045", !"\u3041\u3043\u3047\u3049" )
118+
119+
END_TEST
120+
121+
END_SUITE

tests/udt-zstring/swap.bas

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "fbcunit.bi"
2+
#include once "uzstring-fixed.bi"
3+
#include once "chk-zstring.bi"
4+
5+
type UZSTRING_HAS_ID extends UZSTRING_FIXED_MUTABLE
6+
public:
7+
id as integer
8+
declare constructor()
9+
declare constructor( byval rhs as const wstring const ptr )
10+
declare constructor( byval rhs as const zstring const ptr )
11+
12+
declare operator cast() byref as zstring
13+
end type
14+
15+
constructor UZSTRING_HAS_ID()
16+
end constructor
17+
18+
constructor UZSTRING_HAS_ID( byval s as const wstring const ptr )
19+
base( s )
20+
end constructor
21+
22+
constructor UZSTRING_HAS_ID( byval s as const zstring const ptr )
23+
base( s )
24+
end constructor
25+
26+
operator UZSTRING_HAS_ID.Cast() byref as zstring
27+
operator = *cast(zstring ptr, @_data)
28+
end operator
29+
30+
#define ustring UZSTRING_HAS_ID
31+
32+
SUITE( fbc_tests.udt_zstring_.swap_ )
33+
34+
#macro check( literal1, literal2 )
35+
scope
36+
dim t1 as zstring * 50 = literal1
37+
dim t2 as zstring * 50 = literal2
38+
39+
dim u1 as ustring = literal1
40+
u1.id = 1
41+
dim u2 as ustring = literal2
42+
u2.id = 2
43+
44+
'' initial condition check
45+
scope
46+
dim r1 as zstring * 50 = u1
47+
dim r2 as zstring * 50 = u2
48+
CU_ASSERT( u1.id = 1 )
49+
CU_ASSERT( u2.id = 2 )
50+
CU_ASSERT_ZSTRING_EQUAL( r1, t1 )
51+
CU_ASSERT_ZSTRING_EQUAL( r2, t2 )
52+
end scope
53+
54+
'' swap string only
55+
scope
56+
swap str( u1 ), str( u2 )
57+
58+
dim r1 as zstring * 50 = u1
59+
dim r2 as zstring * 50 = u2
60+
61+
CU_ASSERT( u1.id = 1 )
62+
CU_ASSERT( u2.id = 2 )
63+
CU_ASSERT_ZSTRING_EQUAL( r1, t2 )
64+
CU_ASSERT_ZSTRING_EQUAL( r2, t1 )
65+
end scope
66+
67+
'' swap string only
68+
scope
69+
swap str( u1 ), u2
70+
71+
dim r1 as zstring * 50 = u1
72+
dim r2 as zstring * 50 = u2
73+
74+
CU_ASSERT( u1.id = 1 )
75+
CU_ASSERT( u2.id = 2 )
76+
CU_ASSERT_ZSTRING_EQUAL( r1, t1 )
77+
CU_ASSERT_ZSTRING_EQUAL( r2, t2 )
78+
end scope
79+
80+
'' swap string only
81+
scope
82+
swap u1, str( u2 )
83+
84+
dim r1 as zstring * 50 = u1
85+
dim r2 as zstring * 50 = u2
86+
87+
CU_ASSERT( u1.id = 1 )
88+
CU_ASSERT( u2.id = 2 )
89+
CU_ASSERT_ZSTRING_EQUAL( r1, t2 )
90+
CU_ASSERT_ZSTRING_EQUAL( r2, t1 )
91+
end scope
92+
93+
'' swap complete UDT
94+
scope
95+
swap u1, u2
96+
97+
dim r1 as zstring * 50 = u1
98+
dim r2 as zstring * 50 = u2
99+
100+
CU_ASSERT( u1.id = 2 )
101+
CU_ASSERT( u2.id = 1 )
102+
CU_ASSERT_ZSTRING_EQUAL( r1, t1 )
103+
CU_ASSERT_ZSTRING_EQUAL( r2, t2 )
104+
end scope
105+
end scope
106+
#endmacro
107+
108+
TEST( default )
109+
check( "", "" )
110+
111+
check( "", "a" )
112+
check( "a", "a" )
113+
check( "a", "abcdefghi" )
114+
115+
check( "", !"\u3041\u3043\u3045\u3047\u3049" )
116+
check( "abc", !"\u3041\u3043\u3045\u3047\u3049" )
117+
check( !"\u3045", !"\u3041\u3043\u3047\u3049" )
118+
119+
END_TEST
120+
121+
END_SUITE

tests/wstring/swap.bas

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "fbcunit.bi"
2+
#include once "chk-wstring.bi"
3+
4+
SUITE( fbc_tests.wstring_.swap_ )
5+
6+
#macro check( literal1, literal2 )
7+
scope
8+
dim w1 as wstring * 50 = literal1
9+
dim w2 as wstring * 50 = literal2
10+
11+
dim t1 as wstring * 50 = literal1
12+
dim t2 as wstring * 50 = literal2
13+
14+
CU_ASSERT_WSTRING_EQUAL( w1, t1 )
15+
CU_ASSERT_WSTRING_EQUAL( w2, t2 )
16+
17+
swap w1, w2
18+
19+
CU_ASSERT_WSTRING_EQUAL( w1, t2 )
20+
CU_ASSERT_WSTRING_EQUAL( w2, t1 )
21+
22+
swap w2, w1
23+
24+
CU_ASSERT_WSTRING_EQUAL( w1, t1 )
25+
CU_ASSERT_WSTRING_EQUAL( w2, t2 )
26+
end scope
27+
#endmacro
28+
29+
TEST( default )
30+
check( "", "" )
31+
32+
check( "", "a" )
33+
check( "a", "a" )
34+
check( "a", "abcdefghi" )
35+
36+
check( "", !"\u3041\u3043\u3045\u3047\u3049" )
37+
check( "abc", !"\u3041\u3043\u3045\u3047\u3049" )
38+
check( !"\u3045", !"\u3041\u3043\u3047\u3049" )
39+
40+
END_TEST
41+
42+
END_SUITE

0 commit comments

Comments
 (0)