]> git.donarmstrong.com Git - lilypond.git/commitdiff
Fix #887: Use ly:string-percent-encode for textedit URIs.
authorPatrick McCarty <pnorcks@gmail.com>
Sun, 24 Jan 2010 22:46:51 +0000 (14:46 -0800)
committerPatrick McCarty <pnorcks@gmail.com>
Fri, 29 Jan 2010 00:55:15 +0000 (16:55 -0800)
* Add an overloaded instance of String_convert::bin2hex optimized for
  converting single bytes to hex.

* Add a new callback, ly:string-percent-encode, to be used for percent
  escaping paths in textedit URIs.  This does the following:

  - Leave unreserved characters in textedit URIs unescaped.  This
    includes 0-9, A-Z, a-z, and three punctuation characters (hyphen,
    underscore, and full-stop).

  - Leave the forward slash (/) unescaped, since it is used as a path
    delimiter.

  - Escape all other characters.  Don't check for a null byte, since
    those likely won't sneak into a full pathname.

* Use the callback function in the PS backend.

flower/include/string-convert.hh
flower/string-convert.cc
lily/general-scheme.cc
scm/output-ps.scm

index 1277890929f73176253a00fedddc70cf9d09eb86..21ec7f189e2dcd7789623ae1f31df4b8af29c7d3 100644 (file)
@@ -22,6 +22,7 @@ public:
   static string pad_to (string s, int length);
   static string bool_string (bool b);
   static string bin2dec (string bin_string);
+  static string bin2hex (Byte bin_char);
   static string bin2hex (string bin_string);
   static string dec2bin (string str);
   static int bin2int (string bin_string);
index ef64af25b606fe753ac6d4015cba154f73f899fe..03bb05b319c57b291c11d2146170c630569957fc 100644 (file)
@@ -33,6 +33,15 @@ String_convert::bool_string (bool b)
   return string (b ? "true" : "false");
 }
 
+string
+String_convert::bin2hex (Byte bin_char)
+{
+  string str;
+  str += to_string ((char) nibble2hex_byte (bin_char >> 4));
+  str += to_string ((char) nibble2hex_byte (bin_char++));
+  return str;
+}
+
 string
 String_convert::bin2hex (string bin_string)
 {
index e1414dc602244648448d757578cc87965d9542b8..76cd33ad7867d160c05e8944460d6327a3c5be9e 100644 (file)
@@ -224,6 +224,57 @@ LY_DEFINE (ly_string_substitute, "ly:string-substitute",
   return ly_string2scm (ss);
 }
 
+bool
+is_not_escape_character (Byte c)
+{
+  switch (c)
+    {
+    case '-':
+    case '.':
+    case '/':
+    case '0'...'9':
+    case 'A'...'Z':
+    case '_':
+    case 'a'...'z':
+      return true;
+    }
+
+  return false;
+}
+
+LY_DEFINE (ly_string_percent_encode, "ly:string-percent-encode",
+          1, 0, 0, (SCM str),
+          "Encode all characters in string @var{str} with hexadecimal"
+          " percent escape sequences, with the following exceptions:"
+          " characters @code{-}, @code{.}, @code{/}, and @code{_}; and"
+          " characters in ranges @code{0-9}, @code{A-Z}, and @code{a-z}.")
+{
+  LY_ASSERT_TYPE (scm_is_string, str, 1);
+
+  string orig_str = ly_scm2string (str);
+  string new_str = "";
+
+  vsize i = 0;
+  vsize n = orig_str.size ();
+
+  while (i < n)
+    {
+      Byte cur = orig_str[i];
+
+      if (is_not_escape_character (cur))
+       new_str += cur;
+      else
+       {
+         new_str += '%';
+         new_str += String_convert::bin2hex (cur);
+       }
+
+      i++;
+    }
+
+  return ly_string2scm (new_str);
+}
+
 LY_DEFINE (ly_number_2_string, "ly:number->string",
           1, 0, 0, (SCM s),
           "Convert @var{num} to a string without generating many decimals.")
index bb34b1f3a625bca4cbb2c4bcfbba157dc1981cd4..5cedc80a05d36bdebc8f333f134632a7cc2439d2 100644 (file)
                             (+ (car offset) (cdr x-ext))
                             (+ (cdr offset) (cdr y-ext))
 
-                            ;; TODO
-                            ;;full escaping.
+                            ;; Backslashes are not valid
+                            ;; file URI path separators.
+                            (ly:string-substitute
+                              "\\" "/" (ly:string-percent-encode file))
 
-                            ;; backslash is interpreted by GS.
-                            (ly:string-substitute "\\" "/" 
-                                                  (ly:string-substitute " " "%20" file))
                             (cadr location)
                             (caddr location)
                             (cadddr location))