2 * Copyright (c) 2019-2020. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.scanner;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
23 import org.hedgecode.chess.scanner.format.PGNConstants;
28 * @author Dmitry Samoshin aka gotty
30 public final class StringUtils {
32 private static final String EMPTY = "";
33 private static final char BACKSLASH = '\\';
35 private static final String CRLF_REGEX = "(\\\\r)?\\\\n";
36 private static final String SHIELD_REGEX = "\\\\";
38 private static final String DOMAIN_REGEX_FORMAT = "(\\w+\\.)*\\b%s";
40 public static String match(String source, String regex) {
41 Matcher matcher = Pattern.compile(
46 return matcher.groupCount() > 0
53 public static boolean isPgn(String source) {
56 PGNConstants.PGN_DETECT_REGEX
60 public static String shield(String source, char[] shields) {
61 for (char shield : shields) {
62 if (source.indexOf(shield) >= 0) {
66 : String.valueOf(shield);
67 source = source.replaceAll(
68 String.format("([%s])", regexShield),
69 SHIELD_REGEX.concat("$1")
76 public static String unshield(String source) {
77 return source.replaceAll(SHIELD_REGEX, EMPTY);
80 public static String formatCrlf(String source) {
83 CRLF_REGEX, PGNConstants.PGN_CRLF
88 public static boolean belongDomain(String domain, URL url) {
94 public static boolean belongDomain(String domain, String host) {
96 String.format(DOMAIN_REGEX_FORMAT, domain)
100 private StringUtils() {
101 throw new AssertionError(
102 String.format("No %s instances!", getClass().getName())