FreeBASIC  0.91.0
fb_string.h
Go to the documentation of this file.
1 /* strings */
2 
8 #ifdef HOST_64BIT
9  #define FB_TEMPSTRBIT 0x8000000000000000ll
10 #else
11  #define FB_TEMPSTRBIT 0x80000000
12 #endif
13 
16 #define FB_ISTEMP(s) ((((FBSTRING *)s)->len & FB_TEMPSTRBIT) != 0)
17 
20 #define FB_STRSIZE(s) (((FBSTRING *)s)->len & ~FB_TEMPSTRBIT)
21 
24 #define FB_STRPTR(s,size) \
25  ( s == NULL? NULL : ( size == -1? ((FBSTRING *)s)->data : (char *)s ) )
26 
27 #define FB_STRSETUP_FIX(s,size,ptr,len) \
28 do { \
29  if( s == NULL ) \
30  { \
31  ptr = NULL; \
32  len = 0; \
33  } \
34  else \
35  { \
36  if( size == -1 ) \
37  { \
38  ptr = ((FBSTRING *)s)->data; \
39  len = FB_STRSIZE( s ); \
40  } \
41  else \
42  { \
43  ptr = (char *)s; \
44  /* always get the real len, as fix-len string */ \
45  /* will have garbage at end (nulls or spaces) */ \
46  len = strlen( (char *)s ); \
47  } \
48  } \
49 } while (0)
50 
51 #define FB_STRSETUP_DYN(s,size,ptr,len) \
52 do { \
53  if( s == NULL ) \
54  { \
55  ptr = NULL; \
56  len = 0; \
57  } \
58  else \
59  { \
60  switch ( size ) { \
61  case -1: \
62  ptr = ((FBSTRING *)s)->data; \
63  len = FB_STRSIZE( s ); \
64  break; \
65  case 0: \
66  ptr = (char *) s; \
67  len = strlen( ptr ); \
68  break; \
69  default: \
70  ptr = (char *) s; \
71  len = size - 1; /* without terminating NUL */ \
72  break; \
73  } \
74  } \
75 } while (0)
76 
82 typedef struct _FBSTRING {
83  char *data;
84  ssize_t len;
85  ssize_t size;
86 } FBSTRING;
87 
88 
89 typedef struct _FB_STR_TMPDESC {
93 
94 
95 /* protos */
96 
101 static __inline__ void fb_hStrSetLength( FBSTRING *str, size_t size ) {
102  str->len = size | (str->len & FB_TEMPSTRBIT);
103 }
104 
106 FBCALL int fb_hStrDelTempDesc ( FBSTRING *str );
107 FBCALL FBSTRING *fb_hStrAlloc ( FBSTRING *str, ssize_t size );
108 FBCALL FBSTRING *fb_hStrRealloc ( FBSTRING *str, ssize_t size, int preserve );
109 FBCALL FBSTRING *fb_hStrAllocTemp ( FBSTRING *str, ssize_t size );
111 FBCALL int fb_hStrDelTemp ( FBSTRING *str );
113 FBCALL void fb_hStrCopy ( char *dst, const char *src, ssize_t bytes );
114 FBCALL char *fb_hStrSkipChar ( char *s, ssize_t len, int c );
115 FBCALL char *fb_hStrSkipCharRev ( char *s, ssize_t len, int c );
116 
117 
118 /* public */
119 
120 FBCALL void *fb_StrInit ( void *dst, ssize_t dst_size, void *src, ssize_t src_size, int fill_rem );
121 FBCALL void *fb_StrAssign ( void *dst, ssize_t dst_size, void *src, ssize_t src_size, int fill_rem );
122 FBCALL void *fb_StrAssignEx ( void *dst, ssize_t dst_size, void *src, ssize_t src_size, int fill_rem, int is_init );
123 FBCALL void fb_StrDelete ( FBSTRING *str );
124 FBCALL FBSTRING *fb_StrConcat ( FBSTRING *dst, void *str1, ssize_t str1_size, void *str2, ssize_t str2_size );
125 FBCALL void *fb_StrConcatAssign ( void *dst, ssize_t dst_size, void *src, ssize_t src_size, int fillrem );
126 FBCALL int fb_StrCompare ( void *str1, ssize_t str1_size, void *str2, ssize_t str2_size );
128 FBCALL FBSTRING *fb_StrAllocTempDescF( char *str, ssize_t str_size );
130 FBCALL FBSTRING *fb_StrAllocTempDescZEx( const char *str, ssize_t len );
131 FBCALL FBSTRING *fb_StrAllocTempDescZ( const char *str );
132 FBCALL ssize_t fb_StrLen ( void *str, ssize_t str_size );
133 
134 FBCALL FBSTRING *fb_IntToStr ( int num );
135 FBCALL FBSTRING *fb_IntToStrQB ( int num );
136 FBCALL FBSTRING *fb_UIntToStr ( unsigned int num );
137 FBCALL FBSTRING *fb_UIntToStrQB ( unsigned int num );
138 FBCALL FBSTRING *fb_LongintToStr ( long long num );
139 FBCALL FBSTRING *fb_LongintToStrQB ( long long num );
140 FBCALL FBSTRING *fb_ULongintToStr ( unsigned long long num );
141 FBCALL FBSTRING *fb_ULongintToStrQB ( unsigned long long num );
142 FBCALL FBSTRING *fb_FloatToStr ( float num );
143 FBCALL FBSTRING *fb_FloatToStrQB ( float num );
144 FBCALL FBSTRING *fb_DoubleToStr ( double num );
145 FBCALL FBSTRING *fb_DoubleToStrQB ( double num );
146 
147 #define FB_F2A_ADDBLANK 0x00000001
148 
149 FBCALL double fb_hStr2Double ( char *src, ssize_t len );
150 FBCALL int fb_hStr2Int ( char *src, ssize_t len );
151 FBCALL unsigned int fb_hStr2UInt ( char *src, ssize_t len );
152 FBCALL long long fb_hStr2Longint ( char *src, ssize_t len );
153 FBCALL unsigned long long fb_hStr2ULongint( char *src, ssize_t len );
154 FBCALL int fb_hStrRadix2Int ( char *src, ssize_t len, int radix );
155 FBCALL long long fb_hStrRadix2Longint( char *s, ssize_t len, int radix );
156  char *fb_hFloat2Str ( double val, char *buffer, int digits, int mask );
157 
158  FBSTRING *fb_CHR ( int args, ... );
159 FBCALL unsigned int fb_ASC ( FBSTRING *str, ssize_t pos );
160 FBCALL double fb_VAL ( FBSTRING *str );
161 FBCALL double fb_CVD ( FBSTRING *str );
162 FBCALL float fb_CVS ( FBSTRING *str );
163 FBCALL short fb_CVSHORT ( FBSTRING *str );
164 FBCALL int fb_CVI ( FBSTRING *str ); /* 32bit legacy */
165 FBCALL int fb_CVL ( FBSTRING *str );
166 FBCALL long long fb_CVLONGINT ( FBSTRING *str );
167 FBCALL FBSTRING *fb_HEX ( int num );
168 FBCALL FBSTRING *fb_OCT ( int num );
169 FBCALL FBSTRING *fb_BIN ( int num );
170 
171 FBCALL FBSTRING *fb_BIN_b ( unsigned char num );
172 FBCALL FBSTRING *fb_BIN_s ( unsigned short num );
173 FBCALL FBSTRING *fb_BIN_i ( unsigned int num );
174 FBCALL FBSTRING *fb_BIN_l ( unsigned long long num );
175 FBCALL FBSTRING *fb_BIN_p ( void *p );
176 FBCALL FBSTRING *fb_BINEx_b ( unsigned char num, int digits );
177 FBCALL FBSTRING *fb_BINEx_s ( unsigned short num, int digits );
178 FBCALL FBSTRING *fb_BINEx_i ( unsigned int num, int digits );
179 FBCALL FBSTRING *fb_BINEx_l ( unsigned long long num, int digits );
180 FBCALL FBSTRING *fb_BINEx_p ( void *p, int digits );
181 
182 FBCALL FBSTRING *fb_OCT_b ( unsigned char num );
183 FBCALL FBSTRING *fb_OCT_s ( unsigned short num );
184 FBCALL FBSTRING *fb_OCT_i ( unsigned int num );
185 FBCALL FBSTRING *fb_OCT_l ( unsigned long long num );
186 FBCALL FBSTRING *fb_OCT_p ( void *p );
187 FBCALL FBSTRING *fb_OCTEx_b ( unsigned char num, int digits );
188 FBCALL FBSTRING *fb_OCTEx_s ( unsigned short num, int digits );
189 FBCALL FBSTRING *fb_OCTEx_i ( unsigned int num, int digits );
190 FBCALL FBSTRING *fb_OCTEx_l ( unsigned long long num, int digits );
191 FBCALL FBSTRING *fb_OCTEx_p ( void *p, int digits );
192 
193 FBCALL FBSTRING *fb_HEX_b ( unsigned char num );
194 FBCALL FBSTRING *fb_HEX_s ( unsigned short num );
195 FBCALL FBSTRING *fb_HEX_i ( unsigned int num );
196 FBCALL FBSTRING *fb_HEX_l ( unsigned long long num );
197 FBCALL FBSTRING *fb_HEX_p ( void *p );
198 FBCALL FBSTRING *fb_HEXEx_b ( unsigned char num, int digits );
199 FBCALL FBSTRING *fb_HEXEx_s ( unsigned short num, int digits );
200 FBCALL FBSTRING *fb_HEXEx_i ( unsigned int num, int digits );
201 FBCALL FBSTRING *fb_HEXEx_l ( unsigned long long num, int digits );
202 FBCALL FBSTRING *fb_HEXEx_p ( void *p, int digits );
203 
204 FBCALL FB_WCHAR *fb_WstrBin_b ( unsigned char num );
205 FBCALL FB_WCHAR *fb_WstrBin_s ( unsigned short num );
206 FBCALL FB_WCHAR *fb_WstrBin_i ( unsigned int num );
207 FBCALL FB_WCHAR *fb_WstrBin_l ( unsigned long long num );
208 FBCALL FB_WCHAR *fb_WstrBin_p ( void *p );
209 FBCALL FB_WCHAR *fb_WstrBinEx_b ( unsigned char num, int digits );
210 FBCALL FB_WCHAR *fb_WstrBinEx_s ( unsigned short num, int digits );
211 FBCALL FB_WCHAR *fb_WstrBinEx_i ( unsigned int num, int digits );
212 FBCALL FB_WCHAR *fb_WstrBinEx_l ( unsigned long long num, int digits );
213 FBCALL FB_WCHAR *fb_WstrBinEx_p ( void *p, int digits );
214 
215 FBCALL FB_WCHAR *fb_WstrHex_b ( unsigned char num );
216 FBCALL FB_WCHAR *fb_WstrHex_s ( unsigned short num );
217 FBCALL FB_WCHAR *fb_WstrHex_i ( unsigned int num );
218 FBCALL FB_WCHAR *fb_WstrHex_l ( unsigned long long num );
219 FBCALL FB_WCHAR *fb_WstrHex_p ( void *p );
220 FBCALL FB_WCHAR *fb_WstrHexEx_b ( unsigned char num, int digits );
221 FBCALL FB_WCHAR *fb_WstrHexEx_s ( unsigned short num, int digits );
222 FBCALL FB_WCHAR *fb_WstrHexEx_i ( unsigned int num, int digits );
223 FBCALL FB_WCHAR *fb_WstrHexEx_l ( unsigned long long num, int digits );
224 FBCALL FB_WCHAR *fb_WstrHexEx_p ( void *p, int digits );
225 
226 FBCALL FB_WCHAR *fb_WstrOct_b ( unsigned char num );
227 FBCALL FB_WCHAR *fb_WstrOct_s ( unsigned short num );
228 FBCALL FB_WCHAR *fb_WstrOct_i ( unsigned int num );
229 FBCALL FB_WCHAR *fb_WstrOct_l ( unsigned long long num );
230 FBCALL FB_WCHAR *fb_WstrOct_p ( void *p );
231 FBCALL FB_WCHAR *fb_WstrOctEx_b ( unsigned char num, int digits );
232 FBCALL FB_WCHAR *fb_WstrOctEx_s ( unsigned short num, int digits );
233 FBCALL FB_WCHAR *fb_WstrOctEx_i ( unsigned int num, int digits );
234 FBCALL FB_WCHAR *fb_WstrOctEx_l ( unsigned long long num, int digits );
235 FBCALL FB_WCHAR *fb_WstrOctEx_p ( void *p, int digits );
236 
237 FBCALL FBSTRING *fb_MKD ( double num );
238 FBCALL FBSTRING *fb_MKS ( float num );
239 FBCALL FBSTRING *fb_MKSHORT ( short num );
240 FBCALL FBSTRING *fb_MKI ( ssize_t num );
241 FBCALL FBSTRING *fb_MKL ( int num );
242 FBCALL FBSTRING *fb_MKLONGINT ( long long num );
243 FBCALL FBSTRING *fb_LEFT ( FBSTRING *str, ssize_t chars );
244 FBCALL FBSTRING *fb_RIGHT ( FBSTRING *str, ssize_t chars );
245 FBCALL FBSTRING *fb_SPACE ( ssize_t chars );
246 FBCALL FBSTRING *fb_LTRIM ( FBSTRING *str );
247 FBCALL FBSTRING *fb_LTrimEx ( FBSTRING *str, FBSTRING *pattern );
248 FBCALL FBSTRING *fb_LTrimAny ( FBSTRING *str, FBSTRING *pattern );
249 FBCALL FBSTRING *fb_RTRIM ( FBSTRING *str );
250 FBCALL FBSTRING *fb_RTrimEx ( FBSTRING *str, FBSTRING *pattern );
251 FBCALL FBSTRING *fb_RTrimAny ( FBSTRING *str, FBSTRING *pattern );
252 FBCALL FBSTRING *fb_TRIM ( FBSTRING *src );
253 FBCALL FBSTRING *fb_TrimEx ( FBSTRING *str, FBSTRING *pattern );
254 FBCALL FBSTRING *fb_TrimAny ( FBSTRING *str, FBSTRING *pattern );
255 FBCALL void fb_StrLset ( FBSTRING *dst, FBSTRING *src );
256 FBCALL void fb_StrRset ( FBSTRING *dst, FBSTRING *src );
257 FBCALL FBSTRING *fb_StrLcase2 ( FBSTRING *src, int mode );
258 FBCALL FBSTRING *fb_StrUcase2 ( FBSTRING *src, int mode );
259 FBCALL FBSTRING *fb_StrFill1 ( ssize_t cnt, int fchar );
260 FBCALL FBSTRING *fb_StrFill2 ( ssize_t cnt, FBSTRING *src );
261 FBCALL ssize_t fb_StrInstr ( ssize_t start, FBSTRING *src, FBSTRING *patt );
262 FBCALL ssize_t fb_StrInstrAny ( ssize_t start, FBSTRING *src, FBSTRING *patt );
263 FBCALL ssize_t fb_StrInstrRev ( FBSTRING *src, FBSTRING *patt, ssize_t start );
264 FBCALL ssize_t fb_StrInstrRevAny ( FBSTRING *src, FBSTRING *patt, ssize_t start );
265 FBCALL FBSTRING *fb_StrMid ( FBSTRING *src, ssize_t start, ssize_t len );
266 FBCALL void fb_StrAssignMid ( FBSTRING *dst, ssize_t start, ssize_t len, FBSTRING *src );
267 
268 /**************************************************************************************************
269  * Unicode strings
270  **************************************************************************************************/
271 
272 FBCALL FB_WCHAR *fb_WstrAlloc ( ssize_t chars );
273 FBCALL void *fb_WstrAssignToA_Init( void *dst, ssize_t dst_chars, FB_WCHAR *src, int fill_rem );
274 FBCALL void fb_WstrDelete ( FB_WCHAR *str );
275 FBCALL FB_WCHAR *fb_WstrAssign ( FB_WCHAR *dst, ssize_t dst_chars, FB_WCHAR *src );
276 FBCALL FB_WCHAR *fb_WstrAssignFromA ( FB_WCHAR *dst, ssize_t dst_chars, void *src, ssize_t src_chars );
277 FBCALL void *fb_WstrAssignToA ( void *dst, ssize_t dst_chars, FB_WCHAR *src, int fill_rem );
278 FBCALL void *fb_WstrAssignToAEx ( void *dst, ssize_t dst_chars, FB_WCHAR *src, int fill_rem, int is_init );
279 FBCALL FB_WCHAR *fb_WstrConcat ( const FB_WCHAR *str1, const FB_WCHAR *str2 );
280 FBCALL FB_WCHAR *fb_WstrConcatWA ( const FB_WCHAR *str1, const void *str2, ssize_t str2_size );
281 FBCALL FB_WCHAR *fb_WstrConcatAW ( const void *str1, ssize_t str1_size, const FB_WCHAR *str2 );
282 FBCALL FB_WCHAR *fb_WstrConcatAssign ( FB_WCHAR *dst, ssize_t dst_chars, const FB_WCHAR *src );
283 
284 FBCALL ssize_t fb_WstrLen ( FB_WCHAR *str );
285 FBCALL int fb_WstrCompare ( const FB_WCHAR *str1, const FB_WCHAR *str2 );
286 
287 FBCALL FB_WCHAR *fb_IntToWstr ( int num );
288 FBCALL FB_WCHAR *fb_UIntToWstr ( unsigned int num );
289 FBCALL FB_WCHAR *fb_LongintToWstr ( long long num );
290 FBCALL FB_WCHAR *fb_ULongintToWstr ( unsigned long long num );
291 FBCALL FB_WCHAR *fb_FloatToWstr ( float num );
292  FB_WCHAR *fb_FloatExToWstr ( double val, FB_WCHAR *buffer, int digits, int mask );
293 FBCALL FB_WCHAR *fb_DoubleToWstr ( double num );
294 FBCALL FB_WCHAR *fb_StrToWstr ( const char *src );
295 
296 FBCALL FBSTRING *fb_WstrToStr ( const FB_WCHAR *src );
297 FBCALL double fb_WstrToDouble ( const FB_WCHAR *src, ssize_t len );
298 FBCALL int fb_WstrToInt ( const FB_WCHAR *src, ssize_t len );
299 FBCALL unsigned int fb_WstrToUInt ( const FB_WCHAR *src, ssize_t len );
300 FBCALL long long fb_WstrToLongint ( const FB_WCHAR *src, ssize_t len );
301 FBCALL unsigned long long fb_WstrToULongint( const FB_WCHAR *src, ssize_t len );
302 FBCALL int fb_WstrRadix2Int ( const FB_WCHAR *src, ssize_t len, int radix );
303 FBCALL long long fb_WstrRadix2Longint( const FB_WCHAR *s, ssize_t len, int radix );
304 
305 FB_WCHAR *fb_WstrChr ( int args, ... );
306 FBCALL unsigned int fb_WstrAsc ( const FB_WCHAR *str, ssize_t pos );
307 FBCALL double fb_WstrVal ( const FB_WCHAR *str );
308 FBCALL int fb_WstrValInt ( const FB_WCHAR *str );
309 FBCALL unsigned int fb_WstrValUInt ( const FB_WCHAR *str );
310 FBCALL long long fb_WstrValLng ( const FB_WCHAR *str );
311 FBCALL unsigned long long fb_WstrValULng( const FB_WCHAR *str );
312 FBCALL FB_WCHAR *fb_WstrLeft ( const FB_WCHAR *str, ssize_t chars );
313 FBCALL FB_WCHAR *fb_WstrRight ( const FB_WCHAR *str, ssize_t chars );
314 FBCALL FB_WCHAR *fb_WstrSpace ( ssize_t chars );
315 FBCALL FB_WCHAR *fb_WstrLTrim ( const FB_WCHAR *str );
316 FBCALL FB_WCHAR *fb_WstrLTrimEx ( const FB_WCHAR *str, const FB_WCHAR *pattern );
317 FBCALL FB_WCHAR *fb_WstrLTrimAny ( const FB_WCHAR *str, const FB_WCHAR *pattern );
318 FBCALL FB_WCHAR *fb_WstrRTrim ( const FB_WCHAR *str );
319 FBCALL FB_WCHAR *fb_WstrRTrimEx ( const FB_WCHAR *str, const FB_WCHAR *pattern );
320 FBCALL FB_WCHAR *fb_WstrRTrimAny ( const FB_WCHAR *str, const FB_WCHAR *pattern );
321 FBCALL FB_WCHAR *fb_WstrTrim ( const FB_WCHAR *src );
322 FBCALL FB_WCHAR *fb_WstrTrimEx ( const FB_WCHAR *str, const FB_WCHAR *pattern );
323 FBCALL FB_WCHAR *fb_WstrTrimAny ( const FB_WCHAR *str, const FB_WCHAR *pattern );
324 FBCALL void fb_WstrLset ( FB_WCHAR *dst, FB_WCHAR *src );
325 FBCALL void fb_WstrRset ( FB_WCHAR *dst, FB_WCHAR *src );
326 FBCALL FB_WCHAR *fb_WstrLcase2 ( const FB_WCHAR *src, int mode );
327 FBCALL FB_WCHAR *fb_WstrUcase2 ( const FB_WCHAR *src, int mode );
328 FBCALL FB_WCHAR *fb_WstrFill1 ( ssize_t chars, int c );
329 FBCALL FB_WCHAR *fb_WstrFill2 ( ssize_t cnt, const FB_WCHAR *src );
330 FBCALL ssize_t fb_WstrInstr ( ssize_t start, const FB_WCHAR *src, const FB_WCHAR *patt );
331 FBCALL ssize_t fb_WstrInstrAny ( ssize_t start, const FB_WCHAR *src, const FB_WCHAR *patt );
332 FBCALL ssize_t fb_WstrInstrRev ( const FB_WCHAR *src, const FB_WCHAR *patt, ssize_t start );
333 FBCALL ssize_t fb_WstrInstrRevAny ( const FB_WCHAR *src, const FB_WCHAR *patt, ssize_t start );
334 FBCALL FB_WCHAR *fb_WstrMid ( const FB_WCHAR *src, ssize_t start, ssize_t len );
335 FBCALL void fb_WstrAssignMid ( FB_WCHAR *dst, ssize_t dst_len, ssize_t start, ssize_t len, const FB_WCHAR *src );
336 
337 /**************************************************************************************************
338  * VB-compatible functions
339  **************************************************************************************************/
340 
341 FBCALL FBSTRING * fb_StrFormat ( double value, FBSTRING *mask );
342 FBCALL FBSTRING * fb_hStrFormat ( double value, const char *mask, size_t mask_length );
343 
344 FBCALL int fb_VALINT ( FBSTRING *str );
345 FBCALL long long fb_VALLNG ( FBSTRING *str );
346 FBCALL unsigned int fb_VALUINT ( FBSTRING *str );
347 FBCALL unsigned long long fb_VALULNG ( FBSTRING *str );