- private static int InternalGetBytes(char[] chars, int charIndex,
- int charCount, byte[] bytes,
- int byteIndex, ref uint leftOver,
- bool emitIdentifier, bool flush)
- {
- // Validate the parameters.
- if(chars == null)
- {
- throw new ArgumentNullException("chars");
- }
- if(bytes == null)
- {
- throw new ArgumentNullException("bytes");
- }
- if(charIndex < 0 || charIndex > chars.Length)
- {
- throw new ArgumentOutOfRangeException
- ("charIndex", _("ArgRange_Array"));
- }
- if(charCount < 0 || charCount > (chars.Length - charIndex))
- {
- throw new ArgumentOutOfRangeException
- ("charCount", _("ArgRange_Array"));
- }
- if(byteIndex < 0 || byteIndex > bytes.Length)
- {
- throw new ArgumentOutOfRangeException
- ("byteIndex", _("ArgRange_Array"));
- }
-
- // Convert the characters into bytes.
- char ch;
- int length = bytes.Length;
- uint pair;
- uint left = leftOver;
- int posn = byteIndex;
- if(emitIdentifier)
- {
- if((posn + 3) > length)
- {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)0xEF;
- bytes[posn++] = (byte)0xBB;
- bytes[posn++] = (byte)0xBF;
- }
- while(charCount > 0)
- {
- // Fetch the next UTF-16 character pair value.
- ch = chars[charIndex++];
- --charCount;
- if(left == 0)
- {
- if(ch >= '\uD800' && ch <= '\uDBFF')
- {
- // This is the start of a surrogate pair.
- left = (uint)ch;
- continue;
- }
- else
- {
- // This is a regular character.
- pair = (uint)ch;
- }
- }
- else if(ch >= '\uDC00' && ch <= '\uDFFF')
- {
- // We have a surrogate pair.
- pair = ((left - (uint)0xD800) << 10) +
- (((uint)ch) - (uint)0xDC00) +
- (uint)0x10000;
- left = 0;
- }
- else
- {
- // We have a surrogate start followed by a
- // regular character. Technically, this is
- // invalid, but we have to do something.
- // We write out the surrogate start and then
- // re-visit the current character again.
- pair = (uint)left;
- left = 0;
- --charIndex;
- ++charCount;
- }
-
- // Encode the character pair value.
- if(pair < (uint)0x0080)
- {
- if(posn >= length)
- {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)pair;
- }
- else if(pair < (uint)0x0800)
- {
- if((posn + 2) > length)
- {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(0xC0 | (pair >> 6));
- bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
- }
- else if(pair < (uint)0x10000)
- {
- if((posn + 3) > length)
- {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(0xE0 | (pair >> 12));
- bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
- bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
- }
- else
- {
- if((posn + 4) > length)
- {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(0xF0 | (pair >> 18));
- bytes[posn++] = (byte)(0x80 | ((pair >> 12) & 0x3F));
- bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
- bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
- }
- }
- if(flush && left != 0)
- {
- // Flush the left-over surrogate pair start.
- if((posn + 3) > length)
- {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(0xE0 | (left >> 12));
- bytes[posn++] = (byte)(0x80 | ((left >> 6) & 0x3F));
- bytes[posn++] = (byte)(0x80 | (left & 0x3F));
- left = 0;
- }
- leftOver = left;
+ private static int InternalGetBytes (char[] chars, int charIndex,
+ int charCount, byte[] bytes,
+ int byteIndex, ref uint leftOver,
+ bool flush)
+ {
+ // Validate the parameters.
+ if (chars == null) {
+ throw new ArgumentNullException ("chars");
+ }
+ if (bytes == null) {
+ throw new ArgumentNullException ("bytes");
+ }
+ if (charIndex < 0 || charIndex > chars.Length) {
+ throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
+ }
+ if (charCount < 0 || charCount > (chars.Length - charIndex)) {
+ throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
+ }
+ if (byteIndex < 0 || byteIndex > bytes.Length) {
+ throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
+ }
+
+ // Convert the characters into bytes.
+ char ch;
+ int length = bytes.Length;
+ uint pair;
+ uint left = leftOver;
+ int posn = byteIndex;
+ while (charCount > 0) {
+ // Fetch the next UTF-16 character pair value.
+ ch = chars[charIndex++];
+ --charCount;
+ if (left == 0) {
+ if (ch >= '\uD800' && ch <= '\uDBFF') {
+ // This is the start of a surrogate pair.
+ left = (uint)ch;
+ continue;
+ } else {
+ // This is a regular character.
+ pair = (uint)ch;
+ }
+ } else if (ch >= '\uDC00' && ch <= '\uDFFF') {
+ // We have a surrogate pair.
+ pair = ((left - (uint)0xD800) << 10) +
+ (((uint)ch) - (uint)0xDC00) +
+ (uint)0x10000;
+ left = 0;
+ } else {
+ // We have a surrogate start followed by a
+ // regular character. Technically, this is
+ // invalid, but we have to do something.
+ // We write out the surrogate start and then
+ // re-visit the current character again.
+ pair = (uint)left;
+ left = 0;
+ --charIndex;
+ ++charCount;
+ }