Преглед на файлове

refined normalize Method, fxied Tests for it:
* all unicode spaces are now replaced with \u0020
* if the end string only contains whitspaces, "_" will be returned

Armin Schrenk преди 4 години
родител
ревизия
510ea8a6f6

+ 16 - 9
main/commons/src/main/java/org/cryptomator/common/settings/VaultSettings.java

@@ -79,18 +79,25 @@ public class VaultSettings {
 	String normalizeDisplayName() {
 		StringBuilder builder = new StringBuilder();
 		Set<Integer> notAllowedCharacters = "<>:\"/\\|?*".chars().boxed().collect(Collectors.toUnmodifiableSet());
+
 		if (displayName.isEmpty().get() || displayName.getValueSafe().equals(".") || displayName.getValueSafe().equals("..")) {
-			builder.append("_");
+			return builder.append("_").toString();
+		}
+
+		displayName.get().codePoints().forEach(codePoint -> {
+			if (Character.isDefined(codePoint) && !Character.isIdentifierIgnorable(codePoint) && !notAllowedCharacters.contains(codePoint)) {
+				builder.appendCodePoint(Character.isWhitespace(codePoint) || Character.isSpaceChar(codePoint) ? 0x020 : codePoint);
+			} else {
+				builder.append("_");
+			}
+		});
+
+		String result = builder.toString();
+		if (!result.isBlank()) {
+			return result;
 		} else {
-			displayName.get().codePoints().forEach(codePoint -> {
-				if (Character.isDefined(codePoint) && !Character.isIdentifierIgnorable(codePoint) && !notAllowedCharacters.contains(codePoint)) {
-					builder.appendCodePoint(codePoint);
-				} else {
-					builder.append("_");
-				}
-			});
+			return "_";
 		}
-		return builder.toString();
 	}
 
 	/* Getter/Setter */

+ 1 - 1
main/commons/src/test/java/org/cryptomator/common/settings/VaultSettingsTest.java

@@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 public class VaultSettingsTest {
 
 	@ParameterizedTest
-	@CsvSource({"a a,a_a", "ä,a", "Ĉ,C", ":,_", "汉语,_"})
+	@CsvSource({"a\u000Fa,a_a", ": \\,_ _", "汉语,汉语", "..,_", "a\ta,a\u0020a", "\t\n\r,_"})
 	public void testNormalize(String test, String expected) {
 		VaultSettings settings = new VaultSettings("id");
 		settings.displayName().setValue(test);