rawurldecode
rawurldecode(string $string): string
Decodes a percent-encoded string, keeping plus signs (+) literal.
C C23
#include <stdio.h>
static int hexval(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
static void raw_decode(const char *s) {
for (; *s; s++) {
if (*s == '%' && hexval(s[1]) >= 0 && hexval(s[2]) >= 0) {
putchar(hexval(s[1]) * 16 + hexval(s[2])); // %XX -> one byte
s += 2;
} else {
putchar(*s); // + stays literal (raw semantics)
}
}
putchar('\n');
}
int main(void) {
raw_decode("hello%20world%21%20a%2Bb");
raw_decode("%E4%BD%A0%E5%A5%BD"); // UTF-8 multi-byte bytes pass through untouched
return 0;
}
hello world! a+b 你好
C++ C++26
#include <iostream>
#include <string>
static int hexval(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
static std::string raw_decode(const std::string &s) {
std::string out;
for (size_t i = 0; i < s.size(); i++) {
if (s[i] == '%' && i + 2 < s.size() && hexval(s[i + 1]) >= 0 && hexval(s[i + 2]) >= 0) {
out += (char)(hexval(s[i + 1]) * 16 + hexval(s[i + 2])); // %XX -> one byte
i += 2;
} else {
out += s[i]; // + stays literal
}
}
return out;
}
int main() {
std::cout << raw_decode("hello%20world%21%20a%2Bb") << "\n";
std::cout << raw_decode("%E4%BD%A0%E5%A5%BD") << "\n";
return 0;
}
hello world! a+b 你好
C# v11.0.100
using System;
Console.WriteLine(Uri.UnescapeDataString("hello%20world%21%20a%2Bb")); // + stays literal
Console.WriteLine(Uri.UnescapeDataString("%E4%BD%A0%E5%A5%BD"));
hello world! a+b 你好
Go v1.27.4
package main
import (
"fmt"
"net/url"
)
func main() {
s, err := url.PathUnescape("hello%20world%21%20a%2Bb") // + stays literal
if err != nil {
panic(err)
}
fmt.Println(s)
s, err = url.PathUnescape("%E4%BD%A0%E5%A5%BD")
if err != nil {
panic(err)
}
fmt.Println(s)
}
hello world! a+b 你好
Java v27.0.1
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
// URLDecoder turns + into a space; the raw semantics need hand-rolling
System.out.println(rawDecode("hello%20world%21%20a%2Bb"));
System.out.println(rawDecode("%E4%BD%A0%E5%A5%BD"));
}
static String rawDecode(String s) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '%' && i + 2 < s.length()) {
out.write(Integer.parseInt(s.substring(i + 1, i + 3), 16)); // %XX -> one byte
i += 2;
} else {
out.write(c);
}
}
return out.toString(StandardCharsets.UTF_8);
}
}
hello world! a+b 你好
JavaScript v26.13.1
console.log(decodeURIComponent("hello%20world%21%20a%2Bb")); // + stays literal
console.log(decodeURIComponent("%E4%BD%A0%E5%A5%BD"));
hello world! a+b 你好
PHP v8.6.0
echo rawurldecode("hello%20world%21%20a%2Bb"), "\n"; // + stays literal
echo rawurldecode("%E4%BD%A0%E5%A5%BD"), "\n"; // UTF-8 bytes reassembled
hello world! a+b 你好
Python v3.15.2
from urllib.parse import unquote
print(unquote("hello%20world%21%20a%2Bb")) # + stays literal
print(unquote("%E4%BD%A0%E5%A5%BD"))
hello world! a+b 你好
Rust v1.99.1
fn raw_decode(s: &str) -> String {
let bytes = s.as_bytes();
let mut out = Vec::new();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
let hex = std::str::from_utf8(&bytes[i + 1..i + 3]).unwrap();
if let Ok(v) = u8::from_str_radix(hex, 16) {
out.push(v); // %XX -> one byte
i += 3;
continue;
}
}
out.push(bytes[i]); // + stays literal
i += 1;
}
String::from_utf8(out).unwrap()
}
fn main() {
println!("{}", raw_decode("hello%20world%21%20a%2Bb"));
println!("{}", raw_decode("%E4%BD%A0%E5%A5%BD"));
}
hello world! a+b 你好
TypeScript v26.13.1
console.log(decodeURIComponent("hello%20world%21%20a%2Bb")); // + stays literal
console.log(decodeURIComponent("%E4%BD%A0%E5%A5%BD"));
hello world! a+b 你好
Language differences
C: hand-rolls the %XX loop.
C++: hand-rolls the %XX loop.
C#: Uri.UnescapeDataString (keeps +).
Go: pairs url.PathUnescape (keeps +) with url.QueryUnescape (+→space); failures return an error.
Java: only has URLDecoder.decode, which always converts + to space — raw semantics must be hand-rolled as a %XX loop.
JavaScript: decodeURIComponent (keeps +; decodeURI is even more conservative and leaves reserved characters alone); on malformed sequences it throws URIError.
PHP: the raw policy decodes only %XX and keeps plus signs literal; the urldecode policy turns + into a space (see the urldecode entry); PHP ships both — rawurldecode/urldecode, pairing with rawurlencode (RFC 3986 path policy) and urlencode (form policy) on the encoding side; failure policy is lenient and keeps the input as-is; decoding is byte-level — UTF-8 multi-byte sequences (%E4%BD%A0…) are reassembled as bytes before converting to text.
Python: unquote (keeps +) / unquote_plus (+→space); failure policy is lenient and keeps the input as-is.
Rust: hand-rolls the %XX loop (the ecosystem also has the percent-encoding crate).
TypeScript: same as JS — decodeURIComponent.