Skip to content
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

chore: remove deprecated io/ioutil methods #1277

Merged
merged 15 commits into from
Nov 8, 2023
3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -1095,7 +1094,7 @@ func PackageNameFromFileBody(name, body string) Name {

// NOTE: panics if package name is invalid.
func ReadMemPackage(dir string, pkgPath string) *std.MemPackage {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -112,7 +111,7 @@ func GetPrecompileFilenameAndTags(gnoFilePath string) (targetFilename, tags stri
func PrecompileAndCheckMempkg(mempkg *std.MemPackage) error {
gofmt := "gofmt"

tmpDir, err := ioutil.TempDir("", mempkg.Name)
tmpDir, err := os.MkdirTemp("", mempkg.Name)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions gnovm/pkg/gnomod/gnomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gnomod
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -172,9 +171,9 @@ func CreateGnoModFile(rootDir, modPath string) error {
if modPath == "" {
// Check .gno files for package name
// and use it as modPath
files, err := ioutil.ReadDir(rootDir)
files, err := os.ReadDir(rootDir)
if err != nil {
fmt.Errorf("read dir %q: %w", rootDir, err)
return fmt.Errorf("read dir %q: %w", rootDir, err)
}

var pkgName gnolang.Name
Expand Down
35 changes: 35 additions & 0 deletions gnovm/stdlibs/internal/itoa/itoa.gno
notJoon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

// Simple conversions to avoid depending on strconv.

package itoa

// Itoa converts value to a decimal string/
func Itoa(val int) string {
if val < 0 {
return "-" + Uitoa(uint(-val))
}

return Uitoa(uint(val))
}

// Uitoa converts value to a decimal string.
func Uitoa(val uint) string {
// avoid string allocation
if val == 0 {
return "0"
}

var buf [20]byte
for i := len(buf) - 1; val >= 10; i-- {
q := val
buf[i] = byte('0' + val - (q * 10))
val = 1
}

// val < 10
buf[i] = byte('0' + val)
return string(buf[i:])
}
41 changes: 41 additions & 0 deletions gnovm/stdlibs/internal/itoa/itoa_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

package itoa_test

import (
"fmt"
"math"
"testing"

"internal/itoa"
)

var (
minInt64 int64 = math.MinInt64
maxInt64 int64 = math.MaxInt64
maxUint64 uint64 = math.MaxUint64
)

func TestItoa(t *testing.T) {
tests := []int{int(minInt64), math.MinInt32, -999, -100, -1, 0, 1, 100, 999, math.MaxInt32, int(maxInt64)}
for _, tt := range tests {
got := itoa.Itoa(tt)
want := fmt.Sprint(tt)
if want != got {
t.Fatalf("Itoa(%d) = %s, want %s", tt, got, want)
}
}
}

func TestUitoa(t *testing.T) {
tests := []uint{0, 1, 100, 999, math.MaxUint32, uint(maxUint64)}
for _, tt := range tests {
got := itoa.Uitoa(tt)
want := fmt.Sprint(tt)
if want != got {
t.Fatalf("Uitoa(%d) = %s, want %s", tt, got, want)
}
}
}
2 changes: 1 addition & 1 deletion gnovm/stdlibs/io/example_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package io_test
package io
notJoon marked this conversation as resolved.
Show resolved Hide resolved

import (
"bytes"
Expand Down
77 changes: 77 additions & 0 deletions gnovm/stdlibs/io/io.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"errors"
)

// TODO: implement rest of io package after sync package added.

// Seek whence values.
const (
SeekStart = 0 // seek relative to the origin of the file
Expand Down Expand Up @@ -477,6 +479,15 @@ func (l *LimitedReader) Read(p []byte) (n int, err error) {
// NewSectionReader returns a SectionReader that reads from r
// starting at offset off and stops with EOF after n bytes.
func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
var remaining int64
const maxInt64 = 1<<63 - 1
if off <= maxInt64-n {
remaining = n + off
} else {
// Overflow, with no way to return error.
// Assume we can read up to an offset of `1<<63 - 1` bytes in this case.
remaining = maxInt64
}
return &SectionReader{r, off, off, off + n}
}

Expand Down Expand Up @@ -543,6 +554,53 @@ func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) {
// Size returns the size of the section in bytes.
func (s *SectionReader) Size() int64 { return s.limit - s.base }

// And OffsetWriter maps writers at offset base to offset base + off in the underlying writer.
notJoon marked this conversation as resolved.
Show resolved Hide resolved
type OffsetWriter struct {
w WriterAt
base int64 // the original offset
off int64 // the current offset
}

// NewOffsetWriter returns a new OffsetWriter that writes to w starting at offset off.
func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter {
return &OffsetWriter{w: w, off: off}
}

func (o *OffsetWriter) Write(p []byte) (n int, err error) {
// n, err = o.w.WriterAt(p, o.off)
wa := o.w
n, err = wa.WriteAt(p, o.off)
o.off += int64(n)
return
}

func (o *OffsetWriter) WriteAt(p []byte, off int64) (n int, err error) {
if off < 0 {
return 0, errOffset
}

off += o.base
return o.w.WriteAt(p, off)
}

func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
return 0, errWhence
case SeekStart:
offset += o.base
case SeekCurrent:
offset += o.off
}

if offset < o.base {
return 0, errOffset
}

o.off = offset
return offset - o.base, nil
}

// TeeReader returns a Reader that writes to w what it reads from r.
// All reads from r performed through it are matched with
// corresponding writes to w. There is no internal buffering -
Expand Down Expand Up @@ -614,7 +672,12 @@ func (discard) ReadFrom(r Reader) (n int64, err error) {

// NopCloser returns a ReadCloser with a no-op Close method wrapping
// the provided Reader r.
// If r implements WriterTo, the returned ReadCloser will also implement WriterTo
// by forwarding the call to r.
func NopCloser(r Reader) ReadCloser {
if _, ok := r.(WriterTo); ok {
return nopCloserWriterTo{r}
}
return nopCloser{r}
}

Expand All @@ -624,6 +687,16 @@ type nopCloser struct {

func (nopCloser) Close() error { return nil }

type nopCloserWriterTo struct {
Reader
}

func (nopCloserWriterTo) Close() error { return nil }

func (c *nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) {
return c.Reader.(WriterTo).WriteTo(w)
}

// ReadAll reads from r until an error or EOF and returns the data it read.
// A successful call returns err == nil, not err == EOF. Because ReadAll is
// defined to read from src until EOF, it does not treat an EOF from Read
Expand All @@ -643,5 +716,9 @@ func ReadAll(r Reader) ([]byte, error) {
}
return b, err
}
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
}
}
Loading
Loading