public static MutableString ToJson(RubyContext context, IList self, GeneratorState state, int?depth) { MutableString result; if (state == null) { result = MutableString.CreateMutable(2 + Math.Max(self.Count * 4, 0), RubyEncoding.UTF8); result.Append('['); context.TaintObjectBy <Object>(result, self); if (self.Count > 0) { for (int i = 0; i < self.Count; i++) { Object element = self[i]; result.Append(Generator.ToJson(context, element, null, 0)); context.TaintObjectBy <Object>(result, element); if (i < self.Count - 1) { result.Append(','); } } } result.Append(']'); } else { result = Transform(context, self, state, depth.HasValue ? depth.Value : 0); } return(context.TaintObjectBy <MutableString>(result, self)); }
internal static object DumpAll(RubyRepresenter /*!*/ rep, IEnumerable /*!*/ objs, RubyIO io) { TextWriter writer; if (io != null) { writer = new RubyIOWriter(io); } else { // the output is ascii: writer = new MutableStringWriter(MutableString.CreateMutable(RubyEncoding.Binary)); } YamlOptions cfg = YamlOptions.DefaultOptions; Serializer s = new Serializer(writer, cfg); foreach (object obj in objs) { s.Serialize(rep.Represent(obj)); rep.ForgetObjects(); } s.Close(); if (io != null) { return(io); } else { return(((MutableStringWriter)writer).String); } }
public static MutableString /*!*/ Dump(BigDecimal /*!*/ self, [Optional] object limit) { // We ignore the limit value as BigDecimal does not contain other objects. return(MutableString.CreateMutable( string.Format("{0}:{1}", self.MaxPrecisionDigits, self))); }
public static MutableString ToJson(MutableString self) { MutableString result = MutableString.CreateMutable(self.Length + 2, RubyEncoding.UTF8); char[] chars = Encoding.UTF8.GetChars(self.ToByteArray()); byte[] escapeSequence = new byte[] { (byte)'\\', 0 }; result.Append('"'); foreach (char c in chars) { switch (c) { case '"': case '/': case '\\': escapeSequence[1] = (byte)c; result.Append(escapeSequence); break; case '\n': escapeSequence[1] = (byte)'n'; result.Append(escapeSequence); break; case '\r': escapeSequence[1] = (byte)'r'; result.Append(escapeSequence); break; case '\t': escapeSequence[1] = (byte)'t'; result.Append(escapeSequence); break; case '\f': escapeSequence[1] = (byte)'f'; result.Append(escapeSequence); break; case '\b': escapeSequence[1] = (byte)'b'; result.Append(escapeSequence); break; default: if (c >= 0x20 && c <= 0x7F) { result.Append((byte)c); } else { result.Append(Helpers.EscapeUnicodeChar(c)); } break; } } result.Append('"'); return(result); }
public static MutableString /*!*/ Dump(BigDecimal /*!*/ self, [Optional] object limit) { // We ignore the limit value as BigDecimal does not contain other objects. return(MutableString.CreateMutable(RubyEncoding.Binary). Append(self.MaxPrecisionDigits.ToString(CultureInfo.InvariantCulture)). Append(':'). Append(self.ToString() )); }
public static MutableString ToJson(ConversionStorage <MutableString> toS, IDictionary self, GeneratorState state, int?depth) { MutableString result; RubyContext context = toS.Context; if (state == null) { MutableString json; result = MutableString.CreateMutable(2 + Math.Max(self.Count * 12, 0), RubyEncoding.Default); result.Append('{'); if (self.Count > 0) { int i = 0; foreach (DictionaryEntry kv in self) { // TODO: added state and depth json = Generator.ToJson(context, Protocols.ConvertToString(toS, kv.Key), null, 0); result.Append(json); context.TaintObjectBy <Object>(result, json); result.Append(':'); json = Generator.ToJson(context, kv.Value, null, 0); result.Append(json); context.TaintObjectBy <Object>(result, json); if (++i < self.Count) { result.Append(','); } } } result.Append('}'); } else { GeneratorState.Ensure(state); if (state.CheckCircular) { if (state.Seen(context, self)) { Helpers.ThrowCircularDataStructureException("circular data structures not supported!"); } state.Remember(context, self); result = Transform(toS, self, state, depth.HasValue ? depth.Value : 0); state.Forget(context, self); } else { result = Transform(toS, self, state, depth.HasValue ? depth.Value : 0); } } return(result); }
public static MutableString Rest(StringScanner /*!*/ self) { int len = self.Length - self.CurrentPosition; if (len <= 0) { return(MutableString.CreateMutable()); } return(self.ScanString.GetSlice(self.CurrentPosition, len)); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, BigDecimal /*!*/ self) { MutableString str = MutableString.CreateMutable(); str.AppendFormat("#<{0}:", context.GetClassOf(self).Name); RubyUtils.AppendFormatHexObjectId(str, RubyUtils.GetObjectId(context, self)); str.AppendFormat(",'{0}',", self.ToString(10)); str.AppendFormat("{0}({1})>", self.PrecisionDigits.ToString(), self.MaxPrecisionDigits.ToString()); return(str); }
public static MutableString Inspect(RubyContext /*!*/ context, YamlStream /*!*/ self) { MutableString result = MutableString.CreateMutable("#<YAML::Stream:"); RubyUtils.AppendFormatHexObjectId(result, RubyUtils.GetObjectId(context, self)) .Append(" @documents=") .Append(RubySites.Inspect(context, self.Documents)) .Append(", options=") .Append(RubySites.Inspect(context, self.Options)) .Append('>'); return(result); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, YamlStream /*!*/ self) { MutableString result = MutableString.CreateMutable("#<YAML::Stream:", RubyEncoding.Binary); RubyUtils.AppendFormatHexObjectId(result, RubyUtils.GetObjectId(context, self)) .Append(" @documents=") .Append(context.Inspect(self._documents)) .Append(", options=") .Append(context.Inspect(self._options)) .Append('>'); return(result); }
public void Dir1() { RubyClass dir = Context.GetClass(typeof(RubyDir)); Pal1 pal = (Pal1)Context.Platform; var sjis = RubyEncoding.KCodeSJIS.StrictEncoding.GetBytes("ホ"); var toPath = new ConversionStorage <MutableString>(Context); // transcode to UTF8 if no KCODE specified Context.KCode = null; RubyDir.MakeDirectory(toPath, dir, MutableString.CreateBinary(new byte[] { 0xce, 0xa3 }, RubyEncoding.Binary), null); Assert(pal.Entries["Σ"]); pal.Entries.Clear(); // transcode to UTF8 if no KCODE specified Context.KCode = null; RubyDir.MakeDirectory(toPath, dir, MutableString.CreateMutable("ホア", RubyEncoding.KCodeSJIS), null); Assert(pal.Entries["α"]); Assert(FileTest.IsDirectory(toPath, Context.KernelModule, MutableString.CreateMutable("ホア", RubyEncoding.KCodeSJIS))); Assert(FileTest.IsDirectory(toPath, Context.KernelModule, MutableString.CreateMutable("α", RubyEncoding.KCodeUTF8))); pal.Entries.Clear(); // transcode to KCODE if specified Context.KCode = RubyEncoding.KCodeUTF8; RubyDir.MakeDirectory(toPath, dir, MutableString.CreateBinary(new byte[] { 0xce, 0xa3 }, RubyEncoding.KCodeSJIS), null); Assert(pal.Entries["Σ"]); pal.Entries.Clear(); // transcode to KCODE if specified Context.KCode = RubyEncoding.KCodeSJIS; RubyDir.MakeDirectory(toPath, dir, MutableString.CreateBinary(sjis, RubyEncoding.Binary), null); Assert(pal.Entries["ホ"]); pal.Entries.Clear(); // ignore entries whose name cannot be encoded using the current KCODE Context.KCode = RubyEncoding.KCodeSJIS; AssertExceptionThrown <EncoderFallbackException>(() => RubyEncoding.KCodeSJIS.StrictEncoding.GetBytes("Ԋ")); pal.Entries["Ԋ"] = true; pal.Entries["ホ"] = true; var entries = RubyDir.GetEntries(toPath, dir, MutableString.CreateEmpty()); Assert(entries.Count == 3); foreach (MutableString entry in entries) { Assert(entry.Encoding == RubyEncoding.KCodeSJIS); } Assert(((MutableString)entries[0]).Equals(MutableString.CreateAscii("."))); Assert(((MutableString)entries[1]).Equals(MutableString.CreateAscii(".."))); Assert(((MutableString)entries[2]).Equals(MutableString.Create("ホ", RubyEncoding.KCodeSJIS))); }
public static MutableString /*!*/ TagUri(RubyStruct /*!*/ self) { MutableString str = MutableString.CreateMutable("tag:ruby.yaml.org,2002:struct:", self.ImmediateClass.Context.GetIdentifierEncoding()); string name = self.ImmediateClass.GetNonSingletonClass().Name; if (name != null) { string structPrefix = "Struct::"; if (name.StartsWith(structPrefix, StringComparison.Ordinal)) { name = name.Substring(structPrefix.Length); } } return(str.Append(name)); }
public static MutableString PostMatch(StringScanner /*!*/ self) { if (self.LastMatch == null) { return(null); } int position = self.FoundPosition + self.LastMatch.Length; int len = self.Length - position; if (len <= 0) { return(MutableString.CreateMutable()); } return(self.ScanString.GetSlice(position, len)); }
public MutableString Load(string fileName) { if (Path.GetExtension(fileName) == "") { fileName += ".dat"; } fileName = "Saves/" + fileName; var sr = new StreamReader(fileName); var returnObject = MutableString.CreateMutable(sr.ReadToEnd(), RubyEncoding.UTF8); sr.Close(); return(returnObject); }
/// <summary> /// Returns MutableString or RubySymbol. /// </summary> private static object ConstructRubyString(RubyConstructor /*!*/ ctor, Node /*!*/ node) { ScalarNode scalar = (ScalarNode)node; string value = ctor.ConstructScalar(node); if (value == null) { return(null); } if (value.Length > 1 && value[0] == ':' && scalar.Style == ScalarQuotingStyle.None) { return(ctor.GlobalScope.Context.CreateAsciiSymbol(value.Substring(1))); } return(MutableString.CreateMutable(value, ctor.RubyEncoding)); }
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 object AddDomainType(RubyContext /*!*/ context, BlockParam /*!*/ block, RubyModule /*!*/ self, MutableString /*!*/ domainAndDate, RubyRegex /*!*/ typeRegex) { if (block == null) { throw RubyExceptions.NoBlockGiven(); } MutableString tag = MutableString.CreateMutable(typeRegex.Encoding). Append("tag:"). Append(domainAndDate). Append(':'). Append(typeRegex.Pattern); RubyConstructor.AddExternalMultiConstructor(tag.ConvertToString(), block); return(null); }
public static MutableString Peek(StringScanner /*!*/ self, int len) { if (len < 0) { throw RubyExceptions.CreateArgumentError("negative string size (or size too big)"); } int maxlen = self.Length - self.CurrentPosition; if (len > maxlen) { len = maxlen; } if (self.CurrentPosition >= self.Length || len == 0) { return(MutableString.CreateMutable()); } return(self.ScanString.GetSlice(self.CurrentPosition, len)); }
private static RubyRegex /*!*/ ConstructRubyRegexp(RubyConstructor /*!*/ ctor, Node /*!*/ node) { ScalarNode scalar = node as ScalarNode; if (node == null) { throw RubyExceptions.CreateTypeError("Can only create regex from scalar node"); } Match match = _regexPattern.Match(scalar.Value); if (!match.Success) { throw new ConstructorException("Invalid Regular expression: \"" + scalar.Value + "\""); } RubyRegexOptions options = new RubyRegexOptions(); foreach (char c in match.Groups["opts"].Value) { switch (c) { case 'i': options |= RubyRegexOptions.IgnoreCase; break; case 'x': options |= RubyRegexOptions.Extended; break; case 'm': options |= RubyRegexOptions.Multiline; break; case 'o': break; case 'n': options |= RubyRegexOptions.FIXED; break; case 'e': options |= RubyRegexOptions.EUC; break; case 's': options |= RubyRegexOptions.SJIS; break; case 'u': options |= RubyRegexOptions.UTF8; break; default: throw new ConstructorException("Unknown regular expression option: '" + c + "'"); } } // TODO: encoding (ignore kcode on 1.9, string enc?): return(new RubyRegex(MutableString.CreateMutable(match.Groups["expr"].Value, RubyEncoding.UTF8), options)); }
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 /*!*/ ObjectToMutableString(RubyContext /*!*/ context, object obj) { using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(obj)) { if (handle == null) { return(MutableString.Create("...")); } MutableString str = MutableString.CreateMutable(); str.Append("#<"); str.Append(context.GetClassOf(obj).Name); // Ruby prints 2*object_id for objects str.Append(':'); AppendFormatHexObjectId(str, GetObjectId(context, obj)); // display instance variables RubyInstanceData data = context.TryGetInstanceData(obj); if (data != null) { var vars = data.GetInstanceVariablePairs(); bool first = true; foreach (KeyValuePair <string, object> var in vars) { if (first) { str.Append(" "); first = false; } else { str.Append(", "); } str.Append(var.Key); str.Append("="); str.Append(RubySites.Inspect(context, var.Value)); } } str.Append(">"); return(str); } }
/// <summary> /// If the SCRIPT_LINES__ constant is set, we need to publish the file being loaded, /// along with the contents of the file /// </summary> private void AddScriptLines(SourceUnit file) { ConstantStorage storage; if (!_context.ObjectClass.TryResolveConstant(null, "SCRIPT_LINES__", out storage)) { return; } IDictionary scriptLines = storage.Value as IDictionary; if (scriptLines == null) { return; } lock (scriptLines) { // Read in the contents of the file RubyArray lines = new RubyArray(); SourceCodeReader reader = file.GetReader(); RubyEncoding encoding = RubyEncoding.GetRubyEncoding(reader.Encoding); using (reader) { reader.SeekLine(1); while (true) { string lineStr = reader.ReadLine(); if (lineStr == null) { break; } MutableString line = MutableString.CreateMutable(lineStr.Length + 1, encoding); line.Append(lineStr).Append('\n'); lines.Add(line); } } // Publish the contents of the file, keyed by the file name MutableString path = MutableString.Create(file.Document.FileName, _context.GetPathEncoding()); scriptLines[path] = lines; } }
public static object QuickEmit(YamlCallSiteStorage /*!*/ siteStorage, [NotNull] BlockParam /*!*/ block, RubyModule /*!*/ self, object objectId, [NotNull] Hash /*!*/ opts) { // TODO: load from opts YamlOptions cfg = YamlOptions.DefaultOptions; MutableStringWriter writer = new MutableStringWriter(MutableString.CreateMutable(RubyEncoding.Binary)); Serializer s = new Serializer(writer, cfg); RubyRepresenter rep = new RubyRepresenter(siteStorage); object result; if (block.Yield(new Syck.Out(rep), out result)) { return(result); } s.Serialize(rep.ToNode(result)); s.Close(); return(writer.String); }
public override void Add(SourceUnit sourceUnit, string message, SourceSpan span, int errorCode, Severity severity) { if (severity == Severity.Warning && !ReportWarning(_context.Verbose, errorCode)) { return; } CountError(severity); string path; string codeLine; int line = span.Start.Line; if (sourceUnit != null) { path = sourceUnit.Path; codeLine = (line > 0) ? sourceUnit.GetCodeLine(line) : null; } else { path = null; codeLine = null; } if (severity == Severity.Error || severity == Severity.FatalError) { throw new SyntaxError(message, path, line, span.Start.Column, codeLine); } else { if (_WriteSite == null) { Interlocked.CompareExchange( ref _WriteSite, CallSite <Func <CallSite, RubyContext, object, object, object> > .Create(RubyCallAction.Make("write", 1)), null ); } message = RubyContext.FormatErrorMessage(message, "warning", path, line, span.Start.Column, null); _WriteSite.Target(_WriteSite, _context, _context.StandardErrorOutput, MutableString.CreateMutable(message)); } }
public static object Tagurize(ConversionStorage <MutableString> /*!*/ stringTryCast, RubyModule /*!*/ self, object arg) { var str = Protocols.TryCastToString(stringTryCast, arg); return((str != null) ? MutableString.CreateMutable(str.Encoding).Append("tag:yaml.org,2002:").Append(str) : arg); }
private static MutableString Transform(RubyContext context, IList self, GeneratorState state, int depth) { MutableString result = MutableString.CreateMutable(2 + Math.Max(self.Count * 4, 0), RubyEncoding.UTF8); byte[] indentUnit = state.Indent.ToByteArray(); byte[] shift = Helpers.Repeat(indentUnit, depth + 1); byte[] arrayNl = state.ArrayNl.ToByteArray(); byte[] delim = new byte[1 + arrayNl.Length]; delim[0] = (byte)','; Array.Copy(arrayNl, 0, delim, 1, arrayNl.Length); state.CheckMaxNesting(depth + 1); context.TaintObjectBy <Object>(result, self); if (state.CheckCircular) { state.Remember(context, self); result.Append('['); result.Append(arrayNl); if (self.Count > 0) { for (int i = 0; i < self.Count; i++) { Object element = self[i]; if (state.Seen(context, element)) { Helpers.ThrowCircularDataStructureException("circular data structures not supported!"); } context.TaintObjectBy <Object>(result, element); if (i > 0) { result.Append(delim); } result.Append(shift); result.Append(Generator.ToJson(context, element, state, depth + 1)); } // TODO: this might be needed outside of the count check for compat if (arrayNl.Length != 0) { result.Append(arrayNl); result.Append(shift, 0, depth * indentUnit.Length); } } result.Append(']'); state.Forget(context, self); } else { result.Append('['); result.Append(arrayNl); if (self.Count > 0) { for (int i = 0; i < self.Count; i++) { Object element = self[i]; context.TaintObjectBy <Object>(result, element); if (i > 0) { result.Append(delim); } result.Append(shift); result.Append(Generator.ToJson(context, element, state, depth + 1)); } // TODO: this might be needed outside of the count check for compatibility if (arrayNl.Length != 0) { result.Append(arrayNl); result.Append(shift, 0, depth * indentUnit.Length); } } result.Append(']'); } return(result); }
public static MutableString CreateMutableStringFromBuffer(char[] buffer, int raw, int rawlen) { return(MutableString.CreateMutable(rawlen, RubyEncoding.Binary).Append(buffer, raw, rawlen)); }