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-2003, International Business Machines
     11 *   Corporation and others.  All Rights Reserved.
     12 *
     13 *******************************************************************************
     14 */
     15 
     16 #include <stdio.h>
     17 #include <unicode/brkiter.h>
     18 #include <stdlib.h>
     19 
     20 using namespace icu;
     21 
     22 U_CFUNC int c_main(void);
     23 
     24 void printUnicodeString(const UnicodeString &s) {
     25     char charBuf[1000];
     26     s.extract(0, s.length(), charBuf, sizeof(charBuf)-1, 0);
     27     charBuf[sizeof(charBuf)-1] = 0;
     28     printf("%s", charBuf);
     29 }
     30 
     31 
     32 void printTextRange( BreakIterator& iterator,
     33                     int32_t start, int32_t end )
     34 {
     35     CharacterIterator *strIter = iterator.getText().clone();
     36     UnicodeString  s;
     37     strIter->getText(s);
     38 
     39     printf(" %ld %ld\t", (long)start, (long)end);
     40     printUnicodeString(UnicodeString(s, 0, start));
     41     printf("|");
     42     printUnicodeString(UnicodeString(s, start, end-start));
     43     printf("|");
     44     printUnicodeString(UnicodeString(s, end));
     45     puts("");
     46     delete strIter;
     47 }
     48 
     49 
     50 /* Print each element in order: */
     51 void printEachForward( BreakIterator& boundary)
     52 {
     53     int32_t start = boundary.first();
     54     for (int32_t end = boundary.next();
     55          end != BreakIterator::DONE;
     56          start = end, end = boundary.next())
     57     {
     58         printTextRange( boundary, start, end );
     59     }
     60 }
     61 
     62 /* Print each element in reverse order: */
     63 void printEachBackward( BreakIterator& boundary)
     64 {
     65     int32_t end = boundary.last();
     66     for (int32_t start = boundary.previous();
     67          start != BreakIterator::DONE;
     68          end = start, start = boundary.previous())
     69     {
     70         printTextRange( boundary, start, end );
     71     }
     72 }
     73 
     74 /* Print the first element */
     75 void printFirst(BreakIterator& boundary)
     76 {
     77     int32_t start = boundary.first();
     78     int32_t end = boundary.next();
     79     printTextRange( boundary, start, end );
     80 }
     81 
     82 /* Print the last element */
     83 void printLast(BreakIterator& boundary)
     84 {
     85     int32_t end = boundary.last();
     86     int32_t start = boundary.previous();
     87     printTextRange( boundary, start, end );
     88 }
     89 
     90 /* Print the element at a specified position */
     91 void printAt(BreakIterator &boundary, int32_t pos )
     92 {
     93     int32_t end = boundary.following(pos);
     94     int32_t start = boundary.previous();
     95     printTextRange( boundary, start, end );
     96 }
     97 
     98 /* Creating and using text boundaries */
     99 int main( void )
    100 {
    101     puts("ICU Break Iterator Sample Program\n");
    102     puts("C++ Break Iteration\n");
    103     BreakIterator* boundary;
    104     UnicodeString stringToExamine("Aaa bbb ccc. Ddd eee fff.");
    105     printf("Examining: ");
    106     printUnicodeString(stringToExamine);
    107     puts("");
    108 
    109     //print each sentence in forward and reverse order
    110     UErrorCode status = U_ZERO_ERROR;
    111     boundary = BreakIterator::createSentenceInstance(
    112         Locale::getUS(), status );
    113     if (U_FAILURE(status)) {
    114         printf("failed to create sentence break iterator.  status = %s",
    115             u_errorName(status));
    116         exit(1);
    117     }
    118 
    119     boundary->setText(stringToExamine);
    120     puts("\n Sentence Boundaries... ");
    121     puts("----- forward: -----------");
    122     printEachForward(*boundary);
    123     puts("----- backward: ----------");
    124     printEachBackward(*boundary);
    125     delete boundary;
    126 
    127     //print each word in order
    128     printf("\n Word Boundaries... \n");
    129     boundary = BreakIterator::createWordInstance(
    130         Locale::getUS(), status);
    131     boundary->setText(stringToExamine);
    132     puts("----- forward: -----------");
    133     printEachForward(*boundary);
    134     //print first element
    135     puts("----- first: -------------");
    136     printFirst(*boundary);
    137     //print last element
    138     puts("----- last: --------------");
    139     printLast(*boundary);
    140     //print word at charpos 10
    141     puts("----- at pos 10: ---------");
    142     printAt(*boundary, 10 );
    143 
    144     delete boundary;
    145 
    146     puts("\nEnd C++ Break Iteration");
    147 
    148     // Call the C version
    149     return c_main();
    150 }
    151