Skip to content

Instantly share code, notes, and snippets.

@cwarden
Created March 18, 2011 23:54
Show Gist options
  • Select an option

  • Save cwarden/877046 to your computer and use it in GitHub Desktop.

Select an option

Save cwarden/877046 to your computer and use it in GitHub Desktop.
version of URLdecode function that handles unencoded %
myURLdecode <- function(URL)
{
x <- charToRaw(URL)
pc <- charToRaw("%")
out <- raw(0L)
i <- 1L
len <- length(x)
while(i <= len) {
if(x[i] != pc || i + 2 > len) {
out <- c(out, x[i])
i <- i + 1L
} else {
y <- as.integer(x[i + 1L:2L])
y[y > 96L] <- y[y > 96L] - 32L # a-f -> A-F
y[y > 57L] <- y[y > 57L] - 7L # A-F
if (y[1] > 48L) {
y <- sum((y - 48L) * c(16L, 1L))
out <- c(out, as.raw(as.character(y)))
i <- i + 3L
} else {
out <- c(out, pc)
i <- i + 1L
}
}
}
rawToChar(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment