1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 1998-1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File ustr.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 05/28/99 stephen Creation.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ustr.h"
|
|
|
|
#include "cmemory.h"
|
2000-07-14 22:31:35 +00:00
|
|
|
#include "cstring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* Protos */
|
|
|
|
static void ustr_resize(struct UString *s, int32_t len, UErrorCode *status);
|
|
|
|
|
|
|
|
/* Macros */
|
|
|
|
#define ALLOCATION(minSize) (minSize < 0x80 ? 0x80 : (2 * minSize + 0x80) & ~(0x80 - 1))
|
|
|
|
|
|
|
|
void
|
|
|
|
ustr_init(struct UString *s)
|
|
|
|
{
|
|
|
|
s->fChars = 0;
|
|
|
|
s->fLength = s->fCapacity = 0;
|
|
|
|
}
|
|
|
|
|
2000-07-14 22:31:35 +00:00
|
|
|
void
|
|
|
|
ustr_initChars(struct UString *s, const char* source, int32_t length, UErrorCode *status)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
if (U_FAILURE(*status)) return;
|
|
|
|
s->fChars = 0;
|
|
|
|
s->fLength = s->fCapacity = 0;
|
|
|
|
if (length == -1) length = uprv_strlen(source);
|
|
|
|
if(s->fCapacity < length) {
|
|
|
|
ustr_resize(s, ALLOCATION(length), status);
|
|
|
|
if(U_FAILURE(*status)) return;
|
|
|
|
}
|
|
|
|
for (; i < length; i++)
|
|
|
|
{
|
2000-07-28 03:18:30 +00:00
|
|
|
UChar charToAppend;
|
|
|
|
u_charsToUChars(source+i, &charToAppend, 1);
|
|
|
|
ustr_ucat(s, charToAppend, status);
|
|
|
|
/*
|
2000-07-14 22:31:35 +00:00
|
|
|
#if U_CHARSET_FAMILY==U_ASCII_FAMILY
|
|
|
|
ustr_ucat(s, (UChar)(uint8_t)(source[i]), status);
|
|
|
|
#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
|
|
|
|
ustr_ucat(s, (UChar)asciiFromEbcdic[(uint8_t)(*cs++)], status);
|
|
|
|
#else
|
|
|
|
# error U_CHARSET_FAMILY is not valid
|
|
|
|
#endif
|
2000-07-28 03:18:30 +00:00
|
|
|
*/
|
2000-07-14 22:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
ustr_deinit(struct UString *s)
|
|
|
|
{
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_free(s->fChars);
|
1999-08-16 21:50:52 +00:00
|
|
|
s->fChars = 0;
|
|
|
|
s->fLength = s->fCapacity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ustr_cpy(struct UString *dst,
|
|
|
|
const struct UString *src,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status) || dst == src) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(dst->fCapacity < src->fLength) {
|
|
|
|
ustr_resize(dst, ALLOCATION(src->fLength), status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(dst->fChars, src->fChars, sizeof(UChar) * src->fLength);
|
1999-08-16 21:50:52 +00:00
|
|
|
dst->fLength = src->fLength;
|
|
|
|
dst->fChars[dst->fLength] = 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ustr_setlen(struct UString *s,
|
|
|
|
int32_t len,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(s->fCapacity < (len + 1)) {
|
|
|
|
ustr_resize(s, ALLOCATION(len), status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->fLength = len;
|
|
|
|
s->fChars[len] = 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ustr_cat(struct UString *dst,
|
|
|
|
const struct UString *src,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
ustr_ncat(dst, src, src->fLength, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ustr_ncat(struct UString *dst,
|
|
|
|
const struct UString *src,
|
|
|
|
int32_t n,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status) || dst == src) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(dst->fCapacity < (dst->fLength + n)) {
|
|
|
|
ustr_resize(dst, ALLOCATION(dst->fLength + n), status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(dst->fChars + dst->fLength, src->fChars,
|
1999-08-16 21:50:52 +00:00
|
|
|
sizeof(UChar) * n);
|
|
|
|
dst->fLength += src->fLength;
|
|
|
|
dst->fChars[dst->fLength] = 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ustr_ucat(struct UString *dst,
|
|
|
|
UChar c,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(dst->fCapacity < (dst->fLength + 1)) {
|
|
|
|
ustr_resize(dst, ALLOCATION(dst->fLength + 1), status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_memcpy(dst->fChars + dst->fLength, &c,
|
1999-08-16 21:50:52 +00:00
|
|
|
sizeof(UChar) * 1);
|
|
|
|
dst->fLength += 1;
|
|
|
|
dst->fChars[dst->fLength] = 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroys data in the string */
|
|
|
|
static void
|
|
|
|
ustr_resize(struct UString *s,
|
|
|
|
int32_t len,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* +1 for trailing 0x0000 */
|
1999-12-28 23:57:50 +00:00
|
|
|
s->fChars = (UChar*) uprv_realloc(s->fChars, sizeof(UChar) * (len + 1));
|
1999-08-16 21:50:52 +00:00
|
|
|
if(s->fChars == 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
s->fChars = 0;
|
|
|
|
s->fLength = s->fCapacity = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->fCapacity = len;
|
|
|
|
}
|