]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufmt_cmn.c
2 ******************************************************************************
4 * Copyright (C) 1998-2006, 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
- DIGIT_0
- (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 ? (DIGIT_0 + a) : (0x0037 + a)
60 #define TO_LC_DIGIT(a) a <= 9 ? (DIGIT_0 + 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
++] = DIGIT_0
; /*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 */
161 #define NIBBLE_PER_BYTE 2
163 ufmt_utop(const UChar
*buffer
,
166 int32_t count
, resultIdx
, incVal
, offset
;
167 /* This union allows the pointer to be written as an array. */
170 uint8_t bytes
[sizeof(void*)];
173 /* intialize variables */
178 /* Skip the leading zeros */
179 while(buffer
[count
] == DIGIT_0
|| u_isspace(buffer
[count
])) {
184 /* iterate through buffer, stop when you hit the end */
185 while(ufmt_isdigit(buffer
[count
], 16) && count
< *len
) {
186 /* increment the count consumed */
190 /* detect overflow */
191 if (count
- offset
> (int32_t)(sizeof(void*)*NIBBLE_PER_BYTE
)) {
192 offset
= count
- (int32_t)(sizeof(void*)*NIBBLE_PER_BYTE
);
195 /* Initialize the direction of the input */
198 resultIdx
= (int32_t)(sizeof(void*) - 1);
203 /* Write how much was consumed. */
205 while(--count
>= offset
) {
206 /* Get the first nibble of the byte */
207 uint8_t byte
= (uint8_t)ufmt_digitvalue(buffer
[count
]);
209 if (count
> offset
) {
210 /* Get the second nibble of the byte when available */
211 byte
= (uint8_t)(byte
+ (ufmt_digitvalue(buffer
[--count
]) << 4));
213 /* Write the byte into the array */
214 result
.bytes
[resultIdx
] = byte
;
222 ufmt_defaultCPToUnicode(const char *s
, int32_t sSize
,
223 UChar
*target
, int32_t tSize
)
226 UErrorCode status
= U_ZERO_ERROR
;
227 UConverter
*defConverter
= u_getDefaultConverter(&status
);
229 if(U_FAILURE(status
) || defConverter
== 0)
233 sSize
= uprv_strlen(s
) + 1;
236 /* perform the conversion in one swoop */
240 ucnv_toUnicode(defConverter
, &alias
, alias
+ tSize
, &s
, s
+ sSize
- 1,
241 NULL
, TRUE
, &status
);
244 /* add the null terminator */
248 u_releaseDefaultConverter(defConverter
);