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