]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cstring.c
2 ******************************************************************************
4 * Copyright (C) 1997-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
13 * Modification History:
15 * Date Name Description
16 * 6/18/98 hshih Created
17 * 09/08/98 stephen Added include for ctype, for Mac Port
18 * 11/15/99 helena Integrated S/390 IEEE changes.
19 ******************************************************************************
26 #include "unicode/utypes.h"
32 * We hardcode case conversion for invariant characters to match our expectation
33 * and the compiler execution charset.
34 * This prevents problems on systems
35 * - with non-default casing behavior, like Turkish system locales where
36 * tolower('I') maps to dotless i and toupper('i') maps to dotted I
37 * - where there are no lowercase Latin characters at all, or using different
38 * codes (some old EBCDIC codepages)
40 * This works because the compiler usually runs on a platform where the execution
41 * charset includes all of the invariant characters at their expected
42 * code positions, so that the char * string literals in ICU code match
43 * the char literals here.
45 * Note that the set of lowercase Latin letters is discontiguous in EBCDIC
46 * and the set of uppercase Latin letters is discontiguous as well.
49 U_CAPI UBool U_EXPORT2
50 uprv_isASCIILetter(char c
) {
51 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
53 ('a'<=c
&& c
<='i') || ('j'<=c
&& c
<='r') || ('s'<=c
&& c
<='z') ||
54 ('A'<=c
&& c
<='I') || ('J'<=c
&& c
<='R') || ('S'<=c
&& c
<='Z');
56 return ('a'<=c
&& c
<='z') || ('A'<=c
&& c
<='Z');
61 uprv_toupper(char c
) {
62 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
63 if(('a'<=c
&& c
<='i') || ('j'<=c
&& c
<='r') || ('s'<=c
&& c
<='z')) {
64 c
=(char)(c
+('A'-'a'));
67 if('a'<=c
&& c
<='z') {
68 c
=(char)(c
+('A'-'a'));
77 * Commented out because cstring.h defines uprv_tolower() to be
78 * the same as either uprv_asciitolower() or uprv_ebcdictolower()
79 * to reduce the amount of code to cover with tests.
81 * Note that this uprv_tolower() definition is likely to work for most
82 * charset families, not just ASCII and EBCDIC, because its #else branch
83 * is written generically.
86 uprv_tolower(char c
) {
87 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
88 if(('A'<=c
&& c
<='I') || ('J'<=c
&& c
<='R') || ('S'<=c
&& c
<='Z')) {
89 c
=(char)(c
+('a'-'A'));
92 if('A'<=c
&& c
<='Z') {
93 c
=(char)(c
+('a'-'A'));
100 U_CAPI
char U_EXPORT2
101 uprv_asciitolower(char c
) {
102 if(0x41<=c
&& c
<=0x5a) {
108 U_CAPI
char U_EXPORT2
109 uprv_ebcdictolower(char c
) {
110 if( (0xc1<=(uint8_t)c
&& (uint8_t)c
<=0xc9) ||
111 (0xd1<=(uint8_t)c
&& (uint8_t)c
<=0xd9) ||
112 (0xe2<=(uint8_t)c
&& (uint8_t)c
<=0xe9)
120 U_CAPI
char* U_EXPORT2
121 T_CString_toLowerCase(char* str
)
127 *str
= (char)uprv_tolower(*str
);
134 U_CAPI
char* U_EXPORT2
135 T_CString_toUpperCase(char* str
)
141 *str
= (char)uprv_toupper(*str
);
149 * Takes a int32_t and fills in a char* string with that number "radix"-based.
150 * Does not handle negative values (makes an empty string for them).
151 * Writes at most 12 chars ("-2147483647" plus NUL).
152 * Returns the length of the string (not including the NUL).
154 U_CAPI
int32_t U_EXPORT2
155 T_CString_integerToString(char* buffer
, int32_t v
, int32_t radix
)
158 int32_t tbx
= sizeof(tbuf
);
163 U_ASSERT(radix
>=2 && radix
<=16);
165 if(v
<0 && radix
== 10) {
166 /* Only in base 10 do we conside numbers to be signed. */
167 uval
= (uint32_t)(-v
);
168 buffer
[length
++] = '-';
171 tbx
= sizeof(tbuf
)-1;
172 tbuf
[tbx
] = 0; /* We are generating the digits backwards. Null term the end. */
174 digit
= (uint8_t)(uval
% radix
);
175 tbuf
[--tbx
] = (char)(T_CString_itosOffset(digit
));
179 /* copy converted number into user buffer */
180 uprv_strcpy(buffer
+length
, tbuf
+tbx
);
181 length
+= sizeof(tbuf
) - tbx
-1;
188 * Takes a int64_t and fills in a char* string with that number "radix"-based.
189 * Writes at most 21: chars ("-9223372036854775807" plus NUL).
190 * Returns the length of the string, not including the terminating NULL.
192 U_CAPI
int32_t U_EXPORT2
193 T_CString_int64ToString(char* buffer
, int64_t v
, uint32_t radix
)
196 int32_t tbx
= sizeof(tbuf
);
201 U_ASSERT(radix
>=2 && radix
<=16);
203 if(v
<0 && radix
== 10) {
204 /* Only in base 10 do we conside numbers to be signed. */
205 uval
= (uint64_t)(-v
);
206 buffer
[length
++] = '-';
209 tbx
= sizeof(tbuf
)-1;
210 tbuf
[tbx
] = 0; /* We are generating the digits backwards. Null term the end. */
212 digit
= (uint8_t)(uval
% radix
);
213 tbuf
[--tbx
] = (char)(T_CString_itosOffset(digit
));
217 /* copy converted number into user buffer */
218 uprv_strcpy(buffer
+length
, tbuf
+tbx
);
219 length
+= sizeof(tbuf
) - tbx
-1;
224 U_CAPI
int32_t U_EXPORT2
225 T_CString_stringToInteger(const char *integerString
, int32_t radix
)
228 return uprv_strtoul(integerString
, &end
, radix
);
233 uprv_stricmp(const char *str1
, const char *str2
) {
240 } else if(str2
==NULL
) {
243 /* compare non-NULL strings lexically with lowercase */
245 unsigned char c1
, c2
;
248 c1
=(unsigned char)*str1
;
249 c2
=(unsigned char)*str2
;
259 /* compare non-zero characters with lowercase */
260 rc
=(int)(unsigned char)uprv_tolower(c1
)-(int)(unsigned char)uprv_tolower(c2
);
272 uprv_strnicmp(const char *str1
, const char *str2
, uint32_t n
) {
279 } else if(str2
==NULL
) {
282 /* compare non-NULL strings lexically with lowercase */
284 unsigned char c1
, c2
;
287 c1
=(unsigned char)*str1
;
288 c2
=(unsigned char)*str2
;
298 /* compare non-zero characters with lowercase */
299 rc
=(int)(unsigned char)uprv_tolower(c1
)-(int)(unsigned char)uprv_tolower(c2
);
312 U_CAPI
char* U_EXPORT2
313 uprv_strdup(const char *src
) {
314 size_t len
= uprv_strlen(src
) + 1;
315 char *dup
= (char *) uprv_malloc(len
);
318 uprv_memcpy(dup
, src
, len
);
324 U_CAPI
char* U_EXPORT2
325 uprv_strndup(const char *src
, int32_t n
) {
329 dup
= uprv_strdup(src
);
331 dup
= (char*)uprv_malloc(n
+1);
333 uprv_memcpy(dup
, src
, n
);