1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius*   Copyright (C) 1997-2011, International Business Machines
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* File CSTRING.C
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* @author       Helena Shih
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Modification History:
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Date        Name        Description
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   6/18/98     hshih       Created
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   09/08/98    stephen     Added include for ctype, for Mac Port
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   11/15/99    helena      Integrated S/390 IEEE changes.
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdlib.h>
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdio.h>
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h"
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cstring.h"
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uassert.h"
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * We hardcode case conversion for invariant characters to match our expectation
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and the compiler execution charset.
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * This prevents problems on systems
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - with non-default casing behavior, like Turkish system locales where
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *   tolower('I') maps to dotless i and toupper('i') maps to dotted I
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - where there are no lowercase Latin characters at all, or using different
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *   codes (some old EBCDIC codepages)
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * This works because the compiler usually runs on a platform where the execution
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * charset includes all of the invariant characters at their expected
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * code positions, so that the char * string literals in ICU code match
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the char literals here.
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Note that the set of lowercase Latin letters is discontiguous in EBCDIC
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and the set of uppercase Latin letters is discontiguous as well.
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
4983a171d1a62abf406f7f44ae671823d5ec20db7dCraig CorneliusU_CAPI UBool U_EXPORT2
5083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Corneliusuprv_isASCIILetter(char c) {
5183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
5283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    return
5383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        ('a'<=c && c<='i') || ('j'<=c && c<='r') || ('s'<=c && c<='z') ||
5483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        ('A'<=c && c<='I') || ('J'<=c && c<='R') || ('S'<=c && c<='Z');
5583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius#else
5683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
5783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius#endif
5883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius}
5983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_toupper(char c) {
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(('a'<=c && c<='i') || ('j'<=c && c<='r') || ('s'<=c && c<='z')) {
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('A'-'a'));
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if('a'<=c && c<='z') {
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('A'-'a'));
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if 0
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Commented out because cstring.h defines uprv_tolower() to be
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the same as either uprv_asciitolower() or uprv_ebcdictolower()
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * to reduce the amount of code to cover with tests.
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Note that this uprv_tolower() definition is likely to work for most
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * charset families, not just ASCII and EBCDIC, because its #else branch
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * is written generically.
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_tolower(char c) {
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(('A'<=c && c<='I') || ('J'<=c && c<='R') || ('S'<=c && c<='Z')) {
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('a'-'A'));
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if('A'<=c && c<='Z') {
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('a'-'A'));
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_asciitolower(char c) {
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(0x41<=c && c<=0x5a) {
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+0x20);
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_ebcdictolower(char c) {
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( (0xc1<=(uint8_t)c && (uint8_t)c<=0xc9) ||
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (0xd1<=(uint8_t)c && (uint8_t)c<=0xd9) ||
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (0xe2<=(uint8_t)c && (uint8_t)c<=0xe9)
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c-0x40);
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_toLowerCase(char* str)
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char* origPtr = str;
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (str) {
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        do
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            *str = (char)uprv_tolower(*str);
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        while (*(str++));
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return origPtr;
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_toUpperCase(char* str)
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char* origPtr = str;
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (str) {
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        do
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            *str = (char)uprv_toupper(*str);
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        while (*(str++));
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return origPtr;
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Takes a int32_t and fills in  a char* string with that number "radix"-based.
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Does not handle negative values (makes an empty string for them).
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Writes at most 12 chars ("-2147483647" plus NUL).
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Returns the length of the string (not including the NUL).
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_integerToString(char* buffer, int32_t v, int32_t radix)
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char      tbuf[30];
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   tbx    = sizeof(tbuf);
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint8_t   digit;
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   length = 0;
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t  uval;
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_ASSERT(radix>=2 && radix<=16);
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uval = (uint32_t) v;
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(v<0 && radix == 10) {
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* Only in base 10 do we conside numbers to be signed. */
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval = (uint32_t)(-v);
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length++] = '-';
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbx = sizeof(tbuf)-1;
172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbuf[tbx] = 0;   /* We are generating the digits backwards.  Null term the end. */
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    do {
174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        digit = (uint8_t)(uval % radix);
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        tbuf[--tbx] = (char)(T_CString_itosOffset(digit));
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval  = uval / radix;
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } while (uval != 0);
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* copy converted number into user buffer  */
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_strcpy(buffer+length, tbuf+tbx);
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    length += sizeof(tbuf) - tbx -1;
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Takes a int64_t and fills in  a char* string with that number "radix"-based.
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Writes at most 21: chars ("-9223372036854775807" plus NUL).
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Returns the length of the string, not including the terminating NULL.
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_int64ToString(char* buffer, int64_t v, uint32_t radix)
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char      tbuf[30];
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   tbx    = sizeof(tbuf);
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint8_t   digit;
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   length = 0;
199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint64_t  uval;
200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_ASSERT(radix>=2 && radix<=16);
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uval = (uint64_t) v;
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(v<0 && radix == 10) {
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* Only in base 10 do we conside numbers to be signed. */
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval = (uint64_t)(-v);
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length++] = '-';
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbx = sizeof(tbuf)-1;
210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbuf[tbx] = 0;   /* We are generating the digits backwards.  Null term the end. */
211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    do {
212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        digit = (uint8_t)(uval % radix);
213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        tbuf[--tbx] = (char)(T_CString_itosOffset(digit));
214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval  = uval / radix;
215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } while (uval != 0);
216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* copy converted number into user buffer  */
218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_strcpy(buffer+length, tbuf+tbx);
219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    length += sizeof(tbuf) - tbx -1;
220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_stringToInteger(const char *integerString, int32_t radix)
226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *end;
228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return uprv_strtoul(integerString, &end, radix);
229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
23183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int U_EXPORT2
23383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Corneliusuprv_stricmp(const char *str1, const char *str2) {
234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(str1==NULL) {
235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(str2==NULL) {
236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return 0;
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else {
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return -1;
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else if(str2==NULL) {
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 1;
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* compare non-NULL strings lexically with lowercase */
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int rc;
245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        unsigned char c1, c2;
246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for(;;) {
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c1=(unsigned char)*str1;
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c2=(unsigned char)*str2;
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(c1==0) {
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(c2==0) {
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return 0;
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                } else {
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return -1;
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else if(c2==0) {
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return 1;
258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else {
259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                /* compare non-zero characters with lowercase */
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(rc!=0) {
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return rc;
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str1;
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str2;
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int U_EXPORT2
27283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Corneliusuprv_strnicmp(const char *str1, const char *str2, uint32_t n) {
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(str1==NULL) {
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(str2==NULL) {
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return 0;
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else {
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return -1;
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else if(str2==NULL) {
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 1;
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* compare non-NULL strings lexically with lowercase */
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int rc;
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        unsigned char c1, c2;
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for(; n--;) {
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c1=(unsigned char)*str1;
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c2=(unsigned char)*str2;
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(c1==0) {
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(c2==0) {
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return 0;
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                } else {
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return -1;
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else if(c2==0) {
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return 1;
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else {
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                /* compare non-zero characters with lowercase */
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(rc!=0) {
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return rc;
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str1;
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str2;
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return 0;
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_strdup(const char *src) {
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    size_t len = uprv_strlen(src) + 1;
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *dup = (char *) uprv_malloc(len);
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (dup) {
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uprv_memcpy(dup, src, len);
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return dup;
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_strndup(const char *src, int32_t n) {
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *dup;
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(n < 0) {
329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        dup = uprv_strdup(src);
330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        dup = (char*)uprv_malloc(n+1);
332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (dup) {
333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            uprv_memcpy(dup, src, n);
334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            dup[n] = 0;
335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return dup;
339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
340