Skip to content

Commit 135f364

Browse files
committed
udt-wstring: MID statement will accept UDT as Z|WSTRING
1 parent d5272bd commit 135f364

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Version 1.07.0
1717
- MID function will accept UDT as Z|WSTRING
1818
- SADD/STRPTR will accept UDT as Z|WSTRING to return Z|WSTRING ptr
1919
- LSET/RSET statements will accept UDT as Z|WSTRING
20+
- MID statement will accept UDT as Z|WSTRING
2021

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

src/compiler/rtl-string.bas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,6 +3131,8 @@ function rtlStrAssignMid _
31313131

31323132
function = NULL
31333133

3134+
astTryOvlStringCONV( expr1 )
3135+
31343136
''
31353137
if( astGetDataType( expr1 ) <> FB_DATATYPE_WCHAR ) then
31363138
proc = astNewCALL( PROCLOOKUP( STRASSIGNMID ) )

tests/udt-wstring/midstmt.bas

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include "fbcunit.bi"
2+
#include once "uwstring-fixed.bi"
3+
#include once "chk-wstring.bi"
4+
5+
#define ustring UWSTRING_FIXED_MUTABLE
6+
7+
#define BUFFERSIZE 50
8+
9+
'' fixed length strings are not cleared
10+
'' and the garbage in the buffer will affect the tests
11+
#macro INIT_FIXED_STRING( s, dtype, size, value )
12+
dim s as dtype * size
13+
clear @s, 0, sizeof(s)
14+
s = value
15+
#endmacro
16+
17+
SUITE( fbc_tests.udt_wstring_.midstmt )
18+
19+
'' we already test MID statment in ../wstring/midstmt.bas
20+
'' just compare that we get same results
21+
22+
#macro check_mid_start( dst, start, src, length1, length2 )
23+
scope
24+
INIT_FIXED_STRING( w1, wstring, BUFFERSIZE, left( dst, (length1) ) )
25+
INIT_FIXED_STRING( w2, wstring, BUFFERSIZE, left( src, (length2) ) )
26+
mid( w1, start ) = w2
27+
28+
dim u1 as ustring = w1
29+
mid( u1, start ) = w2
30+
31+
dim r1 as wstring * BUFFERSIZE = u1
32+
CU_ASSERT_WSTRING_EQUAL( w1, r1 )
33+
end scope
34+
#endmacro
35+
36+
#macro check_mid_start_n( dst, start, length, src, length1, length2 )
37+
scope
38+
INIT_FIXED_STRING( w1, wstring, BUFFERSIZE, left( dst, (length1) ) )
39+
INIT_FIXED_STRING( w2, wstring, BUFFERSIZE, left( src, (length2) ) )
40+
mid( w1, start, length ) = w2
41+
42+
dim u1 as ustring = w1
43+
dim u2 as ustring = w2
44+
mid( u1, start, length ) = w2
45+
46+
dim r1 as wstring * BUFFERSIZE = u1
47+
CU_ASSERT_WSTRING_EQUAL( w1, r1 )
48+
end scope
49+
#endmacro
50+
51+
TEST( ascii )
52+
const MAX = 8
53+
54+
#define def_dst "12345678"
55+
#define def_src "ABCDEFGH"
56+
57+
dim dst as wstring * (MAX*2+1)
58+
dim src as wstring * (MAX*2+1)
59+
60+
for i1 as integer = 0 to MAX
61+
for i2 as integer = 0 to MAX
62+
for istart as integer = -2 to MAX+2
63+
64+
dst = def_src
65+
src = def_dst
66+
67+
check_mid_start( dst, istart, src, i1, i2 )
68+
69+
for length as integer = -2 to MAX+2
70+
71+
dst = def_src
72+
src = def_dst
73+
74+
check_mid_start_n( dst, istart, length, src, i1, i2 )
75+
next
76+
77+
next
78+
next i2
79+
next i1
80+
81+
END_TEST
82+
83+
TEST( ucs2 )
84+
const MAX = 5
85+
86+
#define def_dst !"\u3041\u3043\u3045\u3047\u3049"
87+
#define def_src !"\u3042\u3044\u3046\u3048\u3050"
88+
89+
dim dst as wstring * (MAX*2+1)
90+
dim src as wstring * (MAX*2+1)
91+
92+
for i1 as integer = 0 to MAX
93+
for i2 as integer = 0 to MAX
94+
for istart as integer = -1 to MAX+2
95+
96+
dst = def_src
97+
src = def_dst
98+
99+
check_mid_start( dst, istart, src, i1, i2 )
100+
101+
for length as integer = -2 to MAX+2
102+
103+
dst = def_src
104+
src = def_dst
105+
106+
check_mid_start_n( dst, istart, length, src, i1, i2 )
107+
next
108+
109+
next
110+
next i2
111+
next i1
112+
113+
END_TEST
114+
115+
END_SUITE

tests/udt-zstring/midstmt.bas

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "fbcunit.bi"
2+
#include once "uzstring-fixed.bi"
3+
#include once "chk-zstring.bi"
4+
5+
#define ustring UZSTRING_FIXED_MUTABLE
6+
7+
#define BUFFERSIZE 50
8+
9+
'' fixed length strings are not cleared
10+
'' and the garbage in the buffer will affect the tests
11+
#macro INIT_FIXED_STRING( s, dtype, size, value )
12+
dim s as dtype * size
13+
clear @s, 0, sizeof(s)
14+
s = value
15+
#endmacro
16+
17+
SUITE( fbc_tests.udt_zstring_.midstmt )
18+
19+
#macro check_mid_start( dst, start, src, length1, length2 )
20+
scope
21+
INIT_FIXED_STRING( w1, zstring, BUFFERSIZE, left( dst, (length1) ) )
22+
INIT_FIXED_STRING( w2, zstring, BUFFERSIZE, left( src, (length2) ) )
23+
mid( w1, start ) = w2
24+
25+
dim u1 as ustring = w1
26+
mid( u1, start ) = w2
27+
28+
dim r1 as zstring * BUFFERSIZE = u1
29+
CU_ASSERT_ZSTRING_EQUAL( w1, r1 )
30+
end scope
31+
#endmacro
32+
33+
#macro check_mid_start_n( dst, start, length, src, length1, length2 )
34+
scope
35+
INIT_FIXED_STRING( w1, zstring, BUFFERSIZE, left( dst, (length1) ) )
36+
INIT_FIXED_STRING( w2, zstring, BUFFERSIZE, left( src, (length2) ) )
37+
mid( w1, start, length ) = w2
38+
39+
dim u1 as ustring = w1
40+
dim u2 as ustring = w2
41+
mid( u1, start, length ) = w2
42+
43+
dim r1 as zstring * BUFFERSIZE = u1
44+
CU_ASSERT_ZSTRING_EQUAL( w1, r1 )
45+
end scope
46+
#endmacro
47+
48+
TEST( ascii )
49+
const MAX = 8
50+
51+
#define def_dst "12345678"
52+
#define def_src "ABCDEFGH"
53+
54+
dim dst as zstring * (MAX*2+1)
55+
dim src as zstring * (MAX*2+1)
56+
57+
for i1 as integer = 0 to MAX
58+
for i2 as integer = 0 to MAX
59+
for istart as integer = -2 to MAX+2
60+
61+
dst = def_src
62+
src = def_dst
63+
64+
check_mid_start( dst, istart, src, i1, i2 )
65+
66+
for length as integer = -2 to MAX+2
67+
68+
dst = def_src
69+
src = def_dst
70+
71+
check_mid_start_n( dst, istart, length, src, i1, i2 )
72+
next
73+
74+
next
75+
next i2
76+
next i1
77+
78+
END_TEST
79+
80+
TEST( ucs2 )
81+
const MAX = 5
82+
83+
#define def_dst !"\u3041\u3043\u3045\u3047\u3049"
84+
#define def_src !"\u3042\u3044\u3046\u3048\u3050"
85+
86+
dim dst as zstring * (MAX*2+1)
87+
dim src as zstring * (MAX*2+1)
88+
89+
for i1 as integer = 0 to MAX
90+
for i2 as integer = 0 to MAX
91+
for istart as integer = -1 to MAX+2
92+
93+
dst = def_src
94+
src = def_dst
95+
96+
check_mid_start( dst, istart, src, i1, i2 )
97+
98+
for length as integer = -2 to MAX+2
99+
100+
dst = def_src
101+
src = def_dst
102+
103+
check_mid_start_n( dst, istart, length, src, i1, i2 )
104+
next
105+
106+
next
107+
next i2
108+
next i1
109+
110+
END_TEST
111+
112+
END_SUITE

tests/wstring/midstmt.bas

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include "fbcunit.bi"
2+
#include once "chk-wstring.bi"
3+
4+
#define BUFFERSIZE 50
5+
6+
'' fixed length strings are not cleared
7+
'' and the garbage in the buffer will affect the tests
8+
#macro INIT_FIXED_STRING( s, dtype, size, value )
9+
dim s as dtype * size
10+
clear @s, 0, sizeof(s)
11+
s = value
12+
#endmacro
13+
14+
SUITE( fbc_tests.wstring_.midstmt )
15+
16+
#macro check_mid_start( dst, start, src, length1, length2 )
17+
scope
18+
INIT_FIXED_STRING( w1, wstring, BUFFERSIZE, left( dst, (length1) ) )
19+
INIT_FIXED_STRING( w2, wstring, BUFFERSIZE, left( src, (length2) ) )
20+
INIT_FIXED_STRING( e1, wstring, BUFFERSIZE, w1 )
21+
dim n1 as integer = 50 '' len(w1)
22+
dim n2 as integer = len(w2)
23+
dim idx1 as integer = start
24+
dim idx2 as integer = 1
25+
26+
'' compute the expected string
27+
if( (n1 > 0) and (start >= 1) and (start <= n1) ) then
28+
while( idx1 >= 1 and idx1 <= n1 and idx2 >= 1 and idx2 <= n2 )
29+
e1[idx1-1] = w2[idx2-1]
30+
idx1 += 1
31+
idx2 += 1
32+
wend
33+
e1[n1] = 0
34+
end if
35+
36+
mid( w1, start ) = w2
37+
38+
CU_ASSERT_WSTRING_EQUAL( w1, e1 )
39+
end scope
40+
#endmacro
41+
42+
#macro check_mid_start_n( dst, start, length, src, length1, length2 )
43+
scope
44+
INIT_FIXED_STRING( w1, wstring, BUFFERSIZE, left( dst, (length1) ) )
45+
INIT_FIXED_STRING( w2, wstring, BUFFERSIZE, left( src, (length2) ) )
46+
INIT_FIXED_STRING( e1, wstring, BUFFERSIZE, w1 )
47+
dim n1 as integer = BUFFERSIZE '' len(w1)
48+
dim n2 as integer = len(w2)
49+
dim idx1 as integer = start
50+
dim idx2 as integer = 1
51+
dim n as integer = 0
52+
53+
'' compute the expected string
54+
if( (n1 > 0) and (start >= 1) and (start <= n1) ) then
55+
while( idx1 >= 1 and idx1 <= n1 and idx2 >= 1 and idx2 <= n2 and (n < length or length < 1) )
56+
e1[idx1-1] = w2[idx2-1]
57+
idx1 += 1
58+
idx2 += 1
59+
n += 1
60+
wend
61+
e1[n1] = 0
62+
end if
63+
64+
mid( w1, start, length ) = w2
65+
66+
CU_ASSERT_WSTRING_EQUAL( w1, e1 )
67+
end scope
68+
#endmacro
69+
70+
TEST( ascii )
71+
const MAX = 8
72+
73+
#define def_dst "12345678"
74+
#define def_src "ABCDEFGH"
75+
76+
dim dst as wstring * (MAX*2+1)
77+
dim src as wstring * (MAX*2+1)
78+
79+
for i1 as integer = 0 to MAX
80+
for i2 as integer = 0 to MAX
81+
for istart as integer = -2 to MAX+2
82+
83+
dst = def_src
84+
src = def_dst
85+
86+
check_mid_start( dst, istart, src, i1, i2 )
87+
88+
for length as integer = -2 to MAX+2
89+
90+
dst = def_src
91+
src = def_dst
92+
93+
check_mid_start_n( dst, istart, length, src, i1, i2 )
94+
next
95+
96+
next
97+
next i2
98+
next i1
99+
100+
END_TEST
101+
102+
TEST( ucs2 )
103+
const MAX = 5
104+
105+
#define def_dst !"\u3041\u3043\u3045\u3047\u3049"
106+
#define def_src !"\u3042\u3044\u3046\u3048\u3050"
107+
108+
dim dst as wstring * (MAX*2+1)
109+
dim src as wstring * (MAX*2+1)
110+
111+
for i1 as integer = 0 to MAX
112+
for i2 as integer = 0 to MAX
113+
for istart as integer = -1 to MAX+2
114+
115+
dst = def_src
116+
src = def_dst
117+
118+
check_mid_start( dst, istart, src, i1, i2 )
119+
120+
for length as integer = -2 to MAX+2
121+
122+
dst = def_src
123+
src = def_dst
124+
125+
check_mid_start_n( dst, istart, length, src, i1, i2 )
126+
next
127+
128+
next
129+
next i2
130+
next i1
131+
132+
END_TEST
133+
134+
END_SUITE

0 commit comments

Comments
 (0)