Author: atsushi Date: 2006-01-20 08:01:41 -0500 (Fri, 20 Jan 2006) New Revision: 55831
Modified:
trunk/mcs/class/I18N/CJK/CP932.cs
trunk/mcs/class/I18N/CJK/ChangeLog
trunk/mcs/class/I18N/CJK/Test/ChangeLog
trunk/mcs/class/I18N/CJK/Test/I18N.CJK.Test.cs
Log:
2006-01-20 Atsushi Enomoto <[EMAIL PROTECTED]>
* CP932.cs :
Implemented Decoder to handle incomplete bytes correctly. Hold
different incomplete bytes for GetChars() and GetCharCount().
"reflesh" parameter in 2.0 is now handled correctly.
Now GetChars()/GetCharCount() in Encoding are implemented
correctly to use Decoder.
* I18N.CJK.Test.cs : added Decoder932Refresh().
Modified: trunk/mcs/class/I18N/CJK/CP932.cs
===================================================================
--- trunk/mcs/class/I18N/CJK/CP932.cs 2006-01-20 12:49:51 UTC (rev 55830)
+++ trunk/mcs/class/I18N/CJK/CP932.cs 2006-01-20 13:01:41 UTC (rev 55831)
@@ -22,6 +22,10 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
+//
+// Copyright (C) 2005-2006 Novell, Inc.
+//
+
namespace I18N.CJK
{
@@ -356,177 +360,19 @@
return posn - byteIndex;
}
- // Get the number of characters needed to decode a byte buffer.
- public override int GetCharCount(byte[] bytes, int index, int count)
+ public override int GetCharCount (byte [] bytes, int index, int count)
{
- // Validate the parameters.
- 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"));
- }
-
- // Determine the total length of the converted
string.
- int length = 0;
- int byteval;
- while(count > 0)
- {
- byteval = bytes[index++];
- --count;
- ++length;
- if(byteval < 0x80)
- {
- // Ordinary ASCII/Latin1
character, or the
- // single-byte Yen or overline
signs.
- continue;
- }
- else if(byteval >= 0xA1 && byteval <=
0xDF)
- {
- // Half-width katakana.
- continue;
- }
- else if(byteval < 0x81 ||
- (byteval > 0x9F && byteval <
0xE0) ||
- byteval > 0xEF)
- {
- // Invalid first byte.
- continue;
- }
- if(count == 0)
- {
- // Missing second byte.
- continue;
- }
- ++index;
- --count;
- }
-
- // Return the total length.
- return length;
+ return new CP932Decoder
(JISConvert.Convert).GetCharCount (
+ bytes, index, count, true);
}
- // Get the characters that result from decoding a byte buffer.
- public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
- char[] chars,
int charIndex)
+ public override int GetChars (
+ byte [] bytes, int byteIndex, int byteCount,
+ char [] chars, int charIndex)
{
- // Validate the parameters.
- 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 || byteCount > (bytes.Length -
byteIndex))
- {
- throw new ArgumentOutOfRangeException
- ("byteCount",
Strings.GetString("ArgRange_Array"));
- }
- if(charIndex < 0 || charIndex > chars.Length)
- {
- throw new ArgumentOutOfRangeException
- ("charIndex",
Strings.GetString("ArgRange_Array"));
- }
-
- // Determine the total length of the converted
string.
- int charLength = chars.Length;
- int posn = charIndex;
- int length = 0;
- int byteval, value;
-#if __PNET__
- byte *table =
JISConvert.Convert.jisx0208ToUnicode;
-#else
- byte[] table =
JISConvert.Convert.jisx0208ToUnicode;
-#endif
- while(byteCount > 0)
- {
- byteval = bytes[byteIndex++];
- --byteCount;
- ++length;
- if(posn >= charLength)
- {
- throw new ArgumentException
-
(Strings.GetString("Arg_InsufficientSpace"),
- "chars");
- }
- else if(byteval < 0x80)
- {
- // Ordinary ASCII/Latin1
character.
- chars[posn++] = (char)byteval;
- continue;
- }
- else if(byteval >= 0xA1 && byteval <=
0xDF)
- {
- // Half-width katakana.
- chars[posn++] = (char)(byteval
- 0xA1 + 0xFF61);
- continue;
- }
- else if(byteval >= 0x81 && byteval <=
0x9F)
- {
- value = (byteval - 0x81) * 0xBC;
- }
- else if(byteval >= 0xE0 && byteval <=
0xEF)
- {
- value = (byteval - 0xE0 + (0xA0
- 0x81)) * 0xBC;
- }
- else
- {
- // Invalid first byte.
- chars[posn++] = '?';
- continue;
- }
- if(byteCount == 0)
- {
- // Missing second byte.
- chars[posn++] = '?';
- continue;
- }
- byteval = bytes[byteIndex++];
- --byteCount;
- if(byteval >= 0x40 && byteval <= 0x7E)
- {
- value += (byteval - 0x40);
- }
- else if(byteval >= 0x80 && byteval <=
0xFC)
- {
- value += (byteval - 0x80 +
0x3F);
- }
- else
- {
- // Invalid second byte.
- chars[posn++] = '?';
- continue;
- }
- value *= 2;
- value = ((int)(table[value])) |
- (((int)(table[value +
1])) << 8);
- if(value != 0)
- {
- chars[posn++] = (char)value;
- }
- else
- {
- chars[posn++] = '?';
- }
- }
-
- // Return the total length.
- return posn - charIndex;
+ return new CP932Decoder
(JISConvert.Convert).GetChars (bytes,
+ byteIndex, byteCount, chars, charIndex,
+ true);
}
// Get the maximum number of bytes needed to encode a
@@ -650,17 +496,28 @@
private sealed class CP932Decoder : Decoder
{
private JISConvert convert;
- private int lastByte;
+ private int last_byte_count;
+ private int last_byte_chars;
// Constructor.
public CP932Decoder(JISConvert convert)
{
this.convert = convert;
- this.lastByte = 0;
}
// Override inherited methods.
- public override int GetCharCount(byte[] bytes, int index, int
count)
+
+ public override int GetCharCount (
+ byte [] bytes, int index, int count)
+ {
+ return GetCharCount (bytes, index, count, false);
+ }
+
+ public
+#if NET_2_0
+ override
+#endif
+ int GetCharCount (byte [] bytes, int index, int count, bool
refresh)
{
// Validate the parameters.
if(bytes == null)
@@ -681,7 +538,7 @@
// Determine the total length of the
converted string.
int length = 0;
int byteval;
- int last = lastByte;
+ int last = last_byte_count;
while(count > 0)
{
byteval = bytes[index++];
@@ -702,13 +559,33 @@
last = 0;
}
}
+ if (refresh) {
+ if (last != 0)
+ length++;
+ last_byte_count = '\0';
+ }
+ else
+ last_byte_count = last;
// Return the total length.
return length;
}
- public override int GetChars(byte[] bytes, int byteIndex,
- int
byteCount, char[] chars,
- int
charIndex)
+
+ public override int GetChars (
+ byte [] bytes, int byteIndex, int byteCount,
+ char [] chars, int charIndex)
+ {
+ return GetChars (bytes, byteIndex, byteCount,
+ chars, charIndex, false);
+ }
+
+ public
+#if NET_2_0
+ override
+#endif
+ int GetChars (
+ byte [] bytes, int byteIndex, int byteCount,
+ char [] chars, int charIndex, bool refresh)
{
// Validate the parameters.
if(bytes == null)
@@ -739,7 +616,7 @@
int posn = charIndex;
int charLength = chars.Length;
int byteval, value;
- int last = lastByte;
+ int last = last_byte_chars;
#if __PNET__
byte *table = convert.jisx0208ToUnicode;
#else
@@ -825,7 +702,13 @@
}
}
}
- lastByte = last;
+ if (refresh) {
+ if (last != 0)
+ chars[posn++] =
'\u30FB';
+ last_byte_chars = '\0';
+ }
+ else
+ last_byte_chars = last;
// Return the final length to the
caller.
return posn - charIndex;
Modified: trunk/mcs/class/I18N/CJK/ChangeLog
===================================================================
--- trunk/mcs/class/I18N/CJK/ChangeLog 2006-01-20 12:49:51 UTC (rev 55830)
+++ trunk/mcs/class/I18N/CJK/ChangeLog 2006-01-20 13:01:41 UTC (rev 55831)
@@ -1,5 +1,14 @@
2006-01-20 Atsushi Enomoto <[EMAIL PROTECTED]>
+ * CP932.cs :
+ Implemented Decoder to handle incomplete bytes correctly. Hold
+ different incomplete bytes for GetChars() and GetCharCount().
+ "reflesh" parameter in 2.0 is now handled correctly.
+ Now GetChars()/GetCharCount() in Encoding are implemented
+ correctly to use Decoder.
+
+2006-01-20 Atsushi Enomoto <[EMAIL PROTECTED]>
+
Fixed bug #77307.
* GB18030Encoding.cs : GetEncoder() and GetDecoder() should be
overriden. in the Encoder, refresh parameter should handle
Modified: trunk/mcs/class/I18N/CJK/Test/ChangeLog
===================================================================
--- trunk/mcs/class/I18N/CJK/Test/ChangeLog 2006-01-20 12:49:51 UTC (rev
55830)
+++ trunk/mcs/class/I18N/CJK/Test/ChangeLog 2006-01-20 13:01:41 UTC (rev
55831)
@@ -1,5 +1,9 @@
2006-01-20 Atsushi Enomoto <[EMAIL PROTECTED]>
+ * I18N.CJK.Test.cs : added Decoder932Refresh().
+
+2006-01-20 Atsushi Enomoto <[EMAIL PROTECTED]>
+
* I18N.CJK.Test.cs : Actually some of new tests fail under .NET 1.x.
Added Encoder54936Refresh() which tests Encoder.GetBytes() where
refresh==true.
Modified: trunk/mcs/class/I18N/CJK/Test/I18N.CJK.Test.cs
===================================================================
--- trunk/mcs/class/I18N/CJK/Test/I18N.CJK.Test.cs 2006-01-20 12:49:51 UTC
(rev 55830)
+++ trunk/mcs/class/I18N/CJK/Test/I18N.CJK.Test.cs 2006-01-20 13:01:41 UTC
(rev 55831)
@@ -289,6 +289,28 @@
bytes, "#6");
}
+#if NET_2_0
+ [Test]
+ public void Decoder932Refresh ()
+ {
+ Encoding e = Encoding.GetEncoding (932);
+ Decoder d = e.GetDecoder ();
+ char [] chars;
+
+ chars = new char [1];
+ Assert.AreEqual (0, d.GetChars (new byte [] {0x81}, 0,
1, chars, 0, false), "#1");
+ Assert.AreEqual (new char [] {'\0'}, chars, "#2");
+
+ chars = new char [1];
+ Assert.AreEqual (1, d.GetChars (new byte [] {0x81}, 0,
1, chars, 0, true), "#3");
+ Assert.AreEqual (new char [] {'\uFF1D'}, chars, "#4");
+
+ chars = new char [1];
+ Assert.AreEqual (1, d.GetChars (new byte [] {0x81}, 0,
1, chars, 0, true), "#5");
+ Assert.AreEqual (new char [] {'\u30FB'}, chars, "#6");
+ }
+#endif
+
#endregion
#region Korean
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches
