From d52a23c49bf5ab2511aebf7e811a8bc2de48b405 Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Thu, 15 Aug 2013 13:04:58 -0700 Subject: [PATCH] Avoid adding empty tickers --- line_editor.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/line_editor.go b/line_editor.go index 7046dfd..0816510 100644 --- a/line_editor.go +++ b/line_editor.go @@ -11,18 +11,20 @@ import ( ) type LineEditor struct { - command rune - prompt string - cursor int - input string - screen *Screen - quotes *Quotes + command rune // Keyboard command such as '+' or '-'. + cursor int // Current cursor position within the input line. + prompt string // Prompt string for the command. + input string // User typed input string. + screen *Screen // Pointer to Screen. + quotes *Quotes // Pointer to Quotes. + regex *regexp.Regexp // Regex to split comma-delimited input string. } //----------------------------------------------------------------------------- func (self *LineEditor) Initialize(screen *Screen, quotes *Quotes) *LineEditor { self.screen = screen self.quotes = quotes + self.regex = regexp.MustCompile(`[,\s]+`) return self } @@ -168,9 +170,6 @@ func (self *LineEditor) execute() *LineEditor { for i := before; i > after; i-- { self.screen.ClearLine(0, i + 4) } - if after == 0 { // Hide quotes header is the are no tickers left. - self.screen.ClearLine(0, 4) - } } } } @@ -186,8 +185,11 @@ func (self *LineEditor) done() bool { return true } -//----------------------------------------------------------------------------- +// +// Split by whitespace/comma to convert a string to array of tickers. Make sure +// the string is trimmed to avoid empty tickers in the array. +// func (self *LineEditor) tokenize() []string { - input := strings.ToUpper(strings.TrimSpace(self.input)) - return regexp.MustCompile(`[,\s]+`).Split(input, -1) + input := strings.ToUpper(strings.Trim(self.input, `, `)) + return self.regex.Split(input, -1) }