package jx

import (
	
)

const hexChars = "0123456789abcdef"

// safeSet holds the value true if the ASCII character with the given array
// position can be represented inside a JSON string without any further
// escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), and the backslash character ("\").
var safeSet = [256]byte{
	// First 31 characters.
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	'"':  1,
	'\\': 1,
}

// Str encodes string without html escaping.
//
// Use StrEscape to escape html, this is default for encoding/json and
// should be used by default for untrusted strings.
func ( *Writer) ( string) bool {
	return writeStr(, )
}

// ByteStr encodes string without html escaping.
//
// Use ByteStrEscape to escape html, this is default for encoding/json and
// should be used by default for untrusted strings.
func ( *Writer) ( []byte) bool {
	return writeStr(, )
}

func [ byteseq.Byteseq]( *Writer,  ) ( bool) {
	 = .byte('"')

	// Fast path, without utf8 and escape support.
	var (
		      = 0
		 = len()
	)
	for ;  <  && !; ++ {
		 := []
		if safeSet[] != 0 {
			break
		}
	}
	 =  || writeStreamByteseq(, [:])
	if  ==  {
		return  || .byte('"')
	}
	return  || strSlow[](, [:])
}

func [ byteseq.Byteseq]( *Writer,  ) ( bool) {
	var ,  int
	// for the remaining parts, we process them char by char
	for  < len() && ! {
		 := []
		if safeSet[] == 0 {
			++
			continue
		}
		if  <  {
			 =  || writeStreamByteseq(, [:])
		}

		switch  {
		case '\\', '"':
			 =  || .twoBytes('\\', )
		case '\n':
			 =  || .twoBytes('\\', 'n')
		case '\r':
			 =  || .twoBytes('\\', 'r')
		case '\t':
			 =  || .twoBytes('\\', 't')
		default:
			// This encodes bytes < 0x20 except for \t, \n and \r.
			// If escapeHTML is set, it also escapes <, >, and &
			// because they can lead to security holes when
			// user-controlled strings are rendered into JSON
			// and served to some browsers.
			 =  || .rawStr(`\u00`) || .twoBytes(hexChars[>>4], hexChars[&0xF])
		}
		++
		 = 
		continue
	}
	if  < len() {
		 =  || writeStreamByteseq(, [:])
	}
	return  || .byte('"')
}