Files
archived-hifumi/src/main/java/io/github/redpanda4552/HifumiBot/util/Strings.java

61 lines
2.2 KiB
Java

/**
* This file is part of HifumiBot, licensed under the MIT License (MIT)
*
* Copyright (c) 2020 RedPanda4552 (https://github.com/RedPanda4552)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.redpanda4552.HifumiBot.util;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Strings {
private static final Pattern URL_PATTERN = Pattern.compile("((?:https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
public static String unescapeNewlines(String input) {
return input.replace("\\n", "\n");
}
public static ArrayList<String> extractUrls(String input) {
ArrayList<String> urls = new ArrayList<String>();
Matcher m = URL_PATTERN.matcher(input);
while (m.find()) {
String url = m.group();
if (url != null) {
urls.add(url);
}
}
return urls;
}
public static String getEnvVarOrPanic(String envVar) {
var value = System.getenv().getOrDefault(envVar, null);
if (value == null) {
throw new RuntimeException(String.format("Did not provide env-var: '%s'", envVar));
}
return value;
}
}