1 //------------------------------------------------------------------------------
2 // <copyright file="CodePageUtils.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Web.Util {
8 using System.Collections;
11 // Utility class to help with determining if a given code page
12 // is ASCII compatible (preserves 0-127 Unicode characters as is)
15 internal static class CodePageUtils {
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:
22 using System.Collections;
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++) {
31 Encoding e = Encoding.GetEncoding(i);
32 byte[] xx = e.GetBytes(asciiString);
33 if (xx.Length == 128) {
35 for (int j = 0; j < 128; j++) { if (bb[j] != xx[j]) { good = false; break; } }
36 if (good) list.Add(i);
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 ");
48 Console.Write("\r\n};\r\n");
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
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
73 int i = (lo + hi) >> 1;
75 c = _asciiCompatCodePages[i] - codepage;
76 if (c == 0) return true; //i is the index of the item
85 //lo is the index of the item immediately after
86 //~lo returned in some implementations for false indicator with additional info
89 internal const int CodePageUT8 = 65001;