001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.archive.util;
019:
020: public class Util {
021:
022: public static boolean ASCIIIgnoreCaseRegionMatches(String s1,
023: int start1, String s2, int start2, int length) {
024:
025: if (s1 != null && s2 != null) {
026: if (start1 < 0 || length > s1.length() - start1) {
027: return false;
028: }
029: if (start2 < 0 || length > s2.length() - start2) {
030: return false;
031: }
032:
033: s1 = s1.substring(start1, start1 + length);
034: s2 = s2.substring(start2, start2 + length);
035:
036: return toASCIILowerCase(s1).equals(toASCIILowerCase(s2));
037: }
038: throw new NullPointerException();
039: }
040:
041: public static String toASCIILowerCase(String s) {
042: int len = s.length();
043: StringBuilder buffer = new StringBuilder(len);
044: for (int i = 0; i < len; i++) {
045: char c = s.charAt(i);
046: if ('A' <= c && c <= 'Z') {
047: buffer.append((char) (c + ('a' - 'A')));
048: } else {
049: buffer.append(c);
050: }
051: }
052: return buffer.toString();
053: }
054:
055: public static String toASCIIUpperCase(String s) {
056: int len = s.length();
057: StringBuilder buffer = new StringBuilder(len);
058: for (int i = 0; i < len; i++) {
059: char c = s.charAt(i);
060: if ('a' <= c && c <= 'z') {
061: buffer.append((char) (c - ('a' - 'A')));
062: } else {
063: buffer.append(c);
064: }
065: }
066: return buffer.toString();
067: }
068:
069: public static final boolean equalsIgnoreCase(String s1, String s2) {
070: if (s1 == s2) {
071: return true;
072: }
073:
074: if (s1 == null || s2 == null || s1.length() != s2.length()) {
075: return false;
076: }
077:
078: char c1, c2;
079:
080: for (int i = 0; i < s1.length(); i++) {
081: if ((c1 = s1.charAt(i)) != (c2 = s2.charAt(i))
082: && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) {
083: return false;
084: }
085: }
086: return true;
087: }
088:
089: public static final char toASCIILowerCase(char c) {
090: if ('A' <= c && c <= 'Z') {
091: return (char) (c + ('a' - 'A'));
092: }
093: return c;
094: }
095:
096: public static final char toASCIIUpperCase(char c) {
097: if ('a' <= c && c <= 'z') {
098: return (char) (c - ('a' - 'A'));
099: }
100: return c;
101: }
102: }
|