1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Copyright (C) 1997-2003, 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
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_toupper(char c) {
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(('a'<=c && c<='i') || ('j'<=c && c<='r') || ('s'<=c && c<='z')) {
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('A'-'a'));
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if('a'<=c && c<='z') {
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('A'-'a'));
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if 0
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Commented out because cstring.h defines uprv_tolower() to be
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the same as either uprv_asciitolower() or uprv_ebcdictolower()
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * to reduce the amount of code to cover with tests.
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Note that this uprv_tolower() definition is likely to work for most
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * charset families, not just ASCII and EBCDIC, because its #else branch
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * is written generically.
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_tolower(char c) {
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(('A'<=c && c<='I') || ('J'<=c && c<='R') || ('S'<=c && c<='Z')) {
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('a'-'A'));
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if('A'<=c && c<='Z') {
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+('a'-'A'));
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_asciitolower(char c) {
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(0x41<=c && c<=0x5a) {
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c+0x20);
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char U_EXPORT2
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_ebcdictolower(char c) {
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( (0xc1<=(uint8_t)c && (uint8_t)c<=0xc9) ||
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (0xd1<=(uint8_t)c && (uint8_t)c<=0xd9) ||
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (0xe2<=(uint8_t)c && (uint8_t)c<=0xe9)
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        c=(char)(c-0x40);
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return c;
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_toLowerCase(char* str)
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char* origPtr = str;
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (str) {
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        do
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            *str = (char)uprv_tolower(*str);
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        while (*(str++));
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return origPtr;
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_toUpperCase(char* str)
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char* origPtr = str;
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (str) {
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        do
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            *str = (char)uprv_toupper(*str);
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        while (*(str++));
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return origPtr;
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Takes a int32_t and fills in  a char* string with that number "radix"-based.
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Does not handle negative values (makes an empty string for them).
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Writes at most 12 chars ("-2147483647" plus NUL).
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Returns the length of the string (not including the NUL).
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_integerToString(char* buffer, int32_t v, int32_t radix)
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char      tbuf[30];
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   tbx    = sizeof(tbuf);
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint8_t   digit;
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   length = 0;
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t  uval;
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_ASSERT(radix>=2 && radix<=16);
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uval = (uint32_t) v;
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(v<0 && radix == 10) {
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* Only in base 10 do we conside numbers to be signed. */
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval = (uint32_t)(-v);
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length++] = '-';
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbx = sizeof(tbuf)-1;
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbuf[tbx] = 0;   /* We are generating the digits backwards.  Null term the end. */
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    do {
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        digit = (uint8_t)(uval % radix);
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        tbuf[--tbx] = (char)(T_CString_itosOffset(digit));
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval  = uval / radix;
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } while (uval != 0);
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* copy converted number into user buffer  */
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_strcpy(buffer+length, tbuf+tbx);
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    length += sizeof(tbuf) - tbx -1;
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Takes a int64_t and fills in  a char* string with that number "radix"-based.
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Writes at most 21: chars ("-9223372036854775807" plus NUL).
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Returns the length of the string, not including the terminating NULL.
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_int64ToString(char* buffer, int64_t v, uint32_t radix)
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char      tbuf[30];
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   tbx    = sizeof(tbuf);
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint8_t   digit;
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t   length = 0;
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint64_t  uval;
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_ASSERT(radix>=2 && radix<=16);
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uval = (uint64_t) v;
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(v<0 && radix == 10) {
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* Only in base 10 do we conside numbers to be signed. */
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval = (uint64_t)(-v);
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length++] = '-';
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbx = sizeof(tbuf)-1;
199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    tbuf[tbx] = 0;   /* We are generating the digits backwards.  Null term the end. */
200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    do {
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        digit = (uint8_t)(uval % radix);
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        tbuf[--tbx] = (char)(T_CString_itosOffset(digit));
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uval  = uval / radix;
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } while (uval != 0);
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* copy converted number into user buffer  */
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_strcpy(buffer+length, tbuf+tbx);
208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    length += sizeof(tbuf) - tbx -1;
209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_stringToInteger(const char *integerString, int32_t radix)
215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *end;
217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return uprv_strtoul(integerString, &end, radix);
218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int U_EXPORT2
222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_stricmp(const char *str1, const char *str2) {
223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(str1==NULL) {
224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(str2==NULL) {
225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return 0;
226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else {
227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return -1;
228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else if(str2==NULL) {
230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 1;
231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* compare non-NULL strings lexically with lowercase */
233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int rc;
234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        unsigned char c1, c2;
235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for(;;) {
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c1=(unsigned char)*str1;
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c2=(unsigned char)*str2;
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(c1==0) {
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(c2==0) {
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return 0;
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                } else {
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return -1;
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else if(c2==0) {
246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return 1;
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else {
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                /* compare non-zero characters with lowercase */
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(rc!=0) {
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return rc;
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str1;
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str2;
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int U_EXPORT2
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruT_CString_strnicmp(const char *str1, const char *str2, uint32_t n) {
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(str1==NULL) {
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(str2==NULL) {
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return 0;
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        } else {
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return -1;
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else if(str2==NULL) {
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 1;
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* compare non-NULL strings lexically with lowercase */
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int rc;
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        unsigned char c1, c2;
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for(; n--;) {
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c1=(unsigned char)*str1;
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            c2=(unsigned char)*str2;
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            if(c1==0) {
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(c2==0) {
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return 0;
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                } else {
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return -1;
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else if(c2==0) {
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                return 1;
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            } else {
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                /* compare non-zero characters with lowercase */
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                if(rc!=0) {
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                    return rc;
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                }
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str1;
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            ++str2;
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return 0;
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_strdup(const char *src) {
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    size_t len = uprv_strlen(src) + 1;
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *dup = (char *) uprv_malloc(len);
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (dup) {
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uprv_memcpy(dup, src, len);
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return dup;
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI char* U_EXPORT2
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_strndup(const char *src, int32_t n) {
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *dup;
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(n < 0) {
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        dup = uprv_strdup(src);
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        dup = (char*)uprv_malloc(n+1);
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (dup) {
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            uprv_memcpy(dup, src, n);
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            dup[n] = 0;
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return dup;
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
329