aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/bfc/platform/std_string_osx.cpp
blob: ecfb16db7eff65aff82d181e282a587ecf1ffb3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <bfc/std.h>

// not perfect but it should work
int WCSICMP(const wchar_t *str1, const wchar_t *str2)
{
	while (*str1 && *str2) 
	{
		if (towlower(*str1) < towlower(*str2))
			return -1;
		if (towlower(*str2) < towlower(*str1)) 
			return 1;
		str1++;
		str2++;
	}
	
	if (!*str1 && !*str2) return 0;
	if (*str1) return 1;
	if (*str2) return -1;

	return -1; // shouldn't get here but we'll make the compiler shut up
}

int WCSNICMP(const wchar_t *str1, const wchar_t *str2, size_t len)
{
	while (*str1 && *str2 && len) 
	{
		if (towlower(*str1) < towlower(*str2)) 
			return -1;
		if (towlower(*str2) < towlower(*str1)) 
			return 1;
		str1++;
		str2++;
		len--;
	}

	if (!len) return 0;
	if (!*str1 && !*str2) return 0;
	if (*str1) return 1;
	if (*str2) return -1;

	return -1; // shouldn't get here but we'll make the compiler shut up
}


/* these are super slow because of memory allocation, but will tide us over until apple adds wcscasecmp and family to their BSD API */

#if 0 // remember this if we ever decide to use -fshort-wchar
int WCSICMP(const wchar_t *str1, const wchar_t *str2)
{
	CFStringRef cfstr1 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str1, wcslen(str1)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	CFStringRef cfstr2 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str2, wcslen(str2)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	int result = CFStringCompare(cfstr1, cfstr2, kCFCompareCaseInsensitive);
	CFRelease(cfstr1);
	CFRelease(cfstr2);
	return result;
}

int WCSNICMP(const wchar_t *str1, const wchar_t *str2, size_t len)
{
	CFStringRef cfstr1 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str1, wcslen(str1)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	CFStringRef cfstr2 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str2, wcslen(str2)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	int result =   CFStringCompareWithOptions(cfstr1, cfstr2, CFRangeMake(0, len), kCFCompareCaseInsensitive);
	CFRelease(cfstr1);
	CFRelease(cfstr2);
	return result;
}
#endif

int WCSICOLL(const wchar_t *str1, const wchar_t *str2)
{
	CFStringRef cfstr1 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str1, wcslen(str1)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	CFStringRef cfstr2 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str2, wcslen(str2)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	int result = CFStringCompare(cfstr1, cfstr2, kCFCompareCaseInsensitive|kCFCompareNonliteral);
	CFRelease(cfstr1);
	CFRelease(cfstr2);
	return result;
}

bool WCSIPREFIX(const wchar_t *str1, const wchar_t *prefix)
{
	CFStringRef cfstr1 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str1, wcslen(str1)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	CFStringRef cfstr2 =  CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)prefix, wcslen(prefix)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
	bool result = CFStringHasPrefix(cfstr1, cfstr2);
	CFRelease(cfstr1);
	CFRelease(cfstr2);
	return result;
}

wchar_t *DO_WCSDUP(const wchar_t *ptr EXTRA_INFO) 
{
	if (ptr == NULL) return NULL;
	int size = wcslen(ptr);
	wchar_t *ret = (wchar_t *)MALLOC((size + 1) * sizeof(wchar_t));
	if (ret != NULL) 
	{
		WCSCPYN(ret, ptr, size+1);
	}
	return ret;
}

void WCSCPYN(wchar_t *dest, const wchar_t *src, int maxchar)
{
	ASSERT(dest != NULL);
	ASSERT(src != NULL);
	wcsncpy(dest, src, maxchar-1); // TODO: switch to a less brain dead function
  dest[maxchar-1]=0;
}

void STRTOUPPER(char *str) 
{
if (str)
{
  while (*str)
  {
    *str = toupper(*str);
  }
}
}
void STRTOLOWER(char *str) 
{
  if (str)
  {
    while (*str)
    {
      *str = towlower(*str);
    }
  }
}

COMEXP void WCSTOUPPER(wchar_t *str)
{
  if (str)
  {
    while (*str)
    {
      *str = towupper(*str);
    }
  }
}

COMEXP void WCSTOLOWER(wchar_t *str)
{
  if (str)
  {
    while (*str)
    {
      *str = towlower(*str);
    }
  }
}

int WCSICMPSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1, const wchar_t *defval2) 
{
  if (str1 == NULL) str1 = defval1;
  if (str2 == NULL) str2 = defval2;
  return WCSICMP(str1, str2);
}

wchar_t *WCSTOK(wchar_t *str, const wchar_t *sep, wchar_t **last)
{
	return wcstok(str, sep, last);
}

int WCSCASEEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1, const wchar_t *defval2) 
{
  return !WCSICMPSAFE(str1, str2, defval1, defval2);
}

int STRLEN(const char *str) 
{
  ASSERT(str != NULL);
  return strlen(str);
}

bool ISALPHA(wchar_t alpha) 
{
	return iswalpha(alpha); 
}

bool ISDIGIT(wchar_t digit)
{
  return iswdigit(digit);
}

bool ISSPACE(wchar_t space)
{
  return iswspace(space);
}

bool ISPUNCT(wchar_t punct)
{
  return iswpunct(punct);
}

int STRCMP(const char *str1, const char *str2) 
{
  ASSERT(str1!=NULL);
  ASSERT(str2!=NULL);
  return strcmp(str1, str2);
}

int STRCMPSAFE(const char *str1, const char *str2, const char *defval1, const char *defval2) 
{
  if (str1 == NULL) str1 = defval1;
  if (str2 == NULL) str2 = defval2;
  return STRCMP(str1, str2);
}

void STRNCPY(char *dest, const char *src, int maxchar) 
{
  ASSERT(dest != NULL);
  ASSERT(src != NULL);
  strncpy(dest, src, maxchar);
  //INLINE
}

int STRICMPSAFE(const char *str1, const char *str2, const char *defval1, const char *defval2) 
{
  if (str1 == NULL) str1 = defval1;
  if (str2 == NULL) str2 = defval2;
  return STRICMP(str1, str2);
}

int STRICMP(const char *str1, const char *str2) 
{
  ASSERT(str1!=NULL);
  ASSERT(str2!=NULL);
  return strcasecmp(str1, str2);
}

void STRCPY(char *dest, const char *src) 
{
  ASSERT(dest != NULL);
  ASSERT(src != NULL);
  ASSERT(dest != src);
  strcpy(dest, src);
  //INLINE
}

char *STRSTR(const char *str1, const char *str2) 
{
  ASSERT(str1!=NULL);
  ASSERT(str2!=NULL);
  ASSERT(str1 != str2);
  return strstr(str1, str2);
}