// Copyright 2009 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.

// Copied and modified from Go 1.14 stdlib's encoding/xml

package xml

import (
	
)

// Copied from Go 1.14 stdlib's encoding/xml
var (
	escQuot = []byte(""") // shorter than """
	escApos = []byte("'") // shorter than "'"
	escAmp  = []byte("&")
	escLT   = []byte("<")
	escGT   = []byte(">")
	escTab  = []byte("	")
	escNL   = []byte("
")
	escCR   = []byte("
")
	escFFFD = []byte("\uFFFD") // Unicode replacement character

	// Additional Escapes
	escNextLine = []byte("…")
	escLS       = []byte("
")
)

// Decide whether the given rune is in the XML Character Range, per
// the Char production of https://www.xml.com/axml/testaxml.htm,
// Section 2.2 Characters.
func ( rune) ( bool) {
	return  == 0x09 ||
		 == 0x0A ||
		 == 0x0D ||
		 >= 0x20 &&  <= 0xD7FF ||
		 >= 0xE000 &&  <= 0xFFFD ||
		 >= 0x10000 &&  <= 0x10FFFF
}

// TODO: When do we need to escape the string?
// Based on encoding/xml escapeString from the Go Standard Library.
// https://golang.org/src/encoding/xml/xml.go
func ( writer,  string) {
	var  []byte
	 := 0
	for  := 0;  < len(); {
		,  := utf8.DecodeRuneInString([:])
		 += 
		switch  {
		case '"':
			 = escQuot
		case '\'':
			 = escApos
		case '&':
			 = escAmp
		case '<':
			 = escLT
		case '>':
			 = escGT
		case '\t':
			 = escTab
		case '\n':
			 = escNL
		case '\r':
			 = escCR
		case '\u0085':
			// Not escaped by stdlib
			 = escNextLine
		case '\u2028':
			// Not escaped by stdlib
			 = escLS
		default:
			if !isInCharacterRange() || ( == 0xFFFD &&  == 1) {
				 = escFFFD
				break
			}
			continue
		}
		.WriteString([ : -])
		.Write()
		 = 
	}
	.WriteString([:])
}

// escapeText writes to w the properly escaped XML equivalent
// of the plain text data s. If escapeNewline is true, newline
// characters will be escaped.
//
// Based on encoding/xml escapeText from the Go Standard Library.
// https://golang.org/src/encoding/xml/xml.go
func ( writer,  []byte) {
	var  []byte
	 := 0
	for  := 0;  < len(); {
		,  := utf8.DecodeRune([:])
		 += 
		switch  {
		case '"':
			 = escQuot
		case '\'':
			 = escApos
		case '&':
			 = escAmp
		case '<':
			 = escLT
		case '>':
			 = escGT
		case '\t':
			 = escTab
		case '\n':
			// This always escapes newline, which is different than stdlib's optional
			// escape of new line.
			 = escNL
		case '\r':
			 = escCR
		case '\u0085':
			// Not escaped by stdlib
			 = escNextLine
		case '\u2028':
			// Not escaped by stdlib
			 = escLS
		default:
			if !isInCharacterRange() || ( == 0xFFFD &&  == 1) {
				 = escFFFD
				break
			}
			continue
		}
		.Write([ : -])
		.Write()
		 = 
	}
	.Write([:])
}