]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/break/ubreak.c
2 *******************************************************************************
4 * Copyright (C) 2002, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
12 #include <unicode/ustring.h>
13 #include <unicode/ubrk.h>
15 U_CFUNC
int c_main(void);
17 void printTextRange(UChar
* str
, int32_t start
, int32_t end
)
22 savedEndChar
= str
[end
];
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
;
32 /* Print each element in order: */
33 void printEachForward( UBreakIterator
* boundary
, UChar
* str
) {
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
);
43 /* Print each element in reverse order: */
44 void printEachBackward( UBreakIterator
* boundary
, UChar
* str
) {
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
);
53 /* Print first element */
54 void printFirst(UBreakIterator
* boundary
, UChar
* str
) {
56 int32_t start
= ubrk_first(boundary
);
57 end
= ubrk_next(boundary
);
58 printTextRange( str
, start
, end
);
61 /* Print last element */
62 void printLast(UBreakIterator
* boundary
, UChar
* str
) {
64 int32_t end
= ubrk_last(boundary
);
65 start
= ubrk_previous(boundary
);
66 printTextRange(str
, start
, end
);
69 /* Print the element at a specified position */
71 void printAt(UBreakIterator
* boundary
, int32_t pos
, UChar
* str
) {
73 int32_t end
= ubrk_following(boundary
, pos
);
74 start
= ubrk_previous(boundary
);
75 printTextRange(str
, start
, end
);
78 /* Creating and using text boundaries*/
81 UBreakIterator
*boundary
;
82 char cStringToExamine
[] = "Aaa bbb ccc. Ddd eee fff.";
83 UChar stringToExamine
[sizeof(cStringToExamine
)+1];
84 UErrorCode status
= U_ZERO_ERROR
;
87 "C Boundary Analysis\n"
88 "-------------------\n\n");
90 printf("Examining: %s\n", cStringToExamine
);
91 u_uastrcpy(stringToExamine
, cStringToExamine
);
93 /*print each sentence in forward and reverse order*/
94 boundary
= ubrk_open(UBRK_SENTENCE
, "en_us", stringToExamine
,
96 if (U_FAILURE(status
)) {
97 printf("ubrk_open error: %s\n", u_errorName(status
));
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
);
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
);
124 ubrk_close(boundary
);
126 printf("\nEnd of C boundary analysis\n");