private static void FromHex(BinaryWriter /*!*/ writer, MutableString /*!*/ str, int nibbleCount, bool swap) { int maxCount = Math.Min(nibbleCount, str.GetByteCount()); for (int i = 0, j = 0; i < (nibbleCount + 1) / 2; i++, j += 2) { int hiNibble = (j < maxCount) ? FromHexDigit(str.GetByte(j)) : 0; int loNibble = (j + 1 < maxCount) ? FromHexDigit(str.GetByte(j + 1)) : 0; Debug.Assert(hiNibble >= 0 && hiNibble < 16 && loNibble >= 0 && loNibble < 16); int c = (swap) ? (loNibble << 4) | hiNibble : (hiNibble << 4) | loNibble; writer.Write((byte)c); } }
public override int Read(byte[] /*!*/ buffer, int offset, int count) { int maxReadLen = _string.GetByteCount() - _position; if (count > maxReadLen) { count = maxReadLen; } for (int i = 0; i < count; i++) { buffer[offset + i] = _string.GetByte(_position++); } return(count); }
public MutableString ReadLine(MutableString /*!*/ separator, RubyEncoding /*!*/ encoding, bool preserveEndOfLines) { int b = ReadByteNormalizeEoln(preserveEndOfLines); if (b == -1) { return(null); } int separatorOffset = 0; int separatorLength = separator.GetByteCount(); MutableString result = MutableString.CreateBinary(encoding); do { result.Append((byte)b); if (b == separator.GetByte(separatorOffset)) { if (separatorOffset == separatorLength - 1) { break; } separatorOffset++; } else if (separatorOffset > 0) { separatorOffset = 0; } b = ReadByteNormalizeEoln(preserveEndOfLines); } while (b != -1); return(result); }
/// <summary> /// Searches the pattern for hexadecimal and octal character escapes that represent a non-ASCII character. /// </summary> private static bool HasEscapedNonAsciiBytes(MutableString /*!*/ pattern) { int i = 0; int length = pattern.GetByteCount(); while (i < length - 2) { int c = pattern.GetByte(i++); if (c == '\\') { c = pattern.GetByte(i++); if (c == 'x') { // hexa escape: int d1 = Tokenizer.ToDigit(PeekByte(pattern, length, i++)); if (d1 < 16) { int d2 = Tokenizer.ToDigit(PeekByte(pattern, length, i++)); if (d2 < 16) { return(d1 * 16 + d2 >= 0x80); } } } else if (c >= '2' && c <= '7') { // a backreference (\1..\9) or an octal escape: int d = Tokenizer.ToDigit(PeekByte(pattern, length, i++)); if (d < 8) { int value = Tokenizer.ToDigit(c) * 8 + d; d = Tokenizer.ToDigit(PeekByte(pattern, length, i++)); if (d < 8) { value = value * 8 + d; } return(value >= 0x80); } } } } return(false); }
private static int PeekByte(MutableString /*!*/ str, int length, int i) { return((i < length) ? str.GetByte(i) : -1); }