package main

import (
	"bufio"
	"unicode"
	"unicode/utf8"
)

type scanProvider struct {
	isList  bool
	text    string
	list    []string
	scanner *bufio.Scanner
}

func (sp *scanProvider) Scan() bool {
	if sp.isList {
		for sp.text = ""; len(sp.text) == 0 && len(sp.list) > 0; {
			sp.text, sp.list = sp.list[0], sp.list[1:]
		}
		return len(sp.text) > 0
	}
	return sp.scanner.Scan()
}

func (sp *scanProvider) Text() string {
	if sp.isList {
		return sp.text
	}
	return sp.scanner.Text()
}

func splitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
	start := 0
	for start < len(data) {
		r, width := utf8.DecodeRune(data[start:])
		if r == '\n' {
			return start + width, data[start : start+width], nil
		}
		if !unicode.IsSpace(r) {
			break
		}
		start += width
	}
	for i := start; i < len(data); {
		r, width := utf8.DecodeRune(data[i:])
		if r == '\n' {
			return i, data[start:i], nil
		}
		if unicode.IsSpace(r) {
			return i + width, data[start:i], nil
		}
		i += width
	}
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	return start, nil, nil
}
