Skip to content

Commit

Permalink
add example for validating an identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
smasher164 committed Oct 23, 2020
1 parent 9c5ab49 commit 6ce40ef
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions xid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xid
import (
"bufio"
"compress/gzip"
"fmt"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -57,3 +58,47 @@ func TestExhaustive(t *testing.T) {
})
}
}

func Example() {
isIdent := func(input string) bool {
// identifier should be non-empty
if input == "" {
return false
}
for i, r := range input {
if i == 0 {
// first rune should be in xid_start
if !Start(r) {
return false
}
} else {
// other runes should be in xid_continue
if !Continue(r) {
return false
}
}
}
return true
}
fmt.Println(isIdent("index"))
fmt.Println(isIdent("snake_case"))
fmt.Println(isIdent("Δx"))
fmt.Println(isIdent("xₒ"))
fmt.Println(isIdent("ä"))
fmt.Println(isIdent("aᵢ"))
fmt.Println(isIdent("मूलधन"))
fmt.Println(isIdent("kebab-case"))
fmt.Println(isIdent("3x"))
fmt.Println(isIdent("_id"))
// Output:
// true
// true
// true
// true
// true
// true
// true
// false
// false
// false
}

0 comments on commit 6ce40ef

Please sign in to comment.