]> git.saurik.com Git - apple/icu.git/blob - icuSources/samples/translit/unaccent.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / samples / translit / unaccent.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1999-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unaccent.h"
8
9 const char UnaccentTransliterator::fgClassID = 0;
10
11 /**
12 * Constructor
13 */
14 UnaccentTransliterator::UnaccentTransliterator() :
15 normalizer("", UNORM_NFD),
16 Transliterator("Unaccent", 0) {
17 }
18
19 /**
20 * Destructor
21 */
22 UnaccentTransliterator::~UnaccentTransliterator() {
23 }
24
25 /**
26 * Remove accents from a character using Normalizer.
27 */
28 UChar UnaccentTransliterator::unaccent(UChar c) const {
29 UnicodeString str(c);
30 UErrorCode status = U_ZERO_ERROR;
31 UnaccentTransliterator* t = (UnaccentTransliterator*)this;
32
33 t->normalizer.setText(str, status);
34 if (U_FAILURE(status)) {
35 return c;
36 }
37 return (UChar) t->normalizer.next();
38 }
39
40 /**
41 * Implement Transliterator API
42 */
43 void UnaccentTransliterator::handleTransliterate(Replaceable& text,
44 UTransPosition& index,
45 UBool incremental) const {
46 UnicodeString str("a");
47 while (index.start < index.limit) {
48 UChar c = text.charAt(index.start);
49 UChar d = unaccent(c);
50 if (c != d) {
51 str.setCharAt(0, d);
52 text.handleReplaceBetween(index.start, index.start+1, str);
53 }
54 index.start++;
55 }
56 }