A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / util / CharArrayOps.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
12
13 /**
14  * A class to do characters operations so that we can use char arrays more
15  * effectively.
16  */
17 public class CharArrayOps { // TODO: should promote to CharOperation
18         /**
19          * Returns the char arrays as an array of Strings
20          */
21         public static String[] charcharToString(char[][] charchar) {
22                 if (charchar == null) {
23                         return null;
24                 }
25                 String[] strings = new String[charchar.length];
26                 for (int i = 0; i < charchar.length; i++) {
27                         strings[i] = new String(charchar[i]);
28                 }
29                 return strings;
30         }
31
32         /**
33          * Returns the char array as a String
34          */
35         public static String charToString(char[] chars) {
36                 if (chars == null) {
37                         return null;
38                 } else {
39                         return new String(chars);
40                 }
41         }
42
43         /**
44          * Concatinates the two arrays into one big array. If the first array is
45          * null, returns the second array. If the second array is null, returns the
46          * first array.
47          * 
48          * @param first -
49          *            the array which the other array is concatinated onto
50          * @param second -
51          *            the array which is to be concatinated onto the first array
52          */
53         public static char[] concat(char[] first, char[] second) {
54                 if (first == null)
55                         return second;
56                 if (second == null)
57                         return first;
58
59                 int length1 = first.length;
60                 int length2 = second.length;
61                 char[] result = new char[length1 + length2];
62                 System.arraycopy(first, 0, result, 0, length1);
63                 System.arraycopy(second, 0, result, length1, length2);
64                 return result;
65         }
66
67         /**
68          * Checks the two character arrays for equality.
69          * 
70          * @param first -
71          *            one of the arrays to be compared
72          * @param second -
73          *            the other array which is to be compared
74          */
75         public static boolean equals(char[] first, char[] second) {
76                 if (first == second)
77                         return true;
78                 if (first == null || second == null)
79                         return false;
80                 if (first.length != second.length)
81                         return false;
82
83                 for (int i = 0, length = first.length; i < length; i++)
84                         if (first[i] != second[i])
85                                 return false;
86                 return true;
87         }
88
89         /**
90          * Returns the index of the first occurrence of character in buffer,
91          * starting from offset, or -1 if not found.
92          */
93         public static int indexOf(char character, char[] buffer, int offset) {
94                 for (int i = offset; i < buffer.length; i++) {
95                         if (buffer[i] == character) {
96                                 return i;
97                         }
98                 }
99                 return -1;
100         }
101
102         /**
103          * Extracts a sub-array from the given array, starting at the given
104          * startIndex and proceeding for length characters. Returns null if: 1. the
105          * src array is null 2. the start index is out of bounds 3. the length
106          * parameter specifies a end point which is out of bounds Does not return a
107          * copy of the array if possible, in other words, if start is zero and
108          * length equals the length of the src array.
109          * 
110          * @param src -
111          *            the array from which elements need to be copied
112          * @param start -
113          *            the start index in the src array
114          * @param length -
115          *            the number of characters to copy
116          */
117         public static char[] subarray(char[] src, int start, int length) {
118                 if (src == null)
119                         return null;
120                 int srcLength = src.length;
121                 if (start < 0 || start >= srcLength)
122                         return null;
123                 if (length < 0 || start + length > srcLength)
124                         return null;
125                 if (srcLength == length && start == 0)
126                         return src;
127
128                 char[] result = new char[length];
129                 if (length > 0)
130                         System.arraycopy(src, start, result, 0, length);
131                 return result;
132         }
133
134         /**
135          * Extracts a substring from the given array, starting at the given
136          * startIndex and proceeding for length characters. Returns null if: 1. the
137          * src array is null 2. the start index is out of bounds 3. the length
138          * parameter specifies a end point which is out of bounds Does not return a
139          * copy of the array if possible (if start is zero and length equals the
140          * length of the src array).
141          * 
142          * @param src -
143          *            the array from which elements need to be copied
144          * @param start -
145          *            the start index in the src array
146          * @param length -
147          *            the number of characters to copy
148          */
149         public static String substring(char[] src, int start, int length) {
150                 char[] chars = subarray(src, start, length);
151                 if (chars != null) {
152                         return new String(chars);
153                 } else {
154                         return null;
155                 }
156         }
157 }