]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | /*********************************************************************** |
2 | * © 2016 and later: Unicode, Inc. and others. | |
3 | * License & terms of use: http://www.unicode.org/copyright.html#License | |
4 | *********************************************************************** | |
b75a7d8f A |
5 | * COPYRIGHT: |
6 | * Copyright (c) 1999-2002, International Business Machines Corporation and | |
7 | * others. All Rights Reserved. | |
f3c0d7a5 | 8 | ***********************************************************************/ |
b75a7d8f A |
9 | |
10 | #include "unaccent.h" | |
11 | ||
12 | /** | |
13 | * Constructor | |
14 | */ | |
15 | UnaccentTransliterator::UnaccentTransliterator() : | |
16 | normalizer("", Normalizer::DECOMP), | |
17 | Transliterator("Unaccent", 0) { | |
18 | } | |
19 | ||
20 | /** | |
21 | * Destructor | |
22 | */ | |
23 | UnaccentTransliterator::~UnaccentTransliterator() { | |
24 | } | |
25 | ||
26 | /** | |
27 | * Remove accents from a character using Normalizer. | |
28 | */ | |
29 | 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 | */ | |
44 | 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 | } |