Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active October 18, 2023 08:31
Show Gist options
  • Save mattt/b46ab5027f1ee6ab1a45583a41240033 to your computer and use it in GitHub Desktop.
Save mattt/b46ab5027f1ee6ab1a45583a41240033 to your computer and use it in GitHub Desktop.
func zalgo(_ string: String, intensity: Int = 5) -> String {
let combiningDiacriticMarks = 0x0300...0x036f
let latinAlphabetUppercase = 0x0041...0x005a
let latinAlphabetLowercase = 0x0061...0x007a
var output: [UnicodeScalar] = []
for scalar in string.unicodeScalars {
output.append(scalar)
guard (latinAlphabetUppercase).contains(numericCast(scalar.value)) ||
(latinAlphabetLowercase).contains(numericCast(scalar.value))
else {
continue
}
for _ in 0...(Int.random(in: 1...intensity)) {
let randomScalarValue = Int.random(in: combiningDiacriticMarks)
output.append(Unicode.Scalar(randomScalarValue)!)
}
}
return String(String.UnicodeScalarView(output))
}
@MatthiasLamoureux
Copy link

Hello @mattt,

Should not the latin uppercase and lowercase ranges be 0x0041...0x005a and 0x0061...0x007a ?

@mattt
Copy link
Author

mattt commented Nov 21, 2018

You're right, @MatthiasLamoureux — I'm not sure why I was using those ranges here. Thanks for pointing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment