Skip to content
This repository
tree: 7ae412add3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 335 lines (303 sloc) 13.104 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
using Niecza;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;


public partial class Builtins {

    public static Variable sprintf(Variable[] args) {
        char[] fmt = args[0].Fetch().mo.mro_raw_Str.Get(args[0]).ToCharArray();
        List<PrintfFormat> fmtlist = ParseFormatString(fmt);
        return RenderFormat(fmtlist, args);
    }

    private enum PrintfDirective {
        LiteralText,
        CodePoint,
        String,
        Int,
        FloatScientific,
        FloatFixedDecimal,
        FloatEF,
        InvokeCode
    }

    private enum PrintfModifier {
        None,
        NativeShort,
        NativeLong,
        NativeLongLong,
        NativeQuad,
        Complex
    }

    private struct PrintfFormat {
        internal int index; // 0 means "next one" (default), 1-indexed
        internal bool nonNegativeSpacePrefix;
        internal bool nonNegativePlusPrefix;
        internal bool leftJustify;
        internal bool rightJustifyZeroes;
        internal bool leadingRadix;
        internal bool vector;
        internal bool vectorOverride;
        internal int vectorOverrideIndex; // 1-indexed
        internal int minimumWidth;
        internal int minimumWidthIndex; // 1-indexed
        internal int precision;
        internal int precisionIndex; // 1-indexed
        internal PrintfDirective directive;
        internal PrintfModifier modifier;
        internal uint radix;
        internal bool upper;
        internal string literaltext;
    }

    private static List<PrintfFormat> ParseFormatString(char[] fmtstring) {
        List<PrintfFormat> fmtlist = new List<PrintfFormat>();
        for (int pos=0; pos < fmtstring.Length; ++pos) {
            char c = fmtstring[pos];
            PrintfFormat format = new PrintfFormat();
            // We do not have to initialize all these fields, but it's
            // handy to have them all listed together in one place.
            format.index = 0;
            format.nonNegativeSpacePrefix = false;
            format.nonNegativePlusPrefix = false;
            format.leftJustify = false;
            format.rightJustifyZeroes = false;
            format.leadingRadix = false;
            format.vector = false;
            format.vectorOverride = false;
            format.vectorOverrideIndex = 0; // 1-indexed
            format.minimumWidth = 0;
            format.minimumWidthIndex = 0; // 1-indexed
            format.precision = 0;
            format.precisionIndex = 0; // 1-indexed
            format.directive = PrintfDirective.String;
            format.modifier = PrintfModifier.None;
            format.radix = 10;
            format.upper = false;
            format.literaltext = "";
            if (c == '%') { // begin format specifier
                bool continue_parsing_specifier = true;
                bool parse_minimum_width = true;
                bool seen_first_digit = false;
                while (continue_parsing_specifier && ++pos<fmtstring.Length) {
                    switch (c=fmtstring[pos]) {
                        case '0': case '1': case '2': case '3': case '4':
                        case '5': case '6': case '7': case '8': case '9':
                            int digitvalue = c - '0';
                            if (!seen_first_digit && c == '0') {
                                format.rightJustifyZeroes = true;
                            }
                            seen_first_digit = true;
                            if (parse_minimum_width)
                                format.minimumWidth = 10 * format.minimumWidth + digitvalue;
                            else
                                format.precision = 10 * format.precision + digitvalue;
                            break;
                        case '.':
                            parse_minimum_width = false;
                            break;
                        case '-': // negative width
                            format.leftJustify = true;
                            break;
                        case '$': // "%1$" arglist position
                            format.index = format.minimumWidth;
                            format.minimumWidth = 0;
                            break;
                        case '%': // "%%" a literal '%'
                            format.directive = PrintfDirective.LiteralText;
                            format.literaltext = "" + c;
                            continue_parsing_specifier = false;
                            break;
                        case 'b': // unsigned binary integer
                            format.directive = PrintfDirective.Int;
                            format.radix = 2;
                            continue_parsing_specifier = false;
                            break;
                        case 'C': // Code
                            format.directive = PrintfDirective.InvokeCode;
                            continue_parsing_specifier = false;
                            break;
                        case 'c': // codepoint
                            format.directive = PrintfDirective.CodePoint;
                            continue_parsing_specifier = false;
                            break;
                        case 'd': // decimal
                        case 'i': // integer (alias for decimal)
                        case 'u': // integer (alias for decimal)
                            format.directive = PrintfDirective.Int;
                            format.radix = 10;
                            continue_parsing_specifier = false;
                            break;
                        case 'E': // scientific uppercase E
                            format.directive = PrintfDirective.FloatScientific;
                            format.upper = true;
                            continue_parsing_specifier = false;
                            break;
                        case 'e': // scientific lowercase e
                            format.directive = PrintfDirective.FloatScientific;
                            continue_parsing_specifier = false;
                            break;
                        case 'f': // fixed point
                        case 'F': // fixed point synonym
                            format.directive = PrintfDirective.FloatFixedDecimal;
                            continue_parsing_specifier = false;
                            break;
                        case 'G': // e or f uppercase E
                            format.directive = PrintfDirective.FloatEF;
                            format.upper = true;
                            continue_parsing_specifier = false;
                            break;
                        case 'g': // e or f lowercase e
                            format.directive = PrintfDirective.FloatEF;
                            continue_parsing_specifier = false;
                            break;
                        case 'o': // unsigned octal
                            format.directive = PrintfDirective.Int;
                            format.radix = 8;
                            continue_parsing_specifier = false;
                            break;
                        case 's': // string
                            format.directive = PrintfDirective.String;
                            continue_parsing_specifier = false;
                            break;
                        case 'X': // hex uppercase
                            format.directive = PrintfDirective.Int;
                            format.radix = 16;
                            format.upper = true;
                            continue_parsing_specifier = false;
                            break;
                        case 'x': // hex lowercase
                            format.directive = PrintfDirective.Int;
                            format.radix = 16;
                            format.upper = false;
                            continue_parsing_specifier = false;
                            break;
                        default:
                            throw new NieczaException("invalid format specifier");
                    }
                }
            }
            else { // non '%' characters
                format.directive = PrintfDirective.LiteralText;
                format.literaltext = "" + c;
                while (++pos < fmtstring.Length && (c=fmtstring[pos]) != '%') {
                    format.literaltext += c;
                }
                // pos has gone one char too far, but should come back
                // only if there is a '%' before the end of fmtstring
                if (pos < fmtstring.Length && (c=fmtstring[pos]) == '%')
                    --pos;
            }
            fmtlist.Add(format);
        }
        return fmtlist;
    }

    private static string RemoveInitialZero(String s) {
        if (s.Length > 1 && s[0] == '0') {
            return s.Substring(1);
        }
        return s;
    }

    private static string RenderFormat(PrintfFormat format, Variable arg) {
        int n;

        if (format.directive == PrintfDirective.String) {
            return arg.Fetch().mo.mro_raw_Str.Get(arg);
        }

        if (format.directive == PrintfDirective.CodePoint) {
            n = (int) arg.Fetch().mo.mro_raw_Numeric.Get(arg);
            return "" + (char) n;
        }

        bool add_minus_sign = false;
        if (format.directive == PrintfDirective.Int) {

            P6any o1 = arg.Fetch();
            int r1;
            P6any n1 = GetNumber(arg, o1, out r1);
            Variable arg2;

            if (r1 != NR_BIGINT && r1 != NR_FIXINT) {
                arg2 = Builtins.InvokeMethod("Int", arg);
                o1 = arg2.Fetch();
                n1 = GetNumber(arg2, o1, out r1);
            }

            BigInteger value = PromoteToBigInt(r1, n1);
            if (value < 0) {
                add_minus_sign = true;
                value = -value;
            }

            String number;
            if (format.radix == 16) {
                number = RemoveInitialZero(value.ToString(format.radix, null)).ToLower();
            } else {
                number = value.ToString(format.radix, null);
            }

            if (format.upper) {
                number = number.ToUpper();
            }

            if (add_minus_sign) {
                if (format.rightJustifyZeroes) {
                    return "-" + number.PadLeft(format.minimumWidth - 1, '0');
                } else {
                    return "-" + number;
                }
            }

            return number;

        } else {

            double f = arg.Fetch().mo.mro_raw_Numeric.Get(arg);

            if (f < 0.0) {
                add_minus_sign = true;
                f = -f;
            }

            int precision = format.precision > 0 ? format.precision : 6;

            String number = "??";
            switch (format.directive) {
                case PrintfDirective.FloatFixedDecimal:
                    number = f.ToString("F" + precision);
                    break;
                case PrintfDirective.FloatScientific:
                    number = f.ToString("e" + precision);
                    break;
                case PrintfDirective.FloatEF:
                    number = f.ToString("g" + precision);
                    break;
            }

            if (format.upper) {
                number = number.ToUpper();
            }

            if (add_minus_sign) {
                if (format.rightJustifyZeroes) {
                    return "-" + number.PadLeft(format.minimumWidth - 1, '0');
                } else {
                    return "-" + number;
                }
            }

            return number;
        }
    }

    private static Variable RenderFormat(List<PrintfFormat> formatlist, Variable[] args) {
        string result = "";
        int argi = 0;
        foreach (PrintfFormat format in formatlist) {
            if (format.directive == PrintfDirective.LiteralText) {
                result += format.literaltext;
                continue;
            }

            if (format.directive == PrintfDirective.InvokeCode) {
                continue; // Should this do something more?
            }

            int i = format.index > 0 ? format.index : ++argi;
            if (i < args.Length) {
                string s = RenderFormat(format, args[i]);
                if (format.leftJustify) {
                    result += s.PadRight(format.minimumWidth, ' ');
                } else {
                    if (format.rightJustifyZeroes) {
                        result += s.PadLeft(format.minimumWidth, '0');
                    }
                    else {
                        result += s.PadLeft(format.minimumWidth, ' ');
                    }
                }
            }
            else {
                throw new NieczaException("index out of range");
            }
        }
        return Kernel.BoxAnyMO(result, Compartment.Top.StrMO);
    }
}

Something went wrong with that request. Please try again.