• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1999-2002, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  ********************************************************************/
6 
7 #include "unaccent.h"
8 
9 /**
10  * Constructor
11  */
UnaccentTransliterator()12 UnaccentTransliterator::UnaccentTransliterator() :
13     normalizer("", Normalizer::DECOMP),
14     Transliterator("Unaccent", 0) {
15 }
16 
17 /**
18  * Destructor
19  */
~UnaccentTransliterator()20 UnaccentTransliterator::~UnaccentTransliterator() {
21 }
22 
23 /**
24  * Remove accents from a character using Normalizer.
25  */
unaccent(UChar c) const26 UChar UnaccentTransliterator::unaccent(UChar c) const {
27     UnicodeString str(c);
28     UErrorCode status = U_ZERO_ERROR;
29     UnaccentTransliterator* t = (UnaccentTransliterator*)this;
30 
31     t->normalizer.setText(str, status);
32     if (U_FAILURE(status)) {
33         return c;
34     }
35     return (UChar) t->normalizer.next();
36 }
37 
38 /**
39  * Implement Transliterator API
40  */
handleTransliterate(Replaceable & text,UTransPosition & index,UBool incremental) const41 void UnaccentTransliterator::handleTransliterate(Replaceable& text,
42                                                  UTransPosition& index,
43                                                  UBool incremental) const {
44     UnicodeString str("a");
45     while (index.start < index.limit) {
46         UChar c = text.charAt(index.start);
47         UChar d = unaccent(c);
48         if (c != d) {
49             str.setCharAt(0, d);
50             text.handleReplaceBetween(index.start, index.start+1, str);
51         }
52         index.start++;
53     }
54 }
55