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

Move appproto private functions to the only file that uses them #2296

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions private/pkg/app/appproto/appproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
package appproto

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"path/filepath"
"unicode"
"unicode/utf8"

"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/protodescriptor"
Expand Down Expand Up @@ -256,43 +252,3 @@ func newRunFunc(handler Handler) func(context.Context, app.Container) error {
func NewResponseBuilder(container app.StderrContainer) ResponseBuilder {
return newResponseBuilder(container)
}

// leadingWhitespace iterates through the given string,
// and returns the leading whitespace substring, if any,
// respecting utf-8 encoding.
//
// leadingWhitespace("\u205F foo ") -> "\u205F "
func leadingWhitespace(buf []byte) []byte {
leadingSize := 0
iterBuf := buf
for len(iterBuf) > 0 {
r, size := utf8.DecodeRune(iterBuf)
// protobuf strings must always be valid UTF8
// https://developers.google.com/protocol-buffers/docs/proto3#scalar
// Additionally, utf8.RuneError is not a space so we'll terminate
// and return the leading, valid, UTF8 whitespace sequence.
if !unicode.IsSpace(r) {
out := make([]byte, leadingSize)
copy(out, buf)
return out
}
leadingSize += size
iterBuf = iterBuf[size:]
}
return buf
}

// scanWithPrefixAndLineEnding iterates over each of the given scanner's lines
// prepends prefix, and appends the newline sequence.
func scanWithPrefixAndLineEnding(scanner *bufio.Scanner, prefix []byte, newline []byte) []byte {
result := bytes.NewBuffer(nil)
result.Grow(averageInsertionPointSize)
for scanner.Scan() {
// These writes cannot fail, they will panic if they cannot
// allocate
_, _ = result.Write(prefix)
_, _ = result.Write(scanner.Bytes())
_, _ = result.Write(newline)
}
return result.Bytes()
}
42 changes: 42 additions & 0 deletions private/pkg/app/appproto/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"bytes"
"context"
"io"
"unicode"
"unicode/utf8"

"github.com/bufbuild/buf/private/pkg/storage"
"go.uber.org/multierr"
Expand Down Expand Up @@ -145,6 +147,46 @@ func writeInsertionPoint(
return postInsertionBytes[:len(postInsertionBytes)-1], nil
}

// leadingWhitespace iterates through the given string,
// and returns the leading whitespace substring, if any,
// respecting utf-8 encoding.
//
// leadingWhitespace("\u205F foo ") -> "\u205F "
func leadingWhitespace(buf []byte) []byte {
leadingSize := 0
iterBuf := buf
for len(iterBuf) > 0 {
r, size := utf8.DecodeRune(iterBuf)
// protobuf strings must always be valid UTF8
// https://developers.google.com/protocol-buffers/docs/proto3#scalar
// Additionally, utf8.RuneError is not a space so we'll terminate
// and return the leading, valid, UTF8 whitespace sequence.
if !unicode.IsSpace(r) {
out := make([]byte, leadingSize)
copy(out, buf)
return out
}
leadingSize += size
iterBuf = iterBuf[size:]
}
return buf
}

// scanWithPrefixAndLineEnding iterates over each of the given scanner's lines
// prepends prefix, and appends the newline sequence.
func scanWithPrefixAndLineEnding(scanner *bufio.Scanner, prefix []byte, newline []byte) []byte {
result := bytes.NewBuffer(nil)
result.Grow(averageInsertionPointSize)
for scanner.Scan() {
// These writes cannot fail, they will panic if they cannot
// allocate
_, _ = result.Write(prefix)
_, _ = result.Write(scanner.Bytes())
_, _ = result.Write(newline)
}
return result.Bytes()
}

type writeResponseOptions struct {
insertionPointReadBucket storage.ReadBucket
}
Expand Down
Loading