1/*
2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.impl;
8
9import com.ibm.icu.text.Replaceable;
10import com.ibm.icu.text.ReplaceableString;
11import com.ibm.icu.text.Transliterator;
12import com.ibm.icu.text.UnicodeMatcher;
13/**
14 * @author Ram
15 */
16//This class contains utility functions so testing not needed
17///CLOVER:OFF
18public class UtilityExtensions {
19    /**
20     * Append the given string to the rule.  Calls the single-character
21     * version of appendToRule for each character.
22     */
23    public static void appendToRule(StringBuffer rule,
24                                    String text,
25                                    boolean isLiteral,
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);
31        }
32    }
33
34
35    /**
36     * Given a matcher reference, which may be null, append its
37     * pattern as a literal to the given rule.
38     */
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);
46        }
47    }
48    /**
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.
52     */
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());
58    }
59
60    /**
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.
64     */
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()) {
73
74            String  b, c, d;
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());
80            appendTo.//append(a).
81                append('{').append(b).
82                append('|').append(c).append('|').append(d).
83                append('}')
84                //.append(e)
85                ;
86        } else {
87            appendTo.append("INVALID Position {cs=" +
88                            pos.contextStart + ", s=" + pos.start + ", l=" +
89                            pos.limit + ", cl=" + pos.contextLimit + "} on " +
90                            input);
91        }
92        return appendTo;
93    }
94
95    /**
96     * Convenience method.
97     */
98    public static String formatInput(Replaceable input,
99                                     Transliterator.Position pos) {
100        return formatInput((ReplaceableString) input, pos);
101    }
102
103    /**
104     * Convenience method.
105     */
106    public static StringBuffer formatInput(StringBuffer appendTo,
107                                           Replaceable input,
108                                           Transliterator.Position pos) {
109        return formatInput(appendTo, (ReplaceableString) input, pos);
110    }
111
112}
113//CLOVER:ON