001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.Formatters
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: public class Formatters {
025:
026: static final char[] hexDigits = { '0', '1', '2', '3', '4', '5',
027: '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
028:
029: /** This method converts the non-ASCII characters in the input
030: * parameter to unicode escape sequences.
031: * @param in String to format
032: * @return String containing unicode escape sequences for non-ASCII chars
033: */
034: public static String format(String in) {
035: if (in == null)
036: return null;
037:
038: StringBuffer out = new StringBuffer(in.length());
039: char hexValue[] = new char[4];
040:
041: for (int i = 0; i < in.length(); i++) {
042: char inChar = in.charAt(i);
043:
044: if (inChar < 128) {
045: out.append(inChar);
046: } else {
047: out.append("\\u");
048:
049: int number = (int) inChar;
050:
051: int digit = number % 16;
052:
053: hexValue[3] = hexDigits[digit];
054:
055: number /= 16;
056:
057: digit = number % 16;
058:
059: hexValue[2] = hexDigits[digit];
060:
061: number /= 16;
062:
063: digit = number % 16;
064:
065: hexValue[1] = hexDigits[digit];
066:
067: number /= 16;
068:
069: digit = number % 16;
070:
071: hexValue[0] = hexDigits[digit];
072:
073: out.append(hexValue);
074: }
075: }
076:
077: return out.toString();
078: }
079:
080: /**
081: * repeatChar is used to create strings of varying lengths.
082: * called from various tests to test edge cases and such.
083: *
084: * @param c character to repeat
085: * @param repeatCount Number of times to repeat character
086: * @return String of repeatCount characters c
087: */
088: public static String repeatChar(String c, int repeatCount) {
089: char ch = c.charAt(0);
090:
091: char[] chArray = new char[repeatCount];
092: for (int i = 0; i < repeatCount; i++) {
093: chArray[i] = ch;
094: }
095:
096: return new String(chArray);
097:
098: }
099:
100: /**
101: * Pads out a string to the specified size
102: *
103: * @param oldValue value to be padded
104: * @param size size of resulting string
105: * @return oldValue padded with spaces to the specified size
106: */
107: public static String padString(String oldValue, int size) {
108: String newValue = oldValue;
109: if (newValue != null) {
110: char[] newCharArr = new char[size];
111: oldValue.getChars(0, oldValue.length(), newCharArr, 0);
112: java.util.Arrays.fill(newCharArr, oldValue.length(),
113: newCharArr.length - 1, ' ');
114: newValue = new String(newCharArr);
115: }
116:
117: return newValue;
118: }
119:
120: }
|