Instantly share code, notes, and snippets.

@mattt /zalgo.swift
Last active Nov 21, 2018

Embed
What would you like to do?
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

This comment has been minimized.

MatthiasLamoureux commented Oct 23, 2018

Hello @mattt,

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

@mattt

This comment has been minimized.

Owner

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