#include <unicode/ustring.h> /* some more string fcns*/
#include <unicode/uchar.h> /* char names */
-
-
#include <unicode/ucol.h>
-
-int icu_check_status (UErrorCode status)
+int icu_check_status(UErrorCode status)
{
if (U_FAILURE(status))
{
return 0;
}
return 1;
-
}
-
-
struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
{
struct icu_buf_utf16 * buf16
buf16->utf16_len = 0;
buf16->utf16_cap = 0;
- if (capacity > 0){
+ if (capacity > 0)
+ {
buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
buf16->utf16[0] = (UChar) 0;
buf16->utf16_cap = capacity;
struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16)
{
- if (buf16){
+ if (buf16)
+ {
if (buf16->utf16)
buf16->utf16[0] = (UChar) 0;
buf16->utf16_len = 0;
if (!buf16)
return 0;
- if (capacity > 0){
+ if (capacity > 0)
+ {
if (0 == buf16->utf16)
buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
else
icu_buf_utf16_clear(buf16);
buf16->utf16_cap = capacity;
}
- else {
+ else
+ {
xfree(buf16->utf16);
buf16->utf16 = 0;
buf16->utf16_len = 0;
struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
struct icu_buf_utf16 * src16)
{
- if(!dest16 || !src16
- || dest16 == src16)
+ if (!dest16 || !src16 || dest16 == src16)
return 0;
if (dest16->utf16_cap < src16->utf16_len)
return dest16;
}
-
void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
{
if (buf16)
xfree(buf16);
}
-
-
struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
{
struct icu_buf_utf8 * buf8
buf8->utf8_len = 0;
buf8->utf8_cap = 0;
- if (capacity > 0){
+ if (capacity > 0)
+ {
buf8->utf8 = (uint8_t *) xmalloc(sizeof(uint8_t) * capacity);
buf8->utf8[0] = (uint8_t) 0;
buf8->utf8_cap = capacity;
return buf8;
}
-
struct icu_buf_utf8 * icu_buf_utf8_clear(struct icu_buf_utf8 * buf8)
{
- if (buf8){
+ if (buf8)
+ {
if (buf8->utf8)
buf8->utf8[0] = (uint8_t) 0;
buf8->utf8_len = 0;
return buf8;
}
-
struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
size_t capacity)
{
return buf8;
}
-
const char *icu_buf_utf8_to_cstr(struct icu_buf_utf8 *src8)
{
if (!src8 || src8->utf8_len == 0)
return (const char *) src8->utf8;
}
-
void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
{
if (buf8)
xfree(buf8);
}
-
UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
const char * src8cstr,
UErrorCode * status)
return *status;
}
-
-
-
UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
struct icu_buf_utf16 * src16,
UErrorCode * status)
u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
&utf8_len,
src16->utf16, src16->utf16_len, status);
-
}
if (U_SUCCESS(*status)
= (struct icu_casemap *) xmalloc(sizeof(struct icu_casemap));
casemap->action = action;
- switch(casemap->action) {
+ switch(casemap->action)
+ {
case 'l':
case 'L':
case 'u':
icu_casemap_destroy(casemap);
return 0;
}
-
return casemap;
}
xfree(casemap);
}
-
int icu_casemap_casemap(struct icu_casemap * casemap,
struct icu_buf_utf16 * dest16,
struct icu_buf_utf16 * src16,
casemap->action, status);
}
-
int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
struct icu_buf_utf16 * src16,
const char *locale, char action,
{
int32_t dest16_len = 0;
-
- if (!src16->utf16_len){ /* guarding for empty source string */
+ if (!src16->utf16_len)
+ { /* guarding for empty source string */
if (dest16->utf16)
dest16->utf16[0] = (UChar) 0;
dest16->utf16_len = 0;
return U_ZERO_ERROR;
}
-
- switch(action) {
+ switch(action)
+ {
case 'l':
case 'L':
dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
/* check for buffer overflow, resize and retry */
if (*status == U_BUFFER_OVERFLOW_ERROR
&& dest16 != src16 /* do not resize if in-place conversion */
- ){
+ )
+ {
icu_buf_utf16_resize(dest16, dest16_len * 2);
*status = U_ZERO_ERROR;
-
switch(action) {
case 'l':
case 'L':
if (U_SUCCESS(*status)
&& dest16_len <= dest16->utf16_cap)
dest16->utf16_len = dest16_len;
- else {
+ else
+ {
if (dest16->utf16)
dest16->utf16[0] = (UChar) 0;
dest16->utf16_len = 0;
return *status;
}
-
-
void icu_sortkey8_from_utf16(UCollator *coll,
struct icu_buf_utf8 * dest8,
struct icu_buf_utf16 * src16,
UErrorCode * status)
{
-
int32_t sortkey_len = 0;
sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
dest8->utf8, dest8->utf8_cap);
/* check for buffer overflow, resize and retry */
- if (sortkey_len > dest8->utf8_cap) {
+ if (sortkey_len > dest8->utf8_cap)
+ {
icu_buf_utf8_resize(dest8, sortkey_len * 2);
sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
dest8->utf8, dest8->utf8_cap);
tokenizer->token_start = 0;
tokenizer->token_end = 0;
-
- switch(tokenizer->action) {
+ switch(tokenizer->action)
+ {
case 'l':
case 'L':
tokenizer->bi = ubrk_open(UBRK_LINE, locale, 0, 0, status);
if (!tokenizer || !tokenizer->bi || !src16)
return 0;
-
tokenizer->buf16 = src16;
tokenizer->token_count = 0;
tokenizer->token_id = 0;
tokenizer->token_end = 0;
ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
-
-
+
if (U_FAILURE(*status))
return 0;
int32_t tkn_start = 0;
int32_t tkn_end = 0;
int32_t tkn_len = 0;
-
if (!tokenizer || !tokenizer->bi
|| !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
return 0;
-
/*
never change tokenizer->buf16 and keep always invariant
0 <= tokenizer->token_start
tokenizer->token_id = 0;
}
tokenizer->token_start = tkn_start;
- tokenizer->token_end = tkn_end;
-
+ tokenizer->token_end = tkn_end;
/* copying into token buffer if it exists */
if (tkn16){
return tkn_len;
}
-
int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
{
return tokenizer->token_id;
return tokenizer->token_count;
}
-
-
struct icu_transform * icu_transform_create(const char *id, char action,
const char *rules,
UErrorCode *status)
return 0;
}
-
-void icu_transform_destroy(struct icu_transform * transform){
- if (transform) {
+void icu_transform_destroy(struct icu_transform * transform)
+{
+ if (transform)
+ {
if (transform->trans)
utrans_close(transform->trans);
xfree(transform);
}
}
-
-
int icu_transform_trans(struct icu_transform * transform,
struct icu_buf_utf16 * dest16,
struct icu_buf_utf16 * src16,
UErrorCode *status)
{
if (!transform || !transform->trans
- || !src16
- || !dest16)
+ || !src16 || !dest16)
return 0;
- if (!src16->utf16_len){ /* guarding for empty source string */
+ if (!src16->utf16_len)
+ { /* guarding for empty source string */
icu_buf_utf16_clear(dest16);
return 0;
}
if (!icu_buf_utf16_copy(dest16, src16))
return 0;
-
utrans_transUChars (transform->trans,
dest16->utf16, &(dest16->utf16_len),
dest16->utf16_cap,
return dest16->utf16_len;
}
-
-
-
struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
enum icu_chain_step_type type,
const uint8_t * rule,
step->buf16 = buf16;
/* create auxilary objects */
- switch(step->type) {
+ switch(step->type)
+ {
case ICU_chain_step_type_display:
break;
case ICU_chain_step_type_casemap:
default:
break;
}
-
return step;
}
-void icu_chain_step_destroy(struct icu_chain_step * step){
-
+void icu_chain_step_destroy(struct icu_chain_step * step)
+{
if (!step)
return;
icu_chain_step_destroy(step->previous);
- switch(step->type) {
+ switch(step->type)
+ {
case ICU_chain_step_type_display:
break;
case ICU_chain_step_type_casemap:
}
-
struct icu_chain * icu_chain_create(const char *locale, int sort,
UErrorCode * status)
{
}
}
-
-
struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node,
int sort,
UErrorCode * status)
return chain;
}
-
-
struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
enum icu_chain_step_type type,
const uint8_t * rule,
else
return 0;
-
/* create utf16 destination buffers as needed, or */
switch(type)
{
}
-int icu_chain_assign_cstr(struct icu_chain * chain,
- const char * src8cstr,
+int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr,
UErrorCode *status)
{
struct icu_chain_step * stp = 0;
return 1;
}
-
-
-int icu_chain_next_token(struct icu_chain * chain,
- UErrorCode *status)
+int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
{
int got_token = 0;
icu_sortkey8_from_utf16(chain->coll,
chain->sort8, chain->steps->buf16,
status);
-
return chain->token_count;
}
}
return chain->token_count;
}
-
const char * icu_chain_token_display(struct icu_chain * chain)
{
if (chain->display8)
#include <string.h>
#include <stdlib.h>
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
-
#define MAX_KEY_SIZE 256
struct icu_termmap
{
};
-
int icu_termmap_cmp(const void *vp1, const void *vp2)
{
struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
cmp = strcmp((const char *)itmp1->sort_key,
(const char *)itmp2->sort_key);
return cmp;
-};
-
-
+}
int test_icu_casemap(const char * locale, char action,
/* converting to UTF8 */
icu_utf16_to_utf8(dest8, dest16, &status);
-
-
/* determine success */
if (dest8->utf8
&& (dest8->utf8_len == strlen(chk8cstr))
icu_buf_utf8_destroy(dest8);
icu_buf_utf16_destroy(src16);
icu_buf_utf16_destroy(dest16);
-
-
+
return success;
}
-
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
void test_icu_I18N_casemap(int argc, char **argv)
{
-
/* Locale 'en' */
/* successful tests */
"A ReD fOx hunTS sQUirriLs",
"A Red Fox Hunts Squirrils"));
-
/* Locale 'da' */
/* success expected */
}
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
int test_icu_sortmap(const char * locale, int src_list_len,
const char ** src_list, const char ** chk_list)
{
return 0;
/* assigning display terms and sort keys using buf 8 and buf16 */
- for( i = 0; i < src_list_len; i++)
- {
+ for (i = 0; i < src_list_len; i++)
+ {
- list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
+ list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
- /* copy display term */
- strcpy(list[i]->disp_term, src_list[i]);
+ /* copy display term */
+ strcpy(list[i]->disp_term, src_list[i]);
- /* transforming to UTF16 */
- icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
- icu_check_status(status);
+ /* transforming to UTF16 */
+ icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
+ icu_check_status(status);
- /* computing sortkeys */
- icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
- icu_check_status(status);
+ /* computing sortkeys */
+ icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
+ icu_check_status(status);
- /* assigning sortkeys */
- memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
- }
-
+ /* assigning sortkeys */
+ memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
+ }
/* do the sorting */
- qsort(list, src_list_len,
- sizeof(struct icu_termmap *), icu_termmap_cmp);
+ qsort(list, src_list_len, sizeof(struct icu_termmap *), icu_termmap_cmp);
/* checking correct sorting */
- for (i = 0; i < src_list_len; i++){
+ for (i = 0; i < src_list_len; i++)
+ {
if (0 != strcmp(list[i]->disp_term, chk_list[i])){
success = 0;
}
}
- if(!success){
+ if (!success)
+ {
printf("\nERROR\n");
printf("Input str: '%s' : ", locale);
for (i = 0; i < src_list_len; i++) {
printf("\n");
}
-
-
- for( i = 0; i < src_list_len; i++)
+ for (i = 0; i < src_list_len; i++)
free(list[i]);
-
-
+
ucol_close(coll);
icu_buf_utf8_destroy(buf8);
return success;
}
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
void test_icu_I18N_sortmap(int argc, char **argv)
{
-
/* successful tests */
size_t en_1_len = 6;
const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
}
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
-
-
-
int test_icu_normalizer(const char * rules8cstr,
- const char * src8cstr,
- const char * chk8cstr)
+ const char * src8cstr,
+ const char * chk8cstr)
{
int success = 0;
if(!strcmp((const char *) dest8->utf8,
(const char *) chk8cstr))
success = 1;
- else {
+ else
+ {
success = 0;
printf("Normalization\n");
printf("Rules: '%s'\n", rules8cstr);
printf("Normalized: '%s'\n", dest8->utf8);
printf("Expected: '%s'\n", chk8cstr);
}
-
icu_transform_destroy(transform);
icu_buf_utf16_destroy(src16);
icu_buf_utf8_destroy(dest8);
return success;
-};
-
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
+}
void test_icu_I18N_normalizer(int argc, char **argv)
{
-
YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
"Don't shoot!",
"Dont shoot"));
" word4you? ",
"word4you"));
-
YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
"à côté de l'alcôve ovoïde",
"a cote de l'alcove ovoide"));
-
}
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
int test_icu_tokenizer(const char * locale, char action,
- const char * src8cstr, int count)
+ const char * src8cstr, int count)
{
int success = 1;
return success;
}
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
void test_icu_I18N_tokenizer(int argc, char **argv)
{
-
-
const char * en_str
= "O Romeo, Romeo! wherefore art thou Romeo?";
YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
-
-
{
const char * da_str
= "Blåbærtærte. Denne kage stammer fra Finland. "
YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
}
-
}
-
void test_icu_I18N_chain(int argc, char **argv)
{
const char * en_str
UErrorCode status = U_ZERO_ERROR;
struct icu_chain * chain = 0;
-
const char * xml_str = "<icu locale=\"en\">"
"<transform rule=\"[:Control:] Any-Remove\"/>"
"<tokenize rule=\"l\"/>"
YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
- while (icu_chain_next_token(chain, &status)){
+ while (icu_chain_next_token(chain, &status))
+ {
;
/* printf("%d '%s' '%s'\n",
- icu_chain_token_number(chain),
- icu_chain_token_norm(chain),
- icu_chain_token_display(chain)); */
+ icu_chain_token_number(chain),
+ icu_chain_token_norm(chain),
+ icu_chain_token_display(chain)); */
}
YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
- while (icu_chain_next_token(chain, &status)){
+ while (icu_chain_next_token(chain, &status))
+ {
;
/* printf("%d '%s' '%s'\n",
- icu_chain_token_number(chain),
- icu_chain_token_norm(chain),
- icu_chain_token_display(chain)); */
+ icu_chain_token_number(chain),
+ icu_chain_token_norm(chain),
+ icu_chain_token_display(chain)); */
}
chain, "O Romeo, Romeo! wherefore art thou\t Romeo?",
&status));
- while (icu_chain_next_token(chain, &status)){
+ while (icu_chain_next_token(chain, &status))
+ {
;
/* printf("%d '%s' '%s'\n",
- icu_chain_token_number(chain),
- icu_chain_token_norm(chain),
- icu_chain_token_display(chain)); */
-
+ icu_chain_token_number(chain),
+ icu_chain_token_norm(chain),
+ icu_chain_token_display(chain)); */
}
YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
- while (icu_chain_next_token(chain, &status)){
- ;
- /* printf("%d '%s' '%s'\n",
- icu_chain_token_number(chain),
- icu_chain_token_norm(chain),
- icu_chain_token_display(chain)); */
+ while (icu_chain_next_token(chain, &status))
+ {
+ ;
+ /* printf("%d '%s' '%s'\n",
+ icu_chain_token_number(chain),
+ icu_chain_token_norm(chain),
+ icu_chain_token_display(chain)); */
}
/* we expect 'what' 'is' 'this', i.e. 3 tokens */
}
-
void test_chain_empty_token(void)
{
UErrorCode status = U_ZERO_ERROR;
chain, "a string with 15 tokenss and 8 displays",
&status));
- while (icu_chain_next_token(chain, &status)){
+ while (icu_chain_next_token(chain, &status))
+ {
;
/* printf("%d '%s' '%s'\n",
- icu_chain_token_number(chain),
- icu_chain_token_norm(chain),
- icu_chain_token_display(chain)); */
+ icu_chain_token_number(chain),
+ icu_chain_token_norm(chain),
+ icu_chain_token_display(chain)); */
}
YAZ_CHECK_EQ(icu_chain_token_number(chain), 15);
chain, src8,
&status));
- while (icu_chain_next_token(chain, &status)){
+ while (icu_chain_next_token(chain, &status))
+ {
;
/* printf("%d '%s' '%s'\n",
- icu_chain_token_number(chain),
- icu_chain_token_norm(chain),
- icu_chain_token_display(chain)); */
+ icu_chain_token_number(chain),
+ icu_chain_token_norm(chain),
+ icu_chain_token_display(chain)); */
}
YAZ_CHECK_EQ(icu_chain_token_number(chain), 1);
dest8 = (char *) icu_chain_token_norm(chain);
YAZ_CHECK_EQ(strcmp(src8, dest8), 0);
-
icu_chain_destroy(chain);
}
#endif /* YAZ_HAVE_ICU */
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
int main(int argc, char **argv)
{
-
YAZ_CHECK_INIT(argc, argv);
YAZ_CHECK_LOG();
YAZ_CHECK_TERM;
}
-
-/* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
-
-
-
/*
* Local variables:
* c-basic-offset: 4