Skip to content

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

@GoogleCodeExporter

Description

@GoogleCodeExporter
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 ([email protected])
        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 ([email protected])
        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 [email protected] on 19 Apr 2014 at 7:22

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions