// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package strings

import (
	
	
	
)

// Lines returns an iterator over the newline-terminated lines in the string s.
// The lines yielded by the iterator include their terminating newlines.
// If s is empty, the iterator yields no lines at all.
// If s does not end in a newline, the final yielded line will not end in a newline.
// It returns a single-use iterator.
func ( string) iter.Seq[string] {
	return func( func(string) bool) {
		for len() > 0 {
			var  string
			if  := IndexByte(, '\n');  >= 0 {
				,  = [:+1], [+1:]
			} else {
				,  = , ""
			}
			if !() {
				return
			}
		}
		return
	}
}

// explodeSeq returns an iterator over the runes in s.
func explodeSeq( string) iter.Seq[string] {
	return func( func(string) bool) {
		for len() > 0 {
			,  := utf8.DecodeRuneInString()
			if !([:]) {
				return
			}
			 = [:]
		}
	}
}

// splitSeq is SplitSeq or SplitAfterSeq, configured by how many
// bytes of sep to include in the results (none or all).
func splitSeq(,  string,  int) iter.Seq[string] {
	if len() == 0 {
		return explodeSeq()
	}
	return func( func(string) bool) {
		for {
			 := Index(, )
			if  < 0 {
				break
			}
			 := [:+]
			if !() {
				return
			}
			 = [+len():]
		}
		()
	}
}

// SplitSeq returns an iterator over all substrings of s separated by sep.
// The iterator yields the same strings that would be returned by Split(s, sep),
// but without constructing the slice.
// It returns a single-use iterator.
func (,  string) iter.Seq[string] {
	return splitSeq(, , 0)
}

// SplitAfterSeq returns an iterator over substrings of s split after each instance of sep.
// The iterator yields the same strings that would be returned by SplitAfter(s, sep),
// but without constructing the slice.
// It returns a single-use iterator.
func (,  string) iter.Seq[string] {
	return splitSeq(, , len())
}

// FieldsSeq returns an iterator over substrings of s split around runs of
// whitespace characters, as defined by unicode.IsSpace.
// The iterator yields the same strings that would be returned by Fields(s),
// but without constructing the slice.
func ( string) iter.Seq[string] {
	return func( func(string) bool) {
		 := -1
		for  := 0;  < len(); {
			 := 1
			 := rune([])
			 := asciiSpace[[]] != 0
			if  >= utf8.RuneSelf {
				,  = utf8.DecodeRuneInString([:])
				 = unicode.IsSpace()
			}
			if  {
				if  >= 0 {
					if !([:]) {
						return
					}
					 = -1
				}
			} else if  < 0 {
				 = 
			}
			 += 
		}
		if  >= 0 {
			([:])
		}
	}
}

// FieldsFuncSeq returns an iterator over substrings of s split around runs of
// Unicode code points satisfying f(c).
// The iterator yields the same strings that would be returned by FieldsFunc(s),
// but without constructing the slice.
func ( string,  func(rune) bool) iter.Seq[string] {
	return func( func(string) bool) {
		 := -1
		for  := 0;  < len(); {
			 := 1
			 := rune([])
			if  >= utf8.RuneSelf {
				,  = utf8.DecodeRuneInString([:])
			}
			if () {
				if  >= 0 {
					if !([:]) {
						return
					}
					 = -1
				}
			} else if  < 0 {
				 = 
			}
			 += 
		}
		if  >= 0 {
			([:])
		}
	}
}