001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.util;
004:
005: /**
006: * Various character utilities.
007: */
008: public class CharUtil {
009:
010: // ---------------------------------------------------------------- conversions
011:
012: /**
013: * Converts char array into byte array. Chars are truncated to byte size.
014: */
015: public static byte[] toByteArray(char[] carr) {
016: if (carr == null) {
017: return null;
018: }
019: byte[] barr = new byte[carr.length];
020: for (int i = 0; i < carr.length; i++) {
021: barr[i] = (byte) carr[i];
022: }
023: return barr;
024: }
025:
026: /**
027: * Converts byte array to char array.
028: */
029: public static char[] toCharArray(byte[] barr) {
030: if (barr == null) {
031: return null;
032: }
033: char[] carr = new char[barr.length];
034: for (int i = 0; i < barr.length; i++) {
035: carr[i] = (char) barr[i];
036: }
037: return carr;
038: }
039:
040: // ---------------------------------------------------------------- find
041:
042: /**
043: * Match if one character equals to any of the given character.
044: *
045: * @return <code>true</code> if characters match any chararacter from given array,
046: * otherwise <code>false</code>
047: */
048: public static boolean equalsOne(char c, char[] match) {
049: for (char aMatch : match) {
050: if (c == aMatch) {
051: return true;
052: }
053: }
054: return false;
055: }
056:
057: /**
058: * Finds index of the first character in given array the matches any from the
059: * given set of characters.
060: *
061: * @return index of matched character or -1
062: */
063: public static int findFirstEqual(char[] source, int index,
064: char[] match) {
065: for (int i = index; i < source.length; i++) {
066: if (equalsOne(source[i], match) == true) {
067: return i;
068: }
069: }
070: return -1;
071: }
072:
073: /**
074: * Finds index of the first character in given array the matches any from the
075: * given set of characters.
076: *
077: * @return index of matched character or -1
078: */
079: public static int findFirstEqual(char[] source, int index,
080: char match) {
081: for (int i = index; i < source.length; i++) {
082: if (source[i] == match) {
083: return i;
084: }
085: }
086: return -1;
087: }
088:
089: /**
090: * Finds index of the first character in given array the differes from the
091: * given set of characters.
092: *
093: * @return index of matched character or -1
094: */
095: public static int findFirstDiff(char[] source, int index,
096: char[] match) {
097: for (int i = index; i < source.length; i++) {
098: if (equalsOne(source[i], match) == false) {
099: return i;
100: }
101: }
102: return -1;
103: }
104:
105: /**
106: * Finds index of the first character in given array the differes from the
107: * given set of characters.
108: *
109: * @return index of matched character or -1
110: */
111: public static int findFirstDiff(char[] source, int index, char match) {
112: for (int i = index; i < source.length; i++) {
113: if (source[i] != match) {
114: return i;
115: }
116: }
117: return -1;
118: }
119:
120: // ---------------------------------------------------------------- is
121:
122: /**
123: * Returns <code>true</code> if specified character is lowercase ascii.
124: * If user uses only ASCIIs, it is much much faster.
125: */
126: public static boolean isLowerAscii(char c) {
127: return (c >= 'a') && (c <= 'z');
128: }
129:
130: /**
131: * Returns <code>true</code> if specified character is uppercase ascii.
132: * If user uses only ASCIIs, it is much much faster.
133: */
134: public static boolean isUpperAscii(char c) {
135: return (c >= 'A') && (c <= 'Z');
136: }
137:
138: /**
139: * Uppers lowercase ascii char.
140: */
141: public static char toUpperAscii(char c) {
142: if ((c >= 'a') && (c <= 'z')) {
143: c -= (char) 0x20;
144: }
145: return c;
146: }
147:
148: /**
149: * Lowers upercase ascii char.
150: */
151: public static char toLowerAscii(char c) {
152: if ((c >= 'A') && (c <= 'Z')) {
153: c += (char) 0x20;
154: }
155: return c;
156: }
157:
158: }
|