craigsketchley/google-diff-match-patch

Consider SQLCLR compatibility / eliminate dependency on System.Web for UrlEncode and UrlDecode

Opened this issue · 3 comments

DiffMatchPatch can be useful as a SQLCLR assembly so methods can be called from 
TSQL functions.

However, the dependency on System.Web for UrlEncode and UrlDecode is an 
obstacle to installing the assembly into SQL, as System.Web depends on 
remoting, which is not allowed under SQL.

I have successfully created and added local UrlEncode and UrlDecode methods to 
class CompatibilityExtensions, thereby allowing DiffMatchPatch to be used in a 
SQLCLR assembly.

This solution may be useful to others.  Please consider removing the dependency 
on System.Web

I am providing the URLEncode and URLDecode source that I wrote below, in case 
that would be helpful.


        //A local UrlEncode, because we can't use System.Web in SQL
        //UrlEncode by David Rueter (drueter@assyst.com)
        public static string UrlEncode(string s)
        {
            string output = "";
            int p = 0;

            Regex regex = new Regex("([^a-zA-Z0-9_.])");

            Match match = regex.Match(s);
            while (match.Success)
            {
                if (match.Index > p)
                {
                    output += s.Substring(p, match.Index - p);
                }
                if (match.Value[0] == ' ')
                {
                    output += '+';
                }
                else
                {
                    string hexVal = "%" + String.Format("{0:X2}", (int)match.Value[0]);
                    output += hexVal.ToUpper();
                }
                p = match.Index + 1;

                match = match.NextMatch();
            }

            if (p < s.Length)
            {
                output += s.Substring(p);
            }

            return output;
        }

        //A local UrlDecode, because we can't use System.Web in SQL
        //UrlDecode by David Rueter (drueter@assyst.com)
        public static string UrlDecode(string s)
        {
            string output = "";
            int p = 0;

            Regex regex = new Regex("([%+])");

            Match match = regex.Match(s);
            while (match.Success)
            {
                if (match.Index > p)
                {
                    output += s.Substring(p, match.Index - p);
                }
                if (match.Value[0] == '+')
                {
                    output += ' ';
                    p = match.Index + 1;
                }
                else
                {
                    string hexVal = match.Value.Substring(1, 2);
                    output += int.Parse(hexVal);
                }
                p = match.Index + 3;

                match = match.NextMatch();
            }

            if (p < s.Length)
            {
                output += s.Substring(p);
            }

            return output;
        }   

Original issue reported on code.google.com by dbrue...@gmail.com on 19 Apr 2014 at 7:22

Note:  this is not a defect, but is an enhancement request.  I can't seem to 
edit the issue to reflect the correct category.

Original comment by dbrue...@gmail.com on 19 Apr 2014 at 7:25

Also note an error in UrlDecode I pasted above:  line "p = match.Index + 3;" 
should be within the else block.

Why can't I edit the issue I submitted??? sigh.

Original comment by dbrue...@gmail.com on 19 Apr 2014 at 7:29

This will become even more important as we look to .NET VNext.

Original comment by jetski5...@gmail.com on 29 Aug 2014 at 12:14