FreeBASIC  0.91.0
swap_wstr.c
Go to the documentation of this file.
1 /* swap for wstrings */
2 
3 #include "fb.h"
4 
5 FBCALL void fb_WstrSwap( FB_WCHAR *str1, ssize_t size1, FB_WCHAR *str2, ssize_t size2 )
6 {
7  if( (str1 == NULL) || (str2 == NULL) )
8  return;
9 
10  /* Retrieve lengths */
11  ssize_t len1, len2;
12 
13  /* user-allocated wstring? */
14  if( size1 <= 0 )
15  len1 = fb_wstr_Len( str1 );
16  else
17  len1 = size1 - 1;
18 
19  if( size2 <= 0 )
20  len2 = fb_wstr_Len( str2 );
21  else
22  len2 = size2 - 1;
23 
24  /* Same length? Only need to do an fb_MemSwap() */
25  if( len1 == len2 ) {
26  if( len1 > 0 ) {
27  fb_MemSwap( (unsigned char *)str1,
28  (unsigned char *)str2,
29  len1 * sizeof( FB_WCHAR ) );
30  /* null terminators don't need to change */
31  }
32  return;
33  }
34 
35  /* Make str1/str2 be the smaller/larger string respectively */
36  if( len1 > len2 ) {
37  {
38  FB_WCHAR *str = str1;
39  str1 = str2;
40  str2 = str;
41  }
42 
43  {
44  ssize_t len = len1;
45  len1 = len2;
46  len2 = len;
47  }
48 
49  {
50  ssize_t size = size1;
51  size1 = size2;
52  size2 = size;
53  }
54  }
55 
56  /* MemSwap as much as possible (i.e. the smaller length) */
57  if( len1 > 0 ) {
58  fb_MemSwap( (unsigned char *)str1,
59  (unsigned char *)str2,
60  len1 * sizeof( FB_WCHAR ) );
61  }
62 
63  /* and copy over the remainder from larger to smaller, unless it's
64  a fixed-size wstring that doesn't have enough room left */
65  if( (size1 > 0) && (len2 >= size1) ) {
66  len2 = len1;
67  } else if( len2 > len1 ) {
68  fb_wstr_Move( (str1 + len1),
69  (str2 + len1),
70  len2 - len1 );
71  }
72 
73  /* set null terminators */
74  str1[len2] = L'\0';
75  str2[len1] = L'\0';
76 }