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