2024-06-28 03:01:21 +01:00
|
|
|
package cc.fascinated.bat.common;
|
|
|
|
|
2024-07-04 06:55:56 +01:00
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
|
2024-06-28 03:01:21 +01:00
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
2024-07-04 06:55:56 +01:00
|
|
|
@UtilityClass
|
2024-06-28 03:01:21 +01:00
|
|
|
public class StringUtils {
|
|
|
|
/**
|
|
|
|
* Generates a random string
|
|
|
|
*
|
|
|
|
* @param length the length of the string
|
|
|
|
* @return the random string
|
|
|
|
*/
|
|
|
|
public static String randomString(int length) {
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
stringBuilder.append((char) (Math.random() * 26 + 'a'));
|
|
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
2024-07-02 01:20:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes meta characters in a string
|
|
|
|
*
|
|
|
|
* @param inputString the input string
|
|
|
|
* @return the string with escaped meta characters
|
|
|
|
*/
|
|
|
|
public static String escapeMetaCharacters(String inputString){
|
|
|
|
final String[] metaCharacters = {"\\","^","$","{","}","[","]","(",")",".","*","+","?","|","<",">","-","&","%"};
|
|
|
|
|
|
|
|
for (String metaCharacter : metaCharacters) {
|
|
|
|
if (inputString.contains(metaCharacter)) {
|
|
|
|
inputString = inputString.replace(metaCharacter, "\\" + metaCharacter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputString;
|
|
|
|
}
|
2024-07-07 01:34:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats bytes into a human-readable format
|
|
|
|
*
|
|
|
|
* @param bytes the bytes
|
|
|
|
* @return the formatted bytes
|
|
|
|
*/
|
|
|
|
public static String formatBytes(long bytes) {
|
|
|
|
int unit = 1024;
|
|
|
|
if (bytes < unit) return bytes + " B";
|
|
|
|
int exp = (int) (Math.log(bytes) / Math.log(unit));
|
|
|
|
char pre = "KMGTPE".charAt(exp-1);
|
|
|
|
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
|
|
|
}
|
2024-06-28 03:01:21 +01:00
|
|
|
}
|