Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DbcsEncoding.cs « CJK « I18N « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e73a86b81694e253ad7aa9f868515b1963d4ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// I18N.CJK.DbcsEncoding
//
// Author:
//   Alan Tam Siu Lung (Tam@SiuLung.com)
//

using System;
using System.Text;
using I18N.Common;

namespace I18N.CJK
{
	[Serializable]
	internal abstract class DbcsEncoding : MonoEncoding
	{
		public DbcsEncoding(int codePage) : base(codePage) {}

		internal abstract DbcsConvert GetConvert ();

		// Get the number of bytes needed to encode a character buffer.
		public override int GetByteCount(char[] chars, int index, int count)
		{
			if (chars == null)
				throw new ArgumentNullException("chars");
			if (index < 0 || index > chars.Length)
				throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
			if (count < 0 || index + count > chars.Length)
				throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
			byte[] buffer = new byte[count * 2];
			return GetBytes(chars, index, count, buffer, 0);
		}
		
/*
		// Get the bytes that result from encoding a character buffer.
		public override int GetBytes(char[] chars, int charIndex, int charCount,
					     byte[] bytes, int byteIndex)
		{
			if (chars == null)
				throw new ArgumentNullException("chars");
			if (bytes == null)
				throw new ArgumentNullException("bytes");
			if (charIndex < 0 || charIndex > chars.Length)
				throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
			if (charCount < 0 || charIndex + charCount > chars.Length)
				throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_Array"));
			if (byteIndex < 0 || byteIndex > bytes.Length)
				throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
			return 0; // For subclasses to implement
		}
*/
		
		// Get the number of characters needed to decode a byte buffer.
		public override int GetCharCount(byte[] bytes, int index, int count)
		{
			if (bytes == null)
				throw new ArgumentNullException("bytes");
			if (index < 0 || index > bytes.Length)
				throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
			if (count < 0 || index + count > bytes.Length)
				throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
			char[] buffer = new char[count];
			return GetChars(bytes, index, count, buffer, 0);
		}
		
		// Get the characters that result from decoding a byte buffer.
		public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
					     char[] chars, int charIndex)
		{
			if (bytes == null)
				throw new ArgumentNullException("bytes");
			if (chars == null)
				throw new ArgumentNullException("chars");
			if (byteIndex < 0 || byteIndex > bytes.Length)
				throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
			if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
				throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
			if (charIndex < 0 || charIndex > chars.Length)
				throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
			return 0; // For subclasses to implement
		}
		
		// Get the maximum number of bytes needed to encode a
		// specified number of characters.
		public override int GetMaxByteCount(int charCount)
		{
			if (charCount < 0)
				throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
			return charCount * 2;
		}
		
		// Get the maximum number of characters needed to decode a
		// specified number of bytes.
		public override int GetMaxCharCount(int byteCount)
		{
			if (byteCount < 0) {
				throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
			}
			return byteCount;
		}
		
		// Get a decoder that handles a rolling state.
		public override Decoder GetDecoder()
		{
			return new DbcsDecoder(GetConvert ());
		}
		
		// Determine if this encoding can be displayed in a Web browser.
		public override bool IsBrowserDisplay
		{
			get { return true; }
		}
		
		// Determine if this encoding can be saved from a Web browser.
		public override bool IsBrowserSave
		{
			get { return true; }
		}
		
		// Determine if this encoding can be displayed in a mail/news agent.
		public override bool IsMailNewsDisplay
		{
			get { return true; }
		}
		
		// Determine if this encoding can be saved from a mail/news agent.
		public override bool IsMailNewsSave
		{
			get { return true; }
		}
		
		// Decoder that handles a rolling state.
		internal class DbcsDecoder : Decoder
		{
			protected DbcsConvert convert;
			protected int lastByte;
			
			// Constructor.
			public DbcsDecoder(DbcsConvert convert)
			{
				this.convert = convert;
				this.lastByte = 0;
			}
			
			// Override inherited methods.
			public override int GetCharCount(byte[] bytes, int index, int count)
			{
				if (bytes == null)
					throw new ArgumentNullException("bytes");
				if (index < 0 || index > bytes.Length)
					throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
				if (count < 0 || count > (bytes.Length - index))
					throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
				char[] buffer = new char[count * 2];
				return GetChars(bytes, index, count, buffer, 0);
			}
			
			public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
						     char[] chars, int charIndex)
			{
				if (bytes == null)
					throw new ArgumentNullException("bytes");
				if (chars == null)
					throw new ArgumentNullException("chars");
				if (byteIndex < 0 || byteIndex > bytes.Length)
					throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
				if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
					throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
				if (charIndex < 0 || charIndex > chars.Length)
					throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
				return 0; // For subclasses to implement
			}
		}
	}
}