From ca48a86ff3e410c9469a8deed3fc42b402a643e8 Mon Sep 17 00:00:00 2001 From: Chad Retz Date: Mon, 14 May 2018 15:36:29 -0500 Subject: [PATCH] Wrap godoc at 80 chars --- control/cmd_authenticate.go | 8 +++-- control/cmd_event.go | 48 +++++++++++++++++------------ control/cmd_onion.go | 15 ++++++--- control/cmd_protocolinfo.go | 3 +- control/conn.go | 27 ++++++++-------- control/doc.go | 10 +++--- control/keyval.go | 17 ++++++----- control/response.go | 18 ++++++----- process/embedded/process.go | 6 ++-- process/process.go | 21 ++++++++----- tor/doc.go | 2 +- tor/log.go | 3 +- tor/tor.go | 61 ++++++++++++++++++++++--------------- util/strings.go | 30 ++++++++++-------- 14 files changed, 162 insertions(+), 107 deletions(-) diff --git a/control/cmd_authenticate.go b/control/cmd_authenticate.go index 518f809..c43d864 100644 --- a/control/cmd_authenticate.go +++ b/control/cmd_authenticate.go @@ -9,9 +9,11 @@ import ( "strings" ) -// Authenticate authenticates with the Tor instance using the "best" possible authentication method if not already -// authenticated and sets the Authenticated field. The password argument is optional, and will only be used if the -// "SAFECOOKIE" and "NULL" authentication methods are not available and "HASHEDPASSWORD" is. +// Authenticate authenticates with the Tor instance using the "best" possible +// authentication method if not already authenticated and sets the Authenticated +// field. The password argument is optional, and will only be used if the +// "SAFECOOKIE" and "NULL" authentication methods are not available and +// "HASHEDPASSWORD" is. func (c *Conn) Authenticate(password string) error { if c.Authenticated { return nil diff --git a/control/cmd_event.go b/control/cmd_event.go index c79f941..2f503c4 100644 --- a/control/cmd_event.go +++ b/control/cmd_event.go @@ -48,8 +48,9 @@ const ( EventCodeTransportLaunched EventCode = "TRANSPORT_LAUNCHED" ) -// EventCodeUnrecognized is a special event code that is only used with AddEventListener and RemoveEventListener to listen -// for events that are unrecognized by this library (i.e. UnrecognizedEvent). +// EventCodeUnrecognized is a special event code that is only used with +// AddEventListener and RemoveEventListener to listen for events that are +// unrecognized by this library (i.e. UnrecognizedEvent). var EventCodeUnrecognized EventCode = "" var recognizedEventCodes = []EventCode{ @@ -97,16 +98,18 @@ func mapEventCodes() map[EventCode]struct{} { return ret } -// EventCodes returns a new slice of all event codes that are recognized (i.e. does not include EventCodeUnrecognized). +// EventCodes returns a new slice of all event codes that are recognized (i.e. +// does not include EventCodeUnrecognized). func EventCodes() []EventCode { ret := make([]EventCode, len(recognizedEventCodes)) copy(ret, recognizedEventCodes) return ret } -// HandleEvents loops until the context is closed dispatching async events. Can dispatch events even after context is -// done and of course during synchronous request. This will always end with an error, either from ctx.Done() or from an -// error reading/handling the event. +// HandleEvents loops until the context is closed dispatching async events. Can +// dispatch events even after context is done and of course during synchronous +// request. This will always end with an error, either from ctx.Done() or from +// an error reading/handling the event. func (c *Conn) HandleEvents(ctx context.Context) error { errCh := make(chan error, 1) go func() { @@ -125,8 +128,9 @@ func (c *Conn) HandleEvents(ctx context.Context) error { } } -// HandleNextEvent attempts to read and handle the next event. It will return on first message seen, event or not. -// Otherwise it will wait until there is a message read. +// HandleNextEvent attempts to read and handle the next event. It will return on +// first message seen, event or not. Otherwise it will wait until there is a +// message read. func (c *Conn) HandleNextEvent() error { c.readLock.Lock() defer c.readLock.Unlock() @@ -148,10 +152,12 @@ func (c *Conn) HandleNextEvent() error { return nil } -// AddEventListener adds the given channel as an event listener for the given events. Then Tor is notified about which -// events should be listened to. Callers are expected to call RemoveEventListener for the channel and all event codes -// used here before closing the channel. If no events are provided, this is essentially a no-op. The -// EventCodeUnrecognized event code can be used to listen for unrecognized events. +// AddEventListener adds the given channel as an event listener for the given +// events. Then Tor is notified about which events should be listened to. +// Callers are expected to call RemoveEventListener for the channel and all +// event codes used here before closing the channel. If no events are provided, +// this is essentially a no-op. The EventCodeUnrecognized event code can be used +// to listen for unrecognized events. func (c *Conn) AddEventListener(ch chan<- Event, events ...EventCode) error { // TODO: do we want to set the local map first? Or do we want to lock on the net request too? c.eventListenersLock.Lock() @@ -167,9 +173,11 @@ func (c *Conn) AddEventListener(ch chan<- Event, events ...EventCode) error { return c.sendSetEvents() } -// RemoveEventListener removes the given channel from being sent to by the given event codes. It is not an error to -// remove a channel from events AddEventListener was not called for. Tor is notified about events which may no longer be -// listened to. If no events are provided, this is essentially a no-op. +// RemoveEventListener removes the given channel from being sent to by the given +// event codes. It is not an error to remove a channel from events +// AddEventListener was not called for. Tor is notified about events which may +// no longer be listened to. If no events are provided, this is essentially a +// no-op. func (c *Conn) RemoveEventListener(ch chan<- Event, events ...EventCode) error { c.eventListenersLock.Lock() for _, event := range events { @@ -266,14 +274,16 @@ func parseISOTime2Frac(str string) time.Time { return ret } -// Event is the base interface for all known asynchronous +// Event is the base interface for all known asynchronous events. type Event interface { Code() EventCode } -// ParseEvent returns an Event for the given code and data info. Raw is the raw single line if it is a single-line -// event (even if it has newlines), dataArray is the array of lines for multi-line events. Only one of the two needs to -// be set. The response is never nil, but may be UnrecognizedEvent. Format errors are ignored per the Tor spec. +// ParseEvent returns an Event for the given code and data info. Raw is the raw +// single line if it is a single-line event (even if it has newlines), dataArray +// is the array of lines for multi-line events. Only one of the two needs to be +// set. The response is never nil, but may be UnrecognizedEvent. Format errors +// are ignored per the Tor spec. func ParseEvent(code EventCode, raw string, dataArray []string) Event { switch code { case EventCodeAddrMap: diff --git a/control/cmd_onion.go b/control/cmd_onion.go index aa94a7f..f4095c9 100644 --- a/control/cmd_onion.go +++ b/control/cmd_onion.go @@ -36,7 +36,8 @@ const ( KeyAlgoED25519V3 KeyAlgo = "ED25519-V3" ) -// Key is a type of key to use for AddOnion. Implementations include GenKey, RSAKey, and ED25519Key. +// Key is a type of key to use for AddOnion. Implementations include GenKey, +// RSAKey, and ED25519Key. type Key interface { // Type is the KeyType for AddOnion. Type() KeyType @@ -59,10 +60,12 @@ func KeyFromString(str string) (Key, error) { } } -// GenKey is a Key for AddOnion that asks Tor to generate a key for the given algorithm. +// GenKey is a Key for AddOnion that asks Tor to generate a key for the given +// algorithm. type GenKey KeyAlgo -// GenKeyFromBlob creates a GenKey for the given response blob which is a KeyAlgo. +// GenKeyFromBlob creates a GenKey for the given response blob which is a +// KeyAlgo. func GenKeyFromBlob(blob string) GenKey { return GenKey(KeyAlgo(blob)) } // Type implements Key.Type. @@ -121,9 +124,11 @@ type AddOnionRequest struct { Flags []string // MaxStreams is ADD_ONION MaxStreams. MaxStreams int - // Ports are ADD_ONION Port values. Key is virtual port, value is target port (or can be empty to use virtual port). + // Ports are ADD_ONION Port values. Key is virtual port, value is target + // port (or can be empty to use virtual port). Ports map[string]string - // ClientAuths are ADD_ONION ClientAuth values. If value is empty string, Tor will generate the password. + // ClientAuths are ADD_ONION ClientAuth values. If value is empty string, + // Tor will generate the password. ClientAuths map[string]string } diff --git a/control/cmd_protocolinfo.go b/control/cmd_protocolinfo.go index ece3fa4..e539d6d 100644 --- a/control/cmd_protocolinfo.go +++ b/control/cmd_protocolinfo.go @@ -24,7 +24,8 @@ func (p *ProtocolInfo) HasAuthMethod(authMethod string) bool { return false } -// ProtocolInfo invokes PROTOCOLINFO on first invocation and returns a cached result on all others. +// ProtocolInfo invokes PROTOCOLINFO on first invocation and returns a cached +// result on all others. func (c *Conn) ProtocolInfo() (*ProtocolInfo, error) { var err error if c.protocolInfo == nil { diff --git a/control/conn.go b/control/conn.go index 86b0b22..02995e8 100644 --- a/control/conn.go +++ b/control/conn.go @@ -9,15 +9,14 @@ import ( // Conn is the connection to the Tor control port. type Conn struct { - // No debug logs if nil - // DebugWriter is the writer that debug logs for this library (not Tor itself) will be written to. If nil, no debug - // logs are generated/written. + // DebugWriter is the writer that debug logs for this library (not Tor + // itself) will be written to. If nil, no debug logs are generated/written. DebugWriter io.Writer // This is the underlying connection. conn *textproto.Conn - // This is set lazily by ProtocolInfo() + // This is set lazily by ProtocolInfo(). protocolInfo *ProtocolInfo // True if Authenticate has been called successfully. @@ -25,12 +24,14 @@ type Conn struct { // The lock fot eventListeners eventListenersLock sync.RWMutex - // The value slices can be traversed outside of lock, they are completely replaced on change, never mutated. But the - // map itself must be locked on when reading or writing. + // The value slices can be traversed outside of lock, they are completely + // replaced on change, never mutated. But the map itself must be locked on + // when reading or writing. eventListeners map[EventCode][]chan<- Event - // This mutex is locked on when an entire response needs to be read. It helps synchronize accesses to the response - // by the asynchronous response listeners and the synchronous responses. + // This mutex is locked on when an entire response needs to be read. It + // helps synchronize accesses to the response by the asynchronous response + // listeners and the synchronous responses. readLock sync.Mutex } @@ -47,8 +48,9 @@ func (c *Conn) sendRequestIgnoreResponse(format string, args ...interface{}) err return err } -// SendRequest sends a synchronous request to Tor and awaits the response. If the response errors, the error result will -// be set, but the response will be set also. This is usually not directly used by callers, but instead called by +// SendRequest sends a synchronous request to Tor and awaits the response. If +// the response errors, the error result will be set, but the response will be +// set also. This is usually not directly used by callers, but instead called by // higher-level methods. func (c *Conn) SendRequest(format string, args ...interface{}) (*Response, error) { if c.debugEnabled() { @@ -76,8 +78,9 @@ func (c *Conn) SendRequest(format string, args ...interface{}) (*Response, error return resp, err } -// Close sends a QUIT and closes the underlying Tor connection. This does not error if the QUIT is not accepted but does -// relay any error that occurs while closing the underlying connection. +// Close sends a QUIT and closes the underlying Tor connection. This does not +// error if the QUIT is not accepted but does relay any error that occurs while +// closing the underlying connection. func (c *Conn) Close() error { // Ignore the response and ignore the error c.Quit() diff --git a/control/doc.go b/control/doc.go index 91a1171..2be3dc1 100644 --- a/control/doc.go +++ b/control/doc.go @@ -1,8 +1,10 @@ -// Package control implements a low-level client for the Tor control spec version 1. +// Package control implements a low-level client for the Tor control spec +// version 1. // -// The primary entrypoint is the Conn struct, instantiated with NewConn. This is the low-level layer to the control -// port of an already-running Tor instance. Most developers will prefer the tor package adjacent to this one for a -// higher level abstraction over the process and this connection. +// The primary entrypoint is the Conn struct, instantiated with NewConn. This is +// the low-level layer to the control port of an already-running Tor instance. +// Most developers will prefer the tor package adjacent to this one for a higher +// level abstraction over the process and this connection. // // Some of this code is lifted from https://github.com/yawning/bulb with thanks. package control diff --git a/control/keyval.go b/control/keyval.go index 3d20332..00cefd8 100644 --- a/control/keyval.go +++ b/control/keyval.go @@ -1,17 +1,18 @@ package control -// KeyVal is a simple key-value struct. In cases where Val can be nil, an empty string represents that unless -// ValSetAndEmpty is true. +// KeyVal is a simple key-value struct. In cases where Val can be nil, an empty +// string represents that unless ValSetAndEmpty is true. type KeyVal struct { // Key is the always-present key Key string - // Val is the value. If it's an empty string and nils are accepted/supported where this is used, it means nil unless - // ValSetAndEmpty is true. + // Val is the value. If it's an empty string and nils are accepted/supported + // where this is used, it means nil unless ValSetAndEmpty is true. Val string - // ValSetAndEmpty is true when Val is an empty string, the associated command supports nils, and Val should NOT be - // treated as nil. False otherwise. + // ValSetAndEmpty is true when Val is an empty string, the associated + // command supports nils, and Val should NOT be treated as nil. False + // otherwise. ValSetAndEmpty bool } @@ -20,8 +21,8 @@ func NewKeyVal(key string, val string) *KeyVal { return &KeyVal{Key: key, Val: val} } -// KeyVals creates multiple new key-value pairs from the given strings. The provided set of strings must have a length -// that is a multiple of 2. +// KeyVals creates multiple new key-value pairs from the given strings. The +// provided set of strings must have a length that is a multiple of 2. func KeyVals(keysAndVals ...string) []*KeyVal { if len(keysAndVals)%2 != 0 { panic("Expected multiple of 2") diff --git a/control/response.go b/control/response.go index f9011b2..705905e 100644 --- a/control/response.go +++ b/control/response.go @@ -8,22 +8,25 @@ import ( // Response is a response to a control port command or an asynchronous event. type Response struct { - // Err is the status code and string representation associated with a response. Responses that have completed - // successfully will also have Err set to indicate such. + // Err is the status code and string representation associated with a + // response. Responses that have completed successfully will also have Err + // set to indicate such. Err *textproto.Error // Reply is the text on the EndReplyLine of the response. Reply string - // Data is the MidReplyLines/DataReplyLines of the response. Dot encoded data is "decoded" and presented as a single - // string (terminal ".CRLF" removed, all intervening CRs stripped). + // Data is the MidReplyLines/DataReplyLines of the response. Dot encoded + // data is "decoded" and presented as a single string (terminal ".CRLF" + // removed, all intervening CRs stripped). Data []string // RawLines is all of the lines of a response, without CRLFs. RawLines []string } -// IsOk returns true if the response status code indicates success or an asynchronous event. +// IsOk returns true if the response status code indicates success or an +// asynchronous event. func (r *Response) IsOk() bool { switch r.Err.Code { case StatusOk, StatusOkUnnecessary, StatusAsyncEvent: @@ -33,7 +36,8 @@ func (r *Response) IsOk() bool { } } -// DataWithReply returns a combination of Data and Reply to give a full set of the lines of the response. +// DataWithReply returns a combination of Data and Reply to give a full set of +// the lines of the response. func (r *Response) DataWithReply() []string { ret := make([]string, len(r.Data)+1) copy(ret, r.Data) @@ -41,7 +45,7 @@ func (r *Response) DataWithReply() []string { return ret } -// IsAsync returns true if the response is an asyncrhonous event. +// IsAsync returns true if the response is an asynchronous event. func (r *Response) IsAsync() bool { return r.Err.Code == StatusAsyncEvent } diff --git a/process/embedded/process.go b/process/embedded/process.go index 4929370..295c258 100644 --- a/process/embedded/process.go +++ b/process/embedded/process.go @@ -1,11 +1,13 @@ -// Package embedded implements process interfaces for statically linked, embedded Tor. +// Package embedded implements process interfaces for statically linked, +// embedded Tor. // // TODO: not finished yet package embedded import "github.com/cretz/bine/process" -// NewCreator creates a process.Creator for statically linked embedded in the binary Tor. +// NewCreator creates a process.Creator for statically-linked Tor embedded in +// the binary. func NewCreator() process.Creator { panic("TODO: embedding not implemented yet") } diff --git a/process/process.go b/process/process.go index 1d6b32b..06614f3 100644 --- a/process/process.go +++ b/process/process.go @@ -1,8 +1,10 @@ // Package process is the low-level abstraction for a Tor instance. // -// The standard use is to create a Creator with NewCreator and the path to the Tor executable. The child package -// 'embedded' can be used if Tor is statically linked in the binary. Most developers will prefer the tor package -// adjacent to this one for a higher level abstraction over the process and control port connection. +// The standard use is to create a Creator with NewCreator and the path to the +// Tor executable. The child package 'embedded' can be used if Tor is statically +// linked in the binary. Most developers will prefer the tor package adjacent to +// this one for a higher level abstraction over the process and control port +// connection. package process import ( @@ -18,10 +20,11 @@ import ( // Process is the interface implemented by Tor processes. type Process interface { - // Start starts the Tor process in the background and returns. It is analagous to os/exec.Cmd.Start. + // Start starts the Tor process in the background and returns. It is + // analagous to os/exec.Cmd.Start. Start() error - // Wait waits for the Tor process to exit and returns error if it was not a successful exit. It is analagous to - // os/exec.Cmd.Wait. + // Wait waits for the Tor process to exit and returns error if it was not a + // successful exit. It is analagous to os/exec.Cmd.Wait. Wait() error } @@ -34,7 +37,8 @@ type exeProcessCreator struct { exePath string } -// NewCreator creates a Creator for external Tor process execution based on the given exe path. +// NewCreator creates a Creator for external Tor process execution based on the +// given exe path. func NewCreator(exePath string) Creator { return &exeProcessCreator{exePath} } @@ -46,7 +50,8 @@ func (e *exeProcessCreator) New(ctx context.Context, args ...string) (Process, e return cmd, nil } -// ControlPortFromFileContents reads a control port file that is written by Tor when ControlPortWriteToFile is set. +// ControlPortFromFileContents reads a control port file that is written by Tor +// when ControlPortWriteToFile is set. func ControlPortFromFileContents(contents string) (int, error) { contents = strings.TrimSpace(contents) _, port, ok := util.PartitionString(contents, ':') diff --git a/tor/doc.go b/tor/doc.go index c2454ea..63312d1 100644 --- a/tor/doc.go +++ b/tor/doc.go @@ -1,2 +1,2 @@ -// Package tor is the high-level client for Tor +// Package tor is the high-level client for Tor. package tor diff --git a/tor/log.go b/tor/log.go index c350439..b53f318 100644 --- a/tor/log.go +++ b/tor/log.go @@ -7,7 +7,8 @@ func (t *Tor) DebugEnabled() bool { return t.DebugWriter != nil } -// Debugf writes the formatted string with a newline appended to the DebugWriter if present. +// Debugf writes the formatted string with a newline appended to the DebugWriter +// if present. func (t *Tor) Debugf(format string, args ...interface{}) { if w := t.DebugWriter; w != nil { fmt.Fprintf(w, format+"\n", args...) diff --git a/tor/tor.go b/tor/tor.go index c5ab174..5259325 100644 --- a/tor/tor.go +++ b/tor/tor.go @@ -16,8 +16,9 @@ import ( "github.com/cretz/bine/process" ) -// Tor is the wrapper around the Tor process and control port connection. It should be created with Start and developers -// should always call Close when done. +// Tor is the wrapper around the Tor process and control port connection. It +// should be created with Start and developers should always call Close when +// done. type Tor struct { // Process is the Tor instance that is running. Process process.Process @@ -25,8 +26,8 @@ type Tor struct { // Control is the Tor controller connection. Control *control.Conn - // ProcessCancelFunc is the context cancellation func for the Tor process. It is used by Close and should not be - // called directly. This can be nil. + // ProcessCancelFunc is the context cancellation func for the Tor process. + // It is used by Close and should not be called directly. This can be nil. ProcessCancelFunc context.CancelFunc // ControlPort is the port that Control is connected on. @@ -35,40 +36,49 @@ type Tor struct { // DataDir is the path to the data directory that Tor is using. DataDir string - // DeleteDataDirOnClose is true if, when Close is invoked, the entire directory will be deleted. + // DeleteDataDirOnClose is true if, when Close is invoked, the entire + // directory will be deleted. DeleteDataDirOnClose bool - // DebugWriter is the writer used for debug logs, or nil if debug logs should not be emitted. + // DebugWriter is the writer used for debug logs, or nil if debug logs + // should not be emitted. DebugWriter io.Writer // StopProcessOnClose, if true, will attempt to halt the process on close. StopProcessOnClose bool } -// StartConf is the configuration used for Start when starting a Tor instance. A default instance with no fields set is -// the default used for Start. +// StartConf is the configuration used for Start when starting a Tor instance. A +// default instance with no fields set is the default used for Start. type StartConf struct { - // ExePath is the path to the Tor executable. If it is not present, "tor" is used either locally or on the PATH. + // ExePath is the path to the Tor executable. If it is not present, "tor" is + // used either locally or on the PATH. ExePath string - // Embedded is true if Tor is statically compiled. If true, ExePath is ignored. + // Embedded is true if Tor is statically compiled. If true, ExePath is + // ignored. Embedded bool - // ControlPort is the port to use for the Tor controller. If it is 0, Tor picks a port for use. + // ControlPort is the port to use for the Tor controller. If it is 0, Tor + // picks a port for use. ControlPort int - // DataDir is the directory used by Tor. If it is empty, a temporary directory is created in TempDataDirBase. + // DataDir is the directory used by Tor. If it is empty, a temporary + // directory is created in TempDataDirBase. DataDir string - // TempDataDirBase is the parent directory that a temporary data directory will be created under for use by Tor. - // This is ignored if DataDir is not empty. If empty it is assumed to be the current working directory. + // TempDataDirBase is the parent directory that a temporary data directory + // will be created under for use by Tor. This is ignored if DataDir is not + // empty. If empty it is assumed to be the current working directory. TempDataDirBase string - // RetainTempDataDir, if true, will not set the created temporary data directory to be deleted on close. This is - // ignored if DataDir is not empty. + // RetainTempDataDir, if true, will not set the created temporary data + // directory to be deleted on close. This is ignored if DataDir is not + // empty. RetainTempDataDir bool - // DisableCookieAuth, if true, will not use the default SAFECOOKIE authentication mechanism for the Tor controller. + // DisableCookieAuth, if true, will not use the default SAFECOOKIE + // authentication mechanism for the Tor controller. DisableCookieAuth bool // DisableEagerAuth, if true, will not authenticate on Start. @@ -77,19 +87,21 @@ type StartConf struct { // EnableNetwork, if true, will connect to the wider Tor network on start. EnableNetwork bool - // ExtraArgs is the set of extra args passed to the Tor instance when started. + // ExtraArgs is the set of extra args passed to the Tor instance when + // started. ExtraArgs []string - // TorrcFile is the torrc file to on start. If empty, a blank torrc is created in the data directory and is used - // instead. + // TorrcFile is the torrc file to on start. If empty, a blank torrc is + // created in the data directory and is used instead. TorrcFile string - // DebugWriter is the writer to use for debug logs, or nil for no debug logs. + // DebugWriter is the writer to use for debug logs, or nil for no debug + // logs. DebugWriter io.Writer } -// Start a Tor instance and connect to it. If ctx is nil, context.Background() is used. If conf is nil, a default -// instance is used. +// Start a Tor instance and connect to it. If ctx is nil, context.Background() +// is used. If conf is nil, a default instance is used. func Start(ctx context.Context, conf *StartConf) (*Tor, error) { if ctx == nil { ctx = context.Background() @@ -231,7 +243,8 @@ func (t *Tor) connectController(ctx context.Context, conf *StartConf) error { return nil } -// Close sends a halt to the Tor process if it can, closes the controller connection, and stops the process. +// Close sends a halt to the Tor process if it can, closes the controller +// connection, and stops the process. func (t *Tor) Close() error { errs := []error{} // If controller is authenticated, send the quit signal to the process. Otherwise, just close the controller. diff --git a/util/strings.go b/util/strings.go index 48aed29..07a4e5e 100644 --- a/util/strings.go +++ b/util/strings.go @@ -5,8 +5,9 @@ import ( "strings" ) -// PartitionString returns the two parts of a string delimited by the first occurrence of ch. If ch does not exist, the -// second string is empty and the resulting bool is false. Otherwise it is true. +// PartitionString returns the two parts of a string delimited by the first +// occurrence of ch. If ch does not exist, the second string is empty and the +// resulting bool is false. Otherwise it is true. func PartitionString(str string, ch byte) (string, string, bool) { index := strings.IndexByte(str, ch) if index == -1 { @@ -15,8 +16,8 @@ func PartitionString(str string, ch byte) (string, string, bool) { return str[:index], str[index+1:], true } -// PartitionStringFromEnd is same as PartitionString except it delimts by the last occurrence of ch instead of the -// first. +// PartitionStringFromEnd is same as PartitionString except it delimts by the +// last occurrence of ch instead of the first. func PartitionStringFromEnd(str string, ch byte) (string, string, bool) { index := strings.LastIndexByte(str, ch) if index == -1 { @@ -25,8 +26,9 @@ func PartitionStringFromEnd(str string, ch byte) (string, string, bool) { return str[:index], str[index+1:], true } -// EscapeSimpleQuotedStringIfNeeded calls EscapeSimpleQuotedString only if the string contains a space, backslash, -// double quote, newline, or carriage return character. +// EscapeSimpleQuotedStringIfNeeded calls EscapeSimpleQuotedString only if the +// string contains a space, backslash, double quote, newline, or carriage return +// character. func EscapeSimpleQuotedStringIfNeeded(str string) string { if strings.ContainsAny(str, " \\\"\r\n") { return EscapeSimpleQuotedString(str) @@ -41,18 +43,20 @@ var simpleQuotedStringEscapeReplacer = strings.NewReplacer( "\n", "\\n", ) -// EscapeSimpleQuotedString calls EscapeSimpleQuotedStringContents and then surrounds the entire string with double -// quotes. +// EscapeSimpleQuotedString calls EscapeSimpleQuotedStringContents and then +// surrounds the entire string with double quotes. func EscapeSimpleQuotedString(str string) string { return "\"" + simpleQuotedStringEscapeReplacer.Replace(str) + "\"" } -// EscapeSimpleQuotedStringContents escapes backslashes, double quotes, newlines, and carriage returns in str. +// EscapeSimpleQuotedStringContents escapes backslashes, double quotes, +// newlines, and carriage returns in str. func EscapeSimpleQuotedStringContents(str string) string { return simpleQuotedStringEscapeReplacer.Replace(str) } -// UnescapeSimpleQuotedStringIfNeeded calls UnescapeSimpleQuotedString only if str is surrounded with double quotes. +// UnescapeSimpleQuotedStringIfNeeded calls UnescapeSimpleQuotedString only if +// str is surrounded with double quotes. func UnescapeSimpleQuotedStringIfNeeded(str string) (string, error) { if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' { return UnescapeSimpleQuotedString(str) @@ -60,7 +64,8 @@ func UnescapeSimpleQuotedStringIfNeeded(str string) (string, error) { return str, nil } -// UnescapeSimpleQuotedString removes surrounding double quotes and calls UnescapeSimpleQuotedStringContents. +// UnescapeSimpleQuotedString removes surrounding double quotes and calls +// UnescapeSimpleQuotedStringContents. func UnescapeSimpleQuotedString(str string) (string, error) { if len(str) < 2 || str[0] != '"' || str[len(str)-1] != '"' { return "", fmt.Errorf("Missing quotes") @@ -68,7 +73,8 @@ func UnescapeSimpleQuotedString(str string) (string, error) { return UnescapeSimpleQuotedStringContents(str[1 : len(str)-1]) } -// UnescapeSimpleQuotedStringContents unescapes backslashes, double quotes, newlines, and carriage returns. +// UnescapeSimpleQuotedStringContents unescapes backslashes, double quotes, +// newlines, and carriage returns. func UnescapeSimpleQuotedStringContents(str string) (string, error) { ret := "" escaping := false