// SanitizeRect returns the top left corner and the positive width and height of the
// given (possibly unsanitized) rectangle taking into account the line width r.
549070
func SanitizeRect(x, y, w, h, r int) (int, int, int, int) {
	if w < 0 {
		x += w
		w = -w
	}
	if h < 0 {
		y += h
		h = -h
	}

	d := (imax(1, r) - 1) / 2
	// TODO: what if w-2D <= 0 ?
	return x + d, y + d, w - 2*d, h - 2*d
}
625032
func (b *NaiveBayes) UpdateSanitize(sanitize func(rune) bool) {
	b.sanitize = transform.RemoveFunc(sanitize)
}
759548
func safeJoinLabels(labels []string) string {
	sanitizedLabels := make([]string, len(labels))
	for idx, label := range labels {
		sanitizedLabels[idx] = safeLabel(label)
	}
	return strings.Join(sanitizedLabels, ".")
}
798402
func sanitize(cert []byte) string {
	s := string(cert)
	r := strings.NewReplacer("-----BEGIN CERTIFICATE-----", "",
		"-----END CERTIFICATE-----", "",
		"\n", "")
	cleaned := r.Replace(s)

	return url.QueryEscape(cleaned)
}
915788
func sanitize(b []byte) []byte {
	// Replace ` with `+"`"+`
	b = bytes.Replace(b, []byte("`"), []byte("`+\"`\"+`"), -1)

	// Replace BOM with `+"\xEF\xBB\xBF"+`
	// (A BOM is valid UTF-8 but not permitted in Go source files.
	// I wouldn't bother handling this, but for some insane reason
	// jquery.js has a BOM somewhere in the middle.)
	return bytes.Replace(b, []byte("\xEF\xBB\xBF"), []byte("`+\"\\xEF\\xBB\\xBF\"+`"), -1)
}
1323671
func (ns *Namespace) SanitizeURL(a interface{}) (string, error) {
	s, err := cast.ToStringE(a)
	return helpers.SanitizeURL(s), err
}
1069088
func (meta MetaData) sanitize(filters []string) interface{} {
	return sanitizer{
		Filters: filters,
		Seen:    make([]interface{}, 0),
	}.Sanitize(meta)

}
889713
func SanitizeURL(in string) string {
	return sanitizeURLWithFlags(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
}
953410
func (p *Service) SanitizedAddress() (sanitizedAddress string, err error) {
	var (
		canonicalizedAddress string
	)

	if p.Outputaddress != "" {
		return p.Outputaddress, nil
	}

	if strings.HasPrefix(p.Address, "postgres://") || strings.HasPrefix(p.Address, "postgresql://") {
		if canonicalizedAddress, err = parseURL(p.Address); err != nil {
			return sanitizedAddress, err
		}
	} else {
		canonicalizedAddress = p.Address
	}

	sanitizedAddress = kvMatcher.ReplaceAllString(canonicalizedAddress, "")

	return sanitizedAddress, err
}
1189690
func (m *MultiSelect) Cleanup(val interface{}) error {
	// execute the output summary template with the answer
	return m.Render(
		MultiSelectQuestionTemplate,
		MultiSelectTemplateData{
			MultiSelect:   *m,
			SelectedIndex: m.selectedIndex,
			Checked:       m.checked,
			Answer:        strings.Join(val.([]string), ", "),
			ShowAnswer:    true,
		},
	)
}
838775
func (s *Selection) FilterSelection(sel *Selection) *Selection {
	if sel == nil {
		return pushStack(s, winnowNodes(s, nil, true))
	}
	return pushStack(s, winnowNodes(s, sel.Nodes, true))
}
57184
func resizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) image.Image {
	ww, hh := uint64(w), uint64(h)
	dx, dy := uint64(r.Dx()), uint64(r.Dy())
	// See comment in Resize.
	n, sum := dx*dy, make([]uint64, 4*w*h)
	for y := r.Min.Y; y < r.Max.Y; y++ {
		pix := m.Pix[(y-m.Rect.Min.Y)*m.Stride:]
		for x := r.Min.X; x < r.Max.X; x++ {
			// Get the source pixel.
			p := pix[(x-m.Rect.Min.X)*4:]
			r64 := uint64(p[0])
			g64 := uint64(p[1])
			b64 := uint64(p[2])
			a64 := uint64(p[3])
			// Spread the source pixel over 1 or more destination rows.
			py := uint64(y-r.Min.Y) * hh
			for remy := hh; remy > 0; {
				qy := dy - (py % dy)
				if qy > remy {
					qy = remy
				}
				// Spread the source pixel over 1 or more destination columns.
				px := uint64(x-r.Min.X) * ww
				index := 4 * ((py/dy)*ww + (px / dx))
				for remx := ww; remx > 0; {
					qx := dx - (px % dx)
					if qx > remx {
						qx = remx
					}
					qxy := qx * qy
					sum[index+0] += r64 * qxy
					sum[index+1] += g64 * qxy
					sum[index+2] += b64 * qxy
					sum[index+3] += a64 * qxy
					index += 4
					px += qx
					remx -= qx
				}
				py += qy
				remy -= qy
			}
		}
	}
	return average(sum, w, h, n)
}
1067350
func sanitizeArch(in ScmpArch) error {
	if in < archStart || in > archEnd {
		return fmt.Errorf("unrecognized architecture %#x", uint(in))
	}

	if in.toNative() == C.C_ARCH_BAD {
		return fmt.Errorf("architecture %v is not supported on this version of the library", in)
	}

	return nil
}
153674
func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {

	// It is possible that the developer has created the policy via:
	//   p := bluemonday.Policy{}
	// rather than:
	//   p := bluemonday.NewPolicy()
	// If this is the case, and if they haven't yet triggered an action that
	// would initiliaze the maps, then we need to do that.
	p.init()

	var (
		buff                     bytes.Buffer
		skipElementContent       bool
		skippingElementsCount    int64
		skipClosingTag           bool
		closingTagToSkipStack    []string
		mostRecentlyStartedToken string
	)

	tokenizer := html.NewTokenizer(r)
	for {
		if tokenizer.Next() == html.ErrorToken {
			err := tokenizer.Err()
			if err == io.EOF {
				// End of input means end of processing
				return &buff
			}

			// Raw tokenizer error
			return &bytes.Buffer{}
		}

		token := tokenizer.Token()
		switch token.Type {
		case html.DoctypeToken:

			// DocType is not handled as there is no safe parsing mechanism
			// provided by golang.org/x/net/html for the content, and this can
			// be misused to insert HTML tags that are not then sanitized
			//
			// One might wish to recursively sanitize here using the same policy
			// but I will need to do some further testing before considering
			// this.

		case html.CommentToken:

			// Comments are ignored by default

		case html.StartTagToken:

			mostRecentlyStartedToken = token.Data

			aps, ok := p.elsAndAttrs[token.Data]
			if !ok {
				if _, ok := p.setOfElementsToSkipContent[token.Data]; ok {
					skipElementContent = true
					skippingElementsCount++
				}
				if p.addSpaces {
					buff.WriteString(" ")
				}
				break
			}

			if len(token.Attr) != 0 {
				token.Attr = p.sanitizeAttrs(token.Data, token.Attr, aps)
			}

			if len(token.Attr) == 0 {
				if !p.allowNoAttrs(token.Data) {
					skipClosingTag = true
					closingTagToSkipStack = append(closingTagToSkipStack, token.Data)
					if p.addSpaces {
						buff.WriteString(" ")
					}
					break
				}
			}

			if !skipElementContent {
				buff.WriteString(token.String())
			}

		case html.EndTagToken:

			if mostRecentlyStartedToken == token.Data {
				mostRecentlyStartedToken = ""
			}

			if skipClosingTag && closingTagToSkipStack[len(closingTagToSkipStack)-1] == token.Data {
				closingTagToSkipStack = closingTagToSkipStack[:len(closingTagToSkipStack)-1]
				if len(closingTagToSkipStack) == 0 {
					skipClosingTag = false
				}
				if p.addSpaces {
					buff.WriteString(" ")
				}
				break
			}

			if _, ok := p.elsAndAttrs[token.Data]; !ok {
				if _, ok := p.setOfElementsToSkipContent[token.Data]; ok {
					skippingElementsCount--
					if skippingElementsCount == 0 {
						skipElementContent = false
					}
				}
				if p.addSpaces {
					buff.WriteString(" ")
				}
				break
			}

			if !skipElementContent {
				buff.WriteString(token.String())
			}

		case html.SelfClosingTagToken:

			aps, ok := p.elsAndAttrs[token.Data]
			if !ok {
				if p.addSpaces {
					buff.WriteString(" ")
				}
				break
			}

			if len(token.Attr) != 0 {
				token.Attr = p.sanitizeAttrs(token.Data, token.Attr, aps)
			}

			if len(token.Attr) == 0 && !p.allowNoAttrs(token.Data) {
				if p.addSpaces {
					buff.WriteString(" ")
				}
				break
			}

			if !skipElementContent {
				buff.WriteString(token.String())
			}

		case html.TextToken:

			if !skipElementContent {
				switch mostRecentlyStartedToken {
				case "script":
					// not encouraged, but if a policy allows JavaScript we
					// should not HTML escape it as that would break the output
					buff.WriteString(token.Data)
				case "style":
					// not encouraged, but if a policy allows CSS styles we
					// should not HTML escape it as that would break the output
					buff.WriteString(token.Data)
				default:
					// HTML escape the text
					buff.WriteString(token.String())
				}
			}
		default:
			// A token that didn't exist in the html package when we wrote this
			return &bytes.Buffer{}
		}
	}
}
477635
func (s *Sanitizer) Delete(ctx context.Context, key []byte) error {
	if !isKeySafe(key) {
		return trace.BadParameter(errorMessage)
	}
	return s.backend.Delete(ctx, key)
}
108830
func (s *FQDNSelector) sanitize() error {
	if len(s.MatchName) > 0 && !allowedMatchNameChars.MatchString(s.MatchName) {
		return fmt.Errorf("Invalid characters in MatchName: \"%s\". Only 0-9, a-z, A-Z and . and - characters are allowed", s.MatchName)
	}

	if len(s.MatchPattern) > 0 && !allowedPatternChars.MatchString(s.MatchPattern) {
		return fmt.Errorf("Invalid characters in MatchPattern: \"%s\". Only 0-9, a-z, A-Z and ., - and * characters are allowed", s.MatchPattern)
	}
	_, err := matchpattern.Validate(s.MatchPattern)
	return err
}
683868
func (self *SizeSpec) ToRect(rect image.Rectangle) image.Rectangle {
	if self.full || self.Width() == -1 || self.Height() == -1 {
		// full-size or only scaling one dimension, means we deal with the whole thing
		return rect
	}

	if self.square {
		if rectIsSquare(rect) {
			// already square. WIN.
			return rect
		}
		if rectIsLandscape(rect) {
			// crop and center on width
			trim := (rect.Dx() - rect.Dy()) / 2
			return image.Rect(trim, 0, rect.Dx()-trim, rect.Dy())
		} else {
			// portrait
			// crop and center on height
			trim := (rect.Dy() - rect.Dx()) / 2
			return image.Rect(0, trim, rect.Dx(), rect.Dy()-trim)
		}
	}
	if self.width == self.height {
		// "square" but not square.
		// fit it in a box with a max dimension, but don't crop
		// or scale up
		// in other words, return the whole thing. TargetWH will have to deal.
		return rect
	}
	// scaling both width and height
	if self.IsLandscape() {
		if rectIsSquare(rect) {
			// keep width, trim height
			ratio := float64(self.Height()) / float64(self.Width())
			targetHeight := int(ratio * float64(rect.Dx()))
			trim := targetHeight / 2
			return image.Rect(0, trim, rect.Dx(), rect.Dy()-trim)
		} else {
			if rectIsLandscape(rect) {
				ratio := float64(rect.Dy()) / float64(rect.Dx())
				outRatio := float64(self.Height()) / float64(self.Width())
				if ratio == outRatio {
					return rect
				}
				if outRatio > ratio {
					// want it even wider, so crop height
				} else {
					// narrower, so crop on width
				}
				rHeight := int(float64(rect.Dy()) * ratio)
				trim := (rect.Dy() - rHeight) / 2
				return image.Rect(0, trim, rect.Dx(), rect.Dy()-trim)
			} else {
				// rect.Dy() is the keeper
				ratio := float64(rect.Dx()) / float64(self.Width())
				rHeight := ratio * float64(self.Height())
				trim := int((float64(rect.Dy()) - rHeight) / 2)
				return image.Rect(0, trim, rect.Dx(), rect.Dy()-trim)
			}
		}
	} else {
		// portrait constraint
		if rectIsSquare(rect) {
			// keep height, trim width
			ratio := float64(self.Width()) / float64(self.Height())
			targetWidth := int(ratio * float64(rect.Dy()))
			trim := (rect.Dx() - targetWidth) / 2
			return image.Rect(trim, 0, rect.Dx()-trim, rect.Dx())
		} else {
			if rectIsLandscape(rect) {
				ratio := float64(self.Width()) / float64(self.Height())
				targetWidth := int(ratio * float64(rect.Dy()))
				trim := (rect.Dx() - targetWidth) / 2
				return image.Rect(trim, 0, rect.Dx()-trim, rect.Dy())
			} else {
				//rect.Dy() is the keeper
				ratio := float64(rect.Dx()) / float64(self.Width())
				rHeight := ratio * float64(self.Height())
				trim := int((float64(rect.Dy()) - rHeight) / 2)
				return image.Rect(0, trim, rect.Dx(), rect.Dy()-trim)
			}
		}

	}
	return rect
}