Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Aug 4, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 19 Apr 2014 at 7:25

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 19 Apr 2014 at 7:29

@GoogleCodeExporter
Copy link
Author

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

Original comment by [email protected] on 29 Aug 2014 at 12:14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant