]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufmt_cmn.c
2 ******************************************************************************
4 * Copyright (C) 1998-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 12/02/98 stephen Creation.
15 * 03/12/99 stephen Modified for new C API.
16 * 03/15/99 stephen Added defaultCPToUnicode, unicodeToDefaultCP
17 * 07/19/99 stephen Fixed bug in defaultCPToUnicode
18 ******************************************************************************
24 #include "unicode/uchar.h"
25 #include "unicode/ucnv.h"
28 #define DIGIT_0 0x0030
29 #define DIGIT_9 0x0039
30 #define LOWERCASE_A 0x0061
31 #define UPPERCASE_A 0x0041
32 #define LOWERCASE_Z 0x007A
33 #define UPPERCASE_Z 0x005A
36 ufmt_digitvalue(UChar c
)
38 if( ((c
>=DIGIT_0
)&&(c
<=DIGIT_9
)) ||
39 ((c
>=LOWERCASE_A
)&&(c
<=LOWERCASE_Z
)) ||
40 ((c
>=UPPERCASE_A
)&&(c
<=UPPERCASE_Z
)) )
42 return c
- 0x0030 - (c
>= 0x0041 ? (c
>= 0x0061 ? 39 : 7) : 0);
54 int digitVal
= ufmt_digitvalue(c
);
56 return (UBool
)(digitVal
< radix
&& digitVal
>= 0);
59 #define TO_UC_DIGIT(a) a <= 9 ? (0x0030 + a) : (0x0037 + a)
60 #define TO_LC_DIGIT(a) a <= 9 ? (0x0030 + a) : (0x0057 + a)
63 ufmt_64tou(UChar
*buffer
,
72 UChar
*left
, *right
, temp
;
75 digit
= (uint32_t)(value
% radix
);
76 value
= value
/ radix
;
77 buffer
[length
++] = (UChar
)(uselower
? TO_LC_DIGIT(digit
)
78 : TO_UC_DIGIT(digit
));
81 /* pad with zeroes to make it minDigits long */
82 if(minDigits
!= -1 && length
< minDigits
) {
83 while(length
< minDigits
&& length
< *len
)
84 buffer
[length
++] = 0x0030; /*zero padding */
87 /* reverse the buffer */
89 right
= buffer
+ length
;
90 while(left
< --right
) {
100 ufmt_ptou(UChar
*buffer
,
107 uint8_t *ptrIdx
= (uint8_t *)&value
;
110 for (i
= 0; i
< (int32_t)sizeof(void *); i
++)
112 for (i
= (int32_t)sizeof(void *)-1; i
>= 0 ; i
--)
115 uint8_t byteVal
= ptrIdx
[i
];
116 uint16_t firstNibble
= (uint16_t)(byteVal
>>4);
117 uint16_t secondNibble
= (uint16_t)(byteVal
&0xF);
119 buffer
[length
++]=TO_LC_DIGIT(firstNibble
);
120 buffer
[length
++]=TO_LC_DIGIT(secondNibble
);
123 buffer
[length
++]=TO_UC_DIGIT(firstNibble
);
124 buffer
[length
++]=TO_UC_DIGIT(secondNibble
);
132 ufmt_uto64(const UChar
*buffer
,
141 /* intialize parameters */
142 limit
= buffer
+ *len
;
146 /* iterate through buffer */
147 while(ufmt_isdigit(*buffer
, radix
) && buffer
< limit
) {
149 /* read the next digit */
151 result
+= ufmt_digitvalue(*buffer
++);
153 /* increment our count */
162 ufmt_utop(const UChar
*buffer
,
166 TODO: Fix this code so that it will work with pointers that are 2<=sizeof(void*)<=16
173 /* intialize parameters */
174 limit
= buffer
+ *len
;
178 /* iterate through buffer */
179 /* limit to sixteen iterations since that is the max that an int64_t can contain for pointer work */
180 while(ufmt_isdigit(*buffer
, 16) && buffer
< limit
) {
182 /* read the next digit */
184 result
+= ufmt_digitvalue(*buffer
++);
186 /* increment our count */
191 return (void *)result
;
195 ufmt_defaultCPToUnicode(const char *s
, int32_t sSize
,
196 UChar
*target
, int32_t tSize
)
199 UErrorCode status
= U_ZERO_ERROR
;
200 UConverter
*defConverter
= u_getDefaultConverter(&status
);
202 if(U_FAILURE(status
) || defConverter
== 0)
206 sSize
= uprv_strlen(s
) + 1;
209 /* perform the conversion in one swoop */
213 ucnv_toUnicode(defConverter
, &alias
, alias
+ tSize
, &s
, s
+ sSize
- 1,
214 NULL
, TRUE
, &status
);
217 /* add the null terminator */
221 u_releaseDefaultConverter(defConverter
);