Disallow 'visually empty' profile names.

This commit is contained in:
Greyson Parrelli
2020-07-01 15:30:39 -04:00
parent b6b499d865
commit cb81a9f783
2 changed files with 35 additions and 1 deletions

View File

@@ -3,10 +3,17 @@ package org.thoughtcrime.securesms.util;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.collect.Sets;
import java.nio.charset.StandardCharsets;
import java.util.Set;
public final class StringUtil {
private static final Set<Character> WHITESPACE = Sets.newHashSet('\u200E', // left-to-right mark
'\u200F', // right-to-left mark
'\u2007'); // figure space
private StringUtil() {
}
@@ -28,4 +35,30 @@ public final class StringUtil {
return name;
}
/**
* @return True if the string is empty, or if it contains nothing but whitespace characters.
* Accounts for various unicode whitespace characters.
*/
public static boolean isVisuallyEmpty(@Nullable String value) {
if (value == null || value.length() == 0) {
return true;
}
for (int i = 0; i < value.length(); i++) {
if (!isVisuallyEmpty(value.charAt(i))) {
return false;
}
}
return true;
}
/**
* @return True if the character is invisible or whitespace. Accounts for various unicode
* whitespace characters.
*/
public static boolean isVisuallyEmpty(char c) {
return Character.isWhitespace(c) || WHITESPACE.contains(c);
}
}