Skip to content

Commit

Permalink
fix: use sliding search depth for read until explicit too
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Sep 10, 2023
1 parent dbf027b commit c37671e
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions channel/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (

const inputSearchDepthMultiplier = 2

func getProcessReadBufSearchDepth(promptSearchDepth, inputLen int) int {
finalSearchDepth := promptSearchDepth

possibleSearchDepth := inputSearchDepthMultiplier * inputLen

if possibleSearchDepth > finalSearchDepth {
finalSearchDepth = possibleSearchDepth
}

return finalSearchDepth
}

func processReadBuf(rb []byte, searchDepth int) []byte {
if len(rb) <= searchDepth {
return rb
Expand Down Expand Up @@ -150,23 +162,18 @@ func (c *Channel) ReadUntilFuzzy(b []byte) ([]byte, error) {

rb = append(rb, nb...)

searchDepth := c.PromptSearchDepth

possibleSearchDepth := inputSearchDepthMultiplier * len(b)

if possibleSearchDepth > searchDepth {
searchDepth = possibleSearchDepth
}

if util.BytesRoughlyContains(b, processReadBuf(rb, searchDepth)) {
if util.BytesRoughlyContains(
b,
processReadBuf(rb, getProcessReadBufSearchDepth(c.PromptSearchDepth, len(b))),
) {
return rb, nil
}
}
}

// ReadUntilPrompt reads bytes out of the channel Q object until the channel PromptPattern regex
// pattern is seen in the output. Once that pattern is seen, all read bytes are returned.
func (c *Channel) ReadUntilPrompt() ([]byte, error) {
// ReadUntilExplicit reads bytes out of the channel Q object until the bytes b are seen in the
// output. Once the bytes are seen all read bytes are returned.
func (c *Channel) ReadUntilExplicit(b []byte) ([]byte, error) {
var rb []byte

for {
Expand All @@ -183,15 +190,18 @@ func (c *Channel) ReadUntilPrompt() ([]byte, error) {

rb = append(rb, nb...)

if c.PromptPattern.Match(processReadBuf(rb, c.PromptSearchDepth)) {
if bytes.Contains(
processReadBuf(rb, getProcessReadBufSearchDepth(c.PromptSearchDepth, len(b))),
b,
) {
return rb, nil
}
}
}

// ReadUntilAnyPrompt reads bytes out of the channel Q object until any of the prompts in the
// "prompts" argument are seen in the output. Once any pattern is seen, all read bytes are returned.
func (c *Channel) ReadUntilAnyPrompt(prompts []*regexp.Regexp) ([]byte, error) {
// ReadUntilPrompt reads bytes out of the channel Q object until the channel PromptPattern regex
// pattern is seen in the output. Once that pattern is seen, all read bytes are returned.
func (c *Channel) ReadUntilPrompt() ([]byte, error) {
var rb []byte

for {
Expand All @@ -208,19 +218,15 @@ func (c *Channel) ReadUntilAnyPrompt(prompts []*regexp.Regexp) ([]byte, error) {

rb = append(rb, nb...)

prb := processReadBuf(rb, c.PromptSearchDepth)

for _, p := range prompts {
if p.Match(prb) {
return rb, nil
}
if c.PromptPattern.Match(processReadBuf(rb, c.PromptSearchDepth)) {
return rb, nil
}
}
}

// ReadUntilExplicit reads bytes out of the channel Q object until the bytes b are seen in the
// output. Once the bytes are seen all read bytes are returned.
func (c *Channel) ReadUntilExplicit(b []byte) ([]byte, error) {
// ReadUntilAnyPrompt reads bytes out of the channel Q object until any of the prompts in the
// "prompts" argument are seen in the output. Once any pattern is seen, all read bytes are returned.
func (c *Channel) ReadUntilAnyPrompt(prompts []*regexp.Regexp) ([]byte, error) {
var rb []byte

for {
Expand All @@ -237,8 +243,12 @@ func (c *Channel) ReadUntilExplicit(b []byte) ([]byte, error) {

rb = append(rb, nb...)

if bytes.Contains(processReadBuf(rb, c.PromptSearchDepth), b) {
return rb, nil
prb := processReadBuf(rb, c.PromptSearchDepth)

for _, p := range prompts {
if p.Match(prb) {
return rb, nil
}
}
}
}

0 comments on commit c37671e

Please sign in to comment.