wobsite.url.normalize
Show source codeHide source code
fn pub normalize(string: String) -> String {
let url = ByteArray.new
let iter = string.bytes.peekable
loop {
match iter.next {
# If the byte is in the ASCII range A-Z, lowercase it; otherwise
# we keep it as-is.
case Some(v) if v >= 65 and v <= 90 -> url.push(v + 32)
case Some(DOT) -> url.push(HYPHEN)
case Some(v) if URL_SAFE.get(v) -> url.push(v)
case Some(TAB or LF or CR or SPACE) -> {
if url.last.or(-1) != HYPHEN { url.push(HYPHEN) }
loop {
match iter.peek {
case Some(TAB or LF or CR or SPACE) -> iter.next
case _ -> break
}
}
}
case Some(_) -> {}
case _ -> break
}
}
url.into_string
}
fn pub static normalize(string: String) -> String
Generates a URL-safe path of the given string (e.g. a blog post title).