-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from hermannm/remove-dependencies
remove redundant dependencies
- Loading branch information
Showing
6 changed files
with
204 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package jsoncolor | ||
|
||
import "unsafe" | ||
|
||
// asciiValid returns true if b contains only ASCII characters. | ||
// | ||
// From https://github.com/segmentio/encoding/blob/v0.1.14/ascii/valid.go#L28 | ||
// | ||
//go:nosplit | ||
func asciiValid(b []byte) bool { | ||
s, n := unsafe.Pointer(&b), uintptr(len(b)) | ||
|
||
i := uintptr(0) | ||
p := *(*unsafe.Pointer)(s) | ||
|
||
for n >= 8 { | ||
if ((*(*uint64)(unsafe.Pointer(uintptr(p) + i))) & 0x8080808080808080) != 0 { | ||
return false | ||
} | ||
i += 8 | ||
n -= 8 | ||
} | ||
|
||
if n >= 4 { | ||
if ((*(*uint32)(unsafe.Pointer(uintptr(p) + i))) & 0x80808080) != 0 { | ||
return false | ||
} | ||
i += 4 | ||
n -= 4 | ||
} | ||
|
||
var x uint32 | ||
switch n { | ||
case 3: | ||
x = uint32(*(*uint8)(unsafe.Pointer(uintptr(p) + i))) | uint32(*(*uint16)(unsafe.Pointer(uintptr(p) + i + 1)))<<8 | ||
case 2: | ||
x = uint32(*(*uint16)(unsafe.Pointer(uintptr(p) + i))) | ||
case 1: | ||
x = uint32(*(*uint8)(unsafe.Pointer(uintptr(p) + i))) | ||
default: | ||
return true | ||
} | ||
return (x & 0x80808080) == 0 | ||
} | ||
|
||
// asciiValidPrint returns true if b contains only printable ASCII characters. | ||
// | ||
// From https://github.com/segmentio/encoding/blob/v0.1.14/ascii/valid.go#L83 | ||
// | ||
//go:nosplit | ||
func asciiValidPrint(b []byte) bool { | ||
s, n := unsafe.Pointer(&b), uintptr(len(b)) | ||
|
||
if n == 0 { | ||
return true | ||
} | ||
|
||
i := uintptr(0) | ||
p := *(*unsafe.Pointer)(s) | ||
|
||
for (n - i) >= 8 { | ||
x := *(*uint64)(unsafe.Pointer(uintptr(p) + i)) | ||
if hasLess64(x, 0x20) || hasMore64(x, 0x7e) { | ||
return false | ||
} | ||
i += 8 | ||
} | ||
|
||
if (n - i) >= 4 { | ||
x := *(*uint32)(unsafe.Pointer(uintptr(p) + i)) | ||
if hasLess32(x, 0x20) || hasMore32(x, 0x7e) { | ||
return false | ||
} | ||
i += 4 | ||
} | ||
|
||
var x uint32 | ||
switch n - i { | ||
case 3: | ||
x = 0x20000000 | uint32(*(*uint8)(unsafe.Pointer(uintptr(p) + i))) | uint32(*(*uint16)(unsafe.Pointer(uintptr(p) + i + 1)))<<8 | ||
case 2: | ||
x = 0x20200000 | uint32(*(*uint16)(unsafe.Pointer(uintptr(p) + i))) | ||
case 1: | ||
x = 0x20202000 | uint32(*(*uint8)(unsafe.Pointer(uintptr(p) + i))) | ||
default: | ||
return true | ||
} | ||
return !(hasLess32(x, 0x20) || hasMore32(x, 0x7e)) | ||
} | ||
|
||
// https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord | ||
const ( | ||
hasLessConstL64 = (^uint64(0)) / 255 | ||
hasLessConstR64 = hasLessConstL64 * 128 | ||
|
||
hasLessConstL32 = (^uint32(0)) / 255 | ||
hasLessConstR32 = hasLessConstL32 * 128 | ||
|
||
hasMoreConstL64 = (^uint64(0)) / 255 | ||
hasMoreConstR64 = hasMoreConstL64 * 128 | ||
|
||
hasMoreConstL32 = (^uint32(0)) / 255 | ||
hasMoreConstR32 = hasMoreConstL32 * 128 | ||
) | ||
|
||
//go:nosplit | ||
func hasLess64(x, n uint64) bool { | ||
return ((x - (hasLessConstL64 * n)) & ^x & hasLessConstR64) != 0 | ||
} | ||
|
||
//go:nosplit | ||
func hasLess32(x, n uint32) bool { | ||
return ((x - (hasLessConstL32 * n)) & ^x & hasLessConstR32) != 0 | ||
} | ||
|
||
//go:nosplit | ||
func hasMore64(x, n uint64) bool { | ||
return (((x + (hasMoreConstL64 * (127 - n))) | x) & hasMoreConstR64) != 0 | ||
} | ||
|
||
//go:nosplit | ||
func hasMore32(x, n uint32) bool { | ||
return (((x + (hasMoreConstL32 * (127 - n))) | x) & hasMoreConstR32) != 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package jsoncolor | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// Based on https://github.com/segmentio/encoding/blob/v0.1.14/ascii/valid_test.go | ||
var testCases = [...]struct { | ||
valid bool | ||
validPrint bool | ||
str string | ||
}{ | ||
{valid: true, validPrint: true, str: ""}, | ||
{valid: true, validPrint: true, str: "hello"}, | ||
{valid: true, validPrint: true, str: "Hello World!"}, | ||
{valid: true, validPrint: true, str: "Hello\"World!"}, | ||
{valid: true, validPrint: true, str: "Hello\\World!"}, | ||
{valid: true, validPrint: false, str: "Hello\nWorld!"}, | ||
{valid: true, validPrint: false, str: "Hello\rWorld!"}, | ||
{valid: true, validPrint: false, str: "Hello\tWorld!"}, | ||
{valid: true, validPrint: false, str: "Hello\bWorld!"}, | ||
{valid: true, validPrint: false, str: "Hello\fWorld!"}, | ||
{valid: true, validPrint: true, str: "H~llo World!"}, | ||
{valid: true, validPrint: true, str: "H~llo"}, | ||
{valid: false, validPrint: false, str: "你好"}, | ||
{valid: true, validPrint: true, str: "~"}, | ||
{valid: false, validPrint: false, str: "\x80"}, | ||
{valid: true, validPrint: false, str: "\x7F"}, | ||
{valid: false, validPrint: false, str: "\xFF"}, | ||
{valid: true, validPrint: true, str: "some kind of long string with only ascii characters."}, | ||
{valid: false, validPrint: false, str: "some kind of long string with a non-ascii character at the end.\xff"}, | ||
{valid: true, validPrint: true, str: strings.Repeat("1234567890", 1000)}, | ||
} | ||
|
||
func TestAsciiValid(t *testing.T) { | ||
for _, tc := range testCases { | ||
t.Run(limit(tc.str), func(t *testing.T) { | ||
expect := tc.validPrint | ||
|
||
if valid := asciiValidPrint([]byte(tc.str)); expect != valid { | ||
t.Errorf("expected %t but got %t", expect, valid) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAsciiValidPrint(t *testing.T) { | ||
for _, tc := range testCases { | ||
t.Run(limit(tc.str), func(t *testing.T) { | ||
expect := tc.validPrint | ||
|
||
if valid := asciiValidPrint([]byte(tc.str)); expect != valid { | ||
t.Errorf("expected %t but got %t", expect, valid) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func limit(s string) string { | ||
if len(s) > 17 { | ||
return s[:17] + "..." | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters