Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
smasher164 committed Oct 23, 2020
0 parents commit af2d488
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.14.x, 1.15.x]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Ignore everything
*
# Whitelist subdirectories
!*/
# Whitelist the following files and extensions
!*.yml
!*.gz
!.gitignore
!go.mod
!go.sum
!*.md
!*.go
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2020 Akhil Indurti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# xid

[![PkgGoDev](https://pkg.go.dev/badge/github.com/smasher164/xid)](https://pkg.go.dev/github.com/smasher164/xid)
![Test](https://github.com/smasher164/xid/workflows/Test/badge.svg)

Package xid implements validation functions for unicode identifiers,
as defined in UAX#31: https://unicode.org/reports/tr31/.
The syntax for an identifier is:

<identifier> := <xid_start> <xid_continue>*

where `<xid_start>` and `<xid_continue>` derive from `<id_start>` and
`<id_continue>`, respectively, and check their NFKC normalized forms.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/smasher164/xid

go 1.15

require golang.org/x/text v0.3.3
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Binary file added testdata/xid_continue12.1.0.txt.gz
Binary file not shown.
Binary file added testdata/xid_start12.1.0.txt.gz
Binary file not shown.
5 changes: 5 additions & 0 deletions unicodeTestVersion_114.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build go1.14

package xid

const unicodeTestVersion = "12.1.0"
84 changes: 84 additions & 0 deletions xid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Package xid implements validation functions for unicode identifiers,
// as defined in UAX#31: https://unicode.org/reports/tr31/.
// The syntax for an identifier is:
//
// <identifier> := <xid_start> <xid_continue>*
//
// where <xid_start> and <xid_continue> derive from <id_start> and
// <id_continue>, respectively, and check their NFKC normalized forms.
package xid

import (
"unicode"

"golang.org/x/text/unicode/norm"
)

type set func(rune) bool

func (a set) add(rt *unicode.RangeTable) set {
b := in(rt)
return func(r rune) bool { return a(r) || b(r) }
}

func (a set) sub(rt *unicode.RangeTable) set {
b := in(rt)
return func(r rune) bool { return a(r) && !b(r) }
}

func in(rt *unicode.RangeTable) set {
return func(r rune) bool { return unicode.Is(rt, r) }
}

var id_start = set(unicode.IsLetter).
add(unicode.Nl).
add(unicode.Other_ID_Start).
sub(unicode.Pattern_Syntax).
sub(unicode.Pattern_White_Space)

var id_continue = id_start.
add(unicode.Mn).
add(unicode.Mc).
add(unicode.Nd).
add(unicode.Pc).
add(unicode.Other_ID_Continue).
sub(unicode.Pattern_Syntax).
sub(unicode.Pattern_White_Space)

// Start checks that the rune begins an identifier.
func Start(r rune) bool {
// id_start(r) && NFKC(r) in "id_start xid_continue*"
if !id_start(r) {
return false
}
s := norm.NFKC.String(string(r))
if s == "" {
return false
}
for i, r := range s {
if i == 0 {
if !id_start(r) {
return false
}
} else {
if !Continue(r) {
return false
}
}
}
return true
}

// Continue checks that the rune continues an identifier.
func Continue(r rune) bool {
// id_continue(r) && NFKC(r) in "id_continue*"
if !id_continue(r) {
return false
}
for _, r := range norm.NFKC.String(string(r)) {
if !id_continue(r) {
return false
}
}
return true
}
59 changes: 59 additions & 0 deletions xid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package xid

import (
"bufio"
"compress/gzip"
"os"
"path/filepath"
"strconv"
"testing"
"unicode"
)

func readMatches(file string) ([]bool, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
rdr, err := gzip.NewReader(f)
if err != nil {
return nil, err
}
defer rdr.Close()
scanner := bufio.NewScanner(rdr)
scanner.Split(bufio.ScanBytes)
var matches []bool
for scanner.Scan() {
b, err := strconv.ParseBool(scanner.Text())
if err != nil {
return nil, err
}
matches = append(matches, b)
}
return matches, nil
}

func TestExhaustive(t *testing.T) {
cases := []struct {
class string
f func(rune) bool
}{
{"xid_start", Start},
{"xid_continue", Continue},
}
for _, c := range cases {
t.Run(c.class, func(t *testing.T) {
matches, err := readMatches(filepath.Join("testdata", c.class+unicodeTestVersion+".txt.gz"))
if err != nil {
t.Fatal(err)
}
for r := rune(0); r <= unicode.MaxRune; r++ {
want := matches[r]
if got := c.f(r); got != want {
t.Fatalf("%s(%s)=%v, got=%v", c.class, strconv.QuoteRuneToASCII(r), want, got)
}
}
})
}
}

0 comments on commit af2d488

Please sign in to comment.