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