]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/break/ubreak.c
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, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
18 #include <unicode/ustring.h>
19 #include <unicode/ubrk.h>
21 U_CFUNC
int c_main(void);
23 void printTextRange(UChar
* str
, int32_t start
, int32_t end
)
28 savedEndChar
= str
[end
];
30 u_austrncpy(charBuf
, str
+start
, sizeof(charBuf
)-1);
31 charBuf
[sizeof(charBuf
)-1]=0;
32 printf("string[%2d..%2d] \"%s\"\n", start
, end
-1, charBuf
);
33 str
[end
] = savedEndChar
;
38 /* Print each element in order: */
39 void printEachForward( UBreakIterator
* boundary
, UChar
* str
) {
41 int32_t start
= ubrk_first(boundary
);
42 for (end
= ubrk_next(boundary
); end
!= UBRK_DONE
; start
= end
, end
=
43 ubrk_next(boundary
)) {
44 printTextRange(str
, start
, end
);
49 /* Print each element in reverse order: */
50 void printEachBackward( UBreakIterator
* boundary
, UChar
* str
) {
52 int32_t end
= ubrk_last(boundary
);
53 for (start
= ubrk_previous(boundary
); start
!= UBRK_DONE
; end
= start
,
54 start
=ubrk_previous(boundary
)) {
55 printTextRange( str
, start
, end
);
59 /* Print first element */
60 void printFirst(UBreakIterator
* boundary
, UChar
* str
) {
62 int32_t start
= ubrk_first(boundary
);
63 end
= ubrk_next(boundary
);
64 printTextRange( str
, start
, end
);
67 /* Print last element */
68 void printLast(UBreakIterator
* boundary
, UChar
* str
) {
70 int32_t end
= ubrk_last(boundary
);
71 start
= ubrk_previous(boundary
);
72 printTextRange(str
, start
, end
);
75 /* Print the element at a specified position */
77 void printAt(UBreakIterator
* boundary
, int32_t pos
, UChar
* str
) {
79 int32_t end
= ubrk_following(boundary
, pos
);
80 start
= ubrk_previous(boundary
);
81 printTextRange(str
, start
, end
);
84 /* Creating and using text boundaries*/
87 UBreakIterator
*boundary
;
88 char cStringToExamine
[] = "Aaa bbb ccc. Ddd eee fff.";
89 UChar stringToExamine
[sizeof(cStringToExamine
)+1];
90 UErrorCode status
= U_ZERO_ERROR
;
93 "C Boundary Analysis\n"
94 "-------------------\n\n");
96 printf("Examining: %s\n", cStringToExamine
);
97 u_uastrcpy(stringToExamine
, cStringToExamine
);
99 /*print each sentence in forward and reverse order*/
100 boundary
= ubrk_open(UBRK_SENTENCE
, "en_us", stringToExamine
,
102 if (U_FAILURE(status
)) {
103 printf("ubrk_open error: %s\n", u_errorName(status
));
107 printf("\n----- Sentence Boundaries, forward: -----------\n");
108 printEachForward(boundary
, stringToExamine
);
109 printf("\n----- Sentence Boundaries, backward: ----------\n");
110 printEachBackward(boundary
, stringToExamine
);
111 ubrk_close(boundary
);
113 /*print each word in order*/
114 boundary
= ubrk_open(UBRK_WORD
, "en_us", stringToExamine
,
115 u_strlen(stringToExamine
), &status
);
116 printf("\n----- Word Boundaries, forward: -----------\n");
117 printEachForward(boundary
, stringToExamine
);
118 printf("\n----- Word Boundaries, backward: ----------\n");
119 printEachBackward(boundary
, stringToExamine
);
120 /*print first element*/
121 printf("\n----- first: -------------\n");
122 printFirst(boundary
, stringToExamine
);
123 /*print last element*/
124 printf("\n----- last: --------------\n");
125 printLast(boundary
, stringToExamine
);
126 /*print word at charpos 10 */
127 printf("\n----- at pos 10: ---------\n");
128 printAt(boundary
, 10 , stringToExamine
);
130 ubrk_close(boundary
);
132 printf("\nEnd of C boundary analysis\n");