public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Hash /*!*/ self) { using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { if (handle == null) { return(MutableString.Create("{...}")); } MutableString str = MutableString.Create("{"); bool first = true; foreach (KeyValuePair <object, object> pair in self) { if (first) { first = false; } else { str.Append(", "); } str.Append(RubySites.Inspect(context, BaseSymbolDictionary.ObjToNull(pair.Key))); str.Append("=>"); str.Append(RubySites.Inspect(context, pair.Value)); } str.Append('}'); return(str); } }
private static void AppendDirectoryName(MutableString /*!*/ result, MutableString /*!*/ name) { int resultLength = result.GetCharCount(); int i; for (i = resultLength - 1; i >= 0; i--) { if (!IsDirectorySeparator(result.GetChar(i))) { break; } } if (i == resultLength - 1) { if (!IsDirectorySeparator(name.GetFirstChar())) { result.Append(DirectorySeparatorChar); } result.Append(name); } else if (IsDirectorySeparator(name.GetFirstChar())) { result.Replace(i + 1, resultLength - i - 1, name); } else { result.Append(name); } }
private void AppendRawBytes(MutableString /*!*/ buffer, int count) { Debug.Assert(count > 0); int remaining = count; if (_bufferCount > 0) { int c = Math.Min(_bufferCount, count); buffer.Append(_buffer, _bufferStart, c); ConsumeBuffered(c); remaining -= c; } if (count == Int32.MaxValue) { const int chunk = 1024; int done = buffer.GetByteCount(); int bytesRead; do { buffer.Append(_stream, chunk); bytesRead = buffer.GetByteCount() - done; done += bytesRead; } while (bytesRead == chunk); } else { buffer.Append(_stream, remaining); } }
private int AppendOptionString(MutableString /*!*/ result, bool enabled) { int count = 0; var options = Options; if (((options & RubyRegexOptions.Multiline) != 0) == enabled) { result.Append('m'); count++; } if (((options & RubyRegexOptions.IgnoreCase) != 0) == enabled) { result.Append('i'); count++; } if (((options & RubyRegexOptions.Extended) != 0) == enabled) { result.Append('x'); count++; } return(count); }
public MutableString /*!*/ ToMutableString(ConversionStorage <MutableString> /*!*/ tosConversion) { MutableString str = Protocols.ConvertToString(tosConversion, _begin); str.Append(Separator); str.Append(Protocols.ConvertToString(tosConversion, _end)); return(str); }
public static MutableString /*!*/ ToS(RubyContext /*!*/ context, Range /*!*/ self) { MutableString str = RubySites.ToS(context, self.Begin); str.Append(self.ExcludeEnd ? "..." : ".."); str.Append(RubySites.ToS(context, self.End)); return(str); }
public MutableString /*!*/ Inspect() { MutableString result = MutableString.CreateMutable(RubyEncoding.Binary); result.Append('/'); AppendEscapeForwardSlash(result, _pattern); result.Append('/'); AppendOptionString(result, true, true); return(result); }
public static MutableString /*!*/ Inspect(RubyRegex /*!*/ self) { MutableString result = MutableString.CreateMutable(); result.Append('/'); AppendEscapeForwardSlash(result, self.GetPattern()); result.Append('/'); AppendOptionString(result, self.Options, true, true); return(result); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyEncoding /*!*/ self) { // TODO: to_s overridden MutableString result = MutableString.CreateMutable(context.GetIdentifierEncoding()); result.Append("#<"); result.Append(context.GetClassDisplayName(self)); result.Append(':'); result.Append(self.Name); result.Append('>'); return(result); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyEncoding /*!*/ self) { // TODO: to_s overridden MutableString result = MutableString.CreateMutable(); result.Append("#<"); result.Append(RubyUtils.GetClassName(context, self)); result.Append(':'); result.Append(self.Name); result.Append(">"); return(result); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Thread /*!*/ self) { RubyThreadInfo.RegisterThread(Thread.CurrentThread); MutableString result = MutableString.CreateMutable(); result.Append("#<"); result.Append(RubyUtils.GetClassName(context, self)); result.Append(':'); RubyUtils.AppendFormatHexObjectId(result, RubyUtils.GetObjectId(context, self)); result.Append(' '); if ((self.ThreadState & ThreadState.WaitSleepJoin) != 0) { result.Append("sleep"); } else if ((self.ThreadState & (ThreadState.Stopped | ThreadState.Aborted | ThreadState.AbortRequested)) != 0) { result.Append("dead"); } else { result.Append("run"); } result.Append('>'); return(result); }
public static MutableString /*!*/ Inspect(RubyStruct /*!*/ self) { RubyContext context = self.Class.Context; using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { // #<struct Struct::Foo name=nil, val=nil> MutableString str = MutableString.Create("#<struct "); str.Append(RubySites.Inspect(context, context.GetClassOf(self))); if (handle == null) { return(str.Append(":...>")); } str.Append(' '); object[] data = self.Values; var members = self.GetNames(); for (int i = 0; i < data.Length; i++) { if (i != 0) { str.Append(", "); } str.Append(members[i]); str.Append("="); str.Append(RubySites.Inspect(context, data[i])); } str.Append('>'); return(str); } }
public static MutableString Join(RubyClass /*!*/ self, [NotNull] params object[] strings) { MutableString result = MutableString.CreateMutable(); for (int i = 0; i < strings.Length; ++i) { result.Append(Protocols.ConvertToString(self.Context, strings[i])); if (i < strings.Length - 1) { result.Append(SEPARATOR); } } return(result); }
private static MutableString /*!*/ Append(RubyRegex /*!*/ self, MutableString /*!*/ result) { Assert.NotNull(self, result); result.Append("(?"); if (AppendOptionString(result, self.Options, true, false) < 3) { result.Append('-'); } AppendOptionString(result, self.Options, false, false); result.Append(':'); AppendEscapeForwardSlash(result, self.GetPattern()); result.Append(')'); return(result); }
public MutableString /*!*/ AppendTo(MutableString /*!*/ result) { Assert.NotNull(result); result.Append("(?"); if (AppendOptionString(result, true, false) < 3) { result.Append('-'); } AppendOptionString(result, false, false); result.Append(':'); AppendEscapeForwardSlash(result, _pattern); result.Append(')'); return(result); }
private static MutableString /*!*/ JoinArguments(MutableString /*!*/[] /*!*/ args) { MutableString result = MutableString.CreateMutable(RubyEncoding.Binary); for (int i = 0; i < args.Length; i++) { result.Append(args[i]); if (args.Length > 1 && i < args.Length - 1) { result.Append(' '); } } return(result); }
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); }
public static MutableString Join(ConversionStorage <MutableString> /*!*/ stringCast, RubyClass /*!*/ self, params object[] /*!*/ parts) { MutableString result = MutableString.CreateMutable(RubyEncoding.Binary); Dictionary <object, bool> visitedLists = null; var worklist = new Stack <object>(); int current = 0; MutableString str; Push(worklist, parts); while (worklist.Count > 0) { object part = worklist.Pop(); var list = part as IList; if (list != null) { if (list.Count == 0) { str = MutableString.FrozenEmpty; } else if (visitedLists != null && visitedLists.ContainsKey(list)) { str = RubyUtils.InfiniteRecursionMarker; } else { if (visitedLists == null) { visitedLists = new Dictionary <object, bool>(ReferenceEqualityComparer.Instance); } visitedLists.Add(list, true); Push(worklist, list); continue; } } else if (part == null) { throw RubyExceptions.CreateTypeConversionError("NilClass", "String"); } else { str = Protocols.CastToString(stringCast, part); } if (current > 0) { AppendDirectoryName(result, str); } else { result.Append(str); } current++; } return(result); }
internal static MutableString /*!*/ AppendEscapeForwardSlash(MutableString /*!*/ result, MutableString /*!*/ pattern) { int first = 0; int i = SkipToUnescapedForwardSlash(pattern, 0); while (i >= 0) { Debug.Assert(i < pattern.Length); Debug.Assert(pattern.GetChar(i) == '/' && (i == 0 || pattern.GetChar(i - 1) != '\\')); result.Append(pattern, first, i - first); result.Append('\\'); first = i; // include forward slash in the next append i = SkipToUnescapedForwardSlash(pattern, i + 1); } result.Append(pattern, first, pattern.Length - first); return(result); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Thread /*!*/ self) { RubyThreadInfo.RegisterThread(Thread.CurrentThread); MutableString result = MutableString.CreateMutable(context.GetIdentifierEncoding()); result.Append("#<"); result.Append(context.GetClassDisplayName(self)); result.Append(':'); RubyUtils.AppendFormatHexObjectId(result, RubyUtils.GetObjectId(context, self)); result.Append(' '); RubyThreadStatus status = GetStatus(self); switch (status) { case RubyThreadStatus.Unstarted: result.Append("unstarted"); break; case RubyThreadStatus.Running: result.Append("run"); break; case RubyThreadStatus.Sleeping: result.Append("sleep"); break; case RubyThreadStatus.Aborting: result.Append("aborting"); break; case RubyThreadStatus.Completed: case RubyThreadStatus.Aborted: result.Append("dead"); break; } result.Append('>'); return(result); }
public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Hash /*!*/ self) { using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { if (handle == null) { return(MutableString.CreateAscii("{...}")); } MutableString str = MutableString.CreateMutable(RubyEncoding.Binary); str.Append('{'); bool first = true; foreach (var entry in self) { if (first) { first = false; } else { str.Append(", "); } str.Append(context.Inspect(BaseSymbolDictionary.ObjToNull(entry.Key))); str.Append("=>"); str.Append(context.Inspect(entry.Value)); } str.Append('}'); return(str); } }
public override void SetLength(long value) { int byteCount = _string.GetByteCount(); if (byteCount < value) { _string.Append(0, (int)(value - byteCount)); } else if (byteCount > value) { _string.Remove((int)value, byteCount - (int)value); } }
public static MutableString /*!*/ Read(RubyIO /*!*/ self, [DefaultProtocol] int bytes, [DefaultProtocol, Optional] MutableString buffer) { self.AssertOpenedForReading(); if (self.IsEndOfStream()) { return(null); } if (buffer == null) { buffer = MutableString.CreateBinary(); } buffer.Clear(); if (!self.PreserveEndOfLines) { for (int i = 0; i < bytes; ++i) { int c = self.ReadByteNormalizeEoln(); if (c == -1) { return(buffer); } else { buffer.Append((byte)c); } } } else { var fixedBuffer = new byte[bytes]; bytes = self.ReadBytes(fixedBuffer, 0, bytes); buffer.Append(fixedBuffer, 0, bytes); } return(buffer); }
private static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, ICollection /*!*/ objs) { if (objs.Count == 0) { return(new RubyRegex(MutableString.CreateAscii("(?!)"), RubyRegexOptions.NONE)); } MutableString result = MutableString.CreateMutable(RubyEncoding.Binary); int i = 0; foreach (var obj in objs) { if (i > 0) { result.Append('|'); } // TODO: to_regexp RubyRegex regex = obj as RubyRegex; if (regex != null) { if (objs.Count == 1) { return(regex); } regex.AppendTo(result); } else { result.Append(RubyRegex.Escape(Protocols.CastToString(stringCast, obj))); } i++; } return(new RubyRegex(result, RubyRegexOptions.NONE)); }
public static void Print(RubyContext /*!*/ context, object self, [NotNull] params object[] /*!*/ args) { MutableString delimiter = context.OutputSeparator; for (int i = 0; i < args.Length; i++) { MutableString str = ToPrintedString(context, args[i]); if (delimiter != null) { str.Append(delimiter); } Print(context, self, str); } }
public static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, RubyClass /*!*/ self, params object[] /*!*/ strings) { if (strings.Length == 0) { return(new RubyRegex(MutableString.CreateAscii("(?!)"), RubyRegexOptions.NONE)); } MutableString result = MutableString.CreateMutable(RubyEncoding.Binary); for (int i = 0; i < strings.Length; i++) { if (i > 0) { result.Append('|'); } RubyRegex regex = strings[i] as RubyRegex; if (regex != null) { if (strings.Length == 1) { return(regex); } regex.AppendTo(result); } else { result.Append(RubyRegex.Escape(Protocols.CastToString(stringCast, strings[i]))); } } // TODO: //RubyClass regexClass = RubyUtils.GetExecutionContext(context).GetClass(typeof(RubyRegex)); //return NewCallSite3.Invoke(context, regexClass, result, null, null); return(new RubyRegex(result, RubyRegexOptions.NONE)); }
public static MutableString /*!*/ SystemRead(RubyIO /*!*/ self, [DefaultProtocol] int bytes) { var fixedBuffer = new byte[bytes]; int len = self.ReadBytes(fixedBuffer, 0, bytes); if (len == 0) { throw new EOFError("end of file reached"); } MutableString result = MutableString.CreateBinary(); result.Append(fixedBuffer, 0, len); return(result); }
public static MutableString /*!*/ Read(RubyIO /*!*/ self) { self.AssertOpenedForReading(); if (!self.PreserveEndOfLines) { MutableString result = MutableString.CreateBinary(); int c; while ((c = self.ReadByteNormalizeEoln()) != -1) { result.Append((byte)c); } return(result); } else { // TODO: change this once Binary mutable string uses resizable byte[] instead of List<byte> return(MutableString.CreateBinary(ReadAllBytes(self))); } }
private int AppendOptionString(MutableString /*!*/ result, bool enabled, bool includeEncoding) { int count = 0; var options = Options; if (((options & RubyRegexOptions.Multiline) != 0) == enabled) { result.Append('m'); count++; } if (((options & RubyRegexOptions.IgnoreCase) != 0) == enabled) { result.Append('i'); count++; } if (((options & RubyRegexOptions.Extended) != 0) == enabled) { result.Append('x'); count++; } if (includeEncoding) { switch (options & RubyRegexOptions.EncodingMask) { case RubyRegexOptions.NONE: break; case RubyRegexOptions.EUC: result.Append('e'); break; case RubyRegexOptions.FIXED: result.Append('n'); break; case RubyRegexOptions.UTF8: result.Append('u'); break; case RubyRegexOptions.SJIS: result.Append('s'); break; default: throw Assert.Unreachable; } } return(count); }
public MutableString ReadLine(MutableString separator) { AssertOpenedForReading(); int c = ReadByteNormalizeEoln(); if (c == -1) { return(null); } int separatorOffset = 0; MutableString result = MutableString.CreateMutable(); do { result.Append((char)c); if (separator != null && c == separator.GetChar(separatorOffset)) { if (separatorOffset == separator.Length - 1) { break; } separatorOffset++; } else if (separatorOffset > 0) { separatorOffset = 0; } c = ReadByteNormalizeEoln(); } while (c != -1); return(result); }