Move EncodingTable and CodePageDataItem to System.Text namespace (#17061)
[platform/upstream/coreclr.git] / src / mscorlib / src / System / Text / CodePageDataItem.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Text;
6 using System;
7 using System.Security;
8
9 namespace System.Text
10 {
11     //
12     // Data item for EncodingTable.  Along with EncodingTable, they are used by 
13     // System.Text.Encoding.
14     // 
15     // This class stores a pointer to the internal data and the index into that data
16     // where our required information is found.  We load the code page, flags and uiFamilyCodePage
17     // immediately because they don't require creating an object.  Creating any of the string
18     // names is delayed until somebody actually asks for them and the names are then cached.
19
20     internal class CodePageDataItem
21     {
22         internal int m_dataIndex;
23         internal int m_uiFamilyCodePage;
24         internal String m_webName;
25         internal String m_headerName;
26         internal String m_bodyName;
27         internal uint m_flags;
28
29         internal unsafe CodePageDataItem(int dataIndex)
30         {
31             m_dataIndex = dataIndex;
32             m_uiFamilyCodePage = EncodingTable.codePageDataPtr[dataIndex].uiFamilyCodePage;
33             m_flags = EncodingTable.codePageDataPtr[dataIndex].flags;
34         }
35
36         internal static unsafe String CreateString(sbyte* pStrings, uint index)
37         {
38             if (pStrings[0] == '|') // |str1|str2|str3
39             {
40                 int start = 1;
41
42                 for (int i = 1; true; i++)
43                 {
44                     sbyte ch = pStrings[i];
45
46                     if ((ch == '|') || (ch == 0))
47                     {
48                         if (index == 0)
49                         {
50                             return new String(pStrings, start, i - start);
51                         }
52
53                         index--;
54                         start = i + 1;
55
56                         if (ch == 0)
57                         {
58                             break;
59                         }
60                     }
61                 }
62
63                 throw new ArgumentException(null, nameof(pStrings));
64             }
65             else
66             {
67                 return new String(pStrings);
68             }
69         }
70
71         public unsafe String WebName
72         {
73             get
74             {
75                 if (m_webName == null)
76                 {
77                     m_webName = CreateString(EncodingTable.codePageDataPtr[m_dataIndex].Names, 0);
78                 }
79                 return m_webName;
80             }
81         }
82
83         public virtual int UIFamilyCodePage
84         {
85             get
86             {
87                 return m_uiFamilyCodePage;
88             }
89         }
90
91         public unsafe String HeaderName
92         {
93             get
94             {
95                 if (m_headerName == null)
96                 {
97                     m_headerName = CreateString(EncodingTable.codePageDataPtr[m_dataIndex].Names, 1);
98                 }
99                 return m_headerName;
100             }
101         }
102
103         public unsafe String BodyName
104         {
105             get
106             {
107                 if (m_bodyName == null)
108                 {
109                     m_bodyName = CreateString(EncodingTable.codePageDataPtr[m_dataIndex].Names, 2);
110                 }
111                 return m_bodyName;
112             }
113         }
114
115         public unsafe uint Flags
116         {
117             get
118             {
119                 return (m_flags);
120             }
121         }
122     }
123 }