public static MutableString /*!*/ PackInetSockAddr(ConversionStorage <MutableString> /*!*/ stringCast, ConversionStorage <int> /*!*/ fixnumCast, RubyClass /*!*/ self, object port, object hostNameOrAddress) { int iPort = ConvertToPortNum(stringCast, fixnumCast, port); IPAddress address = (hostNameOrAddress != null) ? GetHostAddress(ConvertToHostString(stringCast, hostNameOrAddress)) : IPAddress.Loopback; SocketAddress socketAddress = new IPEndPoint(address, iPort).Serialize(); var result = MutableString.CreateBinary(socketAddress.Size); for (int i = 0; i < socketAddress.Size; i++) { result.Append(socketAddress[i]); } return(result); }
public void File_AppendBytes1() { string s; string crlf = "\r\n"; var stream = new TestStream(false, B( "ab\r\r\n" + "e" + (s = "fgh" + crlf + "ijkl" + crlf + "mnop" + crlf + crlf + crlf + crlf + "qrst") + crlf + "!" )); int s_crlf_count = 6; var io = new RubyBufferedStream(stream); Assert(io.PeekByte() == (byte)'a'); var buffer = MutableString.CreateBinary(B("foo:")); Assert(io.AppendBytes(buffer, 4, false) == 4); Assert(buffer.ToString() == "foo:ab\r\n"); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, 1, false) == 1); Assert(buffer.ToString() == "e"); buffer = MutableString.CreateMutable("x:", RubyEncoding.Binary); int c = s.Length - s_crlf_count - 2; Assert(io.AppendBytes(buffer, c, false) == c); Assert(buffer.ToString() == "x:" + s.Replace(crlf, "\n").Substring(0, c)); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, 10, false) == 4); Assert(buffer.ToString() == "st\n!"); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, 10, false) == 0); Assert(buffer.ToString() == ""); stream = new TestStream(false, B(s = "abcd" + crlf + "xyz" + crlf + "qqq;")); io = new RubyBufferedStream(stream); buffer = MutableString.CreateBinary(); Assert(io.AppendBytes(buffer, Int32.MaxValue, true) == s.Length); io.BaseStream.Seek(0, SeekOrigin.Begin); Assert(io.AppendBytes(buffer, Int32.MaxValue, false) == s.Length - 2); Assert(buffer.ToString() == s + s.Replace(crlf, "\n")); }
public static RubyArray /*!*/ ReceiveFrom(ConversionStorage <int> /*!*/ conversionStorage, IPSocket /*!*/ self, int length, [DefaultParameterValue(null)] object /*Numeric*/ flags) { SocketFlags sFlags = ConvertToSocketFlag(conversionStorage, flags); byte[] buffer = new byte[length]; EndPoint fromEP = new IPEndPoint(IPAddress.Any, 0); int received = self.Socket.ReceiveFrom(buffer, sFlags, ref fromEP); MutableString str = MutableString.CreateBinary(); str.Append(buffer, 0, received); var context = conversionStorage.Context; str.IsTainted = true; return(RubyOps.MakeArray2(str, self.GetAddressArray(fromEP))); }
public void Symbols1() { byte[] bytes = Encoding.UTF8.GetBytes("α"); RubySymbol a, b, c, d; a = Context.CreateSymbolInternal(MutableString.CreateBinary(bytes, RubyEncoding.Binary)); b = Context.CreateSymbolInternal(MutableString.CreateBinary(bytes, RubyEncoding.KCodeSJIS)); c = Context.CreateSymbolInternal(MutableString.CreateBinary(bytes, RubyEncoding.KCodeUTF8)); d = Context.CreateSymbolInternal(MutableString.Create("α", RubyEncoding.KCodeUTF8)); Assert(a.Equals(b)); Assert(a.Equals(c)); Assert(a.Equals(d)); a = Context.CreateSymbolInternal(MutableString.CreateBinary(Encoding.ASCII.GetBytes("foo"), RubyEncoding.Binary)); b = Context.CreateSymbolInternal(MutableString.CreateMutable("foo", RubyEncoding.KCodeUTF8)); Assert(a.Equals(b)); }
public static MutableString /*!*/ iconv(Iconv /*!*/ self, [DefaultProtocol] MutableString /*!*/ str, [DefaultProtocol, DefaultParameterValue(0)] int startIndex, [DefaultProtocol, DefaultParameterValue(-1)] int endIndex) { // TODO: int bytesUsed, charsUsed; bool completed; byte[] source = str.ConvertToBytes(); char[] buffer = new char[self._fromEncoding.GetCharCount(source, 0, source.Length)]; self._fromEncoding.Convert(source, 0, source.Length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed); Debug.Assert(charsUsed == buffer.Length && bytesUsed == source.Length); byte[] result = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)]; int bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false); Debug.Assert(bytesEncoded == result.Length); return(MutableString.CreateBinary(result)); }
public static MutableString /*!*/ RandomBytes(RubyModule /*!*/ self, [DefaultProtocol] int length) { if (length < 0) { throw RubyExceptions.CreateArgumentError("negative string size"); } if (length == 0) { return(MutableString.CreateEmpty()); } byte[] data = new byte[length]; var generator = new Crypto.RNGCryptoServiceProvider(); generator.GetBytes(data); return(MutableString.CreateBinary(data)); }
public void Dir2() { RubyClass dir = Context.GetClass(typeof(RubyDir)); Pal1 pal = (Pal1)Context.Platform; var sjis = RubyEncoding.KCodeSJIS.StrictEncoding.GetBytes("ホ"); // use the string encoding if given RubyDir.MakeDirectory(dir, MutableString.CreateBinary(sjis, RubyEncoding.KCodeSJIS.RealEncoding), null); Assert(pal.Entries["ホ"]); // IO system returns UTF8 encoded strings: var entries = RubyDir.GetEntries(dir, MutableString.CreateEmpty()); Assert(entries.Count == 3); Assert(((MutableString)entries[0]).Equals(MutableString.CreateAscii("."))); Assert(((MutableString)entries[1]).Equals(MutableString.CreateAscii(".."))); Assert(((MutableString)entries[2]).Equals(MutableString.Create("ホ", RubyEncoding.UTF8))); pal.Entries.Clear(); }
internal override MutableString /*!*/ Finish() { MutableString result; if (GetStream().IsInitialized) { result = InflateString(this, null); } else { result = MutableString.CreateBinary(); } if (trailingUncompressedData != null) { result.Append(trailingUncompressedData); trailingUncompressedData = null; } return(result); }
internal static RubyArray /*!*/ CreateHostEntryArray(RubyContext /*!*/ context, IPHostEntry /*!*/ hostEntry, bool packIpAddresses) { RubyArray result = new RubyArray(4); // host name: result.Add(HostNameToMutableString(context, hostEntry.HostName)); // aliases: RubyArray aliases = new RubyArray(hostEntry.Aliases.Length); foreach (string alias in hostEntry.Aliases) { aliases.Add(HostNameToMutableString(context, alias)); } result.Add(aliases); // address (the first IPv4): foreach (IPAddress address in hostEntry.AddressList) { if (address.AddressFamily == AddressFamily.InterNetwork) { result.Add((int)address.AddressFamily); if (packIpAddresses) { byte[] bytes = address.GetAddressBytes(); MutableString str = MutableString.CreateBinary(); str.Append(bytes, 0, bytes.Length); result.Add(str); } else { result.Add(MutableString.CreateAscii(address.ToString())); } break; } } return(result); }
private MutableString /*!*/ Close(bool resetEncoder) { char[] buffer = new char[0]; byte[] result = new byte[_toEncoding.GetByteCount(buffer, 0, 0, true)]; int bytesEncoded = _toEncoding.GetBytes(buffer, 0, 0, result, 0, true); Debug.Assert(bytesEncoded == result.Length); if (resetEncoder) { #if SILVERLIGHT // TODO - Create a new encoder throw new NotImplementedException(); #else _toEncoding.Reset(); ResetByteOrderMark(); #endif } else { _isClosed = true; } return(MutableString.CreateBinary(result)); }
private void Inspect2() { const char sq = '\''; var sjisEncoding = RubyEncoding.SJIS; // あ var sjisWide = new byte[] { 0x82, 0xa0 }; // \u{12345} in UTF-8: var utf8 = new byte[] { 0xF0, 0x92, 0x8D, 0x85 }; // \u{12345} in UTF-16: U+d808 U+df45 var utf16 = Encoding.UTF8.GetString(utf8); string s; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.Binary), false, sq).ToString(); Assert(s == @"'\xF0\x92\x8D\x85'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.Binary), true, sq).ToString(); Assert(s == @"'\xF0\x92\x8D\x85'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.UTF8), false, sq).ToString(); Assert(s == @"'\u{12345}'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.UTF8), true, sq).ToString(); Assert(s == @"'\u{12345}'"); // incomplete character: s = MutableStringOps.GetQuotedStringRepresentation(MutableString.Create("\ud808\udf45\ud808", RubyEncoding.UTF8), false, sq).ToString(); Assert(s == @"'\u{12345}\u{d808}'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, sjisEncoding), false, sq).ToString(); Assert(s == @"'\x82\xA0'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, sjisEncoding), true, sq).ToString(); Assert(s == @"'\x82\xA0'"); }
internal static RubyArray /*!*/ CreateHostEntryArray(IPHostEntry hostEntry, bool packIpAddresses) { RubyArray result = new RubyArray(4); // Canonical Hostname result.Add(MutableString.Create(hostEntry.HostName)); // Aliases RubyArray aliases = new RubyArray(hostEntry.Aliases.Length); foreach (string alias in hostEntry.Aliases) { aliases.Add(MutableString.Create(alias)); } result.Add(aliases); // Address Type result.Add((int)hostEntry.AddressList[0].AddressFamily); // IP Address foreach (IPAddress address in hostEntry.AddressList) { if (packIpAddresses) { byte[] bytes = address.GetAddressBytes(); MutableString str = MutableString.CreateBinary(); str.Append(bytes, 0, bytes.Length); result.Add(str); } else { result.Add(MutableString.Create(address.ToString())); } } return(result); }
public static MutableString /*!*/ iconv(Iconv /*!*/ self, [DefaultProtocol] MutableString str, [DefaultProtocol, DefaultParameterValue(0)] int startIndex, [DefaultProtocol, NotNull, DefaultParameterValue(-1)] int length) { if (self._isClosed) { throw RubyExceptions.CreateArgumentError("closed stream"); } if (str == null) { return(self.Close(true)); } // TODO: int bytesUsed, charsUsed; bool completed; byte[] source = str.ConvertToBytes(); if (startIndex < 0) { startIndex = source.Length + startIndex; if (startIndex < 0) { //throw new IllegalSequence("start index is too large of a negative number"); startIndex = 0; length = 0; } } else if (startIndex > source.Length) { startIndex = 0; length = 0; } if ((length < 0) || (startIndex + length > source.Length)) { length = source.Length - startIndex; } char[] buffer = new char[self._fromEncoding.GetCharCount(source, startIndex, length)]; self._fromEncoding.Convert(source, startIndex, length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed); Debug.Assert(charsUsed == buffer.Length && bytesUsed == length); byte[] result = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)]; int bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false); Debug.Assert(bytesEncoded == result.Length); if (self._emitBom && result.Length > 0) { byte[] resultWithBom = new byte[2 + result.Length]; resultWithBom[0] = 0xff; resultWithBom[1] = 0xfe; Array.Copy(result, 0, resultWithBom, 2, result.Length); result = resultWithBom; self._emitBom = false; } return(MutableString.CreateBinary(result)); }
private static MutableString Transform(ConversionStorage <MutableString> toS, IDictionary self, GeneratorState state, int depth) { byte[] objectNl = state.ObjectNl.ToByteArray(); byte[] indent = Helpers.Repeat(state.Indent.ToByteArray(), depth + 1); byte[] spaceBefore = state.SpaceBefore.ToByteArray(); byte[] space = state.Space.ToByteArray(); int subDepth = depth + 1; MutableString result = MutableString.CreateBinary(2 + self.Count * (12 + indent.Length + spaceBefore.Length + space.Length)); RubyContext context = toS.Context; result.Append((byte)'{'); result.Append(objectNl); if (self.Count > 0) { MutableString json; int i = 0; foreach (DictionaryEntry kv in self) { if (i > 0) { result.Append(objectNl); } if (objectNl.Length != 0) { result.Append(indent); } json = Generator.ToJson(context, Protocols.ConvertToString(toS, kv.Key), state, subDepth); result.Append(json); context.TaintObjectBy <Object>(result, json); result.Append(spaceBefore); result.Append((byte)':'); result.Append(space); json = Generator.ToJson(context, kv.Value, state, subDepth); result.Append(json); context.TaintObjectBy <Object>(result, json); if (++i < self.Count) { result.Append(','); } } if (objectNl.Length != 0) { result.Append(objectNl); if (indent.Length != 0) { for (int n = 0; n < depth; n++) { result.Append(indent); } } } } result.Append((byte)'}'); return(result); }
public static MutableString /*!*/ Close(Inflate /*!*/ self) { return(MutableString.CreateBinary(self._outputBuffer)); }
public void RegexEncoding2() { var SJIS = RubyEncoding.KCodeSJIS.StrictEncoding; // 1.9 encodings: var invalidUtf8 = MutableString.CreateBinary(new byte[] { 0x80 }, RubyEncoding.UTF8); AssertExceptionThrown <ArgumentException>(() => new RubyRegex(invalidUtf8, RubyRegexOptions.NONE)); // LastMatch MatchData m; var u = MutableString.CreateBinary(SJIS.GetBytes("あああ"), RubyEncoding.KCodeSJIS); var p = MutableString.CreateBinary(SJIS.GetBytes("あ{2}"), RubyEncoding.KCodeSJIS); var rs = new RubyRegex(p, RubyRegexOptions.SJIS); // /あ{2}/ matches "あああ", the resulting index is in bytes: m = rs.LastMatch(null, u); Assert(m != null && m.Index == 2); rs = new RubyRegex(MutableString.CreateBinary(SJIS.GetBytes("あ")), RubyRegexOptions.SJIS); // "start at" in the middle of a character: m = rs.LastMatch(null, u, 0); Assert(m != null && m.Index == 0); m = rs.LastMatch(null, u, 1); Assert(m != null && m.Index == 0); m = rs.LastMatch(null, u, 2); Assert(m != null && m.Index == 2); m = rs.LastMatch(null, u, 3); Assert(m != null && m.Index == 2); // Split u = MutableString.CreateBinary(SJIS.GetBytes("あちあちあ"), RubyEncoding.UTF8); rs = new RubyRegex(MutableString.CreateBinary(SJIS.GetBytes("ち")), RubyRegexOptions.SJIS); var parts = rs.Split(null, u); Assert(parts.Length == 3); foreach (var part in parts) { Assert(part.Encoding == RubyEncoding.KCodeSJIS); Assert(part.ToString() == "あ"); } // groups rs = new RubyRegex(MutableString.CreateBinary(SJIS.GetBytes("ち(a(あ+)(b+))+あ")), RubyRegexOptions.SJIS); u = MutableString.CreateBinary(SJIS.GetBytes("ちaああbaあbbbあ")); m = rs.Match(null, u); Assert(m.GroupCount == 4); int s, l; Assert(m.GetGroupStart(0) == (s = 0)); Assert(m.GetGroupLength(0) == (l = u.GetByteCount())); Assert(m.GetGroupEnd(0) == s + l); // the group has 2 captures, the last one is its value: Assert(m.GetGroupStart(1) == (s = SJIS.GetByteCount("ちaああb"))); Assert(m.GetGroupLength(1) == (l = SJIS.GetByteCount("aあbbb"))); Assert(m.GetGroupEnd(1) == s + l); // the group has 2 captures, the last one is its value: Assert(m.GetGroupStart(2) == (s = SJIS.GetByteCount("ちaああba"))); Assert(m.GetGroupLength(2) == (l = SJIS.GetByteCount("あ"))); Assert(m.GetGroupEnd(2) == s + l); // the group has 2 captures, the last one is its value: Assert(m.GetGroupStart(3) == (s = SJIS.GetByteCount("ちaああbaあ"))); Assert(m.GetGroupLength(3) == (l = SJIS.GetByteCount("bbb"))); Assert(m.GetGroupEnd(3) == s + l); }
public void RegexEncoding1() { MatchData m; // the k-coding of the pattern string is irrelevant: foreach (var pe in new[] { RubyEncoding.Binary }) { var p = MutableString.CreateBinary(new byte[] { 0x82, 0xa0, (byte)'{', (byte)'2', (byte)'}' }, pe); var r = new RubyRegex(p, RubyRegexOptions.NONE); var rs = new RubyRegex(p, RubyRegexOptions.SJIS); // the k-coding of the string is irrelevant: foreach (var se in new[] { RubyEncoding.Binary }) { var s = MutableString.CreateBinary(new byte[] { 0x82, 0xa0, 0xa0 }, se); var t = MutableString.CreateBinary(new byte[] { 0x82, 0xa0, 0xa0, 0x82, 0xa0, 0xa0, 0xff }, se); var u = MutableString.CreateBinary(new byte[] { 0x82, 0xa0, 0x82, 0xa0, 0x82, 0xa0 }, se); // /あ{2}/ does not match "あ\xa0" m = r.Match(RubyEncoding.KCodeSJIS, s); Assert(m == null); // /\x82\xa0{2}/ matches "[ \x82\xa0\xa0 ] \x82\xa0\xa0\xff" m = r.Match(null, s); Assert(m != null && m.Index == 0); // /\x82\xa0{2}/ matches "\x82\xa0\xa0 [ \x82\xa0\xa0 ] \xff" starting from byte #1: m = r.Match(null, t, 1, false); Assert(m != null && m.Index == 3 && m.Length == 3); // /あ{2}/s does not match "あ\xa0", current KCODE is ignored m = rs.Match(null, s); Assert(m == null); // /あ{2}/s does not match "あ\xa0", current KCODE is ignored m = rs.Match(RubyEncoding.KCodeUTF8, s); Assert(m == null); // /あ{2}/s matches "ああ\xff", current KCODE is ignored m = rs.Match(RubyEncoding.KCodeUTF8, u, 2, false); Assert(m != null && m.Index == 2 && m.Length == 4); // /あ{2}/ does not match "あ\xa0あ\xa0" m = r.LastMatch(RubyEncoding.KCodeSJIS, t); Assert(m == null); // /\x82\xa0{2}/ matches "\x82\xa0\xa0 [ \x82\xa0\xa0 ] \xff" m = r.LastMatch(null, t); Assert(m != null && m.Index == 3); // /あ{2}/s does not match "あ\xa0あ\xa0", current KCODE is ignored m = rs.LastMatch(null, t); Assert(m == null); // /あ{2}/s does not match "あ\xa0あ\xa0", current KCODE is ignored m = rs.LastMatch(RubyEncoding.KCodeUTF8, t); Assert(m == null); } } }
private static MutableString /*!*/ ConstructRubyBinary(RubyConstructor /*!*/ ctor, Node /*!*/ node) { return(MutableString.CreateBinary(BaseConstructor.ConstructYamlBinary(ctor, node))); }
private static int Process(zlib.ZStream /*!*/ zst, zlib.FlushStrategy flush, bool compress, ref MutableString trailingUncompressedData) { if (zst.next_out == null) { zst.next_out = new byte[DEFAULTALLOC]; zst.next_out_index = 0; zst.avail_out = zst.next_out.Length; } int result = compress ? zst.deflate(flush) : zst.inflate(flush); while (result == Z_OK && zst.avail_out == 0) { byte[] output = zst.next_out; int oldLength = output.Length; Array.Resize(ref output, oldLength * 2); zst.next_out = output; zst.avail_out = oldLength; result = compress ? zst.deflate(flush) : zst.inflate(flush); } if (!compress && (result == Z_STREAM_END || result == Z_STREAM_ERROR && !zst.IsInitialized)) { // MRI hack: any data left in the stream are saved into a separate buffer and returned when "finish" is called // This is weird behavior, one would expect the rest of the stream is either ignored or copied to the output buffer. #if COPY_UNCOMPRESSED_DATA_TO_OUTPUT_BUFFER Debug.Assert(zst.next_in_index + zst.avail_in <= zst.next_in.Length); Debug.Assert(zst.next_out_index + zst.avail_out <= zst.next_out.Length); if (zst.avail_in > zst.avail_out) { byte[] output = zst.next_out; int oldLength = output.Length; Array.Resize(ref output, Math.Max(zst.next_out_index + zst.avail_in, oldLength * 2)); zst.next_out = output; } Buffer.BlockCopy(zst.next_in, zst.next_in_index, zst.next_out, zst.next_out_index, zst.avail_in); // MRI subtracts till 0 is reached: zst.avail_out = Math.Max(zst.avail_out - zst.avail_in, 0); zst.next_out_index += zst.avail_in; zst.avail_in = 0; #else if (trailingUncompressedData == null) { trailingUncompressedData = MutableString.CreateBinary(); } trailingUncompressedData.Append(zst.next_in, zst.next_in_index, zst.avail_in); // MRI subtracts till 0 is reached: zst.avail_out = Math.Max(zst.avail_out - zst.avail_in, 0); zst.avail_in = 0; #endif result = Z_STREAM_END; } return(result); }
public static MutableString /*!*/ Finish(RubyContext /*!*/ context, Base /*!*/ self) { byte[] input = self._buffer.ConvertToBytes(); byte[] hash = self._algorithm.ComputeHash(input); return(MutableString.CreateBinary(hash)); }
protected Base(HashAlgorithm /*!*/ algorithm) { Assert.NotNull(algorithm); _algorithm = algorithm; _buffer = MutableString.CreateBinary(); }
public static Base /*!*/ Reset(RubyContext /*!*/ context, Base /*!*/ self) { self._buffer = MutableString.CreateBinary(); self._algorithm.Initialize(); return(self); }
public static StringIO /*!*/ Reopen(StringIO /*!*/ self) { self.SetContent(MutableString.CreateBinary()); self._mode = IOMode.ReadWrite; return(self); }
public StringIO() : this(MutableString.CreateBinary(), IOMode.ReadWrite) { }
public static MutableString ConstructRubyBinary(IConstructor ctor, Node node) { return(MutableString.CreateBinary(SafeConstructor.ConstructYamlBinary(ctor, node))); }