Last active
October 18, 2023 08:31
-
-
Save mattt/b46ab5027f1ee6ab1a45583a41240033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
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
Hello @mattt,
Should not the latin uppercase and lowercase ranges be
0x0041...0x005a
and0x0061...0x007a
?