Add some 4.5 api
[mono.git] / mcs / class / System / Mono.Util / Punycode.cs
1 //
2 // Based on the C implementation from the RFC:
3 // http://tools.ietf.org/html/rfc3492
4 //
5 // Compilation Options:
6 //     MONO_PUNYCODE_PUBLIC:
7 //         Makes the class public instead of the default internal.
8 //
9 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 // 
31 using System;
32 using System.Text;
33
34 namespace Mono.Util {
35 #if MONO_PUNYCODE_PUBLIC
36 public
37 #else
38 internal
39 #endif
40         static class Punycode {
41                 const int BASE = 36;
42                 const int DAMP = 700;
43                 const int TMIN = 1;
44                 const int TMAX = 26;
45                 const int SKEW = 38;
46                 
47                 //  0..25 map to ASCII a..z or A..Z 
48                 // 26..35 map to ASCII 0..9         
49                 static char encode_digit (int d)
50                 {
51                         return (char) (d + 22 + (d < 26 ? 75 : 0));
52                 }
53         
54                 /* Bias adaptation function */
55                 static int adapt (int delta, int numpoints, bool firsttime)
56                 {
57                         int k;
58         
59                         delta = firsttime ? delta / DAMP : delta >> 1;
60                         /* delta >> 1 is a faster way of doing delta / 2 */
61                         delta += delta / numpoints;
62         
63                         for (k = 0;  delta > ((BASE - TMIN) * TMAX) / 2;  k += BASE) 
64                                 delta /= BASE - TMIN;
65         
66                         return k + (BASE - TMIN + 1) * delta / (delta + SKEW);
67                 }
68                 
69                 public static string Encode (string input)
70                 {
71                         var result = new StringBuilder ();
72         
73                         foreach (char c in input){
74                                 if (c < 0x80)
75                                         result.Append (c);
76                         }
77                         int h, b;
78                         h = b = result.Length;
79                         
80                         if (h != input.Length)
81                                 result.Append ('-');
82         
83                         int n = 128;
84                         int delta = 0;
85                         int bias = 72;
86         
87                         while (h < input.Length) {
88                                 int m = Int32.MaxValue;
89         
90                                 // Find the next code point
91                                 foreach (char c in input){
92                                         int ic = (int) c;
93                                         if (ic > n && ic < m)
94                                                 m = ic;
95                                 }
96                                 
97                                 if (m - n > (Int32.MaxValue - delta) / (h + 1))
98                                         throw new OverflowException ();
99                                 
100                                 delta += (m - n) * (h + 1);
101                                 n = m;
102         
103                                 foreach (char c in input){
104                                         int ic = (int) c;
105         
106                                         // Punycode does not need to check whether input[j] is basic
107                                         if (ic < n)
108                                                 if (++delta == 0)
109                                                         throw new OverflowException ();
110         
111                                         /* Represent delta as a generalized variable-length integer: */
112                                         if (ic == n) {
113                                                 int q = delta;
114                                                 for (int k = BASE;  ;  k += BASE) {
115                                                         int t = k <= bias ? TMIN : k >= bias + TMAX ? TMAX : k - bias;
116                                                         if (q < t)
117                                                                 break;
118                                                         result.Append (encode_digit (t + (q - t) % (BASE - t)));
119                                                         q = (q - t) / (BASE - t);
120                                                 }
121         
122                                                 result.Append (encode_digit (q));
123                                                 bias = adapt (delta, h + 1, h == b);
124                                                 delta = 0;
125                                                 ++h;
126                                         }
127                                 }
128         
129                                 ++delta;
130                                 ++n;
131                         }
132                         return result.ToString ();
133                 }
134         }
135 }