001/**
002 * The MIT License (MIT)
003 *
004 * Copyright (c) 2017 tools4j.org (Marco Terzer)
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in all
014 * copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022 * SOFTWARE.
023 */
024package org.tools4j.spockito;
025
026import java.util.regex.Pattern;
027
028/**
029 * Contains static utility methods dealing with strings.
030 */
031final class Strings {
032
033    static final Pattern UNESCAPED_COMMA = Pattern.compile("(?<=[^\\\\]),");
034    static final Pattern UNESCAPED_SEMICOLON = Pattern.compile("(?<=[^\\\\]);");
035    static final Pattern UNESCAPED_COLON = Pattern.compile("(?<=[^\\\\]):");
036    static final Pattern UNESCAPED_EQUAL = Pattern.compile("(?<=[^\\\\])=");
037
038    static String firstCharToUpperCase(final String value) {
039        return value.length() > 0 ? Character.toUpperCase(value.charAt(0)) + value.substring(1) : value;
040    }
041
042    static String removeSurroundingPipes(final String s) {
043        return removeStartAndEndChars(s, '|', '|');
044    }
045
046    static String removeStartAndEndChars(final String s, final char startQuoteChar, final char endQuoteChar) {
047        final int len = s.length();
048        if (len >= 2 && s.charAt(0) == startQuoteChar && s.charAt(len - 1) == endQuoteChar) {
049            return s.substring(1, len - 1);
050        }
051        return s;
052    }
053
054    static boolean allCharsMatchingAnyOf(final String s, final char ch1, final char ch2) {
055        final int len = s.length();
056        for (int i = 0; i < len; i++) {
057            if (s.charAt(i) != ch1 && s.charAt(i) != ch2) {
058                return false;
059            }
060        }
061        return true;
062    }
063
064    static String unescape(final String s) {
065        return unescape(s, ',', ';', '|', '=', '\\','\'');
066    }
067
068    static String unescape(final String s, final char... escapedChars) {
069        int index = s.indexOf('\\');
070        if (index < 0) {
071            return s;
072        }
073        final StringBuilder sb = new StringBuilder(s);
074        while (index >= 0 && index + 1 < sb.length()) {
075            final char ch = sb.charAt(index + 1);
076            if (isCharAnyOf(ch, escapedChars)) {
077                sb.delete(index, index + 1);
078                index = sb.indexOf("\\", index + 1);
079            } else {
080                index = sb.indexOf("\\", index + 2);
081            }
082        }
083        return s.length() == sb.length() ? s : sb.toString();
084    }
085
086    static boolean isCharAnyOf(final char ch, final char... chars) {
087        for (final char c : chars) {
088            if (ch == c) {
089                return true;
090            }
091        }
092        return false;
093    }
094
095    private Strings() {
096        throw new RuntimeException("No Strings for you!");
097    }
098}