-
Notifications
You must be signed in to change notification settings - Fork 981
extend stdlib to allow import of more packages #1099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f068eb6
extend stdlib to allow import of more packages
cornelk c3f7c28
Merge branch 'dev' into extend_stdlib
cornelk 8b46fa3
os: move errors to separate file
cornelk ab9dc3d
reflect: reimplement Unquote
cornelk c5d1578
add struct tag test
cornelk d865e6e
reflect: add TODO to do split at compile time
cornelk 5696b98
Merge branch 'dev' into extend_stdlib
cornelk 76d38a3
add copyright header
cornelk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
package os | ||
|
||
func Getenv(key string) string { | ||
return "" | ||
} |
This file contains hidden or 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,39 @@ | ||
package os | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrInvalid = errors.New("invalid argument") | ||
ErrPermission = errors.New("permission denied") | ||
ErrClosed = errors.New("file already closed") | ||
|
||
// Portable analogs of some common system call errors. | ||
// Note that these are exported for use in the Filesystem interface. | ||
ErrUnsupported = errors.New("operation not supported") | ||
ErrNotImplemented = errors.New("operation not implemented") | ||
ErrNotExist = errors.New("file not found") | ||
ErrExist = errors.New("file exists") | ||
) | ||
|
||
func IsPermission(err error) bool { | ||
return err == ErrPermission | ||
} | ||
|
||
func NewSyscallError(syscall string, err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
return &SyscallError{syscall, err} | ||
} | ||
|
||
// SyscallError records an error from a specific system call. | ||
type SyscallError struct { | ||
Syscall string | ||
Err error | ||
} | ||
|
||
func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() } | ||
|
||
func (e *SyscallError) Unwrap() error { return e.Err } |
This file contains hidden or 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,6 @@ | ||
package os | ||
|
||
type Signal interface { | ||
String() string | ||
Signal() // to distinguish from other Stringers | ||
} |
This file contains hidden or 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 hidden or 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,5 @@ | ||
package os | ||
|
||
func Hostname() (name string, err error) { | ||
return "", ErrNotImplemented | ||
} |
This file contains hidden or 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,9 @@ | ||
package reflect | ||
|
||
func DeepEqual(x, y interface{}) bool { | ||
if x == nil || y == nil { | ||
return x == y | ||
} | ||
|
||
panic("unimplemented: reflect.DeepEqual()") | ||
} |
This file contains hidden or 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,248 @@ | ||
// Copyright 2009 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 reflect | ||
|
||
import ( | ||
"unicode/utf8" | ||
) | ||
|
||
// errSyntax indicates that a value does not have the right syntax for the target type. | ||
var errSyntax = badSyntax{} | ||
|
||
type badSyntax struct{} | ||
|
||
func (badSyntax) Error() string { | ||
return "invalid syntax" | ||
} | ||
|
||
func unhex(b byte) (v rune, ok bool) { | ||
c := rune(b) | ||
switch { | ||
case '0' <= c && c <= '9': | ||
return c - '0', true | ||
case 'a' <= c && c <= 'f': | ||
return c - 'a' + 10, true | ||
case 'A' <= c && c <= 'F': | ||
return c - 'A' + 10, true | ||
} | ||
return | ||
} | ||
|
||
// unquoteChar decodes the first character or byte in the escaped string | ||
// or character literal represented by the string s. | ||
// It returns four values: | ||
// | ||
// 1) value, the decoded Unicode code point or byte value; | ||
// 2) multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation; | ||
// 3) tail, the remainder of the string after the character; and | ||
// 4) an error that will be nil if the character is syntactically valid. | ||
// | ||
// The second argument, quote, specifies the type of literal being parsed | ||
// and therefore which escaped quote character is permitted. | ||
// If set to a single quote, it permits the sequence \' and disallows unescaped '. | ||
// If set to a double quote, it permits \" and disallows unescaped ". | ||
// If set to zero, it does not permit either escape and allows both quote characters to appear unescaped. | ||
func unquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) { | ||
// easy cases | ||
if len(s) == 0 { | ||
err = errSyntax | ||
return | ||
} | ||
switch c := s[0]; { | ||
case c == quote && (quote == '\'' || quote == '"'): | ||
err = errSyntax | ||
return | ||
case c >= utf8.RuneSelf: | ||
r, size := utf8.DecodeRuneInString(s) | ||
return r, true, s[size:], nil | ||
case c != '\\': | ||
return rune(s[0]), false, s[1:], nil | ||
} | ||
|
||
// hard case: c is backslash | ||
if len(s) <= 1 { | ||
err = errSyntax | ||
return | ||
} | ||
c := s[1] | ||
s = s[2:] | ||
|
||
switch c { | ||
case 'a': | ||
value = '\a' | ||
case 'b': | ||
value = '\b' | ||
case 'f': | ||
value = '\f' | ||
case 'n': | ||
value = '\n' | ||
case 'r': | ||
value = '\r' | ||
case 't': | ||
value = '\t' | ||
case 'v': | ||
value = '\v' | ||
case 'x', 'u', 'U': | ||
n := 0 | ||
switch c { | ||
case 'x': | ||
n = 2 | ||
case 'u': | ||
n = 4 | ||
case 'U': | ||
n = 8 | ||
} | ||
var v rune | ||
if len(s) < n { | ||
err = errSyntax | ||
return | ||
} | ||
for j := 0; j < n; j++ { | ||
x, ok := unhex(s[j]) | ||
if !ok { | ||
err = errSyntax | ||
return | ||
} | ||
v = v<<4 | x | ||
} | ||
s = s[n:] | ||
if c == 'x' { | ||
// single-byte string, possibly not UTF-8 | ||
value = v | ||
break | ||
} | ||
if v > utf8.MaxRune { | ||
err = errSyntax | ||
return | ||
} | ||
value = v | ||
multibyte = true | ||
case '0', '1', '2', '3', '4', '5', '6', '7': | ||
v := rune(c) - '0' | ||
if len(s) < 2 { | ||
err = errSyntax | ||
return | ||
} | ||
for j := 0; j < 2; j++ { // one digit already; two more | ||
x := rune(s[j]) - '0' | ||
if x < 0 || x > 7 { | ||
err = errSyntax | ||
return | ||
} | ||
v = (v << 3) | x | ||
} | ||
s = s[2:] | ||
if v > 255 { | ||
err = errSyntax | ||
return | ||
} | ||
value = v | ||
case '\\': | ||
value = '\\' | ||
case '\'', '"': | ||
if c != quote { | ||
err = errSyntax | ||
return | ||
} | ||
value = rune(c) | ||
default: | ||
err = errSyntax | ||
return | ||
} | ||
tail = s | ||
return | ||
} | ||
|
||
// unquote interprets s as a single-quoted, double-quoted, | ||
// or backquoted Go string literal, returning the string value | ||
// that s quotes. (If s is single-quoted, it would be a Go | ||
// character literal; unquote returns the corresponding | ||
// one-character string.) | ||
func unquote(s string) (string, error) { | ||
n := len(s) | ||
if n < 2 { | ||
return "", errSyntax | ||
} | ||
quote := s[0] | ||
if quote != s[n-1] { | ||
return "", errSyntax | ||
} | ||
s = s[1 : n-1] | ||
|
||
if quote == '`' { | ||
if contains(s, '`') { | ||
return "", errSyntax | ||
} | ||
if contains(s, '\r') { | ||
// -1 because we know there is at least one \r to remove. | ||
buf := make([]byte, 0, len(s)-1) | ||
for i := 0; i < len(s); i++ { | ||
if s[i] != '\r' { | ||
buf = append(buf, s[i]) | ||
} | ||
} | ||
return string(buf), nil | ||
} | ||
return s, nil | ||
} | ||
if quote != '"' && quote != '\'' { | ||
return "", errSyntax | ||
} | ||
if contains(s, '\n') { | ||
return "", errSyntax | ||
} | ||
|
||
// Is it trivial? Avoid allocation. | ||
if !contains(s, '\\') && !contains(s, quote) { | ||
switch quote { | ||
case '"': | ||
if utf8.ValidString(s) { | ||
return s, nil | ||
} | ||
case '\'': | ||
r, size := utf8.DecodeRuneInString(s) | ||
if size == len(s) && (r != utf8.RuneError || size != 1) { | ||
return s, nil | ||
} | ||
} | ||
} | ||
|
||
var runeTmp [utf8.UTFMax]byte | ||
buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations. | ||
for len(s) > 0 { | ||
c, multibyte, ss, err := unquoteChar(s, quote) | ||
if err != nil { | ||
return "", err | ||
} | ||
s = ss | ||
if c < utf8.RuneSelf || !multibyte { | ||
buf = append(buf, byte(c)) | ||
} else { | ||
n := utf8.EncodeRune(runeTmp[:], c) | ||
buf = append(buf, runeTmp[:n]...) | ||
} | ||
if quote == '\'' && len(s) != 0 { | ||
// single-quoted must be single character | ||
return "", errSyntax | ||
} | ||
} | ||
return string(buf), nil | ||
} | ||
|
||
// contains reports whether the string contains the byte c. | ||
func contains(s string, c byte) bool { | ||
return indexByteString(s, c) != -1 | ||
} | ||
|
||
// Index finds the index of the first instance of the specified byte in the string. | ||
// If the byte is not found, this returns -1. | ||
func indexByteString(s string, c byte) int { | ||
for i := 0; i < len(s); i++ { | ||
if s[i] == c { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not complete (it doesn't unwrap the error) but I guess it's fine for now.