]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cstring.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1997-2011, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
15 * Modification History:
17 * Date Name Description
18 * 6/18/98 hshih Created
19 * 09/08/98 stephen Added include for ctype, for Mac Port
20 * 11/15/99 helena Integrated S/390 IEEE changes.
21 ******************************************************************************
28 #include "unicode/utypes.h"
34 * We hardcode case conversion for invariant characters to match our expectation
35 * and the compiler execution charset.
36 * This prevents problems on systems
37 * - with non-default casing behavior, like Turkish system locales where
38 * tolower('I') maps to dotless i and toupper('i') maps to dotted I
39 * - where there are no lowercase Latin characters at all, or using different
40 * codes (some old EBCDIC codepages)
42 * This works because the compiler usually runs on a platform where the execution
43 * charset includes all of the invariant characters at their expected
44 * code positions, so that the char * string literals in ICU code match
45 * the char literals here.
47 * Note that the set of lowercase Latin letters is discontiguous in EBCDIC
48 * and the set of uppercase Latin letters is discontiguous as well.
51 U_CAPI UBool U_EXPORT2
52 uprv_isASCIILetter(char c
) {
53 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
55 ('a'<=c
&& c
<='i') || ('j'<=c
&& c
<='r') || ('s'<=c
&& c
<='z') ||
56 ('A'<=c
&& c
<='I') || ('J'<=c
&& c
<='R') || ('S'<=c
&& c
<='Z');
58 return ('a'<=c
&& c
<='z') || ('A'<=c
&& c
<='Z');
63 uprv_toupper(char c
) {
64 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
65 if(('a'<=c
&& c
<='i') || ('j'<=c
&& c
<='r') || ('s'<=c
&& c
<='z')) {
66 c
=(char)(c
+('A'-'a'));
69 if('a'<=c
&& c
<='z') {
70 c
=(char)(c
+('A'-'a'));
79 * Commented out because cstring.h defines uprv_tolower() to be
80 * the same as either uprv_asciitolower() or uprv_ebcdictolower()
81 * to reduce the amount of code to cover with tests.
83 * Note that this uprv_tolower() definition is likely to work for most
84 * charset families, not just ASCII and EBCDIC, because its #else branch
85 * is written generically.
88 uprv_tolower(char c
) {
89 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
90 if(('A'<=c
&& c
<='I') || ('J'<=c
&& c
<='R') || ('S'<=c
&& c
<='Z')) {
91 c
=(char)(c
+('a'-'A'));
94 if('A'<=c
&& c
<='Z') {
95 c
=(char)(c
+('a'-'A'));
102 U_CAPI
char U_EXPORT2
103 uprv_asciitolower(char c
) {
104 if(0x41<=c
&& c
<=0x5a) {
110 U_CAPI
char U_EXPORT2
111 uprv_ebcdictolower(char c
) {
112 if( (0xc1<=(uint8_t)c
&& (uint8_t)c
<=0xc9) ||
113 (0xd1<=(uint8_t)c
&& (uint8_t)c
<=0xd9) ||
114 (0xe2<=(uint8_t)c
&& (uint8_t)c
<=0xe9)
122 U_CAPI
char* U_EXPORT2
123 T_CString_toLowerCase(char* str
)
129 *str
= (char)uprv_tolower(*str
);
136 U_CAPI
char* U_EXPORT2
137 T_CString_toUpperCase(char* str
)
143 *str
= (char)uprv_toupper(*str
);
151 * Takes a int32_t and fills in a char* string with that number "radix"-based.
152 * Does not handle negative values (makes an empty string for them).
153 * Writes at most 12 chars ("-2147483647" plus NUL).
154 * Returns the length of the string (not including the NUL).
156 U_CAPI
int32_t U_EXPORT2
157 T_CString_integerToString(char* buffer
, int32_t v
, int32_t radix
)
160 int32_t tbx
= sizeof(tbuf
);
165 U_ASSERT(radix
>=2 && radix
<=16);
167 if(v
<0 && radix
== 10) {
168 /* Only in base 10 do we conside numbers to be signed. */
169 uval
= (uint32_t)(-v
);
170 buffer
[length
++] = '-';
173 tbx
= sizeof(tbuf
)-1;
174 tbuf
[tbx
] = 0; /* We are generating the digits backwards. Null term the end. */
176 digit
= (uint8_t)(uval
% radix
);
177 tbuf
[--tbx
] = (char)(T_CString_itosOffset(digit
));
181 /* copy converted number into user buffer */
182 uprv_strcpy(buffer
+length
, tbuf
+tbx
);
183 length
+= sizeof(tbuf
) - tbx
-1;
190 * Takes a int64_t and fills in a char* string with that number "radix"-based.
191 * Writes at most 21: chars ("-9223372036854775807" plus NUL).
192 * Returns the length of the string, not including the terminating NULL.
194 U_CAPI
int32_t U_EXPORT2
195 T_CString_int64ToString(char* buffer
, int64_t v
, uint32_t radix
)
198 int32_t tbx
= sizeof(tbuf
);
203 U_ASSERT(radix
>=2 && radix
<=16);
205 if(v
<0 && radix
== 10) {
206 /* Only in base 10 do we conside numbers to be signed. */
207 uval
= (uint64_t)(-v
);
208 buffer
[length
++] = '-';
211 tbx
= sizeof(tbuf
)-1;
212 tbuf
[tbx
] = 0; /* We are generating the digits backwards. Null term the end. */
214 digit
= (uint8_t)(uval
% radix
);
215 tbuf
[--tbx
] = (char)(T_CString_itosOffset(digit
));
219 /* copy converted number into user buffer */
220 uprv_strcpy(buffer
+length
, tbuf
+tbx
);
221 length
+= sizeof(tbuf
) - tbx
-1;
226 U_CAPI
int32_t U_EXPORT2
227 T_CString_stringToInteger(const char *integerString
, int32_t radix
)
230 return uprv_strtoul(integerString
, &end
, radix
);
235 uprv_stricmp(const char *str1
, const char *str2
) {
242 } else if(str2
==NULL
) {
245 /* compare non-NULL strings lexically with lowercase */
247 unsigned char c1
, c2
;
250 c1
=(unsigned char)*str1
;
251 c2
=(unsigned char)*str2
;
261 /* compare non-zero characters with lowercase */
262 rc
=(int)(unsigned char)uprv_tolower(c1
)-(int)(unsigned char)uprv_tolower(c2
);
274 uprv_strnicmp(const char *str1
, const char *str2
, uint32_t n
) {
281 } else if(str2
==NULL
) {
284 /* compare non-NULL strings lexically with lowercase */
286 unsigned char c1
, c2
;
289 c1
=(unsigned char)*str1
;
290 c2
=(unsigned char)*str2
;
300 /* compare non-zero characters with lowercase */
301 rc
=(int)(unsigned char)uprv_tolower(c1
)-(int)(unsigned char)uprv_tolower(c2
);
314 U_CAPI
char* U_EXPORT2
315 uprv_strdup(const char *src
) {
316 size_t len
= uprv_strlen(src
) + 1;
317 char *dup
= (char *) uprv_malloc(len
);
320 uprv_memcpy(dup
, src
, len
);
326 U_CAPI
char* U_EXPORT2
327 uprv_strndup(const char *src
, int32_t n
) {
331 dup
= uprv_strdup(src
);
333 dup
= (char*)uprv_malloc(n
+1);
335 uprv_memcpy(dup
, src
, n
);