2 Dynamic Unicode Strings
3 Copyright (C) Elrond 2000
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define g_free(x) safe_free(x)
26 #define INITIAL_SIZE 32
29 UString
*ustring_new(const char *init
)
33 str
= g_new(UString
, 1);
39 str
->alloc
= INITIAL_SIZE
;
40 str
->str
= g_new(uint16
, str
->alloc
);
50 ustring_assign_ascii_str(str
, init
);
56 void ustring_free(UString
*str
)
66 static size_t nearest_pow(size_t n
)
76 UString
*ustring_grow(UString
*str
, size_t new_alloc
)
82 if(str
->alloc
>= new_alloc
)
86 new_alloc
= nearest_pow(new_alloc
);
87 str
->str
= g_renew(uint16
, str
->str
, new_alloc
);
93 str
->alloc
= new_alloc
;
97 UString
*ustring_shrink(UString
*str
)
105 new_alloc
= str
->len
+ 1;
106 if (new_alloc
== str
->alloc
)
111 str
->str
= g_renew(uint16
, str
->str
, new_alloc
);
112 str
->alloc
= new_alloc
;
116 UString
*ustring_assign(UString
*str
, const uint16
*src
, size_t len
)
122 if ((src
== NULL
) || (len
== 0))
128 ustring_grow(str
, len
+1);
130 memcpy(str
->str
, src
, len
*sizeof(uint16
));
138 UString
*ustring_assign_ascii(UString
*str
, const char *src
, size_t len
)
146 if ((src
== NULL
) || (len
== 0))
152 ustring_grow(str
, len
+1);
170 UString
*ustring_assign_ascii_str(UString
*str
, const char *src
)
172 return ustring_assign_ascii(str
, src
, (src
? strlen(src
) : 0));
175 UString
*ustring_dup(const UString
*str
)
183 new = ustring_new(NULL
);
189 ustring_assign(new, str
->str
, str
->len
);
194 int ustring_compare(const UString
*a
, const UString
*b
)
200 if (a
== b
) return 0;
206 len
= MIN(a
->len
, b
->len
) + 1; /* include the final NUL */
208 while ((len
> 0) && (*pa
== *pb
))
216 if (a
->len
== b
->len
) return 0;
217 if (a
->len
< b
->len
) return 1;
227 int ustring_compare_case(const UString
*a
, const UString
*b
)
233 if (a
== b
) return 0;
239 len
= MIN(a
->len
, b
->len
) + 1; /* include the final NUL */
241 while ((len
> 0) && (toupper(*pa
) == toupper(*pb
)))
249 if (a
->len
== b
->len
) return 0;
250 if (a
->len
< b
->len
) return 1;
260 UString
*ustring_append_c(UString
*str
, uint16 c
)
266 ustring_grow(str
, str
->len
+ 2);
267 str
->str
[str
->len
] = c
;
269 str
->str
[str
->len
] = 0;
274 UString
*ustring_sync_len(UString
*str
)
284 p
= a
+ str
->len
- 1;
285 while((p
> a
) && (*p
== 0))
295 UString
*ustring_upper(UString
*str
)
316 UString
*ustring_lower(UString
*str
)
337 BOOL
ustring_equal(const UString
*s1
, const UString
*s2
)
343 a
= ustring_sync_len(ustring_dup(s1
));
344 b
= ustring_sync_len(ustring_dup(s2
));
345 ret
= ustring_compare_case(a
, b
);