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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
14 * A class to do characters operations so that we can use
15 * char arrays more effectively.
17 public class CharArrayOps { // TODO: should promote to CharOperation
19 * Returns the char arrays as an array of Strings
21 public static String[] charcharToString(char[][] charchar) {
22 if (charchar == null) {
25 String[] strings= new String[charchar.length];
26 for (int i= 0; i < charchar.length; i++) {
27 strings[i]= new String(charchar[i]);
32 * Returns the char array as a String
34 public static String charToString(char[] chars) {
38 return new String(chars);
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.
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
49 public static char[] concat(char[] first, char[] second) {
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);
63 * Checks the two character arrays for equality.
65 * @param first - one of the arrays to be compared
66 * @param second - the other array which is to be compared
68 public static boolean equals(char[] first, char[] second) {
71 if (first == null || second == null)
73 if (first.length != second.length)
76 for (int i = 0, length = first.length; i < length; i++)
77 if (first[i] != second[i])
82 * Returns the index of the first occurrence of character in buffer,
83 * starting from offset, or -1 if not found.
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) {
94 * Extracts a sub-array from the given array, starting
95 * at the given startIndex and proceeding for length characters.
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.
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
107 public static char[] subarray(char[] src, int start, int length) {
110 int srcLength = src.length;
111 if (start < 0 || start >= srcLength)
113 if (length < 0 || start + length > srcLength)
115 if (srcLength == length && start == 0)
118 char[] result = new char[length];
120 System.arraycopy(src, start, result, 0, length);
124 * Extracts a substring from the given array, starting
125 * at the given startIndex and proceeding for length characters.
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).
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
137 public static String substring(char[] src, int start, int length) {
138 char[] chars= subarray(src, start, length);
140 return new String(chars);