// FIXME: backported from go1.13 strings package, remove once terraform is
//        using go >= 1.13
// ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences
// replaced by the replacement string, which may be empty.
backported from go1.13 strings package, remove once terraform is
using go >= 1.13
ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences
replaced by the replacement string, which may be empty.
// FIXME : backported from go1 . 13 strings package remove once terraform is // using go > = 1 . 13 // ToValidUTF8 returns a copy of the string s with each run of invalid UTF - 8 byte sequences // replaced by the replacement string which may be empty .
func toValidUTF8(s, replacement string) string {
	var b strings.Builder
	for i, c := range s {
		if c != utf8.RuneError {
			continue
		}
		_, wid := utf8.DecodeRuneInString(s[i:])
		if wid == 1 {
			b.Grow(len(s) + len(replacement))
			b.WriteString(s[:i])
			s = s[i:]
			break
		}
	}
	if b.Cap() == 0 { 
		return s
	}
	invalid := false 
	for i := 0; i < len(s); {
		c := s[i]
		if c < utf8.RuneSelf {
			i++
			invalid = false
			b.WriteByte(c)
			continue
		}
		_, wid := utf8.DecodeRuneInString(s[i:])
		if wid == 1 {
			i++
			if !invalid {
				invalid = true
				b.WriteString(replacement)
			}
			continue
		}
		invalid = false
		b.WriteString(s[i : i+wid])
		i += wid
	}
	return b.String()
}
func toValidUTF8 ( s , replacement string ) string { var b strings . Builder for i , c := range s { if c != utf8 . RuneError { continue } _ , wid := utf8 . DecodeRuneInString ( s [ i : ] ) if wid == 1 { b . Grow ( len ( s ) + len ( replacement ) ) b . WriteString ( s [ : i ] ) s = s [ i : ] break } } if b . Cap ( ) == 0 { return s } invalid := false for i := 0 ; i < len ( s ) ; { c := s [ i ] if c < utf8 . RuneSelf { i ++ invalid = false b . WriteByte ( c ) continue } _ , wid := utf8 . DecodeRuneInString ( s [ i : ] ) if wid == 1 { i ++ if ! invalid { invalid = true b . WriteString ( replacement ) } continue } invalid = false b . WriteString ( s [ i : i + wid ] ) i += wid } return b . String ( ) }
backported from go1.13 strings package, remove once terraform is
using go >= 1.13
ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences
replaced by the replacement string, which may be empty.
backported from go1 . 13 strings package remove once terraform is using go > = 1 . 13 ToValidUTF8 returns a copy of the string s with each run of invalid UTF - 8 byte sequences replaced by the replacement string which may be empty .
// Fast path for unchanged input // didn't call b.Grow above // previous byte was from an invalid UTF-8 sequence