12e5b6d6dSopenharmony_ci/* 22e5b6d6dSopenharmony_ci******************************************************************************* 32e5b6d6dSopenharmony_ci* 42e5b6d6dSopenharmony_ci* © 2016 and later: Unicode, Inc. and others. 52e5b6d6dSopenharmony_ci* License & terms of use: http://www.unicode.org/copyright.html 62e5b6d6dSopenharmony_ci* 72e5b6d6dSopenharmony_ci******************************************************************************* 82e5b6d6dSopenharmony_ci******************************************************************************* 92e5b6d6dSopenharmony_ci* 102e5b6d6dSopenharmony_ci* Copyright (C) 2002, International Business Machines 112e5b6d6dSopenharmony_ci* Corporation and others. All Rights Reserved. 122e5b6d6dSopenharmony_ci* 132e5b6d6dSopenharmony_ci******************************************************************************* 142e5b6d6dSopenharmony_ci*/ 152e5b6d6dSopenharmony_ci 162e5b6d6dSopenharmony_ci#include <stdio.h> 172e5b6d6dSopenharmony_ci#include <stdlib.h> 182e5b6d6dSopenharmony_ci#include <unicode/ustring.h> 192e5b6d6dSopenharmony_ci#include <unicode/ubrk.h> 202e5b6d6dSopenharmony_ci 212e5b6d6dSopenharmony_ciU_CFUNC int c_main(void); 222e5b6d6dSopenharmony_ci 232e5b6d6dSopenharmony_civoid printTextRange(UChar* str, int32_t start, int32_t end) 242e5b6d6dSopenharmony_ci{ 252e5b6d6dSopenharmony_ci char charBuf[1000]; 262e5b6d6dSopenharmony_ci UChar savedEndChar; 272e5b6d6dSopenharmony_ci 282e5b6d6dSopenharmony_ci savedEndChar = str[end]; 292e5b6d6dSopenharmony_ci str[end] = 0; 302e5b6d6dSopenharmony_ci u_austrncpy(charBuf, str+start, sizeof(charBuf)-1); 312e5b6d6dSopenharmony_ci charBuf[sizeof(charBuf)-1]=0; 322e5b6d6dSopenharmony_ci printf("string[%2d..%2d] \"%s\"\n", start, end-1, charBuf); 332e5b6d6dSopenharmony_ci str[end] = savedEndChar; 342e5b6d6dSopenharmony_ci} 352e5b6d6dSopenharmony_ci 362e5b6d6dSopenharmony_ci 372e5b6d6dSopenharmony_ci 382e5b6d6dSopenharmony_ci/* Print each element in order: */ 392e5b6d6dSopenharmony_civoid printEachForward( UBreakIterator* boundary, UChar* str) { 402e5b6d6dSopenharmony_ci int32_t end; 412e5b6d6dSopenharmony_ci int32_t start = ubrk_first(boundary); 422e5b6d6dSopenharmony_ci for (end = ubrk_next(boundary); end != UBRK_DONE; start = end, end = 432e5b6d6dSopenharmony_ci ubrk_next(boundary)) { 442e5b6d6dSopenharmony_ci printTextRange(str, start, end ); 452e5b6d6dSopenharmony_ci } 462e5b6d6dSopenharmony_ci} 472e5b6d6dSopenharmony_ci 482e5b6d6dSopenharmony_ci 492e5b6d6dSopenharmony_ci/* Print each element in reverse order: */ 502e5b6d6dSopenharmony_civoid printEachBackward( UBreakIterator* boundary, UChar* str) { 512e5b6d6dSopenharmony_ci int32_t start; 522e5b6d6dSopenharmony_ci int32_t end = ubrk_last(boundary); 532e5b6d6dSopenharmony_ci for (start = ubrk_previous(boundary); start != UBRK_DONE; end = start, 542e5b6d6dSopenharmony_ci start =ubrk_previous(boundary)) { 552e5b6d6dSopenharmony_ci printTextRange( str, start, end ); 562e5b6d6dSopenharmony_ci } 572e5b6d6dSopenharmony_ci} 582e5b6d6dSopenharmony_ci 592e5b6d6dSopenharmony_ci/* Print first element */ 602e5b6d6dSopenharmony_civoid printFirst(UBreakIterator* boundary, UChar* str) { 612e5b6d6dSopenharmony_ci int32_t end; 622e5b6d6dSopenharmony_ci int32_t start = ubrk_first(boundary); 632e5b6d6dSopenharmony_ci end = ubrk_next(boundary); 642e5b6d6dSopenharmony_ci printTextRange( str, start, end ); 652e5b6d6dSopenharmony_ci} 662e5b6d6dSopenharmony_ci 672e5b6d6dSopenharmony_ci/* Print last element */ 682e5b6d6dSopenharmony_civoid printLast(UBreakIterator* boundary, UChar* str) { 692e5b6d6dSopenharmony_ci int32_t start; 702e5b6d6dSopenharmony_ci int32_t end = ubrk_last(boundary); 712e5b6d6dSopenharmony_ci start = ubrk_previous(boundary); 722e5b6d6dSopenharmony_ci printTextRange(str, start, end ); 732e5b6d6dSopenharmony_ci} 742e5b6d6dSopenharmony_ci 752e5b6d6dSopenharmony_ci/* Print the element at a specified position */ 762e5b6d6dSopenharmony_ci 772e5b6d6dSopenharmony_civoid printAt(UBreakIterator* boundary, int32_t pos , UChar* str) { 782e5b6d6dSopenharmony_ci int32_t start; 792e5b6d6dSopenharmony_ci int32_t end = ubrk_following(boundary, pos); 802e5b6d6dSopenharmony_ci start = ubrk_previous(boundary); 812e5b6d6dSopenharmony_ci printTextRange(str, start, end ); 822e5b6d6dSopenharmony_ci} 832e5b6d6dSopenharmony_ci 842e5b6d6dSopenharmony_ci/* Creating and using text boundaries*/ 852e5b6d6dSopenharmony_ci 862e5b6d6dSopenharmony_ciint c_main( void ) { 872e5b6d6dSopenharmony_ci UBreakIterator *boundary; 882e5b6d6dSopenharmony_ci char cStringToExamine[] = "Aaa bbb ccc. Ddd eee fff."; 892e5b6d6dSopenharmony_ci UChar stringToExamine[sizeof(cStringToExamine)+1]; 902e5b6d6dSopenharmony_ci UErrorCode status = U_ZERO_ERROR; 912e5b6d6dSopenharmony_ci 922e5b6d6dSopenharmony_ci printf("\n\n" 932e5b6d6dSopenharmony_ci "C Boundary Analysis\n" 942e5b6d6dSopenharmony_ci "-------------------\n\n"); 952e5b6d6dSopenharmony_ci 962e5b6d6dSopenharmony_ci printf("Examining: %s\n", cStringToExamine); 972e5b6d6dSopenharmony_ci u_uastrcpy(stringToExamine, cStringToExamine); 982e5b6d6dSopenharmony_ci 992e5b6d6dSopenharmony_ci /*print each sentence in forward and reverse order*/ 1002e5b6d6dSopenharmony_ci boundary = ubrk_open(UBRK_SENTENCE, "en_us", stringToExamine, 1012e5b6d6dSopenharmony_ci -1, &status); 1022e5b6d6dSopenharmony_ci if (U_FAILURE(status)) { 1032e5b6d6dSopenharmony_ci printf("ubrk_open error: %s\n", u_errorName(status)); 1042e5b6d6dSopenharmony_ci exit(1); 1052e5b6d6dSopenharmony_ci } 1062e5b6d6dSopenharmony_ci 1072e5b6d6dSopenharmony_ci printf("\n----- Sentence Boundaries, forward: -----------\n"); 1082e5b6d6dSopenharmony_ci printEachForward(boundary, stringToExamine); 1092e5b6d6dSopenharmony_ci printf("\n----- Sentence Boundaries, backward: ----------\n"); 1102e5b6d6dSopenharmony_ci printEachBackward(boundary, stringToExamine); 1112e5b6d6dSopenharmony_ci ubrk_close(boundary); 1122e5b6d6dSopenharmony_ci 1132e5b6d6dSopenharmony_ci /*print each word in order*/ 1142e5b6d6dSopenharmony_ci boundary = ubrk_open(UBRK_WORD, "en_us", stringToExamine, 1152e5b6d6dSopenharmony_ci u_strlen(stringToExamine), &status); 1162e5b6d6dSopenharmony_ci printf("\n----- Word Boundaries, forward: -----------\n"); 1172e5b6d6dSopenharmony_ci printEachForward(boundary, stringToExamine); 1182e5b6d6dSopenharmony_ci printf("\n----- Word Boundaries, backward: ----------\n"); 1192e5b6d6dSopenharmony_ci printEachBackward(boundary, stringToExamine); 1202e5b6d6dSopenharmony_ci /*print first element*/ 1212e5b6d6dSopenharmony_ci printf("\n----- first: -------------\n"); 1222e5b6d6dSopenharmony_ci printFirst(boundary, stringToExamine); 1232e5b6d6dSopenharmony_ci /*print last element*/ 1242e5b6d6dSopenharmony_ci printf("\n----- last: --------------\n"); 1252e5b6d6dSopenharmony_ci printLast(boundary, stringToExamine); 1262e5b6d6dSopenharmony_ci /*print word at charpos 10 */ 1272e5b6d6dSopenharmony_ci printf("\n----- at pos 10: ---------\n"); 1282e5b6d6dSopenharmony_ci printAt(boundary, 10 , stringToExamine); 1292e5b6d6dSopenharmony_ci 1302e5b6d6dSopenharmony_ci ubrk_close(boundary); 1312e5b6d6dSopenharmony_ci 1322e5b6d6dSopenharmony_ci printf("\nEnd of C boundary analysis\n"); 1332e5b6d6dSopenharmony_ci return 0; 1342e5b6d6dSopenharmony_ci} 135