         }
 
         if u.field_set & 32 != 0 {
-            fragment = URI.getSubstring(string, start: u.fragment_start, end: u.fragment_end)
+            let string = URI.getSubstring(string, start: u.fragment_start, end: u.fragment_end)
+            fragment = String(URLEncodedString: string)
         } else {
             fragment = nil
         }

        let u = parse_uri(string)

        if u.field_set & 1 != 0 {
            scheme = URI.getSubstring(string, start: u.scheme_start, end: u.scheme_end)
        } else {
            scheme = nil
        }

        if u.field_set & 2 != 0 {
            host = URI.getSubstring(string, start: u.host_start, end: u.host_end)
        } else {
            host = nil
        }

        self.host = host
        self.port = port
        self.path = path
        self.query = query
        self.fragment = fragment
    }
}
