vRp.CD2g_test/utils/str/range.go

18 lines
661 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package str
import "strings"
// RangeLine 对传入的eachString进行按行切片后再进行遍历
// - 该函数会预先对“\r\n”进行处理替换为“\n”。
// - 在遍历到每一行的时候会将结果index和line作为入参传入eachFunc中进行调用。
// - index表示了当前行的行号由0开始line表示了当前行的内容。
func RangeLine(eachString string, eachFunc func(index int, line string) error) error {
formatStr := strings.ReplaceAll(eachString, "\r\n", "\n")
for index, line := range strings.Split(formatStr, "\n") {
if err := eachFunc(index, line); err != nil {
return err
}
}
return nil
}