Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- collator sample:
- https://github.com/unicode-org/icu/tree/master/icu4c/source/samples/coll
- collation examples:
- http://userguide.icu-project.org/collation/examples
- rules:
- https://unicode.org/repos/cldr/trunk/common/collation/lt.xml
- build:
- gcc -Wall -Wextra -Wpedantic -c -o main.o main.c
- gcc -o test_icu_collator main.o `icu-config --ldflags`
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unicode/utypes.h>
- #include <unicode/ucol.h>
- #include <unicode/ustring.h>
- void swap(char** a, char** b)
- {
- char* tmp;
- tmp = *a;
- *a = *b;
- *b = tmp;
- }
- int sCmp(UCollator *coll, char* str2, char* str1)
- {
- UChar str1Unsc[128];
- UChar str2Unsc[128];
- u_unescape(str1, str1Unsc, 128);
- u_unescape(str2, str2Unsc, 128);
- UCollationResult result = ucol_strcoll(coll, str1Unsc, -1, str2Unsc, -1);
- if (result == UCOL_LESS) return -1;
- else if (result == UCOL_GREATER) return 1;
- return 0;
- }
- int main (int argc, char **argv)
- {
- UErrorCode status = U_ZERO_ERROR;
- UChar rulesUnsc[128];
- char strRules[]={"&A<<ą<<<Ą&C<č<<<Č&E<<ę<<<Ę<<ė<<<Ė&I<<į<<<Į<<y<<<Y&S<š<<<Š&U<<ų<<<Ų<<ū<<<Ū&Z<ž<<<Ž"};
- char *strArr[] = {"Žuvis","Arbūzas","Eglė","Klaviatūra","žvejys","klaviatūra","arbūzas","fabrikas","Fabrikas","Ąsotis"};
- uint32_t i, j;
- u_unescape(strRules, rulesUnsc, 128);
- UCollator *coll = ucol_openRules(rulesUnsc, -1, UCOL_OFF, UCOL_TERTIARY, NULL, &status);
- if(!U_SUCCESS(status))
- {
- printf("ERROR: ucol_open failed.\n");
- return -1;
- }
- ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status);
- uint32_t arrSize = sizeof(strArr)/sizeof(strArr[0]);
- for(i=0;i<arrSize;i++) printf("[%s] ",strArr[i]);
- printf("\n");
- for(i=arrSize-1; i>=1; i--)
- {
- for(j=0; j<i; j++)
- {
- if(sCmp(coll, strArr[j], strArr[j+1]) == -1)
- {
- swap(&strArr[j], &strArr[j+1]);
- //char* tmp = strArr[j];
- //strArr[j] = strArr[j+1];
- //strArr[j+1] = tmp;
- }
- }
- }
- for(i=0;i<arrSize;i++) printf("[%s] ",strArr[i]);
- printf("\n");
- ucol_close(coll);
- return 0;
- }
- /*
- output:
- [Žuvis] [Arbūzas] [Eglė] [Klaviatūra] [žvejys] [klaviatūra] [arbūzas] [fabrikas] [Fabrikas] [Ąsotis]
- [Arbūzas] [arbūzas] [Ąsotis] [Eglė] [Fabrikas] [fabrikas] [Klaviatūra] [klaviatūra] [Žuvis] [žvejys]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement