-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Merge remote-tracking branch 'origin/master' into genesis-stdlibs
- Loading branch information
Showing
21 changed files
with
1,016 additions
and
77 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 |
---|---|---|
@@ -1,6 +1,3 @@ | ||
module gno.land/p/demo/groups | ||
|
||
require ( | ||
gno.land/p/demo/rat v0.0.0-latest | ||
gno.land/r/demo/boards v0.0.0-latest | ||
) | ||
require gno.land/p/demo/rat v0.0.0-latest |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,84 @@ | ||
// Package chonk provides a simple way to store arbitrarily large strings | ||
// in a linked list across transactions for efficient storage and retrieval. | ||
// A Chonk support three operations: Add, Flush, and Scanner. | ||
// - Add appends a string to the Chonk. | ||
// - Flush clears the Chonk. | ||
// - Scanner is used to iterate over the chunks in the Chonk. | ||
package chonk | ||
|
||
// Chonk is a linked list string storage and | ||
// retrieval system for fine bois. | ||
type Chonk struct { | ||
first *chunk | ||
last *chunk | ||
} | ||
|
||
// chunk is a linked list node for Chonk | ||
type chunk struct { | ||
text string | ||
next *chunk | ||
} | ||
|
||
// New creates a reference to a new Chonk | ||
func New() *Chonk { | ||
return &Chonk{} | ||
} | ||
|
||
// Add appends a string to the Chonk. If the Chonk is empty, | ||
// the string will be the first and last chunk. Otherwise, | ||
// the string will be appended to the end of the Chonk. | ||
func (c *Chonk) Add(text string) { | ||
next := &chunk{text: text} | ||
if c.first == nil { | ||
c.first = next | ||
c.last = next | ||
return | ||
} | ||
c.last.next = next | ||
c.last = next | ||
} | ||
|
||
// Flush clears the Chonk by setting the first and last | ||
// chunks to nil. This will allow the garbage collector to | ||
// free the memory used by the Chonk. | ||
func (c *Chonk) Flush() { | ||
c.first = nil | ||
c.last = nil | ||
} | ||
|
||
// Scanner returns a new Scanner for the Chonk. The Scanner | ||
// is used to iterate over the chunks in the Chonk. | ||
func (c *Chonk) Scanner() *Scanner { | ||
return &Scanner{ | ||
next: c.first, | ||
} | ||
} | ||
|
||
// Scanner is a simple string scanner for Chonk. It is used | ||
// to iterate over the chunks in a Chonk from first to last. | ||
type Scanner struct { | ||
current *chunk | ||
next *chunk | ||
} | ||
|
||
// Scan advances the scanner to the next chunk. It returns | ||
// true if there is a next chunk, and false if there is not. | ||
func (s *Scanner) Scan() bool { | ||
if s.next != nil { | ||
s.current = s.next | ||
s.next = s.next.next | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// Text returns the current chunk. It is only valid to call | ||
// this method after a call to Scan returns true. Expected usage: | ||
// | ||
// scanner := chonk.Scanner() | ||
// for scanner.Scan() { | ||
// fmt.Println(scanner.Text()) | ||
// } | ||
func (s *Scanner) Text() string { | ||
return s.current.text | ||
} |
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,54 @@ | ||
package chonk | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestChonk(t *testing.T) { | ||
t.Parallel() | ||
c := New() | ||
testTable := []struct { | ||
name string | ||
chunks []string | ||
}{ | ||
{ | ||
name: "empty", | ||
chunks: []string{}, | ||
}, | ||
{ | ||
name: "single chunk", | ||
chunks: []string{"a"}, | ||
}, | ||
{ | ||
name: "multiple chunks", | ||
chunks: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}, | ||
}, | ||
{ | ||
name: "multiline chunks", | ||
chunks: []string{"1a\nb\nc\n\n", "d\ne\nf", "g\nh\ni", "j\nk\nl\n\n\n\n"}, | ||
}, | ||
{ | ||
name: "empty", | ||
chunks: []string{}, | ||
}, | ||
} | ||
testChonk := func(t *testing.T, c *Chonk, chunks []string) { | ||
for _, chunk := range chunks { | ||
c.Add(chunk) | ||
} | ||
scanner := c.Scanner() | ||
i := 0 | ||
for scanner.Scan() { | ||
if scanner.Text() != chunks[i] { | ||
t.Errorf("expected %s, got %s", chunks[i], scanner.Text()) | ||
} | ||
i++ | ||
} | ||
} | ||
for _, test := range testTable { | ||
t.Run(test.name, func(t *testing.T) { | ||
testChonk(t, c, test.chunks) | ||
c.Flush() | ||
}) | ||
} | ||
} |
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 @@ | ||
module gno.land/p/n2p5/chonk |
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,7 @@ | ||
module gno.land/p/n2p5/mgroup | ||
|
||
require ( | ||
gno.land/p/demo/avl v0.0.0-latest | ||
gno.land/p/demo/ownable v0.0.0-latest | ||
gno.land/p/demo/testutils v0.0.0-latest | ||
) |
Oops, something went wrong.