/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.compiler;
/**
* This class is a collection of helper methods to manipulate char arrays.
*
* @since 2.1
*/
public final class CharOperation {
/**
* Constant for an empty char array
*/
public static final char[] NO_CHAR = new char[0];
/**
* Constant for an empty char array with two dimensions.
*/
public static final char[][] NO_CHAR_CHAR = new char[0][];
/**
* Constant for an empty String array.
* @since 3.1
*/
public static final String[] NO_STRINGS = new String[0];
/**
* Answers a new array with appending the suffix character at the end of the array.
*
*
* For example:
*
* array = { 'a', 'b' } * suffix = 'c' * => result = { 'a', 'b' , 'c' } **
* array = null * suffix = 'c' * => result = { 'c' } *
* target = { 'a', 'b', '0' } * index = 2 * array = { 'c', 'd' } * start = 0 * end = 1 * => result = { 'a', 'b' , 'c' } **
* target = { 'a', 'b' } * index = 2 * array = { 'c', 'd' } * start = 0 * end = 1 * => result = { 'a', 'b' , 'c', '0', '0' , '0' } (new array) *
* target = { 'a', 'b', 'c' } * index = 1 * array = { 'c', 'd', 'e', 'f' } * start = 1 * end = 4 * => result = { 'a', 'd' , 'e', 'f', '0', '0', '0', '0' } (new array) *
* first = null * second = null * => result = null **
* first = { { ' a' } } * second = null * => result = { { ' a' } } **
* first = null * second = { { ' a' } } * => result = { { ' a' } } **
* first = { { ' b' } } * second = { { ' a' } } * => result = { { ' b' }, { ' a' } } **
* first = null * second = { 'a' } * => result = { { ' a' } } **
* first = { { ' a' } } * second = null * => result = { { ' a' } } **
* first = { { ' a' } } * second = { ' b' } * => result = { { ' a' } , { ' b' } } **
* For example: *
* array = null * prefix = null * => result = NullPointerException **
* array = { 'a', 'b', 'c', 'd', 'e' } * prefix = { 'a', 'b', 'c'} * => result = 0 **
* array = { 'a', 'b', 'c', 'd', 'e' } * prefix = { 'a', 'B', 'c'} * => result = 32 **
* array = { 'd', 'b', 'c', 'd', 'e' } * prefix = { 'a', 'b', 'c'} * => result = 3 **
* array = { 'a', 'b', 'c', 'd', 'e' } * prefix = { 'd', 'b', 'c'} * => result = -3 **
* array = { 'a', 'a', 'c', 'd', 'e' } * prefix = { 'a', 'e', 'c'} * => result = -4 **
* first = null * second = { 'a' } * => result = { ' a' } **
* first = { ' a' } * second = null * => result = { ' a' } **
* first = { ' a' } * second = { ' b' } * => result = { ' a' , ' b' } **
* first = null * second = { 'a' } * third = { 'b' } * => result = { ' a', 'b' } **
* first = { 'a' } * second = null * third = { 'b' } * => result = { ' a', 'b' } **
* first = { 'a' } * second = { 'b' } * third = null * => result = { ' a', 'b' } **
* first = null * second = null * third = null * => result = null **
* first = { 'a' } * second = { 'b' } * third = { 'c' } * => result = { 'a', 'b', 'c' } **
* first = null * second = { 'a' } * separator = '/' * => result = { ' a' } **
* first = { ' a' } * second = null * separator = '/' * => result = { ' a' } **
* first = { ' a' } * second = { ' b' } * separator = '/' * => result = { ' a' , '/', 'b' } **
* first = null * sep1 = '/' * second = { 'a' } * sep2 = ':' * third = { 'b' } * => result = { ' a' , ':', 'b' } **
* first = { 'a' } * sep1 = '/' * second = null * sep2 = ':' * third = { 'b' } * => result = { ' a' , '/', 'b' } **
* first = { 'a' } * sep1 = '/' * second = { 'b' } * sep2 = ':' * third = null * => result = { ' a' , '/', 'b' } **
* first = { 'a' } * sep1 = '/' * second = { 'b' } * sep2 = ':' * third = { 'c' } * => result = { ' a' , '/', 'b' , ':', 'c' } **
* prefix = 'a' * array = { 'b' } * suffix = 'c' * => result = { 'a', 'b' , 'c' } **
* prefix = 'a' * array = null * suffix = 'c' * => result = { 'a', 'c' } *
* name = { 'c' } * array = { { 'a' }, { 'b' } } * separator = '.' * => result = { 'a', '.', 'b' , '.', 'c' } **
* name = null * array = { { 'a' }, { 'b' } } * separator = '.' * => result = { 'a', '.', 'b' } *
* name = { ' c' } * array = null * separator = '.' * => result = { 'c' } *
* name = { 'c' } * array = { { 'a' }, { 'b' } } * separator = '.' * => result = { 'a', '.', 'b' , '.', 'c' } **
* name = null * array = { { 'a' }, { 'b' } } * separator = '.' * => result = { 'a', '.', 'b' } *
* name = { ' c' } * array = null * separator = '.' * => result = { 'c' } *
* array = { { 'a' }, { 'b' } } * separator = '.' * => result = { 'a', '.', 'b' } **
* array = null * separator = '.' * => result = { } *
* character = 'c' * array = { { ' a' }, { ' b' } } * result => false **
* character = 'a' * array = { { ' a' }, { ' b' } } * result => true **
* character = 'c' * array = { ' b' } * result => false **
* character = 'a' * array = { ' a' , ' b' } * result => true **
* characters = { 'c', 'd' } * array = { 'a', ' b' } * result => false **
* characters = { 'c', 'd' } * array = { 'a', ' b', 'c' } * result => true **
* array = { 'a', 'b', 'c', 'd' } * toBeFound = { 'b', 'c' } * result => false **
* array = { 'a', 'b', 'c' } * toBeFound = { 'b', 'c' } * result => true **
* first = null * second = null * result => true **
* first = { { } } * second = null * result => false **
* first = { { 'a' } } * second = { { 'a' } } * result => true **
* first = { { 'A' } } * second = { { 'a' } } * result => false **
* first = null * second = null * isCaseSensitive = true * result => true **
* first = { { } } * second = null * isCaseSensitive = true * result => false **
* first = { { 'A' } } * second = { { 'a' } } * isCaseSensitive = true * result => false **
* first = { { 'A' } } * second = { { 'a' } } * isCaseSensitive = false * result => true **
* first = null * second = null * result => true **
* first = { } * second = null * result => false **
* first = { 'a' } * second = { 'a' } * result => true **
* first = { 'a' } * second = { 'A' } * result => false **
* first = null * second = null * secondStart = 0 * secondEnd = 0 * result => true **
* first = { } * second = null * secondStart = 0 * secondEnd = 0 * result => false **
* first = { 'a' } * second = { 'a' } * secondStart = 0 * secondEnd = 1 * result => true **
* first = { 'a' } * second = { 'A' } * secondStart = 0 * secondEnd = 1 * result => false **
* first = null * second = null * isCaseSensitive = true * result => true **
* first = { } * second = null * isCaseSensitive = true * result => false **
* first = { 'A' } * second = { 'a' } * isCaseSensitive = true * result => false **
* first = { 'A' } * second = { 'a' } * isCaseSensitive = false * result => true **
* fragment = { 'b', 'c' , 'd' } * name = { 'a', 'b', 'c' , 'd' } * startIndex = 1 * isCaseSensitive = true * result => true **
* fragment = { 'b', 'c' , 'd' } * name = { 'a', 'b', 'C' , 'd' } * startIndex = 1 * isCaseSensitive = true * result => false **
* fragment = { 'b', 'c' , 'd' } * name = { 'a', 'b', 'C' , 'd' } * startIndex = 0 * isCaseSensitive = false * result => false **
* fragment = { 'b', 'c' , 'd' } * name = { 'a', 'b'} * startIndex = 0 * isCaseSensitive = true * result => false **
* c = ' ' * result => true **
* c = '\u3000' * result => false **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd' } * result => 2 **
* toBeFound = 'e' * array = { ' a', 'b', 'c', 'd' } * result => -1 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd' } * start = 2 * result => 2 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd' } * start = 3 * result => -1 **
* toBeFound = 'e' * array = { ' a', 'b', 'c', 'd' } * start = 1 * result => -1 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd' , 'c', 'e' } * result => 4 **
* toBeFound = 'e' * array = { ' a', 'b', 'c', 'd' } * result => -1 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd' } * startIndex = 2 * result => 2 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd', 'e' } * startIndex = 3 * result => -1 **
* toBeFound = 'e' * array = { ' a', 'b', 'c', 'd' } * startIndex = 0 * result => -1 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd' } * startIndex = 2 * endIndex = 2 * result => 2 **
* toBeFound = 'c' * array = { ' a', 'b', 'c', 'd', 'e' } * startIndex = 3 * endIndex = 4 * result => -1 **
* toBeFound = 'e' * array = { ' a', 'b', 'c', 'd' } * startIndex = 0 * endIndex = 3 * result => -1 **
* lastSegment("java.lang.Object".toCharArray(),'.') --> Object ** * @param array the array * @param separator the given separator * @return the last portion of a name given a separator * @throws NullPointerException if array is null */ final static public char[] lastSegment(char[] array, char separator) { int pos = lastIndexOf(separator, array); if (pos < 0) return array; return subarray(array, pos + 1, array.length); } /** * Answers true if the pattern matches the given name, false otherwise. This char[] pattern matching * accepts wild-cards '*' and '?'. * * When not case sensitive, the pattern is assumed to already be lowercased, the * name will be lowercased character per character as comparing. * If name is null, the answer is false. * If pattern is null, the answer is true if name is not null. *
* pattern = { '?', 'b', '*' } * name = { 'a', 'b', 'c' , 'd' } * isCaseSensitive = true * result => true **
* pattern = { '?', 'b', '?' } * name = { 'a', 'b', 'c' , 'd' } * isCaseSensitive = true * result => false **
* pattern = { 'b', '*' } * name = { 'a', 'b', 'c' , 'd' } * isCaseSensitive = true * result => false **
* pattern = { '?', 'b', '*' } * patternStart = 1 * patternEnd = 3 * name = { 'a', 'b', 'c' , 'd' } * nameStart = 1 * nameEnd = 4 * isCaseSensitive = true * result => true **
* pattern = { '?', 'b', '*' } * patternStart = 1 * patternEnd = 2 * name = { 'a', 'b', 'c' , 'd' } * nameStart = 1 * nameEnd = 2 * isCaseSensitive = true * result => false **
* toBeFound = 'b' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => 3 **
* toBeFound = 'c' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => 0 **
* toBeFound = 'b' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * start = 2 * result => 2 **
* toBeFound = 'c' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * start = 0 * result => 0 **
* prefix = { 'a' , 'b' } * name = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => true **
* prefix = { 'a' , 'c' } * name = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => false **
* prefix = { 'a' , 'B' } * name = { 'a' , 'b', 'b', 'a', 'b', 'a' } * isCaseSensitive = false * result => true **
* prefix = { 'a' , 'B' } * name = { 'a' , 'b', 'b', 'a', 'b', 'a' } * isCaseSensitive = true * result => false **
* array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * toBeReplaced = 'b' * replacementChar = 'a' * result => No returned value, but array is now equals to { 'a' , 'a', 'a', 'a', 'a', 'a' } **
* array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * toBeReplaced = 'c' * replacementChar = 'a' * result => No returned value, but array is now equals to { 'a' , 'b', 'b', 'a', 'b', 'a' } **
* array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * toBeReplaced = { 'b' } * replacementChar = { 'a', 'a' } * result => { 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a' } **
* array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * toBeReplaced = { 'c' } * replacementChar = { 'a' } * result => { 'a' , 'b', 'b', 'a', 'b', 'a' } **
* array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * toBeReplaced = 'b' * replacementChar = 'a' * result => A new array that is equals to { 'a' , 'a', 'a', 'a', 'a', 'a' } **
* array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * toBeReplaced = 'c' * replacementChar = 'a' * result => The original array that remains unchanged. **
* divider = 'b' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => { { 'a' }, { }, { 'a' }, { 'a' } } **
* divider = 'c' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => { { 'a', 'b', 'b', 'a', 'b', 'a' } } **
* divider = 'b' * array = { 'a' , ' ', 'b', 'b', 'a', 'b', 'a' } * result => { { 'a' }, { }, { 'a' }, { 'a' } } **
* divider = 'c' * array = { ' ', ' ', 'a' , 'b', 'b', 'a', 'b', 'a', ' ' } * result => { { 'a', 'b', 'b', 'a', 'b', 'a' } } **
* divider = 'b' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => { { 'a' }, { }, { 'a' }, { 'a' } } **
* divider = 'c' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * result => { { 'a', 'b', 'b', 'a', 'b', 'a' } } **
* divider = 'c' * array = { ' ', ' ', 'a' , 'b', 'b', 'a', 'b', 'a', ' ' } * result => { { ' ', 'a', 'b', 'b', 'a', 'b', 'a', ' ' } } **
* divider = 'b' * array = { 'a' , 'b', 'b', 'a', 'b', 'a' } * start = 2 * end = 5 * result => { { }, { 'a' }, { } } **
* array = { { 'a' } , { 'b' } } * start = 0 * end = 1 * result => { { 'a' } } **
* array = { { 'a' } , { 'b' } } * start = 0 * end = -1 * result => { { 'a' }, { 'b' } } **
* array = { 'a' , 'b' } * start = 0 * end = 1 * result => { 'a' } **
* array = { 'a', 'b' } * start = 0 * end = -1 * result => { 'a' , 'b' } **
* chars = { 'a' , 'b' } * result => { 'a' , 'b' } **
* array = { 'A', 'b' } * result => { 'a' , 'b' } **
* chars = { ' ', 'a' , 'b', ' ', ' ' } * result => { 'a' , 'b' } **
* array = { 'A', 'b' } * result => { 'A' , 'b' } **
* array = { { 'a' } , { 'b' } } * result => "a.b" **
* array = { { ' ', 'a' } , { 'b' } } * result => " a.b" **