Merge pull request #3144 from Unity-Technologies/fix-recursive-property-call
[mono.git] / mcs / class / referencesource / System.Web / Util / HttpEncoderUtility.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="HttpEncoderUtility.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*
8  * Helper class for common encoding routines
9  *
10  * Copyright (c) 2009 Microsoft Corporation
11  */
12
13 namespace System.Web.Util {
14     using System;
15     using System.Web;
16
17     internal static class HttpEncoderUtility {
18
19         public static int HexToInt(char h) {
20             return (h >= '0' && h <= '9') ? h - '0' :
21             (h >= 'a' && h <= 'f') ? h - 'a' + 10 :
22             (h >= 'A' && h <= 'F') ? h - 'A' + 10 :
23             -1;
24         }
25
26         public static char IntToHex(int n) {
27             Debug.Assert(n < 0x10);
28
29             if (n <= 9)
30                 return (char)(n + (int)'0');
31             else
32                 return (char)(n - 10 + (int)'a');
33         }
34
35         // Set of safe chars, from RFC 1738.4 minus '+'
36         public static bool IsUrlSafeChar(char ch) {
37             if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))
38                 return true;
39
40             switch (ch) {
41                 case '-':
42                 case '_':
43                 case '.':
44                 case '!':
45                 case '*':
46                 case '(':
47                 case ')':
48                     return true;
49             }
50
51             return false;
52         }
53
54         //  Helper to encode spaces only
55         internal static String UrlEncodeSpaces(string str) {
56             if (str != null && str.IndexOf(' ') >= 0)
57                 str = str.Replace(" ", "%20");
58             return str;
59         }
60
61     }
62 }