1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2002-2010, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.impl; 10 11 import com.ibm.icu.text.Replaceable; 12 import com.ibm.icu.text.ReplaceableString; 13 import com.ibm.icu.text.Transliterator; 14 import com.ibm.icu.text.UnicodeMatcher; 15 /** 16 * @author Ram 17 */ 18 //This class contains utility functions so testing not needed 19 ///CLOVER:OFF 20 public class UtilityExtensions { 21 /** 22 * Append the given string to the rule. Calls the single-character 23 * version of appendToRule for each character. 24 */ appendToRule(StringBuffer rule, String text, boolean isLiteral, boolean escapeUnprintable, StringBuffer quoteBuf)25 public static void appendToRule(StringBuffer rule, 26 String text, 27 boolean isLiteral, 28 boolean escapeUnprintable, 29 StringBuffer quoteBuf) { 30 for (int i=0; i<text.length(); ++i) { 31 // Okay to process in 16-bit code units here 32 Utility.appendToRule(rule, text.charAt(i), isLiteral, escapeUnprintable, quoteBuf); 33 } 34 } 35 36 37 /** 38 * Given a matcher reference, which may be null, append its 39 * pattern as a literal to the given rule. 40 */ appendToRule(StringBuffer rule, UnicodeMatcher matcher, boolean escapeUnprintable, StringBuffer quoteBuf)41 public static void appendToRule(StringBuffer rule, 42 UnicodeMatcher matcher, 43 boolean escapeUnprintable, 44 StringBuffer quoteBuf) { 45 if (matcher != null) { 46 appendToRule(rule, matcher.toPattern(escapeUnprintable), 47 true, escapeUnprintable, quoteBuf); 48 } 49 } 50 /** 51 * For debugging purposes; format the given text in the form 52 * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start 53 * and limit, and the || indicate the start and limit. 54 */ formatInput(ReplaceableString input, Transliterator.Position pos)55 public static String formatInput(ReplaceableString input, 56 Transliterator.Position pos) { 57 StringBuffer appendTo = new StringBuffer(); 58 formatInput(appendTo, input, pos); 59 return com.ibm.icu.impl.Utility.escape(appendTo.toString()); 60 } 61 62 /** 63 * For debugging purposes; format the given text in the form 64 * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start 65 * and limit, and the || indicate the start and limit. 66 */ formatInput(StringBuffer appendTo, ReplaceableString input, Transliterator.Position pos)67 public static StringBuffer formatInput(StringBuffer appendTo, 68 ReplaceableString input, 69 Transliterator.Position pos) { 70 if (0 <= pos.contextStart && 71 pos.contextStart <= pos.start && 72 pos.start <= pos.limit && 73 pos.limit <= pos.contextLimit && 74 pos.contextLimit <= input.length()) { 75 76 String b, c, d; 77 //a = input.substring(0, pos.contextStart); 78 b = input.substring(pos.contextStart, pos.start); 79 c = input.substring(pos.start, pos.limit); 80 d = input.substring(pos.limit, pos.contextLimit); 81 //e = input.substring(pos.contextLimit, input.length()); 82 appendTo.//append(a). 83 append('{').append(b). 84 append('|').append(c).append('|').append(d). 85 append('}') 86 //.append(e) 87 ; 88 } else { 89 appendTo.append("INVALID Position {cs=" + 90 pos.contextStart + ", s=" + pos.start + ", l=" + 91 pos.limit + ", cl=" + pos.contextLimit + "} on " + 92 input); 93 } 94 return appendTo; 95 } 96 97 /** 98 * Convenience method. 99 */ formatInput(Replaceable input, Transliterator.Position pos)100 public static String formatInput(Replaceable input, 101 Transliterator.Position pos) { 102 return formatInput((ReplaceableString) input, pos); 103 } 104 105 /** 106 * Convenience method. 107 */ formatInput(StringBuffer appendTo, Replaceable input, Transliterator.Position pos)108 public static StringBuffer formatInput(StringBuffer appendTo, 109 Replaceable input, 110 Transliterator.Position pos) { 111 return formatInput(appendTo, (ReplaceableString) input, pos); 112 } 113 114 } 115 //CLOVER:ON