From 3a0fc0d57cb4cf09bf5283a16726a0cc708f654b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 9 Aug 2022 01:13:49 -0400 Subject: [PATCH] Added ReadString method to file --- file/file.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/file/file.go b/file/file.go index 2568aba..a1b4926 100644 --- a/file/file.go +++ b/file/file.go @@ -75,6 +75,29 @@ func (file *File) ReadRune () (char rune, size int, err error) { return } +// ReadString reads until the first occurrence of delimiter in the input, +// returning a string containing the data up to and including the delimiter. If +// ReadString encounters an error before finding a delimiter, it returns the +// data read before the error and the error itself (often io.EOF). ReadString +// returns err != nil if and only if the returned data does not end in delim. +func (file *File) ReadString (delimiter byte) (read string, err error) { + read, err = file.reader.ReadString(delimiter) + + // store the character in the file + for _, char := range read { + if char == '\n' { + file.lines = append(file.lines, "") + file.currentLine ++ + file.currentColumn = 0 + } else { + file.lines[file.currentLine] += string(char) + file.currentColumn ++ + } + } + + return +} + // Close closes the file. After the file is closed, data that has been read will // still be retained, and errors can be reported. func (file *File) Close () {