]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/break/break.cpp
   2 ******************************************************************************* 
   4 *   © 2016 and later: Unicode, Inc. and others. 
   5 *   License & terms of use: http://www.unicode.org/copyright.html#License 
   7 ******************************************************************************* 
   8 ******************************************************************************* 
  10 *   Copyright (C) 2002-2003, International Business Machines 
  11 *   Corporation and others.  All Rights Reserved. 
  13 ******************************************************************************* 
  17 #include <unicode/brkiter.h> 
  22 U_CFUNC 
int c_main(void); 
  24 void printUnicodeString(const UnicodeString 
&s
) { 
  26     s
.extract(0, s
.length(), charBuf
, sizeof(charBuf
)-1, 0);    
  27     charBuf
[sizeof(charBuf
)-1] = 0;           
  28     printf("%s", charBuf
); 
  32 void printTextRange( BreakIterator
& iterator
,  
  33                     int32_t start
, int32_t end 
) 
  35     CharacterIterator 
*strIter 
= iterator
.getText().clone(); 
  39     printf(" %ld %ld\t", (long)start
, (long)end
); 
  40     printUnicodeString(UnicodeString(s
, 0, start
)); 
  42     printUnicodeString(UnicodeString(s
, start
, end
-start
)); 
  44     printUnicodeString(UnicodeString(s
, end
)); 
  50 /* Print each element in order: */ 
  51 void printEachForward( BreakIterator
& boundary
) 
  53     int32_t start 
= boundary
.first(); 
  54     for (int32_t end 
= boundary
.next(); 
  55          end 
!= BreakIterator::DONE
; 
  56          start 
= end
, end 
= boundary
.next()) 
  58         printTextRange( boundary
, start
, end 
); 
  62 /* Print each element in reverse order: */ 
  63 void printEachBackward( BreakIterator
& boundary
) 
  65     int32_t end 
= boundary
.last(); 
  66     for (int32_t start 
= boundary
.previous(); 
  67          start 
!= BreakIterator::DONE
; 
  68          end 
= start
, start 
= boundary
.previous()) 
  70         printTextRange( boundary
, start
, end 
); 
  74 /* Print the first element */ 
  75 void printFirst(BreakIterator
& boundary
) 
  77     int32_t start 
= boundary
.first(); 
  78     int32_t end 
= boundary
.next(); 
  79     printTextRange( boundary
, start
, end 
); 
  82 /* Print the last element */ 
  83 void printLast(BreakIterator
& boundary
) 
  85     int32_t end 
= boundary
.last(); 
  86     int32_t start 
= boundary
.previous(); 
  87     printTextRange( boundary
, start
, end 
); 
  90 /* Print the element at a specified position */ 
  91 void printAt(BreakIterator 
&boundary
, int32_t pos 
) 
  93     int32_t end 
= boundary
.following(pos
); 
  94     int32_t start 
= boundary
.previous(); 
  95     printTextRange( boundary
, start
, end 
); 
  98 /* Creating and using text boundaries */ 
 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
); 
 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
)); 
 119     boundary
->setText(stringToExamine
); 
 120     puts("\n Sentence Boundaries... "); 
 121     puts("----- forward: -----------"); 
 122     printEachForward(*boundary
); 
 123     puts("----- backward: ----------"); 
 124     printEachBackward(*boundary
); 
 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
); 
 138     puts("----- last: --------------"); 
 139     printLast(*boundary
); 
 140     //print word at charpos 10 
 141     puts("----- at pos 10: ---------"); 
 142     printAt(*boundary
, 10 ); 
 146     puts("\nEnd C++ Break Iteration"); 
 148     // Call the C version