Example #1
0
        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);
        }
Example #2
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, TypeGroup /*!*/ self)
        {
            var result = MutableString.CreateMutable(context.GetIdentifierEncoding());

            result.Append("#<TypeGroup: ");

            bool isFirst = true;

            foreach (var entry in self.TypesByArity.ToSortedList((x, y) => x.Key - y.Key))
            {
                Type type = entry.Value;

                if (!isFirst)
                {
                    result.Append(", ");
                }
                else
                {
                    isFirst = false;
                }

                result.Append(context.GetTypeName(type, true));
            }
            result.Append('>');

            return(result);
        }
Example #3
0
        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);
            }
        }
Example #4
0
        public static MutableString /*!*/ Inspect(RubyStruct /*!*/ self)
        {
            RubyContext context = self.ImmediateClass.Context;

            using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
                // #<struct Struct::Foo name=nil, val=nil>
                var result = MutableString.CreateMutable(RubyEncoding.Binary);
                result.Append("#<struct ");
                result.Append(context.Inspect(context.GetClassOf(self)));

                if (handle == null)
                {
                    return(result.Append(":...>"));
                }
                result.Append(' ');

                object[] data    = self.Values;
                var      members = self.GetNames();
                for (int i = 0; i < data.Length; i++)
                {
                    if (i != 0)
                    {
                        result.Append(", ");
                    }
                    // TODO (encoding):
                    result.Append(members[i]);
                    result.Append('=');
                    result.Append(context.Inspect(data[i]));
                }
                result.Append('>');
                return(result);
            }
        }
Example #5
0
        internal static MutableString /*!*/ ToS(string /*!*/ methodName, RubyModule /*!*/ declaringModule, RubyModule /*!*/ targetModule,
                                                string /*!*/ classDisplayName)
        {
            MutableString result = MutableString.CreateMutable();

            result.Append("#<");
            result.Append(classDisplayName);
            result.Append(": ");

            if (ReferenceEquals(targetModule, declaringModule))
            {
                result.Append(declaringModule.Name);
            }
            else
            {
                result.Append(targetModule.Name);
                result.Append('(');
                result.Append(declaringModule.Name);
                result.Append(')');
            }

            result.Append('#');
            result.Append(methodName);
            result.Append('>');
            return(result);
        }
Example #6
0
        internal static MutableString /*!*/ ToS(RubyContext /*!*/ context, string /*!*/ methodName, RubyModule /*!*/ declaringModule, RubyModule /*!*/ targetModule,
                                                string /*!*/ classDisplayName)
        {
            MutableString result = MutableString.CreateMutable(context.GetIdentifierEncoding());

            result.Append("#<");
            result.Append(classDisplayName);
            result.Append(": ");

            if (ReferenceEquals(targetModule, declaringModule))
            {
                result.Append(declaringModule.GetDisplayName(context, true));
            }
            else
            {
                result.Append(targetModule.GetDisplayName(context, true));
                result.Append('(');
                result.Append(declaringModule.GetDisplayName(context, true));
                result.Append(')');
            }

            result.Append('#');
            result.Append(methodName);
            result.Append('>');
            return(result);
        }
Example #7
0
 public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(MutableString.CreateMutable(context.GetPathEncoding()).
            Append("#<File:").
            Append(self.Path).
            Append(self.Closed ? " (closed)" : "").
            Append('>'));
 }
Example #8
0
        public static MutableString /*!*/ ToS(RubyRegex /*!*/ self)
        {
            // Ruby: doesn't wrap if there is a single embedded expression that evaluates to non-nil:
            // puts(/#{nil}#{/a/}#{nil}/)
            // We don't do that.

            return(Append(self, MutableString.CreateMutable()));
        }
Example #9
0
        public MutableString /*!*/ Inspect(RubyContext /*!*/ context)
        {
            var result = MutableString.CreateMutable(RubyEncoding.Binary);

            result.Append(context.Inspect(_begin));
            result.Append(Separator);
            result.Append(context.Inspect(_end));
            return(result);
        }
Example #10
0
        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);
        }
Example #11
0
        internal static MutableString /*!*/ ToChr(RubyEncoding /*!*/ encoding, RubyEncoding /*!*/ resultEncoding, int codepoint)
        {
            if (codepoint < 0)
            {
                throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
            }

            switch (encoding.CodePage)
            {
            case RubyEncoding.CodePageUTF7:
            case RubyEncoding.CodePageUTF8:
            case RubyEncoding.CodePageUTF16BE:
            case RubyEncoding.CodePageUTF16LE:
            case RubyEncoding.CodePageUTF32BE:
            case RubyEncoding.CodePageUTF32LE:
                if (codepoint > 0x10ffff)
                {
                    throw RubyExceptions.CreateRangeError("{0} is not a valid Unicode code point (0..0x10ffff)", codepoint);
                }
                return(MutableString.CreateMutable(Tokenizer.UnicodeCodePointToString(codepoint), resultEncoding));

            case RubyEncoding.CodePageSJIS:
                if (codepoint >= 0x81 && codepoint <= 0x9f || codepoint >= 0xe0 && codepoint <= 0xfc)
                {
                    throw RubyExceptions.CreateArgumentError("invalid codepoint 0x{0:x2} in Shift_JIS", codepoint);
                }
                goto default;

            case RubyEncoding.CodePageEUCJP:
                // MRI's bahavior is strange - bug?
                if (codepoint >= 0x80)
                {
                    throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
                }
                goto default;

            default:
                if (codepoint <= 0xff)
                {
                    return(MutableString.CreateBinary(new[] { (byte)codepoint }, resultEncoding));
                }
                if (encoding.IsDoubleByteCharacterSet)
                {
                    if (codepoint <= 0xffff)
                    {
                        return(MutableString.CreateBinary(new[] { (byte)(codepoint >> 8), (byte)(codepoint & 0xff) }, resultEncoding));
                    }
                    throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
                }
                if (encoding.IsSingleByteCharacterSet)
                {
                    throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
                }
                throw new NotSupportedException(RubyExceptions.FormatMessage("Encoding {0} code points not supported", encoding));
            }
        }
Example #12
0
        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);
        }
Example #13
0
        public MutableString /*!*/ Inspect()
        {
            MutableString result = MutableString.CreateMutable(RubyEncoding.Binary);

            result.Append('/');
            AppendEscapeForwardSlash(result, _pattern);
            result.Append('/');
            AppendOptionString(result, true, true);
            return(result);
        }
Example #14
0
        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);
        }
Example #15
0
        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);
        }
Example #16
0
        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);
        }
Example #17
0
        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);
        }
Example #18
0
        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);
        }
Example #19
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, Exception /*!*/ self)
        {
            object message   = RubyExceptionData.GetInstance(self).Message;
            string className = RubyUtils.GetClassName(context, self);

            MutableString result = MutableString.CreateMutable();

            result.Append("#<");
            result.Append(className);
            result.Append(": ");
            if (message != null)
            {
                result.Append(KernelOps.Inspect(context, message));
            }
            else
            {
                result.Append(className);
            }
            result.Append('>');
            return(result);
        }
Example #20
0
        public static MutableString /*!*/ Inspect(UnaryOpStorage /*!*/ inspectStorage, ConversionStorage <MutableString> /*!*/ tosConversion, Exception /*!*/ self)
        {
            object message   = RubyExceptionData.GetInstance(self).Message;
            string className = inspectStorage.Context.GetClassDisplayName(self);

            MutableString result = MutableString.CreateMutable(inspectStorage.Context.GetIdentifierEncoding());

            result.Append("#<");
            result.Append(className);
            result.Append(": ");
            if (message != null)
            {
                result.Append(KernelOps.Inspect(inspectStorage, tosConversion, message));
            }
            else
            {
                result.Append(className);
            }
            result.Append('>');
            return(result);
        }
Example #21
0
        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));
        }
Example #22
0
 public static MutableString Inspect(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self)
 {
     using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) {
         if (handle == null)
         {
             return(MutableString.CreateAscii("{...}"));
         }
         MutableString str = MutableString.CreateMutable(RubyEncoding.Binary);
         str.Append('{');
         foreach (KeyValuePair <object, object> pair in self)
         {
             if (str.Length != 1)
             {
                 str.Append(", ");
             }
             str.Append(context.Inspect(CustomStringDictionary.ObjToNull(pair.Key)));
             str.Append("=>");
             str.Append(context.Inspect(pair.Value));
         }
         str.Append('}');
         return(str);
     }
 }
Example #23
0
        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));
        }
Example #24
0
        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);
        }
Example #25
0
 public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(MutableString.CreateMutable("#<File:").Append(self.Path).Append('>'));
 }
Example #26
0
        public static MutableString /*!*/ Basename(RubyClass /*!*/ self,
                                                   [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol, NotNull, Optional] MutableString extensionFilter)
        {
            if (path.IsEmpty)
            {
                return(path);
            }

            MutableString trimmedPath = TrimTrailingSlashes(path);

            // Special cases of drive letters C:\\ or C:/
            if (trimmedPath.Length == 2)
            {
                if (Char.IsLetter(trimmedPath.GetChar(0)) && trimmedPath.GetChar(1) == ':')
                {
                    var result = (path.Length > 2 ? MutableString.Create(path.GetChar(2).ToString()) : MutableString.CreateMutable());
                    return(result.TaintBy(path));
                }
            }

            string trimmedPathAsString = trimmedPath.ConvertToString();

            if (trimmedPathAsString == "/")
            {
                return(trimmedPath);
            }

            string filename = Path.GetFileName(trimmedPath.ConvertToString());

            // Handle UNC host names correctly
            string root = Path.GetPathRoot(trimmedPath.ConvertToString());

            if (MutableString.IsNullOrEmpty(extensionFilter))
            {
                return(MutableString.Create(trimmedPathAsString == root ? root : filename));
            }

            string fileExtension = Path.GetExtension(filename);
            string basename      = Path.GetFileNameWithoutExtension(filename);

            string strResult = WildcardExtensionMatch(fileExtension, extensionFilter.ConvertToString()) ? basename : filename;

            return(Glob.CanonicalizePath(MutableString.Create(strResult)).TaintBy(path));
        }
Example #27
0
        public static MutableString /*!*/ Basename(RubyClass /*!*/ self,
                                                   [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol, NotNull, Optional] MutableString suffix)
        {
            if (path.IsEmpty)
            {
                return(path);
            }

            string strPath = path.ConvertToString();

            string[] parts = strPath.Split(new[] { DirectorySeparatorChar, AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 0)
            {
                return(MutableString.CreateMutable(path.Encoding).Append((char)path.GetLastChar()).TaintBy(path));
            }

            if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX)
            {
                string first = parts[0];
                if (strPath.Length >= 2 && IsDirectorySeparator(strPath[0]) && IsDirectorySeparator(strPath[1]))
                {
                    // UNC: skip 2 parts
                    if (parts.Length <= 2)
                    {
                        return(MutableString.CreateMutable(path.Encoding).Append(DirectorySeparatorChar).TaintBy(path));
                    }
                }
                else if (first.Length == 2 && Tokenizer.IsLetter(first[0]) && first[1] == ':')
                {
                    // skip drive letter "X:"
                    if (parts.Length <= 1)
                    {
                        var result = MutableString.CreateMutable(path.Encoding).TaintBy(path);
                        if (strPath.Length > 2)
                        {
                            result.Append(strPath[2]);
                        }
                        return(result);
                    }
                }
            }

            string last = parts[parts.Length - 1];

            if (MutableString.IsNullOrEmpty(suffix))
            {
                return(MutableString.CreateMutable(last, path.Encoding));
            }

            StringComparison comparison = Environment.OSVersion.Platform == PlatformID.Unix ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
            int matchLength             = last.Length;

            if (suffix != null)
            {
                string strSuffix = suffix.ToString();
                if (strSuffix.LastCharacter() == '*' && strSuffix.Length > 1)
                {
                    int suffixIdx = last.LastIndexOf(
                        strSuffix.Substring(0, strSuffix.Length - 1),
                        comparison
                        );
                    if (suffixIdx >= 0 && suffixIdx + strSuffix.Length <= last.Length)
                    {
                        matchLength = suffixIdx;
                    }
                }
                else if (last.EndsWith(strSuffix, comparison))
                {
                    matchLength = last.Length - strSuffix.Length;
                }
            }

            return(MutableString.CreateMutable(path.Encoding).Append(last, 0, matchLength).TaintBy(path));
        }
Example #28
0
 public MutableString /*!*/ ToMutableString()
 {
     return(AppendTo(MutableString.CreateMutable(RubyEncoding.Binary)));
 }
Example #29
0
 public static MutableString /*!*/ Inspect(object /*!*/ self)
 {
     return(MutableString.CreateMutable(RubyEncoding.Binary).Append(self.ToString()).Append(" (Decimal)"));
 }
Example #30
0
        public static MutableString /*!*/ FormatTime(RubyContext /*!*/ context, RubyTime /*!*/ self,
                                                     [DefaultProtocol, NotNull] MutableString /*!*/ format)
        {
            MutableString result   = MutableString.CreateMutable(format.Encoding);
            bool          inFormat = false;

            var charEnum = format.GetCharacters();

            while (charEnum.MoveNext())
            {
                var character = charEnum.Current;
                int c         = character.IsValid ? character.Value : -1;

                if (!inFormat)
                {
                    if (c == '%')
                    {
                        inFormat = true;
                    }
                    else
                    {
                        result.Append(character);
                    }
                    continue;
                }
                inFormat = false;
                string dateTimeFormat = null;
                switch (c)
                {
                case '%':
                    result.Append('%');
                    break;

                case 'a':
                    dateTimeFormat = "ddd";
                    break;

                case 'A':
                    dateTimeFormat = "dddd";
                    break;

                case 'b':
                    dateTimeFormat = "MMM";
                    break;

                case 'B':
                    dateTimeFormat = "MMMM";
                    break;

                case 'c':
                    dateTimeFormat = "g";
                    break;

                case 'd':
                    dateTimeFormat = "dd";
                    break;

                case 'D':
                    dateTimeFormat = "MM/dd/yy";
                    break;

                case 'e': {     // Day of the month, blank-padded ( 1..31)
                    int day = self.DateTime.Day;
                    if (day < 10)
                    {
                        result.Append(' ');
                    }
                    result.Append(day.ToString(CultureInfo.InvariantCulture));
                    break;
                }

                case 'H':
                    dateTimeFormat = "HH";
                    break;

                case 'I':
                    dateTimeFormat = "hh";
                    break;

                case 'j':
                    result.AppendFormat("{0:000}", self.DateTime.DayOfYear);
                    break;

                case 'l': {
                    int hour = self.DateTime.Hour;
                    if (hour == 0)
                    {
                        hour = 12;
                    }
                    else if (hour > 12)
                    {
                        hour -= 12;
                    }
                    if (hour < 10)
                    {
                        result.Append(' ');
                    }
                    result.Append(hour.ToString(CultureInfo.InvariantCulture));
                    break;
                }

                case 'm':
                    dateTimeFormat = "MM";
                    break;

                case 'M':
                    dateTimeFormat = "mm";
                    break;

                case 'p':
                    dateTimeFormat = "tt";
                    break;

                case 'S':
                    dateTimeFormat = "ss";
                    break;

                case 'T':
                    dateTimeFormat = "HH:mm:ss";
                    break;

                case 'U':
                    FormatDayOfWeek(result, self.DateTime, 7);
                    break;

                case 'W':
                    FormatDayOfWeek(result, self.DateTime, 8);
                    break;

                case 'w':
                    result.Append(((int)self.DateTime.DayOfWeek).ToString(CultureInfo.InvariantCulture));
                    break;

                case 'x':
                    dateTimeFormat = "d";
                    break;

                case 'X':
                    dateTimeFormat = "t";
                    break;

                case 'y':
                    dateTimeFormat = "yy";
                    break;

                case 'Y':
                    dateTimeFormat = "yyyy";
                    break;

                case 'Z':
                    dateTimeFormat = "%K";
                    break;

                case 'z':
                    if (context.RubyOptions.Compatibility > RubyCompatibility.Ruby186)
                    {
                        result.Append(self.FormatUtcOffset());
                    }
                    else
                    {
                        result.Append(RubyTime.GetCurrentZoneName());
                    }
                    break;

                default:
                    if (context.RubyOptions.Compatibility > RubyCompatibility.Ruby186)
                    {
                        result.Append(character);
                        break;
                    }
                    return(MutableString.CreateEmpty());
                }

                if (dateTimeFormat != null)
                {
                    result.Append(self.ToString(dateTimeFormat, CultureInfo.InvariantCulture));
                }
            }

            if (inFormat)
            {
                if (context.RubyOptions.Compatibility > RubyCompatibility.Ruby186)
                {
                    return(result.Append('%'));
                }
                return(MutableString.CreateEmpty());
            }

            return(result);
        }