// Copyright 2016 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 reflectliteimport ()// Swapper returns a function that swaps the elements in the provided// slice.//// Swapper panics if the provided interface is not a slice.func ( any) func(, int) { := ValueOf()if .Kind() != Slice {panic(&ValueError{Method: "Swapper", Kind: .Kind()}) }// Fast path for slices of size 0 and 1. Nothing to swap.switch .Len() {case0:returnfunc(, int) { panic("reflect: slice index out of range") }case1:returnfunc(, int) {if != 0 || != 0 {panic("reflect: slice index out of range") } } } := .Type().Elem().common() := .Size() := .PtrBytes != 0// Some common & small cases, without using memmove:if {if == goarch.PtrSize { := *(*[]unsafe.Pointer)(.ptr)returnfunc(, int) { [], [] = [], [] } }if .Kind() == String { := *(*[]string)(.ptr)returnfunc(, int) { [], [] = [], [] } } } else {switch {case8: := *(*[]int64)(.ptr)returnfunc(, int) { [], [] = [], [] }case4: := *(*[]int32)(.ptr)returnfunc(, int) { [], [] = [], [] }case2: := *(*[]int16)(.ptr)returnfunc(, int) { [], [] = [], [] }case1: := *(*[]int8)(.ptr)returnfunc(, int) { [], [] = [], [] } } } := (*unsafeheader.Slice)(.ptr) := unsafe_New() // swap scratch spacereturnfunc(, int) {ifuint() >= uint(.Len) || uint() >= uint(.Len) {panic("reflect: slice index out of range") } := arrayAt(.Data, , , "i < s.Len") := arrayAt(.Data, , , "j < s.Len")typedmemmove(, , )typedmemmove(, , )typedmemmove(, , ) }}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.