new version with WorkingCopy Management
[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
15  * char arrays more 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  * Returns the char array as a String
33  */
34 public static String charToString(char[] chars) {
35         if (chars == null) {
36                 return null;
37         } else {
38                 return new String(chars);
39         }
40 }
41 /**
42  * Concatinates the two arrays into one big array.
43  * If the first array is null, returns the second array.
44  * If the second array is null, returns the first array.
45  *
46  * @param first - the array which the other array is concatinated onto
47  * @param second - the array which is to be concatinated onto the first array
48  */
49 public static char[] concat(char[] first, char[] second) {
50         if (first == null)
51                 return second;
52         if (second == null)
53                 return first;
54
55         int length1 = first.length;
56         int length2 = second.length;
57         char[] result = new char[length1 + length2];
58         System.arraycopy(first, 0, result, 0, length1);
59         System.arraycopy(second, 0, result, length1, length2);
60         return result;
61 }
62 /**
63  * Checks the two character arrays for equality.
64  *
65  * @param first - one of the arrays to be compared
66  * @param second - the other array which is to be compared
67  */
68 public static boolean equals(char[] first, char[] second) {
69         if (first == second)
70                 return true;
71         if (first == null || second == null)
72                 return false;
73         if (first.length != second.length)
74                 return false;
75
76         for (int i = 0, length = first.length; i < length; i++)
77                 if (first[i] != second[i])
78                         return false;
79         return true;
80 }
81 /**
82  * Returns the index of the first occurrence of character in buffer,
83  * starting from offset, or -1 if not found.
84  */
85 public static int indexOf(char character, char[] buffer, int offset) {
86         for (int i= offset; i < buffer.length; i++) {
87                 if (buffer[i] == character) {
88                         return i;
89                 }
90         }
91         return -1;
92 }
93 /**
94  * Extracts a sub-array from the given array, starting
95  * at the given startIndex and proceeding for length characters.
96  * Returns null if:
97  *  1. the src array is null
98  *  2. the start index is out of bounds
99  *  3. the length parameter specifies a end point which is out of bounds
100  * Does not return a copy of the array if possible, in other words, if start is zero
101  * and length equals the length of the src array.
102  *
103  * @param src - the array from which elements need to be copied
104  * @param start - the start index in the src array
105  * @param length - the number of characters to copy
106  */
107 public static char[] subarray(char[] src, int start, int length) {
108         if (src == null)
109                 return null;
110         int srcLength = src.length;
111         if (start < 0 || start >= srcLength)
112                 return null;
113         if (length < 0 || start + length > srcLength)
114                 return null;
115         if (srcLength == length && start == 0)
116                 return src;
117                 
118         char[] result = new char[length];
119         if (length > 0)
120                 System.arraycopy(src, start, result, 0, length);
121         return result;
122 }
123 /**
124  * Extracts a substring from the given array, starting
125  * at the given startIndex and proceeding for length characters.
126  * Returns null if:
127  *  1. the src array is null
128  *  2. the start index is out of bounds
129  *  3. the length parameter specifies a end point which is out of bounds
130  * Does not return a copy of the array if possible (if start is zero
131  * and length equals the length of the src array).
132  *
133  * @param src - the array from which elements need to be copied
134  * @param start - the start index in the src array
135  * @param length - the number of characters to copy
136  */
137 public static String substring(char[] src, int start, int length) {
138         char[] chars= subarray(src, start, length);
139         if (chars != null) {
140                 return new String(chars);
141         } else {
142                 return null;
143         }
144 }
145 }