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
|
#include <string.h>
#include <utilstr.h>
#include <ctype.h>
#ifdef _ICU_
#include <unicode/utypes.h>
#include <unicode/ucnv.h>
#include <unicode/ustring.h>
#include <unicode/uchar.h>
#include <unicode/unistr.h>
#include <unicode/translit.h>
#endif
/******************************************************************************
* stdstr - Sets/gets a string
*
* ENT: ipstr - pointer to a string pointer to set if necessary
* istr - string to set to *ipstr
* 0 - only get
*
* RET: *ipstr
*/
char *stdstr(char **ipstr, const char *istr) {
if (istr) {
if (*ipstr)
delete [] *ipstr;
int len = strlen(istr) + 1;
*ipstr = new char [ len*2 ]; // *2 buffer for unicode manipulations
memcpy(*ipstr, istr, len);
}
return *ipstr;
}
/******************************************************************************
* strstrip - Removes leading and trailing spaces from a string
*
* ENT: istr - string pointer to strip
*
* RET: *istr
*/
char *strstrip(char *istr) {
char *tmp = istr;
char *rtmp;
int len = strlen(istr);
if (len < 1)
return istr;
rtmp = istr + (len - 1);
while ((rtmp > istr)&&((*rtmp == ' ')||(*rtmp == '\t')||(*rtmp == 10)||(*rtmp == 13))) *(rtmp--) = 0;
while ((*tmp == ' ')||(*tmp == '\t')||(*tmp == 10)||(*tmp == 13)) tmp++;
memmove(istr, tmp, (rtmp - tmp) + 1);
istr[(rtmp - tmp) + 1] = 0;
return istr;
}
/******************************************************************************
* stristr - Scans a string for the occurrence of a given substring, no case
*
* ENT: scans s1 for the first occurrence of the substring s2, ingnoring case
*
* RET: a pointer to the element in s1, where s2 begins (points to s2 in s1).
* If s2 does not occur in s1, returns null.
*/
const char *stristr(const char *s1, const char *s2) {
int tLen = strlen(s2);
int cLen = strlen(s1);
char *target = new char [ tLen + 1 ];
int i, j;
const char *retVal = 0;
strcpy(target, s2);
for (i = 0; i < tLen; i++)
target[i] = SW_toupper(target[i]);
for (i = 0; i < (cLen - tLen)+1; i++) {
if (SW_toupper(s1[i]) == (unsigned char)*target) {
for (j = 1; j < tLen; j++) {
if (SW_toupper(s1[i+j]) != (unsigned char)target[j])
break;
}
if (j == tLen) {
retVal = s1+i;
break;
}
}
}
delete [] target;
return retVal;
}
/******************************************************************************
* strnicmp - compares the first n bytes of 2 string ignoring case
*
* ENT: compares s1 to s2 comparing the first n byte ingnoring case
*
* RET: same as strcmp
*/
const char strnicmp(const char *s1, const char *s2, int len) {
int tLen = strlen(s2);
int cLen = strlen(s1);
char diff;
int i;
for (i = 0; ((i < len) && (i < tLen) && (i < cLen)); i++) {
if ((diff = SW_toupper(*s1) - SW_toupper(*s2)))
return diff;
s1++;
s2++;
}
return (i < len) ? cLen - tLen : 0;
}
/******************************************************************************
* strlenw - Scans a string for trailing 0x0000 and return size in BYTES
*
* ENT: target - string for which to determine size
*
* RET: length in BYTES
* If s2 does not occur in s1, returns null.
*/
unsigned int strlenw(const char *s1) {
return strlen(s1);
// utf8 says no null in string except terminator, so below code is overkill
/*
const char *ch = s1;
if (!*ch)
ch++;
while (*ch) {
ch++;
if (!*ch)
ch++;
}
return (unsigned int)(ch - s1) - 1;
*/
}
/******************************************************************************
* toupperstr - converts a string to uppercase string
*
* ENT: target - string to convert
*
* RET: target
*/
char *toupperstr(char *buf) {
char *ret = buf;
while (*buf)
*buf = SW_toupper(*buf++);
return ret;
}
/******************************************************************************
* toupperstr - converts a string to uppercase string
*
* ENT: target - string to convert
*
* RET: target
*/
char *toupperstr_utf8(char *buf) {
char *ret = buf;
#ifndef _ICU_
// try to decide if it's worth trying to toupper. Do we have more
// characters that are probably lower latin than not?
long performOp = 0;
for (const char *ch = buf; *ch; ch++)
performOp += (*ch > 0) ? 1 : -1;
if (performOp) {
while (*buf)
*buf = SW_toupper(*buf++);
}
#else
UErrorCode err = U_ZERO_ERROR;
UConverter *conv = ucnv_open("UTF-8", &err);
UnicodeString str(buf, -1, conv, err);
UnicodeString ustr = str.toUpper();
ustr.extract(ret, strlen(ret)*2, conv, err);
ucnv_close(conv);
#endif
return ret;
}
|