// Copyright 2015 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 precis

import (
	
	

	
)

type nickAdditionalMapping struct {
	// TODO: This transformer needs to be stateless somehow…
	notStart  bool
	prevSpace bool
}

func ( *nickAdditionalMapping) () {
	.prevSpace = false
	.notStart = false
}

func ( *nickAdditionalMapping) (,  []byte,  bool) (,  int,  error) {
	// RFC 8266 §2.1.  Rules
	//
	// 2.  Additional Mapping Rule: The additional mapping rule consists of
	//     the following sub-rules.
	//
	//     a.  Map any instances of non-ASCII space to SPACE (U+0020); a
	//         non-ASCII space is any Unicode code point having a general
	//         category of "Zs", naturally with the exception of SPACE
	//         (U+0020).  (The inclusion of only ASCII space prevents
	//         confusion with various non-ASCII space code points, many of
	//         which are difficult to reproduce across different input
	//         methods.)
	//
	//     b.  Remove any instances of the ASCII space character at the
	//         beginning or end of a nickname (e.g., "stpeter " is mapped to
	//         "stpeter").
	//
	//     c.  Map interior sequences of more than one ASCII space character
	//         to a single ASCII space character (e.g., "St  Peter" is
	//         mapped to "St Peter").
	for  < len() {
		,  := utf8.DecodeRune([:])
		if  == 0 { // Incomplete UTF-8 encoding
			if ! {
				return , , transform.ErrShortSrc
			}
			 = 1
		}
		if unicode.Is(unicode.Zs, ) {
			.prevSpace = true
		} else {
			if .prevSpace && .notStart {
				[] = ' '
				 += 1
			}
			if  != copy([:], [:+]) {
				 += 
				return , , transform.ErrShortDst
			}
			 += 
			.prevSpace = false
			.notStart = true
		}
		 += 
	}
	return , , nil
}