1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-11 19:03:03 +00:00

Add pasting support to the standalone emulator

- Move paste normalisation code to StringUtil, so it can be shared by
   emulators.
 - Add paste support to the emulator.
This commit is contained in:
Jonathan Coates
2023-11-08 18:50:26 +00:00
parent 784e623776
commit 87345c6b2e
4 changed files with 54 additions and 33 deletions

View File

@@ -8,18 +8,48 @@ public final class StringUtil {
private StringUtil() {
}
public static String normaliseLabel(String label) {
var length = Math.min(32, label.length());
private static boolean isAllowed(char c) {
return (c >= ' ' && c <= '~') || (c >= 161 && c <= 172) || (c >= 174 && c <= 255);
}
private static String removeSpecialCharacters(String text, int length) {
var builder = new StringBuilder(length);
for (var i = 0; i < length; i++) {
var c = label.charAt(i);
if ((c >= ' ' && c <= '~') || (c >= 161 && c <= 172) || (c >= 174 && c <= 255)) {
builder.append(c);
} else {
builder.append('?');
}
var c = text.charAt(i);
builder.append(isAllowed(c) ? c : '?');
}
return builder.toString();
}
public static String normaliseLabel(String text) {
return removeSpecialCharacters(text, Math.min(32, text.length()));
}
/**
* Normalise a string from the clipboard, suitable for pasting into a computer.
* <p>
* This removes special characters and strips to the first line of text.
*
* @param clipboard The text from the clipboard.
* @return The normalised clipboard text.
*/
public static String normaliseClipboardString(String clipboard) {
// Clip to the first occurrence of \r or \n
var newLineIndex1 = clipboard.indexOf('\r');
var newLineIndex2 = clipboard.indexOf('\n');
int length;
if (newLineIndex1 >= 0 && newLineIndex2 >= 0) {
length = Math.min(newLineIndex1, newLineIndex2);
} else if (newLineIndex1 >= 0) {
length = newLineIndex1;
} else if (newLineIndex2 >= 0) {
length = newLineIndex2;
} else {
length = clipboard.length();
}
return removeSpecialCharacters(clipboard, Math.min(length, 512));
}
}