package main
import (
"fmt"
)
// XORSwapCharacters swaps two characters in a byte slice using the XOR swap algorithm.
//
// This function demonstrates XOR swapping for educational purposes.
// In production Go code, tuple assignment (b[left], b[right] = b[right], b[left])
// is strongly preferred for readability and optimizer friendliness.
//
// Note: This implementation assumes ASCII/single-byte characters. For multi-byte
// UTF-8 strings, use a []rune slice instead.
func XORSwapCharacters(b []byte, left, right int) {
// Bounds checking and empty slice protection
if len(b) == 0 || left < 0 || left >= len(b) || right < 0 || right >= len(b) {
return
}
// XOR swap fails if left == right because (x ^ x) evaluates to 0
if left == right {
return
}
b[left] ^= b[right]
b[right] ^= b[left]
b[left] ^= b[right]
}
func main() {
original := "hello"
textBytes := []byte(original)
fmt.Printf("Before: %s\n", string(textBytes))
XORSwapCharacters(textBytes, 1, 4)
fmt.Printf("After: %s\n", string(textBytes))
}
/*
run:
Before: hello
After: holle
*/