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 char arrays more
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]);
33 * Returns the char array as a String
35 public static String charToString(char[] chars) {
39 return new String(chars);
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
49 * the array which the other array is concatinated onto
51 * the array which is to be concatinated onto the first array
53 public static char[] concat(char[] first, char[] second) {
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);
68 * Checks the two character arrays for equality.
71 * one of the arrays to be compared
73 * the other array which is to be compared
75 public static boolean equals(char[] first, char[] second) {
78 if (first == null || second == null)
80 if (first.length != second.length)
83 for (int i = 0, length = first.length; i < length; i++)
84 if (first[i] != second[i])
90 * Returns the index of the first occurrence of character in buffer,
91 * starting from offset, or -1 if not found.
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) {
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.
111 * the array from which elements need to be copied
113 * the start index in the src array
115 * the number of characters to copy
117 public static char[] subarray(char[] src, int start, int length) {
120 int srcLength = src.length;
121 if (start < 0 || start >= srcLength)
123 if (length < 0 || start + length > srcLength)
125 if (srcLength == length && start == 0)
128 char[] result = new char[length];
130 System.arraycopy(src, start, result, 0, length);
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).
143 * the array from which elements need to be copied
145 * the start index in the src array
147 * the number of characters to copy
149 public static String substring(char[] src, int start, int length) {
150 char[] chars = subarray(src, start, length);
152 return new String(chars);