Java Wildcard wildcardCompare(String s, String z)

Here you can find the source of wildcardCompare(String s, String z)

Description

wildcard Compare

License

Open Source License

Declaration

public static int wildcardCompare(String s, String z) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  . j ava2 s.com*/
 * Copyright (c) Davide Raccagni (2006, 2009). All Rights Reserved.
 * 
 * Licensed 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.
 */

public class Main {
    public static int wildcardCompare(String s, String z) {
        if (s.equals("*") || z.equals("*")) {
            return 0;
        }

        int l = s.length();
        int m = z.length();

        int i = 0;
        int j = 0;
        while (j < m && i < l) {
            char c = s.charAt(i);
            char k = z.charAt(j);

            if (c != '*' && k != '*') {
                if (k != c) {
                    return ((int) k) - ((int) c) > 0 ? 1 : -1;
                }

                i++;
                j++;
            } else if (c == '*') {
                if (i == l - 1) {
                    // s ends w/ a *
                    return 0;
                } else if (i < l - 2 && s.charAt(i + 1) == k) {
                    i += 2;
                }

                j++;
            } else { // if (k == '*') {
                if (j == m - 1) {
                    // z ends w/ a *
                    return 0;
                } else if (j < m - 2 && z.charAt(j + 1) == c) {
                    j += 2;
                }

                i++;
            }
        }

        return i == l && j == m ? 0 : (i < l ? -1 : 1);
    }
}

Related

  1. wildCard(final String _name)
  2. wildCard(String _name)
  3. wildCardBoth(String s)
  4. wildcardCriterionSQL(final String query)
  5. wildcardIfEmpty(String inString)
  6. wildcardOrIsNullIfEmpty(String column, String inString)
  7. wildcardSearchToRegex(final String wildcard)