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