Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / MyDerby-10.3 / java / engine / org / apache / derby / iapi / util / StringUtil.java
diff --git a/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/iapi/util/StringUtil.java b/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/iapi/util/StringUtil.java
new file mode 100644 (file)
index 0000000..9c7331a
--- /dev/null
@@ -0,0 +1,367 @@
+/*\r
+\r
+   Derby - Class com.ihost.cs.StringUtil\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.util;\r
+import java.util.Locale;\r
+\r
+/**\r
+       A set of public static methods for dealing with Strings\r
+*/\r
+public class StringUtil \r
+{\r
+       /**\r
+        * Used to print out a string for error messages, \r
+        * chops is off at 60 chars for historical reasons.\r
+        */\r
+       public final static String formatForPrint(String input)\r
+       {\r
+               if (input.length() > 60)\r
+               {\r
+                       StringBuffer tmp = new StringBuffer(input.substring(0, 60));\r
+                       tmp.append("&");\r
+                       input = tmp.toString();\r
+               }\r
+               return input;\r
+       }\r
+\r
+       /**\r
+        * A method that receive an array of Objects and return a \r
+        * String array representation of that array.\r
+        */\r
+       public static String [] toStringArray(Object [] objArray) {\r
+               int idx;\r
+               int len=objArray.length;\r
+               String [] strArray=new String[len];\r
+\r
+               for (idx=0 ; idx<len ; idx++) {\r
+                       strArray[idx]=objArray[idx].toString();\r
+               }\r
+\r
+               return strArray;\r
+       }\r
+\r
+       /**\r
+               Get 7-bit ASCII character array from input String.\r
+               The lower 7 bits of each character in the input string is assumed to be\r
+               the ASCII character value.\r
+\r
+     Hexadecimal - Character\r
+\r
+     | 00 NUL| 01 SOH| 02 STX| 03 ETX| 04 EOT| 05 ENQ| 06 ACK| 07 BEL|\r
+     | 08 BS | 09 HT | 0A NL | 0B VT | 0C NP | 0D CR | 0E SO | 0F SI |\r
+     | 10 DLE| 11 DC1| 12 DC2| 13 DC3| 14 DC4| 15 NAK| 16 SYN| 17 ETB|\r
+     | 18 CAN| 19 EM | 1A SUB| 1B ESC| 1C FS | 1D GS | 1E RS | 1F US |\r
+     | 20 SP | 21  ! | 22  " | 23  # | 24  $ | 25  % | 26  & | 27  ' |\r
+     | 28  ( | 29  ) | 2A  * | 2B  + | 2C  , | 2D  - | 2E  . | 2F  / |\r
+     | 30  0 | 31  1 | 32  2 | 33  3 | 34  4 | 35  5 | 36  6 | 37  7 |\r
+     | 38  8 | 39  9 | 3A  : | 3B  ; | 3C  < | 3D  = | 3E  > | 3F  ? |\r
+     | 40  @ | 41  A | 42  B | 43  C | 44  D | 45  E | 46  F | 47  G |\r
+     | 48  H | 49  I | 4A  J | 4B  K | 4C  L | 4D  M | 4E  N | 4F  O |\r
+     | 50  P | 51  Q | 52  R | 53  S | 54  T | 55  U | 56  V | 57  W |\r
+     | 58  X | 59  Y | 5A  Z | 5B  [ | 5C  \ | 5D  ] | 5E  ^ | 5F  _ |\r
+     | 60  ` | 61  a | 62  b | 63  c | 64  d | 65  e | 66  f | 67  g |\r
+     | 68  h | 69  i | 6A  j | 6B  k | 6C  l | 6D  m | 6E  n | 6F  o |\r
+     | 70  p | 71  q | 72  r | 73  s | 74  t | 75  u | 76  v | 77  w |\r
+     | 78  x | 79  y | 7A  z | 7B  { | 7C  | | 7D  } | 7E  ~ | 7F DEL|\r
+\r
+        */\r
+       public static byte[] getAsciiBytes(String input)\r
+       {\r
+               char[] c = input.toCharArray();\r
+               byte[] b = new byte[c.length];\r
+               for (int i = 0; i < c.length; i++)\r
+                       b[i] = (byte)(c[i] & 0x007F);\r
+\r
+               return b;\r
+       }\r
+\r
+    /**\r
+     * Trim off trailing blanks but not leading blanks\r
+     *\r
+     * @param str\r
+     *\r
+     * @return The input with trailing blanks stipped off\r
+     */\r
+    public static String trimTrailing( String str)\r
+    {\r
+        if( str == null)\r
+            return null;\r
+        int len = str.length();\r
+        for( ; len > 0; len--)\r
+        {\r
+            if( ! Character.isWhitespace( str.charAt( len - 1)))\r
+                break;\r
+        }\r
+        return str.substring( 0, len);\r
+    } // end of trimTrailing\r
+\r
+\r
+       /**\r
+               Truncate a String to the given length with no warnings\r
+               or error raised if it is bigger.\r
+       \r
+               @param  value String to be truncated\r
+               @param  length  Maximum length of string\r
+\r
+               @return Returns value if value is null or value.length() is less or equal to than length, otherwise a String representing\r
+                       value truncated to length.\r
+       */\r
+       public static String truncate(String value, int length)\r
+       {\r
+               if (value != null && value.length() > length)\r
+                       value = value.substring(0, length);\r
+               return value;\r
+       }\r
+       \r
+       /**\r
+        * Return a slice (substring) of the passed in value, optionally trimmed.\r
+        * WARNING - endOffset is inclusive for historical reasons, unlike\r
+        * String.substring() which has an exclusive ending offset.\r
+        * @param value Value to slice, must be non-null.\r
+        * @param beginOffset Inclusive start character\r
+        * @param endOffset Inclusive end character\r
+        * @param trim To trim or not to trim\r
+        * @return Sliceed value.\r
+        */\r
+       public static String slice(String value,\r
+                       int beginOffset, int endOffset,\r
+                       boolean trim)\r
+       {\r
+               String retval = value.substring(beginOffset, endOffset + 1);\r
+               \r
+               if (trim)\r
+                       retval = retval.trim();\r
+               \r
+               return retval;\r
+       }\r
+\r
+\r
+       private static char[] hex_table = {\r
+                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', \r
+                'a', 'b', 'c', 'd', 'e', 'f'\r
+            };\r
+\r
+\r
+       /**\r
+               Convert a byte array to a String with a hexidecimal format.\r
+               The String may be converted back to a byte array using fromHexString.\r
+               <BR>\r
+               For each byte (b) two characaters are generated, the first character\r
+               represents the high nibble (4 bits) in hexidecimal (<code>b & 0xf0</code>), the second character\r
+               represents the low nibble (<code>b & 0x0f</code>).\r
+               <BR>\r
+               The byte at <code>data[offset]</code> is represented by the first two characters in the returned String.\r
+\r
+               @param  data    byte array\r
+               @param  offset  starting byte (zero based) to convert.\r
+               @param  length  number of bytes to convert.\r
+\r
+               @return the String (with hexidecimal format) form of the byte array\r
+       */\r
+       public static String toHexString(byte[] data, int offset, int length)\r
+       {\r
+               StringBuffer s = new StringBuffer(length*2);\r
+               int end = offset+length;\r
+\r
+               for (int i = offset; i < end; i++)\r
+               {\r
+                       int high_nibble = (data[i] & 0xf0) >>> 4;\r
+                       int low_nibble = (data[i] & 0x0f);\r
+                       s.append(hex_table[high_nibble]);\r
+                       s.append(hex_table[low_nibble]);\r
+               }\r
+\r
+               return s.toString();\r
+       }\r
+\r
+    /**\r
+\r
+        Convert a string into a byte array in hex format.\r
+        <BR>\r
+        For each character (b) two bytes are generated, the first byte \r
+        represents the high nibble (4 bits) in hexidecimal (<code>b & 0xf0</code>),\r
+        the second byte represents the low nibble (<code>b & 0x0f</code>).\r
+        <BR>\r
+        The character at <code>str.charAt(0)</code> is represented by the first two bytes \r
+        in the returned String.\r
+\r
+        @param str string \r
+        @param offset  starting character (zero based) to convert.\r
+        @param length  number of characters to convert.\r
+\r
+        @return the byte[]  (with hexidecimal format) form of the string (str) \r
+    */\r
+    public static byte[] toHexByte(String str, int offset, int length)\r
+    {\r
+        byte[] data = new byte[(length - offset) * 2];\r
+        int end = offset+length;\r
+\r
+        for (int i = offset; i < end; i++)\r
+        {\r
+            char ch = str.charAt(i);\r
+            int high_nibble = (ch & 0xf0) >>> 4;\r
+            int low_nibble = (ch & 0x0f);\r
+            data[i] = (byte)high_nibble;\r
+            data[i+1] = (byte)low_nibble;\r
+        }\r
+        return data;\r
+    }\r
+               \r
+       /**\r
+               Convert a hexidecimal string generated by toHexString() back\r
+               into a byte array.\r
+\r
+               @param s String to convert\r
+               @param offset starting character (zero based) to convert.\r
+               @param length number of characters to convert.\r
+\r
+               @return the converted byte array. Returns null if the length is\r
+               not a multiple of 2.\r
+       */\r
+       public static byte[] fromHexString(String s, int offset, int length)\r
+       {\r
+               if ((length%2) != 0)\r
+                       return null;\r
+\r
+               byte[] byteArray = new byte[length/2];\r
+\r
+               int j = 0;\r
+               int end = offset+length;\r
+               for (int i = offset; i < end; i += 2)\r
+               {\r
+                       int high_nibble = Character.digit(s.charAt(i), 16);\r
+                       int low_nibble = Character.digit(s.charAt(i+1), 16);\r
+\r
+                       if (high_nibble == -1 || low_nibble == -1)\r
+                       {\r
+                               // illegal format\r
+                               return null;\r
+                       }\r
+\r
+                       byteArray[j++] = (byte)(((high_nibble << 4) & 0xf0) | (low_nibble & 0x0f));\r
+               }\r
+               return byteArray;\r
+       }\r
+       /**\r
+               Convert a byte array to a human-readable String for debugging purposes.\r
+       */\r
+       public static String hexDump(byte[] data)\r
+       {\r
+            byte byte_value;\r
+\r
+\r
+            StringBuffer str = new StringBuffer(data.length * 3);\r
+\r
+            str.append("Hex dump:\n");\r
+\r
+            for (int i = 0; i < data.length; i += 16)\r
+            {\r
+                // dump the header: 00000000: \r
+                String offset = Integer.toHexString(i);\r
+\r
+                // "0" left pad offset field so it is always 8 char's long.\r
+                for (int offlen = offset.length(); offlen < 8; offlen++) \r
+                    str.append("0");\r
+                str.append(offset);\r
+                str.append(":");\r
+\r
+                // dump hex version of 16 bytes per line.\r
+                for (int j = 0; (j < 16) && ((i + j) < data.length); j++)\r
+                {\r
+                    byte_value = data[i + j];\r
+\r
+                    // add spaces between every 2 bytes.\r
+                    if ((j % 2) == 0)\r
+                        str.append(" ");\r
+\r
+                    // dump a single byte.\r
+                    byte high_nibble = (byte) ((byte_value & 0xf0) >>> 4); \r
+                    byte low_nibble  = (byte) (byte_value & 0x0f); \r
+\r
+                    str.append(hex_table[high_nibble]);\r
+                    str.append(hex_table[low_nibble]);\r
+                }\r
+\r
+                // dump ascii version of 16 bytes\r
+                str.append("  ");\r
+\r
+                for (int j = 0; (j < 16) && ((i + j) < data.length); j++)\r
+                {\r
+                    char char_value = (char) data[i + j]; \r
+\r
+                    // RESOLVE (really want isAscii() or isPrintable())\r
+                    if (Character.isLetterOrDigit(char_value))\r
+                        str.append(String.valueOf(char_value));\r
+                    else\r
+                        str.append(".");\r
+                }\r
+                    \r
+                // new line\r
+                str.append("\n");\r
+            }\r
+            return(str.toString());\r
+\r
+       }\r
+\r
+       // The functions below are used for uppercasing SQL in a consistent manner.\r
+       // Derby will uppercase Turkish to the English locale to avoid i\r
+       // uppercasing to an uppercase dotted i. In future versions, all \r
+       // casing will be done in English.   The result will be that we will get\r
+       // only the 1:1 mappings  in \r
+       // http://www.unicode.org/Public/3.0-Update1/UnicodeData-3.0.1.txt\r
+       // and avoid the 1:n mappings in \r
+       //http://www.unicode.org/Public/3.0-Update1/SpecialCasing-3.txt\r
+       // \r
+       // Any SQL casing should use these functions\r
+\r
+\r
+       /** Convert string to uppercase\r
+        * Always use the java.util.ENGLISH locale\r
+        * @param s   string to uppercase\r
+        * @return uppercased string\r
+        */\r
+       public static String SQLToUpperCase(String s)\r
+       {\r
+                       return s.toUpperCase(Locale.ENGLISH);\r
+       }\r
+\r
+       /** Compares two strings \r
+        * Strings will be uppercased in english and compared\r
+        * equivalent to s1.equalsIgnoreCase(s2)\r
+        * throws NPE if s1 is null\r
+        *\r
+        * @param s1  first string to compare\r
+        * @param s2  second string to compare\r
+        *\r
+        * @return   true if the two upppercased ENGLISH values are equal\r
+        *           return false if s2 is null\r
+        */\r
+       public static boolean SQLEqualsIgnoreCase(String s1, String s2)\r
+       {\r
+               if (s2 == null)\r
+                       return  false;\r
+               else\r
+                       return SQLToUpperCase(s1).equals(SQLToUpperCase(s2));\r
+\r
+       }\r
+\r
+}\r
+\r