Merge pull request #2964 from ludovic-henry/sgen-monocontext
[mono.git] / mcs / class / referencesource / System.Web / Util / CodePageUtils.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CodePageUtils.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 namespace System.Web.Util {
8     using System.Collections;
9     
10     //
11     // Utility class to help with determining if a given code page
12     // is ASCII compatible (preserves 0-127 Unicode characters as is)
13     //
14
15     internal static class CodePageUtils {
16
17         /*      The following array of ASCII compatible code pages
18                 is generated by running this code on a machine that
19                 has _many_ codepages installed:
20
21                 using System;
22                 using System.Collections;
23                 using System.Text;
24                 public class qq {
25                     public static void Main(string[] args) {
26                         ArrayList list = new ArrayList();
27                         byte[] bb = new byte[128]; for (int i = 0; i < 128; i++) bb[i] = (byte)i;
28                         String asciiString = Encoding.ASCII.GetString(bb);
29                         for (int i = 1; i < 100000; i++) {
30                             try {
31                                 Encoding e = Encoding.GetEncoding(i);
32                                 byte[] xx = e.GetBytes(asciiString);
33                                 if (xx.Length == 128) {
34                                     bool good = true;
35                                     for (int j = 0; j < 128; j++) { if (bb[j] != xx[j]) { good = false; break; } }
36                                     if (good) list.Add(i);
37                                 }
38                             }
39                             catch {}
40                         }
41                         int n = list.Count;
42                         Console.Write("private const int[] _asciiCompatCodePages = new int[" + n + "] {\r\n    ");
43                         for (int i = 0; i < n; i++) {
44                             Console.Write("{0,5}", list[i]);
45                             if (i < n-1) Console.Write(", ");
46                             if (((i+1) % 10) == 0) Console.Write("\r\n    ");
47                         }
48                         Console.Write("\r\n};\r\n");
49                     }
50                 }
51
52         */
53
54         private static int[] _asciiCompatCodePages = new int[79] {
55               437,   708,   720,   737,   775,   850,   852,   855,   857,   858,
56               860,   861,   862,   863,   864,   865,   866,   869,   874,   932,
57               936,   949,   950,  1250,  1251,  1252,  1253,  1254,  1255,  1256,
58              1257,  1258,  1361, 10000, 10001, 10002, 10003, 10004, 10005, 10006,
59             10007, 10008, 10010, 10017, 10029, 10079, 10081, 10082, 20000, 20001,
60             20002, 20003, 20004, 20005, 20127, 20866, 20932, 20936, 20949, 21866,
61             28591, 28592, 28593, 28594, 28595, 28596, 28597, 28598, 28599, 28605,
62             38598, 50220, 50221, 50222, 50225, 50227, 51932, 51949, 65001
63         };
64
65         internal /*public*/ static bool IsAsciiCompatibleCodePage(int codepage) {
66             //alternatives to binary search considered
67             //Hashtable: static initialization increases startup up, perf relative
68             //Byte array would consume ~8K, but lookups constant
69             //with 80 entries, binary search limited to 7 indexes into array
70             int lo = 0;
71             int hi = 78;
72             while(lo <= hi) {
73                 int i = (lo + hi) >> 1;
74                 int c;
75                 c = _asciiCompatCodePages[i] - codepage;
76                 if (c == 0) return true; //i is the index of the item
77                 if (c < 0) {
78                     lo = i + 1;
79                 }
80                 else {
81                     hi = i - 1;
82                 }
83             }
84             return false; 
85             //lo is the index of the item immediately after 
86             //~lo returned in some implementations for false indicator with additional info
87         }
88
89         internal const int CodePageUT8 = 65001;
90     }
91
92
93
94 }
95
96
97