| 355 | | (defun url-hexify-string (str) |
|---|
| 356 | | "Escape characters in a string." |
|---|
| 357 | | (mapconcat |
|---|
| 358 | | (lambda (char) |
|---|
| 359 | | ;; Fixme: use a char table instead. |
|---|
| 360 | | (if (not (memq char url-unreserved-chars)) |
|---|
| 361 | | (if (> char 255) |
|---|
| 362 | | (error "Hexifying multibyte character %s" str) |
|---|
| 363 | | (format "%%%02X" char)) |
|---|
| 364 | | (char-to-string char))) |
|---|
| 365 | | str "")) |
|---|
| | 355 | (defun url-hexify-string (string) |
|---|
| | 356 | "Return a new string that is STRING URI-encoded. |
|---|
| | 357 | First, STRING is converted to utf-8, if necessary. Then, for each |
|---|
| | 358 | character in the utf-8 string, those found in `url-unreserved-chars' |
|---|
| | 359 | are left as-is, all others are represented as a three-character |
|---|
| | 360 | string: \"%\" followed by two lowercase hex digits." |
|---|
| | 361 | ;; To go faster and avoid a lot of consing, we could do: |
|---|
| | 362 | ;; |
|---|
| | 363 | ;; (defconst url-hexify-table |
|---|
| | 364 | ;; (let ((map (make-vector 256 nil))) |
|---|
| | 365 | ;; (dotimes (byte 256) (aset map byte |
|---|
| | 366 | ;; (if (memq byte url-unreserved-chars) |
|---|
| | 367 | ;; (char-to-string byte) |
|---|
| | 368 | ;; (format "%%%02x" byte)))) |
|---|
| | 369 | ;; map)) |
|---|
| | 370 | ;; |
|---|
| | 371 | ;; (mapconcat (curry 'aref url-hexify-table) ...) |
|---|
| | 372 | (mapconcat (lambda (byte) |
|---|
| | 373 | (if (memq byte url-unreserved-chars) |
|---|
| | 374 | (char-to-string byte) |
|---|
| | 375 | (format "%%%02x" byte))) |
|---|
| | 376 | (if (multibyte-string-p string) |
|---|
| | 377 | (encode-coding-string string 'utf-8) |
|---|
| | 378 | string) |
|---|
| | 379 | "")) |
|---|