2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.impl;
\r
9 import com.ibm.icu.text.Replaceable;
\r
10 import com.ibm.icu.text.ReplaceableString;
\r
11 import com.ibm.icu.text.Transliterator;
\r
12 import com.ibm.icu.text.UnicodeMatcher;
\r
16 //This class contains utility functions so testing not needed
\r
18 public class UtilityExtensions {
\r
20 * Append the given string to the rule. Calls the single-character
\r
21 * version of appendToRule for each character.
\r
23 public static void appendToRule(StringBuffer rule,
\r
26 boolean escapeUnprintable,
\r
27 StringBuffer quoteBuf) {
\r
28 for (int i=0; i<text.length(); ++i) {
\r
29 // Okay to process in 16-bit code units here
\r
30 Utility.appendToRule(rule, text.charAt(i), isLiteral, escapeUnprintable, quoteBuf);
\r
36 * Given a matcher reference, which may be null, append its
\r
37 * pattern as a literal to the given rule.
\r
39 public static void appendToRule(StringBuffer rule,
\r
40 UnicodeMatcher matcher,
\r
41 boolean escapeUnprintable,
\r
42 StringBuffer quoteBuf) {
\r
43 if (matcher != null) {
\r
44 appendToRule(rule, matcher.toPattern(escapeUnprintable),
\r
45 true, escapeUnprintable, quoteBuf);
\r
49 * For debugging purposes; format the given text in the form
\r
50 * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start
\r
51 * and limit, and the || indicate the start and limit.
\r
53 public static String formatInput(ReplaceableString input,
\r
54 Transliterator.Position pos) {
\r
55 StringBuffer appendTo = new StringBuffer();
\r
56 formatInput(appendTo, input, pos);
\r
57 return com.ibm.icu.impl.Utility.escape(appendTo.toString());
\r
61 * For debugging purposes; format the given text in the form
\r
62 * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start
\r
63 * and limit, and the || indicate the start and limit.
\r
65 public static StringBuffer formatInput(StringBuffer appendTo,
\r
66 ReplaceableString input,
\r
67 Transliterator.Position pos) {
\r
68 if (0 <= pos.contextStart &&
\r
69 pos.contextStart <= pos.start &&
\r
70 pos.start <= pos.limit &&
\r
71 pos.limit <= pos.contextLimit &&
\r
72 pos.contextLimit <= input.length()) {
\r
75 //a = input.substring(0, pos.contextStart);
\r
76 b = input.substring(pos.contextStart, pos.start);
\r
77 c = input.substring(pos.start, pos.limit);
\r
78 d = input.substring(pos.limit, pos.contextLimit);
\r
79 //e = input.substring(pos.contextLimit, input.length());
\r
80 appendTo.//append(a).
\r
81 append('{').append(b).
\r
82 append('|').append(c).append('|').append(d).
\r
87 appendTo.append("INVALID Position {cs=" +
\r
88 pos.contextStart + ", s=" + pos.start + ", l=" +
\r
89 pos.limit + ", cl=" + pos.contextLimit + "} on " +
\r
96 * Convenience method.
\r
98 public static String formatInput(Replaceable input,
\r
99 Transliterator.Position pos) {
\r
100 return formatInput((ReplaceableString) input, pos);
\r
104 * Convenience method.
\r
106 public static StringBuffer formatInput(StringBuffer appendTo,
\r
108 Transliterator.Position pos) {
\r
109 return formatInput(appendTo, (ReplaceableString) input, pos);
\r