~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/common/cstring.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
******************************************************************************
3
 
*
4
 
*   Copyright (C) 1997-2001, International Business Machines
5
 
*   Corporation and others.  All Rights Reserved.
6
 
*
7
 
******************************************************************************
8
 
*
9
 
* File CSTRING.C
10
 
*
11
 
* @author       Helena Shih
12
 
*
13
 
* Modification History:
14
 
*
15
 
*   Date        Name        Description
16
 
*   6/18/98     hshih       Created
17
 
*   09/08/98    stephen     Added include for ctype, for Mac Port
18
 
*   11/15/99    helena      Integrated S/390 IEEE changes. 
19
 
******************************************************************************
20
 
*/
21
 
 
22
 
 
23
 
 
24
 
#include <stdlib.h>
25
 
#include "unicode/utypes.h"
26
 
#include "cmemory.h"
27
 
#include "cstring.h"
28
 
 
29
 
U_CAPI char* U_EXPORT2
30
 
T_CString_toLowerCase(char* str)
31
 
{
32
 
    char* origPtr = str;
33
 
 
34
 
    if (str) {
35
 
        do
36
 
            *str = (char)tolower(*str);
37
 
        while (*(str++));
38
 
    }
39
 
 
40
 
    return origPtr;
41
 
}
42
 
 
43
 
U_CAPI char* U_EXPORT2
44
 
T_CString_toUpperCase(char* str)
45
 
{
46
 
    char* origPtr = str;
47
 
 
48
 
    if (str) {
49
 
        do
50
 
            *str = (char)toupper(*str);
51
 
        while (*(str++));
52
 
    }
53
 
 
54
 
    return origPtr;
55
 
}
56
 
 
57
 
/*Takes a int32_t and     fills in  a char* string with that number "radix"-based*/
58
 
 
59
 
U_CAPI void U_EXPORT2
60
 
T_CString_integerToString(char* buffer, int32_t i, int32_t radix)
61
 
{
62
 
  int32_t length=0;
63
 
  int32_t num = 0;
64
 
  int8_t digit;
65
 
  char temp;
66
 
 
67
 
  while (i>=radix)
68
 
    {
69
 
      num = i/radix;
70
 
      digit = (int8_t)(i - num*radix);
71
 
      buffer[length++] = (char)(T_CString_itosOffset(digit));
72
 
      i = num;
73
 
    }
74
 
 
75
 
  buffer[length] = (char)(T_CString_itosOffset(i));
76
 
  buffer[length+1] = '\0';
77
 
 
78
 
 
79
 
  /*Reverses the string*/
80
 
  for (i = 0; i < length; ++i, --length) {
81
 
    temp = buffer[length];
82
 
    buffer[length] = buffer[i];
83
 
    buffer[i] = temp;
84
 
  }
85
 
 
86
 
  return;
87
 
}
88
 
 
89
 
 
90
 
U_CAPI int32_t U_EXPORT2
91
 
T_CString_stringToInteger(const char *integerString, int32_t radix)
92
 
{
93
 
    char *end;
94
 
    return strtoul(integerString, &end, radix);
95
 
 
96
 
}
97
 
    
98
 
U_CAPI int U_EXPORT2
99
 
T_CString_stricmp(const char *str1, const char *str2) {
100
 
    if(str1==NULL) {
101
 
        if(str2==NULL) {
102
 
            return 0;
103
 
        } else {
104
 
            return -1;
105
 
        }
106
 
    } else if(str2==NULL) {
107
 
        return 1;
108
 
    } else {
109
 
        /* compare non-NULL strings lexically with lowercase */
110
 
        int rc;
111
 
        unsigned char c1, c2;
112
 
 
113
 
        for(;;) {
114
 
            c1=(unsigned char)*str1;
115
 
            c2=(unsigned char)*str2;
116
 
            if(c1==0) {
117
 
                if(c2==0) {
118
 
                    return 0;
119
 
                } else {
120
 
                    return -1;
121
 
                }
122
 
            } else if(c2==0) {
123
 
                return 1;
124
 
            } else {
125
 
                /* compare non-zero characters with lowercase */
126
 
                rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
127
 
                if(rc!=0) {
128
 
                    return rc;
129
 
                }
130
 
            }
131
 
            ++str1;
132
 
            ++str2;
133
 
        }
134
 
    }
135
 
}
136
 
 
137
 
U_CAPI int U_EXPORT2
138
 
T_CString_strnicmp(const char *str1, const char *str2, uint32_t n) {
139
 
    if(str1==NULL) {
140
 
        if(str2==NULL) {
141
 
            return 0;
142
 
        } else {
143
 
            return -1;
144
 
        }
145
 
    } else if(str2==NULL) {
146
 
        return 1;
147
 
    } else {
148
 
        /* compare non-NULL strings lexically with lowercase */
149
 
        int rc;
150
 
        unsigned char c1, c2;
151
 
 
152
 
        for(; n--;) {
153
 
            c1=(unsigned char)*str1;
154
 
            c2=(unsigned char)*str2;
155
 
            if(c1==0) {
156
 
                if(c2==0) {
157
 
                    return 0;
158
 
                } else {
159
 
                    return -1;
160
 
                }
161
 
            } else if(c2==0) {
162
 
                return 1;
163
 
            } else {
164
 
                /* compare non-zero characters with lowercase */
165
 
                rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
166
 
                if(rc!=0) {
167
 
                    return rc;
168
 
                }
169
 
            }
170
 
            ++str1;
171
 
            ++str2;
172
 
        }
173
 
    }
174
 
 
175
 
    return 0;
176
 
}
177
 
 
178
 
U_CAPI char* U_EXPORT2
179
 
uprv_strdup(const char *src) {
180
 
    size_t len = strlen(src) + 1;
181
 
    char *dup = (char *) uprv_malloc(len);
182
 
 
183
 
    if (dup) {
184
 
        uprv_memcpy(dup, src, len);
185
 
    }
186
 
 
187
 
    return dup;
188
 
}