Advertisement
RicardasSim

icu collator lt

Jan 4th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. /*
  2. collator sample:
  3. https://github.com/unicode-org/icu/tree/master/icu4c/source/samples/coll
  4.  
  5. collation examples:
  6. http://userguide.icu-project.org/collation/examples
  7.  
  8. rules:
  9. https://unicode.org/repos/cldr/trunk/common/collation/lt.xml
  10.  
  11. build:
  12. gcc -Wall -Wextra -Wpedantic -c -o main.o main.c
  13. gcc -o test_icu_collator main.o `icu-config --ldflags`
  14.  
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. #include <unicode/utypes.h>
  21. #include <unicode/ucol.h>
  22. #include <unicode/ustring.h>
  23.  
  24. void swap(char** a, char** b)
  25. {
  26.     char* tmp;
  27.    
  28.     tmp = *a;
  29.     *a = *b;
  30.     *b = tmp;
  31.    
  32. }
  33.  
  34. int sCmp(UCollator *coll, char* str2, char* str1)
  35. {
  36.     UChar str1Unsc[128];
  37.     UChar str2Unsc[128];
  38.    
  39.     u_unescape(str1, str1Unsc, 128);
  40.     u_unescape(str2, str2Unsc, 128);
  41.  
  42.     UCollationResult result = ucol_strcoll(coll, str1Unsc, -1, str2Unsc, -1);
  43.    
  44.     if (result == UCOL_LESS) return -1;
  45.     else if (result == UCOL_GREATER) return 1;
  46.    
  47.     return 0;
  48. }
  49.  
  50. int main (int argc, char **argv)
  51. {
  52.  
  53.     UErrorCode status = U_ZERO_ERROR;
  54.    
  55.     UChar rulesUnsc[128];
  56.    
  57.     char strRules[]={"&A<<ą<<<Ą&C<č<<<Č&E<<ę<<<Ę<<ė<<<Ė&I<<į<<<Į<<y<<<Y&S<š<<<Š&U<<ų<<<Ų<<ū<<<Ū&Z<ž<<<Ž"};
  58.    
  59.     char *strArr[] = {"Žuvis","Arbūzas","Eglė","Klaviatūra","žvejys","klaviatūra","arbūzas","fabrikas","Fabrikas","Ąsotis"};
  60.    
  61.     uint32_t i, j;
  62.    
  63.    
  64.     u_unescape(strRules, rulesUnsc, 128);
  65.    
  66.     UCollator *coll = ucol_openRules(rulesUnsc, -1, UCOL_OFF, UCOL_TERTIARY, NULL, &status);
  67.    
  68.     if(!U_SUCCESS(status))
  69.     {
  70.         printf("ERROR: ucol_open failed.\n");
  71.         return -1;
  72.     }
  73.  
  74.     ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status);
  75.  
  76.     uint32_t arrSize = sizeof(strArr)/sizeof(strArr[0]);
  77.    
  78.     for(i=0;i<arrSize;i++) printf("[%s] ",strArr[i]);
  79.     printf("\n");
  80.    
  81.     for(i=arrSize-1; i>=1; i--)
  82.     {
  83.         for(j=0; j<i; j++)
  84.         {
  85.             if(sCmp(coll, strArr[j], strArr[j+1]) == -1)
  86.             {
  87.                
  88.                 swap(&strArr[j], &strArr[j+1]);
  89.                
  90.                 //char* tmp = strArr[j];
  91.                 //strArr[j] = strArr[j+1];
  92.                 //strArr[j+1] = tmp;
  93.                
  94.             }
  95.         }
  96.     }
  97.  
  98.     for(i=0;i<arrSize;i++) printf("[%s] ",strArr[i]);
  99.     printf("\n");
  100.    
  101.     ucol_close(coll);
  102.    
  103.     return 0;
  104. }
  105.  
  106. /*
  107. output:
  108.  
  109. [Žuvis] [Arbūzas] [Eglė] [Klaviatūra] [žvejys] [klaviatūra] [arbūzas] [fabrikas] [Fabrikas] [Ąsotis]
  110. [Arbūzas] [arbūzas] [Ąsotis] [Eglė] [Fabrikas] [fabrikas] [Klaviatūra] [klaviatūra] [Žuvis] [žvejys]
  111.  
  112.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement