1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1999-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 11/17/99 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/rep.h"
18 #include "unicode/unifilt.h"
19 #include "unicode/uniset.h"
20 #include "unicode/utf16.h"
29 static const UChar FORWARD_OP
[] = {32,62,32,0}; // " > "
34 * Construct a new rule with the given input, output text, and other
35 * attributes. A cursor position may be specified for the output text.
36 * @param input input string, including key and optional ante and
38 * @param anteContextPos offset into input to end of ante context, or -1 if
39 * none. Must be <= input.length() if not -1.
40 * @param postContextPos offset into input to start of post context, or -1
41 * if none. Must be <= input.length() if not -1, and must be >=
43 * @param output output string
44 * @param cursorPosition offset into output at which cursor is located, or -1 if
45 * none. If less than zero, then the cursor is placed after the
46 * <code>output</code>; that is, -1 is equivalent to
47 * <code>output.length()</code>. If greater than
48 * <code>output.length()</code> then an exception is thrown.
49 * @param segs array of UnicodeFunctors corresponding to input pattern
50 * segments, or null if there are none. The array itself is adopted,
51 * but the pointers within it are not.
52 * @param segsCount number of elements in segs[]
53 * @param anchorStart TRUE if the the rule is anchored on the left to
55 * @param anchorEnd TRUE if the rule is anchored on the right to the
58 TransliterationRule::TransliterationRule(const UnicodeString
& input
,
59 int32_t anteContextPos
, int32_t postContextPos
,
60 const UnicodeString
& outputStr
,
61 int32_t cursorPosition
, int32_t cursorOffset
,
62 UnicodeFunctor
** segs
,
64 UBool anchorStart
, UBool anchorEnd
,
65 const TransliterationRuleData
* theData
,
71 if (U_FAILURE(status
)) {
74 // Do range checks only when warranted to save time
75 if (anteContextPos
< 0) {
76 anteContextLength
= 0;
78 if (anteContextPos
> input
.length()) {
79 // throw new IllegalArgumentException("Invalid ante context");
80 status
= U_ILLEGAL_ARGUMENT_ERROR
;
83 anteContextLength
= anteContextPos
;
85 if (postContextPos
< 0) {
86 keyLength
= input
.length() - anteContextLength
;
88 if (postContextPos
< anteContextLength
||
89 postContextPos
> input
.length()) {
90 // throw new IllegalArgumentException("Invalid post context");
91 status
= U_ILLEGAL_ARGUMENT_ERROR
;
94 keyLength
= postContextPos
- anteContextLength
;
96 if (cursorPosition
< 0) {
97 cursorPosition
= outputStr
.length();
98 } else if (cursorPosition
> outputStr
.length()) {
99 // throw new IllegalArgumentException("Invalid cursor position");
100 status
= U_ILLEGAL_ARGUMENT_ERROR
;
103 // We don't validate the segments array. The caller must
104 // guarantee that the segments are well-formed (that is, that
105 // all $n references in the output refer to indices of this
106 // array, and that no array elements are null).
107 this->segments
= segs
;
108 this->segmentsCount
= segsCount
;
113 flags
|= ANCHOR_START
;
120 if (anteContextLength
> 0) {
121 anteContext
= new StringMatcher(pattern
, 0, anteContextLength
,
124 if (anteContext
== 0) {
125 status
= U_MEMORY_ALLOCATION_ERROR
;
132 key
= new StringMatcher(pattern
, anteContextLength
, anteContextLength
+ keyLength
,
136 status
= U_MEMORY_ALLOCATION_ERROR
;
141 int32_t postContextLength
= pattern
.length() - keyLength
- anteContextLength
;
143 if (postContextLength
> 0) {
144 postContext
= new StringMatcher(pattern
, anteContextLength
+ keyLength
, pattern
.length(),
147 if (postContext
== 0) {
148 status
= U_MEMORY_ALLOCATION_ERROR
;
153 this->output
= new StringReplacer(outputStr
, cursorPosition
+ cursorOffset
, data
);
155 if (this->output
== 0) {
156 status
= U_MEMORY_ALLOCATION_ERROR
;
164 TransliterationRule::TransliterationRule(TransliterationRule
& other
) :
169 pattern(other
.pattern
),
170 anteContextLength(other
.anteContextLength
),
171 keyLength(other
.keyLength
),
177 if (other
.segmentsCount
> 0) {
178 segments
= (UnicodeFunctor
**)uprv_malloc(other
.segmentsCount
* sizeof(UnicodeFunctor
*));
179 uprv_memcpy(segments
, other
.segments
, (size_t)other
.segmentsCount
*sizeof(segments
[0]));
182 if (other
.anteContext
!= NULL
) {
183 anteContext
= (StringMatcher
*) other
.anteContext
->clone();
185 if (other
.key
!= NULL
) {
186 key
= (StringMatcher
*) other
.key
->clone();
188 if (other
.postContext
!= NULL
) {
189 postContext
= (StringMatcher
*) other
.postContext
->clone();
191 output
= other
.output
->clone();
194 TransliterationRule::~TransliterationRule() {
203 * Return the preceding context length. This method is needed to
204 * support the <code>Transliterator</code> method
205 * <code>getMaximumContextLength()</code>. Internally, this is
206 * implemented as the anteContextLength, optionally plus one if
207 * there is a start anchor. The one character anchor gap is
208 * needed to make repeated incremental transliteration with
211 int32_t TransliterationRule::getContextLength(void) const {
212 return anteContextLength
+ ((flags
& ANCHOR_START
) ? 1 : 0);
216 * Internal method. Returns 8-bit index value for this rule.
217 * This is the low byte of the first character of the key,
218 * unless the first character of the key is a set. If it's a
219 * set, or otherwise can match multiple keys, the index value is -1.
221 int16_t TransliterationRule::getIndexValue() const {
222 if (anteContextLength
== pattern
.length()) {
223 // A pattern with just ante context {such as foo)>bar} can
227 UChar32 c
= pattern
.char32At(anteContextLength
);
228 return (int16_t)(data
->lookupMatcher(c
) == NULL
? (c
& 0xFF) : -1);
232 * Internal method. Returns true if this rule matches the given
233 * index value. The index value is an 8-bit integer, 0..255,
234 * representing the low byte of the first character of the key.
235 * It matches this rule if it matches the first character of the
236 * key, or if the first character of the key is a set, and the set
237 * contains any character with a low byte equal to the index
238 * value. If the rule contains only ante context, as in foo)>bar,
239 * then it will match any key.
241 UBool
TransliterationRule::matchesIndexValue(uint8_t v
) const {
242 // Delegate to the key, or if there is none, to the postContext.
243 // If there is neither then we match any key; return true.
244 UnicodeMatcher
*m
= (key
!= NULL
) ? key
: postContext
;
245 return (m
!= NULL
) ? m
->matchesIndexValue(v
) : TRUE
;
249 * Return true if this rule masks another rule. If r1 masks r2 then
250 * r1 matches any input string that r2 matches. If r1 masks r2 and r2 masks
251 * r1 then r1 == r2. Examples: "a>x" masks "ab>y". "a>x" masks "a[b]>y".
252 * "[c]a>x" masks "[dc]a>y".
254 UBool
TransliterationRule::masks(const TransliterationRule
& r2
) const {
255 /* Rule r1 masks rule r2 if the string formed of the
256 * antecontext, key, and postcontext overlaps in the following
263 * The strings must be aligned at the first character of the
264 * key. The length of r1 to the left of the alignment point
265 * must be <= the length of r2 to the left; ditto for the
266 * right. The characters of r1 must equal (or be a superset
267 * of) the corresponding characters of r2. The superset
268 * operation should be performed to check for UnicodeSet
271 * Anchors: Two patterns that differ only in anchors only
272 * mask one another if they are exactly equal, and r2 has
273 * all the anchors r1 has (optionally, plus some). Here Y
274 * means the row masks the column, N means it doesn't.
282 * Post context: {a}b masks ab, but not vice versa, since {a}b
283 * matches everything ab matches, and {a}b matches {|a|}b but ab
284 * does not. Pre context is different (a{b} does not align with
288 /* LIMITATION of the current mask algorithm: Some rule
289 * maskings are currently not detected. For example,
290 * "{Lu}]a>x" masks "A]a>y". This can be added later. TODO
293 int32_t len
= pattern
.length();
294 int32_t left
= anteContextLength
;
295 int32_t left2
= r2
.anteContextLength
;
296 int32_t right
= len
- left
;
297 int32_t right2
= r2
.pattern
.length() - left2
;
298 int32_t cachedCompare
= r2
.pattern
.compare(left2
- left
, len
, pattern
);
300 // TODO Clean this up -- some logic might be combinable with the
303 // Test for anchor masking
304 if (left
== left2
&& right
== right2
&&
305 keyLength
<= r2
.keyLength
&&
306 0 == cachedCompare
) {
307 // The following boolean logic implements the table above
308 return (flags
== r2
.flags
) ||
309 (!(flags
& ANCHOR_START
) && !(flags
& ANCHOR_END
)) ||
310 ((r2
.flags
& ANCHOR_START
) && (r2
.flags
& ANCHOR_END
));
313 return left
<= left2
&&
315 (right
== right2
&& keyLength
<= r2
.keyLength
)) &&
316 (0 == cachedCompare
);
319 static inline int32_t posBefore(const Replaceable
& str
, int32_t pos
) {
321 pos
- U16_LENGTH(str
.char32At(pos
-1)) :
325 static inline int32_t posAfter(const Replaceable
& str
, int32_t pos
) {
326 return (pos
>= 0 && pos
< str
.length()) ?
327 pos
+ U16_LENGTH(str
.char32At(pos
)) :
332 * Attempt a match and replacement at the given position. Return
333 * the degree of match between this rule and the given text. The
334 * degree of match may be mismatch, a partial match, or a full
335 * match. A mismatch means at least one character of the text
336 * does not match the context or key. A partial match means some
337 * context and key characters match, but the text is not long
338 * enough to match all of them. A full match means all context
339 * and key characters match.
341 * If a full match is obtained, perform a replacement, update pos,
342 * and return U_MATCH. Otherwise both text and pos are unchanged.
344 * @param text the text
345 * @param pos the position indices
346 * @param incremental if TRUE, test for partial matches that may
347 * be completed by additional text inserted at pos.limit.
348 * @return one of <code>U_MISMATCH</code>,
349 * <code>U_PARTIAL_MATCH</code>, or <code>U_MATCH</code>. If
350 * incremental is FALSE then U_PARTIAL_MATCH will not be returned.
352 UMatchDegree
TransliterationRule::matchAndReplace(Replaceable
& text
,
354 UBool incremental
) const {
355 // Matching and replacing are done in one method because the
356 // replacement operation needs information obtained during the
357 // match. Another way to do this is to have the match method
358 // create a match result struct with relevant offsets, and to pass
359 // this into the replace method.
361 // ============================ MATCH ===========================
363 // Reset segment match data
364 if (segments
!= NULL
) {
365 for (int32_t i
=0; i
<segmentsCount
; ++i
) {
366 ((StringMatcher
*) segments
[i
])->resetMatch();
370 // int32_t lenDelta, keyLimit;
373 // ------------------------ Ante Context ------------------------
375 // A mismatch in the ante context, or with the start anchor,
376 // is an outright U_MISMATCH regardless of whether we are
377 // incremental or not.
378 int32_t oText
; // offset into 'text'
379 // int32_t newStart = 0;
382 // Note (1): We process text in 16-bit code units, rather than
383 // 32-bit code points. This works because stand-ins are
384 // always in the BMP and because we are doing a literal match
385 // operation, which can be done 16-bits at a time.
387 int32_t anteLimit
= posBefore(text
, pos
.contextStart
);
391 // Start reverse match at char before pos.start
392 oText
= posBefore(text
, pos
.start
);
394 if (anteContext
!= NULL
) {
395 match
= anteContext
->matches(text
, oText
, anteLimit
, FALSE
);
396 if (match
!= U_MATCH
) {
401 minOText
= posAfter(text
, oText
);
403 // ------------------------ Start Anchor ------------------------
405 if (((flags
& ANCHOR_START
) != 0) && oText
!= anteLimit
) {
409 // -------------------- Key and Post Context --------------------
414 match
= key
->matches(text
, oText
, pos
.limit
, incremental
);
415 if (match
!= U_MATCH
) {
422 if (postContext
!= NULL
) {
423 if (incremental
&& keyLimit
== pos
.limit
) {
424 // The key matches just before pos.limit, and there is
425 // a postContext. Since we are in incremental mode,
426 // we must assume more characters may be inserted at
427 // pos.limit -- this is a partial match.
428 return U_PARTIAL_MATCH
;
431 match
= postContext
->matches(text
, oText
, pos
.contextLimit
, incremental
);
432 if (match
!= U_MATCH
) {
437 // ------------------------- Stop Anchor ------------------------
439 if (((flags
& ANCHOR_END
)) != 0) {
440 if (oText
!= pos
.contextLimit
) {
444 return U_PARTIAL_MATCH
;
448 // =========================== REPLACE ==========================
450 // We have a full match. The key is between pos.start and
454 int32_t newLength
= output
->toReplacer()->replace(text
, pos
.start
, keyLimit
, newStart
);
455 int32_t lenDelta
= newLength
- (keyLimit
- pos
.start
);
458 pos
.limit
+= lenDelta
;
459 pos
.contextLimit
+= lenDelta
;
460 // Restrict new value of start to [minOText, min(oText, pos.limit)].
461 pos
.start
= uprv_max(minOText
, uprv_min(uprv_min(oText
, pos
.limit
), newStart
));
466 * Create a source string that represents this rule. Append it to the
469 UnicodeString
& TransliterationRule::toRule(UnicodeString
& rule
,
470 UBool escapeUnprintable
) const {
472 // Accumulate special characters (and non-specials following them)
473 // into quoteBuf. Append quoteBuf, within single quotes, when
474 // a non-quoted element must be inserted.
475 UnicodeString str
, quoteBuf
;
477 // Do not emit the braces '{' '}' around the pattern if there
478 // is neither anteContext nor postContext.
480 (anteContext
!= NULL
) || (postContext
!= NULL
);
483 if ((flags
& ANCHOR_START
) != 0) {
484 rule
.append((UChar
)94/*^*/);
487 // Emit the input pattern
488 ICU_Utility::appendToRule(rule
, anteContext
, escapeUnprintable
, quoteBuf
);
491 ICU_Utility::appendToRule(rule
, (UChar
) 0x007B /*{*/, TRUE
, escapeUnprintable
, quoteBuf
);
494 ICU_Utility::appendToRule(rule
, key
, escapeUnprintable
, quoteBuf
);
497 ICU_Utility::appendToRule(rule
, (UChar
) 0x007D /*}*/, TRUE
, escapeUnprintable
, quoteBuf
);
500 ICU_Utility::appendToRule(rule
, postContext
, escapeUnprintable
, quoteBuf
);
503 if ((flags
& ANCHOR_END
) != 0) {
504 rule
.append((UChar
)36/*$*/);
507 ICU_Utility::appendToRule(rule
, UnicodeString(TRUE
, FORWARD_OP
, 3), TRUE
, escapeUnprintable
, quoteBuf
);
509 // Emit the output pattern
511 ICU_Utility::appendToRule(rule
, output
->toReplacer()->toReplacerPattern(str
, escapeUnprintable
),
512 TRUE
, escapeUnprintable
, quoteBuf
);
514 ICU_Utility::appendToRule(rule
, (UChar
) 0x003B /*;*/, TRUE
, escapeUnprintable
, quoteBuf
);
519 void TransliterationRule::setData(const TransliterationRuleData
* d
) {
521 if (anteContext
!= NULL
) anteContext
->setData(d
);
522 if (postContext
!= NULL
) postContext
->setData(d
);
523 if (key
!= NULL
) key
->setData(d
);
524 // assert(output != NULL);
526 // Don't have to do segments since they are in the context or key
530 * Union the set of all characters that may be modified by this rule
531 * into the given set.
533 void TransliterationRule::addSourceSetTo(UnicodeSet
& toUnionTo
) const {
534 int32_t limit
= anteContextLength
+ keyLength
;
535 for (int32_t i
=anteContextLength
; i
<limit
; ) {
536 UChar32 ch
= pattern
.char32At(i
);
538 const UnicodeMatcher
* matcher
= data
->lookupMatcher(ch
);
539 if (matcher
== NULL
) {
542 matcher
->addMatchSetTo(toUnionTo
);
548 * Union the set of all characters that may be emitted by this rule
549 * into the given set.
551 void TransliterationRule::addTargetSetTo(UnicodeSet
& toUnionTo
) const {
552 output
->toReplacer()->addReplacementSetTo(toUnionTo
);
557 #endif /* #if !UCONFIG_NO_TRANSLITERATION */