/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/**
* Various string manipulation methods that are more efficient then chaining
* string operations: all is done in the same buffer without creating a bunch of
* string objects.
*
* @author Dungeon Project
*/
public class Main {
/**
*
* Removes spaces (char <= 32) from start of this array, handling
* null
by returning null
.
*
* Trim removes start characters <= 32.
*
*
* StringUtils.trimLeft(null) = null
* StringUtils.trimLeft("") = ""
* StringUtils.trimLeft(" ") = ""
* StringUtils.trimLeft("abc") = "abc"
* StringUtils.trimLeft(" abc ") = "abc "
*
*
* @param bytes
* the byte array to be trimmed, may be null
* @return the position of the first byte which is not a space, or the last
* position of the array.
*/
public static final int trimLeft( byte[] bytes, int pos )
{
if ( bytes == null )
{
return pos;
}
while ( ( pos < bytes.length ) && ( bytes[pos] == ' ' ) )
{
pos++;
}
return pos;
}
/**
*
* Removes spaces (char <= 32) from end of this array, handling
* null
by returning null
.
*
* Trim removes start characters <= 32.
*
*
* StringUtils.trimRight(null) = null
* StringUtils.trimRight("") = ""
* StringUtils.trimRight(" ") = ""
* StringUtils.trimRight("abc") = "abc"
* StringUtils.trimRight(" abc ") = " abc"
*
*
* @param bytes
* the chars array to be trimmed, may be null
* @return the position of the first char which is not a space, or the last
* position of the array.
*/
public static final int trimRight( byte[] bytes, int pos )
{
if ( bytes == null )
{
return pos;
}
while ( ( pos >= 0 ) && ( bytes[pos] == ' ' ) )
{
pos--;
}
return pos;
}
/**
* Checks if a bytes array is empty or null.
*
* @param bytes
* The bytes array to check, may be null
* @return true
if the bytes array is empty or null
*/
public static final boolean isEmpty( byte[] bytes )
{
return bytes == null || bytes.length == 0;
}
}