mirror of https://github.com/miguelmota/cointop
Update all modules/deps (#288)
parent
2d9b1501c6
commit
62ce8e1adb
@ -1,9 +1,7 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.6.x
|
||||
- 1.7.x
|
||||
- 1.8.x
|
||||
- 1.15.x
|
||||
|
||||
script:
|
||||
- go test
|
||||
|
@ -0,0 +1,21 @@
|
||||
## v1.2.0
|
||||
|
||||
### Added
|
||||
|
||||
- Error enums which can be accessed using `Root.Error.(soup.Error).Type`. Refer to `examples/errors`.
|
||||
|
||||
## v1.1.0
|
||||
|
||||
### Added
|
||||
|
||||
- Cookies can be added to the HTTP request, either via the `Cookies` map or the `Cookie()` function
|
||||
- Function `GetWithClient()` provides the ability to send the request with a custom HTTP client
|
||||
- Function `FindStrict()` finds the first instance of the mentioned tag with the exact matching values of the provided attribute (previously `Find()`)
|
||||
- Function `FindAllStrict()` finds all the instances of the mentioned tag with the exact matching values of the attributes (previously `FindAll()`)
|
||||
|
||||
## Changed
|
||||
|
||||
- Function `Find()` now finds the first instance of the mentioned tag with any matching values of the provided attribute.
|
||||
- Function `FindAll()` now finds all the instances of the mentioned tag with any matching values of the provided attribute.
|
||||
|
||||
---
|
@ -1,6 +1,5 @@
|
||||
//go:build (loongarch32 || loongarch64) && linux
|
||||
//+build linux
|
||||
//+build loongarch32 loongarch64
|
||||
//go:build loong64
|
||||
// +build loong64
|
||||
|
||||
// Created by cgo -godefs - DO NOT EDIT
|
||||
// cgo -godefs types.go
|
@ -0,0 +1,14 @@
|
||||
package dbus
|
||||
|
||||
import "io"
|
||||
|
||||
func (t *unixTransport) SendNullByte() error {
|
||||
n, _, err := t.UnixConn.WriteMsgUnix([]byte{0}, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 1 {
|
||||
return io.ErrShortWrite
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,28 +1,101 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/code,go,linux,macos,windows
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=code,go,linux,macos,windows
|
||||
|
||||
### Code ###
|
||||
.vscode/*
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
*.code-workspace
|
||||
|
||||
### Go ###
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Vim swap files
|
||||
.*.sw?
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
### Go Patch ###
|
||||
/vendor/
|
||||
/Godeps/
|
||||
|
||||
_testmain.go
|
||||
### Linux ###
|
||||
*~
|
||||
|
||||
*.exe
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
### macOS ###
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# Code coverage stuff
|
||||
coverage.out
|
||||
# End of https://www.toptal.com/developers/gitignore/api/code,go,linux,macos,windows
|
||||
|
@ -1,7 +0,0 @@
|
||||
language: go
|
||||
install:
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
- go get github.com/mattn/goveralls
|
||||
script:
|
||||
- go test -v -covermode=count -coverprofile=coverage.out
|
||||
- if [[ "$TRAVIS_PULL_REQUEST" = "false" ]]; then $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN; fi
|
@ -0,0 +1,42 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
The format of this file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
but only releases after v1.0.3 properly adhere to it.
|
||||
|
||||
|
||||
## [1.2.0] - 2021-01-27
|
||||
### Added
|
||||
- HSLuv and HPLuv color spaces (#41, #51)
|
||||
- CIE LCh(uv) color space, called `LuvLCh` in code (#51)
|
||||
- JSON and envconfig serialization support for `HexColor` (#42)
|
||||
- `DistanceLinearRGB` (#53)
|
||||
|
||||
### Fixed
|
||||
- RGB to/from XYZ conversion is more accurate (#51)
|
||||
- A bug in `XYZToLuvWhiteRef` that only applied to very small values was fixed (#51)
|
||||
- `BlendHCL` output is clamped so that it's not invalid (#46)
|
||||
- Properly documented `DistanceCIE76` (#40)
|
||||
- Some small godoc fixes
|
||||
|
||||
|
||||
## [1.0.3] - 2019-11-11
|
||||
- Remove SQLMock dependency
|
||||
|
||||
|
||||
## [1.0.2] - 2019-04-07
|
||||
- Fixes SQLMock dependency
|
||||
|
||||
|
||||
## [1.0.1] - 2019-03-24
|
||||
- Adds support for Go Modules
|
||||
|
||||
|
||||
## [1.0.0] - 2018-05-26
|
||||
- API Breaking change in `MakeColor`: instead of `panic`ing when alpha is zero, it now returns a secondary, boolean return value indicating success. See [the color.Color interface](#the-colorcolor-interface) section and [this FAQ entry](#q-why-would-makecolor-ever-fail) for details.
|
||||
|
||||
|
||||
## [0.9.0] - 2018-05-26
|
||||
- Initial version number after having ignored versioning for a long time :)
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,207 @@
|
||||
package colorful
|
||||
|
||||
import "math"
|
||||
|
||||
// Source: https://github.com/hsluv/hsluv-go
|
||||
// Under MIT License
|
||||
// Modified so that Saturation and Luminance are in [0..1] instead of [0..100].
|
||||
|
||||
// HSLuv uses a rounded version of the D65. This has no impact on the final RGB
|
||||
// values, but to keep high levels of accuracy for internal operations and when
|
||||
// comparing to the test values, this modified white reference is used internally.
|
||||
//
|
||||
// See this GitHub thread for details on these values:
|
||||
// https://github.com/hsluv/hsluv/issues/79
|
||||
var hSLuvD65 = [3]float64{0.95045592705167, 1.0, 1.089057750759878}
|
||||
|
||||
func LuvLChToHSLuv(l, c, h float64) (float64, float64, float64) {
|
||||
// [-1..1] but the code expects it to be [-100..100]
|
||||
c *= 100.0
|
||||
l *= 100.0
|
||||
|
||||
var s, max float64
|
||||
if l > 99.9999999 || l < 0.00000001 {
|
||||
s = 0.0
|
||||
} else {
|
||||
max = maxChromaForLH(l, h)
|
||||
s = c / max * 100.0
|
||||
}
|
||||
return h, clamp01(s / 100.0), clamp01(l / 100.0)
|
||||
}
|
||||
|
||||
func HSLuvToLuvLCh(h, s, l float64) (float64, float64, float64) {
|
||||
l *= 100.0
|
||||
s *= 100.0
|
||||
|
||||
var c, max float64
|
||||
if l > 99.9999999 || l < 0.00000001 {
|
||||
c = 0.0
|
||||
} else {
|
||||
max = maxChromaForLH(l, h)
|
||||
c = max / 100.0 * s
|
||||
}
|
||||
|
||||
// c is [-100..100], but for LCh it's supposed to be almost [-1..1]
|
||||
return clamp01(l / 100.0), c / 100.0, h
|
||||
}
|
||||
|
||||
func LuvLChToHPLuv(l, c, h float64) (float64, float64, float64) {
|
||||
// [-1..1] but the code expects it to be [-100..100]
|
||||
c *= 100.0
|
||||
l *= 100.0
|
||||
|
||||
var s, max float64
|
||||
if l > 99.9999999 || l < 0.00000001 {
|
||||
s = 0.0
|
||||
} else {
|
||||
max = maxSafeChromaForL(l)
|
||||
s = c / max * 100.0
|
||||
}
|
||||
return h, s / 100.0, l / 100.0
|
||||
}
|
||||
|
||||
func HPLuvToLuvLCh(h, s, l float64) (float64, float64, float64) {
|
||||
// [-1..1] but the code expects it to be [-100..100]
|
||||
l *= 100.0
|
||||
s *= 100.0
|
||||
|
||||
var c, max float64
|
||||
if l > 99.9999999 || l < 0.00000001 {
|
||||
c = 0.0
|
||||
} else {
|
||||
max = maxSafeChromaForL(l)
|
||||
c = max / 100.0 * s
|
||||
}
|
||||
return l / 100.0, c / 100.0, h
|
||||
}
|
||||
|
||||
// HSLuv creates a new Color from values in the HSLuv color space.
|
||||
// Hue in [0..360], a Saturation [0..1], and a Luminance (lightness) in [0..1].
|
||||
//
|
||||
// The returned color values are clamped (using .Clamped), so this will never output
|
||||
// an invalid color.
|
||||
func HSLuv(h, s, l float64) Color {
|
||||
// HSLuv -> LuvLCh -> CIELUV -> CIEXYZ -> Linear RGB -> sRGB
|
||||
l, u, v := LuvLChToLuv(HSLuvToLuvLCh(h, s, l))
|
||||
return LinearRgb(XyzToLinearRgb(LuvToXyzWhiteRef(l, u, v, hSLuvD65))).Clamped()
|
||||
}
|
||||
|
||||
// HPLuv creates a new Color from values in the HPLuv color space.
|
||||
// Hue in [0..360], a Saturation [0..1], and a Luminance (lightness) in [0..1].
|
||||
//
|
||||
// The returned color values are clamped (using .Clamped), so this will never output
|
||||
// an invalid color.
|
||||
func HPLuv(h, s, l float64) Color {
|
||||
// HPLuv -> LuvLCh -> CIELUV -> CIEXYZ -> Linear RGB -> sRGB
|
||||
l, u, v := LuvLChToLuv(HPLuvToLuvLCh(h, s, l))
|
||||
return LinearRgb(XyzToLinearRgb(LuvToXyzWhiteRef(l, u, v, hSLuvD65))).Clamped()
|
||||
}
|
||||
|
||||
// HSLuv returns the Hue, Saturation and Luminance of the color in the HSLuv
|
||||
// color space. Hue in [0..360], a Saturation [0..1], and a Luminance
|
||||
// (lightness) in [0..1].
|
||||
func (col Color) HSLuv() (h, s, l float64) {
|
||||
// sRGB -> Linear RGB -> CIEXYZ -> CIELUV -> LuvLCh -> HSLuv
|
||||
return LuvLChToHSLuv(col.LuvLChWhiteRef(hSLuvD65))
|
||||
}
|
||||
|
||||
// HPLuv returns the Hue, Saturation and Luminance of the color in the HSLuv
|
||||
// color space. Hue in [0..360], a Saturation [0..1], and a Luminance
|
||||
// (lightness) in [0..1].
|
||||
//
|
||||
// Note that HPLuv can only represent pastel colors, and so the Saturation
|
||||
// value could be much larger than 1 for colors it can't represent.
|
||||
func (col Color) HPLuv() (h, s, l float64) {
|
||||
return LuvLChToHPLuv(col.LuvLChWhiteRef(hSLuvD65))
|
||||
}
|
||||
|
||||
// DistanceHSLuv calculates Euclidan distance in the HSLuv colorspace. No idea
|
||||
// how useful this is.
|
||||
//
|
||||
// The Hue value is divided by 100 before the calculation, so that H, S, and L
|
||||
// have the same relative ranges.
|
||||
func (c1 Color) DistanceHSLuv(c2 Color) float64 {
|
||||
h1, s1, l1 := c1.HSLuv()
|
||||
h2, s2, l2 := c2.HSLuv()
|
||||
return math.Sqrt(sq((h1-h2)/100.0) + sq(s1-s2) + sq(l1-l2))
|
||||
}
|
||||
|
||||
// DistanceHPLuv calculates Euclidean distance in the HPLuv colorspace. No idea
|
||||
// how useful this is.
|
||||
//
|
||||
// The Hue value is divided by 100 before the calculation, so that H, S, and L
|
||||
// have the same relative ranges.
|
||||
func (c1 Color) DistanceHPLuv(c2 Color) float64 {
|
||||
h1, s1, l1 := c1.HPLuv()
|
||||
h2, s2, l2 := c2.HPLuv()
|
||||
return math.Sqrt(sq((h1-h2)/100.0) + sq(s1-s2) + sq(l1-l2))
|
||||
}
|
||||
|
||||
var m = [3][3]float64{
|
||||
{3.2409699419045214, -1.5373831775700935, -0.49861076029300328},
|
||||
{-0.96924363628087983, 1.8759675015077207, 0.041555057407175613},
|
||||
{0.055630079696993609, -0.20397695888897657, 1.0569715142428786},
|
||||
}
|
||||
|
||||
const kappa = 903.2962962962963
|
||||
const epsilon = 0.0088564516790356308
|
||||
|
||||
func maxChromaForLH(l, h float64) float64 {
|
||||
hRad := h / 360.0 * math.Pi * 2.0
|
||||
minLength := math.MaxFloat64
|
||||
for _, line := range getBounds(l) {
|
||||
length := lengthOfRayUntilIntersect(hRad, line[0], line[1])
|
||||
if length > 0.0 && length < minLength {
|
||||
minLength = length
|
||||
}
|
||||
}
|
||||
return minLength
|
||||
}
|
||||
|
||||
func getBounds(l float64) [6][2]float64 {
|
||||
var sub2 float64
|
||||
var ret [6][2]float64
|
||||
sub1 := math.Pow(l+16.0, 3.0) / 1560896.0
|
||||
if sub1 > epsilon {
|
||||
sub2 = sub1
|
||||
} else {
|
||||
sub2 = l / kappa
|
||||
}
|
||||
for i := range m {
|
||||
for k := 0; k < 2; k++ {
|
||||
top1 := (284517.0*m[i][0] - 94839.0*m[i][2]) * sub2
|
||||
top2 := (838422.0*m[i][2]+769860.0*m[i][1]+731718.0*m[i][0])*l*sub2 - 769860.0*float64(k)*l
|
||||
bottom := (632260.0*m[i][2]-126452.0*m[i][1])*sub2 + 126452.0*float64(k)
|
||||
ret[i*2+k][0] = top1 / bottom
|
||||
ret[i*2+k][1] = top2 / bottom
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func lengthOfRayUntilIntersect(theta, x, y float64) (length float64) {
|
||||
length = y / (math.Sin(theta) - x*math.Cos(theta))
|
||||
return
|
||||
}
|
||||
|
||||
func maxSafeChromaForL(l float64) float64 {
|
||||
minLength := math.MaxFloat64
|
||||
for _, line := range getBounds(l) {
|
||||
m1 := line[0]
|
||||
b1 := line[1]
|
||||
x := intersectLineLine(m1, b1, -1.0/m1, 0.0)
|
||||
dist := distanceFromPole(x, b1+x*m1)
|
||||
if dist < minLength {
|
||||
minLength = dist
|
||||
}
|
||||
}
|
||||
return minLength
|
||||
}
|
||||
|
||||
func intersectLineLine(x1, y1, x2, y2 float64) float64 {
|
||||
return (y1 - y2) / (x2 - x1)
|
||||
}
|
||||
|
||||
func distanceFromPole(x, y float64) float64 {
|
||||
return math.Sqrt(math.Pow(x, 2.0) + math.Pow(y, 2.0))
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
language: go
|
||||
sudo: false
|
||||
go:
|
||||
- 1.13.x
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
- go get -t -v ./...
|
||||
|
||||
script:
|
||||
- ./go.test.sh
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
|
@ -0,0 +1,257 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package charset provides common text encodings for HTML documents.
|
||||
//
|
||||
// The mapping from encoding labels to encodings is defined at
|
||||
// https://encoding.spec.whatwg.org/.
|
||||
package charset // import "golang.org/x/net/html/charset"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/text/encoding"
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
"golang.org/x/text/encoding/htmlindex"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
// Lookup returns the encoding with the specified label, and its canonical
|
||||
// name. It returns nil and the empty string if label is not one of the
|
||||
// standard encodings for HTML. Matching is case-insensitive and ignores
|
||||
// leading and trailing whitespace. Encoders will use HTML escape sequences for
|
||||
// runes that are not supported by the character set.
|
||||
func Lookup(label string) (e encoding.Encoding, name string) {
|
||||
e, err := htmlindex.Get(label)
|
||||
if err != nil {
|
||||
return nil, ""
|
||||
}
|
||||
name, _ = htmlindex.Name(e)
|
||||
return &htmlEncoding{e}, name
|
||||
}
|
||||
|
||||
type htmlEncoding struct{ encoding.Encoding }
|
||||
|
||||
func (h *htmlEncoding) NewEncoder() *encoding.Encoder {
|
||||
// HTML requires a non-terminating legacy encoder. We use HTML escapes to
|
||||
// substitute unsupported code points.
|
||||
return encoding.HTMLEscapeUnsupported(h.Encoding.NewEncoder())
|
||||
}
|
||||
|
||||
// DetermineEncoding determines the encoding of an HTML document by examining
|
||||
// up to the first 1024 bytes of content and the declared Content-Type.
|
||||
//
|
||||
// See http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#determining-the-character-encoding
|
||||
func DetermineEncoding(content []byte, contentType string) (e encoding.Encoding, name string, certain bool) {
|
||||
if len(content) > 1024 {
|
||||
content = content[:1024]
|
||||
}
|
||||
|
||||
for _, b := range boms {
|
||||
if bytes.HasPrefix(content, b.bom) {
|
||||
e, name = Lookup(b.enc)
|
||||
return e, name, true
|
||||
}
|
||||
}
|
||||
|
||||
if _, params, err := mime.ParseMediaType(contentType); err == nil {
|
||||
if cs, ok := params["charset"]; ok {
|
||||
if e, name = Lookup(cs); e != nil {
|
||||
return e, name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(content) > 0 {
|
||||
e, name = prescan(content)
|
||||
if e != nil {
|
||||
return e, name, false
|
||||
}
|
||||
}
|
||||
|
||||
// Try to detect UTF-8.
|
||||
// First eliminate any partial rune at the end.
|
||||
for i := len(content) - 1; i >= 0 && i > len(content)-4; i-- {
|
||||
b := content[i]
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
if utf8.RuneStart(b) {
|
||||
content = content[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
hasHighBit := false
|
||||
for _, c := range content {
|
||||
if c >= 0x80 {
|
||||
hasHighBit = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasHighBit && utf8.Valid(content) {
|
||||
return encoding.Nop, "utf-8", false
|
||||
}
|
||||
|
||||
// TODO: change default depending on user's locale?
|
||||
return charmap.Windows1252, "windows-1252", false
|
||||
}
|
||||
|
||||
// NewReader returns an io.Reader that converts the content of r to UTF-8.
|
||||
// It calls DetermineEncoding to find out what r's encoding is.
|
||||
func NewReader(r io.Reader, contentType string) (io.Reader, error) {
|
||||
preview := make([]byte, 1024)
|
||||
n, err := io.ReadFull(r, preview)
|
||||
switch {
|
||||
case err == io.ErrUnexpectedEOF:
|
||||
preview = preview[:n]
|
||||
r = bytes.NewReader(preview)
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
r = io.MultiReader(bytes.NewReader(preview), r)
|
||||
}
|
||||
|
||||
if e, _, _ := DetermineEncoding(preview, contentType); e != encoding.Nop {
|
||||
r = transform.NewReader(r, e.NewDecoder())
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// NewReaderLabel returns a reader that converts from the specified charset to
|
||||
// UTF-8. It uses Lookup to find the encoding that corresponds to label, and
|
||||
// returns an error if Lookup returns nil. It is suitable for use as
|
||||
// encoding/xml.Decoder's CharsetReader function.
|
||||
func NewReaderLabel(label string, input io.Reader) (io.Reader, error) {
|
||||
e, _ := Lookup(label)
|
||||
if e == nil {
|
||||
return nil, fmt.Errorf("unsupported charset: %q", label)
|
||||
}
|
||||
return transform.NewReader(input, e.NewDecoder()), nil
|
||||
}
|
||||
|
||||
func prescan(content []byte) (e encoding.Encoding, name string) {
|
||||
z := html.NewTokenizer(bytes.NewReader(content))
|
||||
for {
|
||||
switch z.Next() {
|
||||
case html.ErrorToken:
|
||||
return nil, ""
|
||||
|
||||
case html.StartTagToken, html.SelfClosingTagToken:
|
||||
tagName, hasAttr := z.TagName()
|
||||
if !bytes.Equal(tagName, []byte("meta")) {
|
||||
continue
|
||||
}
|
||||
attrList := make(map[string]bool)
|
||||
gotPragma := false
|
||||
|
||||
const (
|
||||
dontKnow = iota
|
||||
doNeedPragma
|
||||
doNotNeedPragma
|
||||
)
|
||||
needPragma := dontKnow
|
||||
|
||||
name = ""
|
||||
e = nil
|
||||
for hasAttr {
|
||||
var key, val []byte
|
||||
key, val, hasAttr = z.TagAttr()
|
||||
ks := string(key)
|
||||
if attrList[ks] {
|
||||
continue
|
||||
}
|
||||
attrList[ks] = true
|
||||
for i, c := range val {
|
||||
if 'A' <= c && c <= 'Z' {
|
||||
val[i] = c + 0x20
|
||||
}
|
||||
}
|
||||
|
||||
switch ks {
|
||||
case "http-equiv":
|
||||
if bytes.Equal(val, []byte("content-type")) {
|
||||
gotPragma = true
|
||||
}
|
||||
|
||||
case "content":
|
||||
if e == nil {
|
||||
name = fromMetaElement(string(val))
|
||||
if name != "" {
|
||||
e, name = Lookup(name)
|
||||
if e != nil {
|
||||
needPragma = doNeedPragma
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case "charset":
|
||||
e, name = Lookup(string(val))
|
||||
needPragma = doNotNeedPragma
|
||||
}
|
||||
}
|
||||
|
||||
if needPragma == dontKnow || needPragma == doNeedPragma && !gotPragma {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(name, "utf-16") {
|
||||
name = "utf-8"
|
||||
e = encoding.Nop
|
||||
}
|
||||
|
||||
if e != nil {
|
||||
return e, name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fromMetaElement(s string) string {
|
||||
for s != "" {
|
||||
csLoc := strings.Index(s, "charset")
|
||||
if csLoc == -1 {
|
||||
return ""
|
||||
}
|
||||
s = s[csLoc+len("charset"):]
|
||||
s = strings.TrimLeft(s, " \t\n\f\r")
|
||||
if !strings.HasPrefix(s, "=") {
|
||||
continue
|
||||
}
|
||||
s = s[1:]
|
||||
s = strings.TrimLeft(s, " \t\n\f\r")
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
if q := s[0]; q == '"' || q == '\'' {
|
||||
s = s[1:]
|
||||
closeQuote := strings.IndexRune(s, rune(q))
|
||||
if closeQuote == -1 {
|
||||
return ""
|
||||
}
|
||||
return s[:closeQuote]
|
||||
}
|
||||
|
||||
end := strings.IndexAny(s, "; \t\n\f\r")
|
||||
if end == -1 {
|
||||
end = len(s)
|
||||
}
|
||||
return s[:end]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var boms = []struct {
|
||||
bom []byte
|
||||
enc string
|
||||
}{
|
||||
{[]byte{0xfe, 0xff}, "utf-16be"},
|
||||
{[]byte{0xff, 0xfe}, "utf-16le"},
|
||||
{[]byte{0xef, 0xbb, 0xbf}, "utf-8"},
|
||||
}
|
@ -0,0 +1,149 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package unix
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Helpers for dealing with ifreq since it contains a union and thus requires a
|
||||
// lot of unsafe.Pointer casts to use properly.
|
||||
|
||||
// An Ifreq is a type-safe wrapper around the raw ifreq struct. An Ifreq
|
||||
// contains an interface name and a union of arbitrary data which can be
|
||||
// accessed using the Ifreq's methods. To create an Ifreq, use the NewIfreq
|
||||
// function.
|
||||
//
|
||||
// Use the Name method to access the stored interface name. The union data
|
||||
// fields can be get and set using the following methods:
|
||||
// - Uint16/SetUint16: flags
|
||||
// - Uint32/SetUint32: ifindex, metric, mtu
|
||||
type Ifreq struct{ raw ifreq }
|
||||
|
||||
// NewIfreq creates an Ifreq with the input network interface name after
|
||||
// validating the name does not exceed IFNAMSIZ-1 (trailing NULL required)
|
||||
// bytes.
|
||||
func NewIfreq(name string) (*Ifreq, error) {
|
||||
// Leave room for terminating NULL byte.
|
||||
if len(name) >= IFNAMSIZ {
|
||||
return nil, EINVAL
|
||||
}
|
||||
|
||||
var ifr ifreq
|
||||
copy(ifr.Ifrn[:], name)
|
||||
|
||||
return &Ifreq{raw: ifr}, nil
|
||||
}
|
||||
|
||||
// TODO(mdlayher): get/set methods for hardware address sockaddr, char array, etc.
|
||||
|
||||
// Name returns the interface name associated with the Ifreq.
|
||||
func (ifr *Ifreq) Name() string {
|
||||
// BytePtrToString requires a NULL terminator or the program may crash. If
|
||||
// one is not present, just return the empty string.
|
||||
if !bytes.Contains(ifr.raw.Ifrn[:], []byte{0x00}) {
|
||||
return ""
|
||||
}
|
||||
|
||||
return BytePtrToString(&ifr.raw.Ifrn[0])
|
||||
}
|
||||
|
||||
// According to netdevice(7), only AF_INET addresses are returned for numerous
|
||||
// sockaddr ioctls. For convenience, we expose these as Inet4Addr since the Port
|
||||
// field and other data is always empty.
|
||||
|
||||
// Inet4Addr returns the Ifreq union data from an embedded sockaddr as a C
|
||||
// in_addr/Go []byte (4-byte IPv4 address) value. If the sockaddr family is not
|
||||
// AF_INET, an error is returned.
|
||||
func (ifr *Ifreq) Inet4Addr() ([]byte, error) {
|
||||
raw := *(*RawSockaddrInet4)(unsafe.Pointer(&ifr.raw.Ifru[:SizeofSockaddrInet4][0]))
|
||||
if raw.Family != AF_INET {
|
||||
// Cannot safely interpret raw.Addr bytes as an IPv4 address.
|
||||
return nil, EINVAL
|
||||
}
|
||||
|
||||
return raw.Addr[:], nil
|
||||
}
|
||||
|
||||
// SetInet4Addr sets a C in_addr/Go []byte (4-byte IPv4 address) value in an
|
||||
// embedded sockaddr within the Ifreq's union data. v must be 4 bytes in length
|
||||
// or an error will be returned.
|
||||
func (ifr *Ifreq) SetInet4Addr(v []byte) error {
|
||||
if len(v) != 4 {
|
||||
return EINVAL
|
||||
}
|
||||
|
||||
var addr [4]byte
|
||||
copy(addr[:], v)
|
||||
|
||||
ifr.clear()
|
||||
*(*RawSockaddrInet4)(
|
||||
unsafe.Pointer(&ifr.raw.Ifru[:SizeofSockaddrInet4][0]),
|
||||
) = RawSockaddrInet4{
|
||||
// Always set IP family as ioctls would require it anyway.
|
||||
Family: AF_INET,
|
||||
Addr: addr,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Uint16 returns the Ifreq union data as a C short/Go uint16 value.
|
||||
func (ifr *Ifreq) Uint16() uint16 {
|
||||
return *(*uint16)(unsafe.Pointer(&ifr.raw.Ifru[:2][0]))
|
||||
}
|
||||
|
||||
// SetUint16 sets a C short/Go uint16 value as the Ifreq's union data.
|
||||
func (ifr *Ifreq) SetUint16(v uint16) {
|
||||
ifr.clear()
|
||||
*(*uint16)(unsafe.Pointer(&ifr.raw.Ifru[:2][0])) = v
|
||||
}
|
||||
|
||||
// Uint32 returns the Ifreq union data as a C int/Go uint32 value.
|
||||
func (ifr *Ifreq) Uint32() uint32 {
|
||||
return *(*uint32)(unsafe.Pointer(&ifr.raw.Ifru[:4][0]))
|
||||
}
|
||||
|
||||
// SetUint32 sets a C int/Go uint32 value as the Ifreq's union data.
|
||||
func (ifr *Ifreq) SetUint32(v uint32) {
|
||||
ifr.clear()
|
||||
*(*uint32)(unsafe.Pointer(&ifr.raw.Ifru[:4][0])) = v
|
||||
}
|
||||
|
||||
// clear zeroes the ifreq's union field to prevent trailing garbage data from
|
||||
// being sent to the kernel if an ifreq is reused.
|
||||
func (ifr *Ifreq) clear() {
|
||||
for i := range ifr.raw.Ifru {
|
||||
ifr.raw.Ifru[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(mdlayher): export as IfreqData? For now we can provide helpers such as
|
||||
// IoctlGetEthtoolDrvinfo which use these APIs under the hood.
|
||||
|
||||
// An ifreqData is an Ifreq which carries pointer data. To produce an ifreqData,
|
||||
// use the Ifreq.withData method.
|
||||
type ifreqData struct {
|
||||
name [IFNAMSIZ]byte
|
||||
// A type separate from ifreq is required in order to comply with the
|
||||
// unsafe.Pointer rules since the "pointer-ness" of data would not be
|
||||
// preserved if it were cast into the byte array of a raw ifreq.
|
||||
data unsafe.Pointer
|
||||
// Pad to the same size as ifreq.
|
||||
_ [len(ifreq{}.Ifru) - SizeofPtr]byte
|
||||
}
|
||||
|
||||
// withData produces an ifreqData with the pointer p set for ioctls which require
|
||||
// arbitrary pointer data.
|
||||
func (ifr Ifreq) withData(p unsafe.Pointer) ifreqData {
|
||||
return ifreqData{
|
||||
name: ifr.raw.Ifrn,
|
||||
data: p,
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package unix
|
||||
|
||||
import "runtime"
|
||||
|
||||
// SysvShmCtl performs control operations on the shared memory segment
|
||||
// specified by id.
|
||||
func SysvShmCtl(id, cmd int, desc *SysvShmDesc) (result int, err error) {
|
||||
if runtime.GOARCH == "arm" ||
|
||||
runtime.GOARCH == "mips64" || runtime.GOARCH == "mips64le" {
|
||||
cmd |= ipc_64
|
||||
}
|
||||
|
||||
return shmctl(id, cmd, desc)
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build (darwin && !ios) || linux
|
||||
// +build darwin,!ios linux
|
||||
|
||||
package unix
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/internal/unsafeheader"
|
||||
)
|
||||
|
||||
// SysvShmAttach attaches the Sysv shared memory segment associated with the
|
||||
// shared memory identifier id.
|
||||
func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) {
|
||||
addr, errno := shmat(id, addr, flag)
|
||||
if errno != nil {
|
||||
return nil, errno
|
||||
}
|
||||
|
||||
// Retrieve the size of the shared memory to enable slice creation
|
||||
var info SysvShmDesc
|
||||
|
||||
_, err := SysvShmCtl(id, IPC_STAT, &info)
|
||||
if err != nil {
|
||||
// release the shared memory if we can't find the size
|
||||
|
||||
// ignoring error from shmdt as there's nothing sensible to return here
|
||||
shmdt(addr)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Use unsafe to convert addr into a []byte.
|
||||
// TODO: convert to unsafe.Slice once we can assume Go 1.17
|
||||
var b []byte
|
||||
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b))
|
||||
hdr.Data = unsafe.Pointer(addr)
|
||||
hdr.Cap = int(info.Segsz)
|
||||
hdr.Len = int(info.Segsz)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// SysvShmDetach unmaps the shared memory slice returned from SysvShmAttach.
|
||||
//
|
||||
// It is not safe to use the slice after calling this function.
|
||||
func SysvShmDetach(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return EINVAL
|
||||
}
|
||||
|
||||
return shmdt(uintptr(unsafe.Pointer(&data[0])))
|
||||
}
|
||||
|
||||
// SysvShmGet returns the Sysv shared memory identifier associated with key.
|
||||
// If the IPC_CREAT flag is specified a new segment is created.
|
||||
func SysvShmGet(key, size, flag int) (id int, err error) {
|
||||
return shmget(key, size, flag)
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build darwin && !ios
|
||||
// +build darwin,!ios
|
||||
|
||||
package unix
|
||||
|
||||
// SysvShmCtl performs control operations on the shared memory segment
|
||||
// specified by id.
|
||||
func SysvShmCtl(id, cmd int, desc *SysvShmDesc) (result int, err error) {
|
||||
return shmctl(id, cmd, desc)
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue