Detect Mixed Scripts With unicode.In RangeTables
Inspect identifier runes against unicode.Latin, unicode.Cyrillic, and custom Range16 tables via unicode.In to block mixed-script spoofing attempts.
Code
package main
import (
"fmt"
"unicode"
)
// Custom Range16 table (entries must be sorted ascending by Lo)
var slugExtra = &unicode.RangeTable{
R16: []unicode.Range16{
{Lo: '-', Hi: '-', Stride: 1},
{Lo: '0', Hi: '9', Stride: 1},
{Lo: '_', Hi: '_', Stride: 1},
},
LatinOffset: 3,
}
func auditSlug(s string) string {
var hasLatin, hasCyrillic, hasDisallowed bool
for _, r := range s {
if unicode.In(r, unicode.Latin) {
hasLatin = true
} else if unicode.In(r, unicode.Cyrillic) {
hasCyrillic = true
} else if !unicode.In(r, slugExtra) {
hasDisallowed = true
}
}
if hasLatin && hasCyrillic {
return "blocked:mixed-latin-cyrillic"
}
if hasCyrillic || hasDisallowed {
return "blocked:outside-allowlist"
}
return "allowed"
}
func main() {
samples := []string{
"api-gateway_v2",
"café-menu",
"p\u0430ypal-billing", // U+0430 is Cyrillic 'а'
"\u0442\u0435\u0441\u0442",
"docs/internal",
}
for _, s := range samples {
fmt.Printf("%-20q -> %s\n", s, auditSlug(s))
}
}Execution summary — Detect Mixed Scripts With unicode.In RangeTables
Detect Mixed Scripts With unicode.In RangeTables is built around the functions auditSlug and main.
Iteration happens inside for _, r := range s — everything outside it runs once.
Points of care
- No external package tool enters; every single operation targets core platform built-in APIs.
Putting it to use
Signature: func auditSlug(s string) string
main sequences auditSlug; executing the file runs that flow once.
Core lines
for _, s := range samples {
fmt.Printf("%-20q -> %s\n", s, auditSlug(s))
}
for _, r := range s {
if unicode.In(r, unicode.Latin) {
hasLatin = true
} else if unicode.In(r, unicode.Cyrillic) {
hasCyrillic = true
} else if !unicode.In(r, slugExtra) {
hasDisallowed = true
}
}
return "blocked:mixed-latin-cyrillic"
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.
FAQ
- How do you run Detect Mixed Scripts With unicode.In RangeTables?
- Run it as a complete program — compile or interpret the file directly; there is nothing to install first.
Report a problem with this exampleOpens a pre-filled issue on GitHub.