Beispiel #1
0
 private CharSequence transformFromCharacter()
 {
     if (arg == null)
     {
         return(padding(CharSequenceProxy.Wrap("null"), 0));
     }
     if (arg is char)
     {
         return(padding(CharSequenceProxy.Wrap(Sharpen.StringHelper.GetValueOf(arg)), 0));
     }
     else
     {
         throw badArgumentType();
     }
 }
Beispiel #2
0
        private CharSequence transform(FormatToken token, object argument)
        {
            this.formatToken = token;
            this.arg         = argument;
            // There are only two format specifiers that matter: "%d" and "%s".
            // Nothing else is common in the wild. We fast-path these two to
            // avoid the heavyweight machinery needed to cope with flags, width,
            // and precision.
            if (token.isDefault())
            {
                switch (token.getConversionType())
                {
                case 's':
                {
                    if (arg == null)
                    {
                        return(CharSequenceProxy.Wrap("null"));
                    }
                    else
                    {
                        if (!(arg is java.util.Formattable))
                        {
                            return(CharSequenceProxy.Wrap(arg.ToString()));
                        }
                    }
                    break;
                }

                case 'd':
                {
                    if (_out is StringBuilder)
                    {
                        if ((arg is int) || (arg is short) || (arg is byte) || (arg is long))
                        {
                            ((StringBuilder)_out).append(arg.ToString());
                            return(null);
                        }
                    }
                    if (arg is int || arg is long || arg is short || arg is byte)
                    {
                        return(CharSequenceProxy.Wrap(arg.ToString()));
                    }
                }
                break;
                }
            }
            formatToken.checkFlags(arg);
            CharSequence result_1;

            switch (token.getConversionType())
            {
            case 'B':
            case 'b':
            {
                result_1 = transformFromBoolean();
                break;
            }

            case 'H':
            case 'h':
            {
                result_1 = transformFromHashCode();
                break;
            }

            case 'S':
            case 's':
            {
                result_1 = transformFromString();
                break;
            }

            case 'C':
            case 'c':
            {
                result_1 = transformFromCharacter();
                break;
            }

            case 'd':
            case 'o':
            case 'x':
            case 'X':
            {
                result_1 = transformFromInteger();
                break;
            }

            case 'A':
            case 'a':
            case 'E':
            case 'e':
            case 'f':
            case 'G':
            case 'g':
            {
                result_1 = transformFromFloat();
                break;
            }

            case '%':
            {
                result_1 = transformFromPercent();
                break;
            }

            case 'n':
            {
                result_1 = CharSequenceProxy.Wrap(Environment.NewLine);
                break;
            }

            case 't':
            case 'T':
            {
                result_1 = transformFromDateTime();
                break;
            }

            default:
            {
                throw token.unknownFormatConversionException();
            }
            }
            if (System.Char.IsUpper(token.getConversionType()))
            {
                if (result_1 != null)
                {
                    result_1 = CharSequenceProxy.Wrap(result_1.ToString().ToUpper(_locale));
                }
            }
            return(result_1);
        }
Beispiel #3
0
        private CharSequence transformFromInteger()
        {
            int           startIndex            = 0;
            StringBuilder result                = new StringBuilder();
            char          currentConversionType = formatToken.getConversionType();
            long          value;

            if (arg is long)
            {
                value = ((long)arg);
            }
            else
            {
                if (arg is int)
                {
                    value = (int)arg;
                }
                else
                {
                    if (arg is short)
                    {
                        value = (short)arg;
                    }
                    else
                    {
                        if (arg is byte)
                        {
                            value = (byte)arg;
                        }
                        else
                        {
                            throw badArgumentType();
                        }
                    }
                }
            }
            if (formatToken.flagSharp)
            {
                if (currentConversionType == 'o')
                {
                    result.append("0");
                    startIndex += 1;
                }
                else
                {
                    result.append("0x");
                    startIndex += 2;
                }
            }
            if (currentConversionType == 'd')
            {
                CharSequence digits = CharSequenceProxy.Wrap(System.Convert.ToString
                                                                 (value));
                if (formatToken.flagComma)
                {
                    digits = insertGrouping(digits);
                }
                if (localeData.zeroDigit != '0')
                {
                    digits = localizeDigits(digits);
                }
                result.append(digits);
                if (value < 0)
                {
                    if (formatToken.flagParenthesis)
                    {
                        return(wrapParentheses(result));
                    }
                    else
                    {
                        if (formatToken.flagZero)
                        {
                            startIndex++;
                        }
                    }
                }
                else
                {
                    if (formatToken.flagPlus)
                    {
                        result.insert(0, '+');
                        startIndex += 1;
                    }
                    else
                    {
                        if (formatToken.flagSpace)
                        {
                            result.insert(0, ' ');
                            startIndex += 1;
                        }
                    }
                }
            }
            else
            {
                // Undo sign-extension, since we'll be using Long.to(Octal|Hex)String.
                if (arg is byte)
                {
                    value &= unchecked ((long)(0xffL));
                }
                else
                {
                    if (arg is short)
                    {
                        value &= unchecked ((long)(0xffffL));
                    }
                    else
                    {
                        if (arg is int)
                        {
                            value &= unchecked ((long)(0xffffffffL));
                        }
                    }
                }
                if (currentConversionType == 'o')
                {
                    result.append(Convert.ToString(value, 10));
                }
                else
                {
                    result.append(Convert.ToString(value, 16));
                }
            }
            return(padding(result, startIndex));
        }
 public static CharSequence SubSequence(this string str, int start, int length)
 {
     return(CharSequenceProxy.Wrap(str.Substring(start, length)));
 }