/*
-*******************************************************************************
-*
-* Copyright (C) 2001-2006, International Business Machines
-* Corporation and others. All Rights Reserved.
-*
-*******************************************************************************
-* file name: ucol_cnt.cpp
-* encoding: US-ASCII
-* tab size: 8 (not used)
-* indentation:4
-*
-* created 02/22/2001
-* created by: Vladimir Weinstein
-*
-* This module maintains a contraction table structure in expanded form
-* and provides means to flatten this structure
-*
-*/
+ *******************************************************************************
+ *
+ * Copyright (C) 2001-2008, International Business Machines
+ * Corporation and others. All Rights Reserved.
+ *
+ *******************************************************************************
+ * file name: ucol_cnt.cpp
+ * encoding: US-ASCII
+ * tab size: 8 (not used)
+ * indentation:4
+ *
+ * created 02/22/2001
+ * created by: Vladimir Weinstein
+ *
+ * This module maintains a contraction table structure in expanded form
+ * and provides means to flatten this structure
+ *
+ */
#include "unicode/utypes.h"
static ContractionTable *addATableElement(CntTable *table, uint32_t *key, UErrorCode *status) {
ContractionTable *el = (ContractionTable *)uprv_malloc(sizeof(ContractionTable));
if(el == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
- return NULL;
+ goto outOfMemory;
}
el->CEs = (uint32_t *)uprv_malloc(INIT_EXP_TABLE_SIZE*sizeof(uint32_t));
if(el->CEs == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
- uprv_free(el);
- return NULL;
+ goto outOfMemory;
}
el->codePoints = (UChar *)uprv_malloc(INIT_EXP_TABLE_SIZE*sizeof(UChar));
if(el->codePoints == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(el->CEs);
- uprv_free(el);
- return NULL;
+ goto outOfMemory;
}
el->position = 0;
// do realloc
/* table->elements = (ContractionTable **)realloc(table->elements, table->capacity*2*sizeof(ContractionTable *));*/
if(newElements == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(el->codePoints);
uprv_free(el->CEs);
- uprv_free(el);
- return NULL;
- } else {
- ContractionTable **oldElements = table->elements;
- uprv_memcpy(newElements, oldElements, table->capacity*sizeof(ContractionTable *));
- uprv_memset(newElements+table->capacity, 0, table->capacity*sizeof(ContractionTable *));
- table->capacity *= 2;
- table->elements = newElements;
- uprv_free(oldElements);
+ goto outOfMemory;
}
+ ContractionTable **oldElements = table->elements;
+ uprv_memcpy(newElements, oldElements, table->capacity*sizeof(ContractionTable *));
+ uprv_memset(newElements+table->capacity, 0, table->capacity*sizeof(ContractionTable *));
+ table->capacity *= 2;
+ table->elements = newElements;
+ uprv_free(oldElements);
}
return el;
+
+outOfMemory:
+ *status = U_MEMORY_ALLOCATION_ERROR;
+ if (el) uprv_free(el);
+ return NULL;
}
U_CAPI int32_t U_EXPORT2
static ContractionTable *uprv_cnttab_cloneContraction(ContractionTable *t, UErrorCode *status) {
ContractionTable *r = (ContractionTable *)uprv_malloc(sizeof(ContractionTable));
if(r == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
- return NULL;
+ goto outOfMemory;
}
r->position = t->position;
r->codePoints = (UChar *)uprv_malloc(sizeof(UChar)*t->size);
if(r->codePoints == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
- uprv_free(r);
- return NULL;
+ goto outOfMemory;
}
r->CEs = (uint32_t *)uprv_malloc(sizeof(uint32_t)*t->size);
if(r->CEs == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(r->codePoints);
- uprv_free(r);
- return NULL;
+ goto outOfMemory;
}
uprv_memcpy(r->codePoints, t->codePoints, sizeof(UChar)*t->size);
uprv_memcpy(r->CEs, t->CEs, sizeof(uint32_t)*t->size);
return r;
+outOfMemory:
+ *status = U_MEMORY_ALLOCATION_ERROR;
+ if (r) uprv_free(r);
+ return NULL;
}
U_CAPI CntTable* U_EXPORT2
CntTable *r = (CntTable *)uprv_malloc(sizeof(CntTable));
/* test for NULL */
if (r == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
- return NULL;
+ goto outOfMemory;
}
r->position = t->position;
r->size = t->size;
r->elements = (ContractionTable **)uprv_malloc(t->capacity*sizeof(ContractionTable *));
/* test for NULL */
if (r->elements == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
- uprv_free(r);
- return NULL;
+ goto outOfMemory;
}
//uprv_memcpy(r->elements, t->elements, t->capacity*sizeof(ContractionTable *));
r->CEs = (uint32_t *)uprv_malloc(t->position*sizeof(uint32_t));
/* test for NULL */
if (r->CEs == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(r->elements);
- uprv_free(r);
- return NULL;
+ goto outOfMemory;
}
uprv_memcpy(r->CEs, t->CEs, t->position*sizeof(uint32_t));
} else {
r->codePoints = (UChar *)uprv_malloc(t->position*sizeof(UChar));
/* test for NULL */
if (r->codePoints == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(r->CEs);
uprv_free(r->elements);
- uprv_free(r);
- return NULL;
+ goto outOfMemory;
}
uprv_memcpy(r->codePoints, t->codePoints, t->position*sizeof(UChar));
} else {
r->offsets = (int32_t *)uprv_malloc(t->size*sizeof(int32_t));
/* test for NULL */
if (r->offsets == NULL) {
- *status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(r->codePoints);
uprv_free(r->CEs);
uprv_free(r->elements);
- uprv_free(r);
- return NULL;
+ goto outOfMemory;
}
uprv_memcpy(r->offsets, t->offsets, t->size*sizeof(int32_t));
} else {
}
return r;
+
+outOfMemory:
+ *status = U_MEMORY_ALLOCATION_ERROR;
+ if (r) uprv_free(r);
+ return NULL;
}
U_CAPI void U_EXPORT2
if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
tbl = addATableElement(table, &element, status);
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
}
uprv_growTable(tbl, status);
if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
tbl = addATableElement(table, &element, status);
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
}
uprv_growTable(tbl, status);
if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
tbl = addATableElement(table, &element, status);
+ if (U_FAILURE(*status)) {
+ return 0;
+ }
+
}
if(offset >= tbl->size) {
element &= 0xFFFFFF;
ContractionTable *tbl = NULL;
- if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
- return NULL;
- } else {
- return tbl;
+ if(element != 0xFFFFFF) {
+ tbl = table->elements[element]; /* This could also return NULL */
}
+ return tbl;
}
static int32_t _cnttab_findCP(ContractionTable *tbl, UChar codePoint) {