UnicodeBlock Identifies Script Ranges

Classify characters with Character.UnicodeBlock so a validator can reject unexpected scripts instead of matching ad-hoc code point ranges.

JavaSecurity Boundaries

Code

snippet
import java.lang.Character.UnicodeBlock;
import java.util.Set;

public class ScriptGate {

    static final Set<UnicodeBlock> ALLOWED = Set.of(
            UnicodeBlock.BASIC_LATIN,
            UnicodeBlock.LATIN_1_SUPPLEMENT,
            UnicodeBlock.GENERAL_PUNCTUATION);

    static String review(String text) {
        for (int i = 0; i < text.length(); ) {
            int cp = text.codePointAt(i);
            i += Character.charCount(cp);
            UnicodeBlock block = UnicodeBlock.of(cp);
            if (block == null || !ALLOWED.contains(block)) {
                return "reject: U+" + Integer.toHexString(cp).toUpperCase()
                        + " belongs to " + block;
            }
        }
        return "accept: all characters in allowed blocks";
    }

    public static void main(String[] args) {
        System.out.println(review("invoice #42"));
        System.out.println(review("facture n°42"));
        System.out.println(review("发票 #42"));
    }
}

How the snippet works

UnicodeBlock Identifies Script Ranges reduces to the class ScriptGate and the functions review and main.

The cycling part of this snippet is for (int i = 0; i < text.length(); ).

What to watch for

  • Nothing arrives from a registry; the language ships every API used.

Using it

Entry signature: static String review(String text)

main calls 1 helper in sequence: review.

Key statements

return "accept: all characters in allowed blocks";
i += Character.charCount(cp);
return "reject: U+" + Integer.toHexString(cp).toUpperCase()
        + " belongs to " + block;

Interactive Tools for This Topic

FAQ

How do you run UnicodeBlock Identifies Script Ranges?
The runtime alone is enough: execute the file as a complete program.