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