package main

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

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

func (ip *scanProvider) Scan() bool {
	if ip.isList {
		if len(ip.list) == 0 {
			return false
		}
		ip.text = ip.list[0]
		ip.list = ip.list[1:]
		return true
	}
	return ip.scanner.Scan()
}

func (ip *scanProvider) Text() string {
	if ip.isList {
		return ip.text
	}
	return ip.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
}
