Home | History | Annotate | Download | only in break
      1 /*
      2 *******************************************************************************
      3 *
      4 *    2016 and later: Unicode, Inc. and others.
      5 *   License & terms of use: http://www.unicode.org/copyright.html#License
      6 *
      7 *******************************************************************************
      8 *******************************************************************************
      9 *
     10 *   Copyright (C) 2002, International Business Machines
     11 *   Corporation and others.  All Rights Reserved.
     12 *
     13 *******************************************************************************
     14 */
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <unicode/ustring.h>
     19 #include <unicode/ubrk.h>
     20 
     21 U_CFUNC int c_main(void);
     22 
     23 void printTextRange(UChar* str, int32_t start, int32_t end)
     24 {
     25   char    charBuf[1000];
     26   UChar   savedEndChar;
     27 
     28   savedEndChar = str[end];
     29   str[end] = 0;
     30   u_austrncpy(charBuf, str+start, sizeof(charBuf)-1);
     31   charBuf[sizeof(charBuf)-1]=0;
     32   printf("string[%2d..%2d] \"%s\"\n", start, end-1, charBuf);
     33   str[end] = savedEndChar;
     34 }
     35 
     36 
     37 
     38 /* Print each element in order: */
     39 void printEachForward( UBreakIterator* boundary, UChar* str) {
     40   int32_t end;
     41   int32_t start = ubrk_first(boundary);
     42   for (end = ubrk_next(boundary); end != UBRK_DONE; start = end, end =
     43 	 ubrk_next(boundary)) {
     44     printTextRange(str, start, end );
     45   }
     46 }
     47 
     48 
     49 /* Print each element in reverse order: */
     50 void printEachBackward( UBreakIterator* boundary, UChar* str) {
     51   int32_t start;
     52   int32_t end = ubrk_last(boundary);
     53   for (start = ubrk_previous(boundary); start != UBRK_DONE;  end = start,
     54 	 start =ubrk_previous(boundary)) {
     55     printTextRange( str, start, end );
     56   }
     57 }
     58 
     59 /* Print first element */
     60 void printFirst(UBreakIterator* boundary, UChar* str) {
     61   int32_t end;
     62   int32_t start = ubrk_first(boundary);
     63   end = ubrk_next(boundary);
     64   printTextRange( str, start, end );
     65 }
     66 
     67 /* Print last element */
     68 void printLast(UBreakIterator* boundary, UChar* str) {
     69   int32_t start;
     70   int32_t end = ubrk_last(boundary);
     71   start = ubrk_previous(boundary);
     72   printTextRange(str, start, end );
     73 }
     74 
     75 /* Print the element at a specified position */
     76 
     77 void printAt(UBreakIterator* boundary, int32_t pos , UChar* str) {
     78   int32_t start;
     79   int32_t end = ubrk_following(boundary, pos);
     80   start = ubrk_previous(boundary);
     81   printTextRange(str, start, end );
     82 }
     83 
     84 /* Creating and using text boundaries*/
     85 
     86 int c_main( void ) {
     87   UBreakIterator *boundary;
     88   char           cStringToExamine[] = "Aaa bbb ccc. Ddd eee fff.";
     89   UChar          stringToExamine[sizeof(cStringToExamine)+1];
     90   UErrorCode     status = U_ZERO_ERROR;
     91 
     92   printf("\n\n"
     93 	 "C Boundary Analysis\n"
     94 	 "-------------------\n\n");
     95 
     96   printf("Examining: %s\n", cStringToExamine);
     97   u_uastrcpy(stringToExamine, cStringToExamine);
     98 
     99   /*print each sentence in forward and reverse order*/
    100   boundary = ubrk_open(UBRK_SENTENCE, "en_us", stringToExamine,
    101 		       -1, &status);
    102   if (U_FAILURE(status)) {
    103     printf("ubrk_open error: %s\n", u_errorName(status));
    104     exit(1);
    105   }
    106 
    107   printf("\n----- Sentence Boundaries, forward: -----------\n");
    108   printEachForward(boundary, stringToExamine);
    109   printf("\n----- Sentence Boundaries, backward: ----------\n");
    110   printEachBackward(boundary, stringToExamine);
    111   ubrk_close(boundary);
    112 
    113   /*print each word in order*/
    114   boundary = ubrk_open(UBRK_WORD, "en_us", stringToExamine,
    115 		       u_strlen(stringToExamine), &status);
    116   printf("\n----- Word Boundaries, forward: -----------\n");
    117   printEachForward(boundary, stringToExamine);
    118   printf("\n----- Word Boundaries, backward: ----------\n");
    119   printEachBackward(boundary, stringToExamine);
    120   /*print first element*/
    121   printf("\n----- first: -------------\n");
    122   printFirst(boundary, stringToExamine);
    123   /*print last element*/
    124   printf("\n----- last: --------------\n");
    125   printLast(boundary, stringToExamine);
    126   /*print word at charpos 10 */
    127   printf("\n----- at pos 10: ---------\n");
    128   printAt(boundary, 10 , stringToExamine);
    129 
    130   ubrk_close(boundary);
    131 
    132   printf("\nEnd of C boundary analysis\n");
    133   return 0;
    134 }
    135