FanInt defines the methods for sys::Int. The actual class used for representation is Fan.Sys.Int.
Ejemplo n.º 1
0
        public static Regex glob(string pattern)
        {
            StringBuilder s = new StringBuilder();

            for (int i = 0; i < pattern.Length; ++i)
            {
                int c = pattern[i];
                if (FanInt.isAlphaNum(c))
                {
                    s.Append((char)c);
                }
                else if (c == '?')
                {
                    s.Append('.');
                }
                else if (c == '*')
                {
                    s.Append('.').Append('*');
                }
                else
                {
                    s.Append('\\').Append((char)c);
                }
            }
            return(new Regex(s.ToString()));
        }
Ejemplo n.º 2
0
        public static string makeTrim(StringBuilder s)
        {
            int start = 0;
            int end   = s.Length;

            while (start < end)
            {
                if (FanInt.isSpace(s[start]))
                {
                    start++;
                }
                else
                {
                    break;
                }
            }
            while (end > start)
            {
                if (FanInt.isSpace(s[end - 1]))
                {
                    end--;
                }
                else
                {
                    break;
                }
            }
            return(s.ToString(start, end - start));
        }
Ejemplo n.º 3
0
        private Month parseMon()
        {
            StringBuilder s = new StringBuilder();

            while (pos < str.Length)
            {
                int ch = str[pos];
                if ('a' <= ch && ch <= 'z')
                {
                    s.Append((char)ch); pos++; continue;
                }
                if ('A' <= ch && ch <= 'Z')
                {
                    s.Append((char)FanInt.lower(ch)); pos++; continue;
                }
                break;
            }
            Month m = locale().monthByName(s.ToString());

            if (m == null)
            {
                throw new Exception();
            }
            return(m);
        }
Ejemplo n.º 4
0
            private long toLong(byte[] bytes)
            {
                // if bytes less then 6 pad with random
                if (bytes.Length < 6)
                {
                    byte[] temp = new byte[6];
                    Array.Copy(bytes, 0, temp, 0, bytes.Length);
                    for (int i = bytes.Length; i < temp.Length; ++i)
                    {
                        temp[i] = (byte)FanInt.random();
                    }
                    bytes = temp;
                }

                // mask bytes into 6 byte long
                long x = ((bytes[0] & 0xFFL) << 40) |
                         ((bytes[1] & 0xFFL) << 32) |
                         ((bytes[2] & 0xFFL) << 24) |
                         ((bytes[3] & 0xFFL) << 16) |
                         ((bytes[4] & 0xFFL) << 8) |
                         ((bytes[5] & 0xFFL) << 0);

                // if we have more then six bytes mask against primary six bytes
                for (int i = 6; i < bytes.Length; ++i)
                {
                    x ^= (bytes[i] & 0xFFL) << (((i - 6) % 6) * 8);
                }

                return(x);
            }
Ejemplo n.º 5
0
        public virtual string readStrToken(Long max, Func f)
        {
            // max limit
            int maxChars = (max != null) ? max.intValue() : System.Int32.MaxValue;

            if (maxChars <= 0)
            {
                return(string.Empty);
            }

            // read first char, if at end of file bail
            int c = rChar();

            if (c < 0)
            {
                return(null);
            }

            // loop reading chars until our closure returns false
            StringBuilder buf = new StringBuilder();

            while (true)
            {
                // check for \n, \r\n, or \r
                bool terminate;
                if (f == null)
                {
                    terminate = FanInt.isSpace(c);
                }
                else
                {
                    terminate = ((Boolean)f.call(c)).booleanValue();
                }
                if (terminate)
                {
                    unreadChar(c);
                    break;
                }

                // Append to working buffer
                buf.Append((char)c);
                if (buf.Length >= maxChars)
                {
                    break;
                }

                // read next char
                c = rChar();
                if (c < 0)
                {
                    break;
                }
            }
            return(buf.ToString());
        }
Ejemplo n.º 6
0
 private static bool neic(int a, int b)
 {
     if (a == b)
     {
         return(false);
     }
     if ((a | 0x20) == (b | 0x20))
     {
         return(FanInt.lower(a) != FanInt.lower(b));
     }
     return(true);
 }
Ejemplo n.º 7
0
        public object random()
        {
            if (m_size == 0)
            {
                return(null);
            }
            int i = (int)FanInt.random();

            if (i < 0)
            {
                i = -i;
            }
            return(m_values[i % m_size]);
        }
Ejemplo n.º 8
0
 public List shuffle()
 {
     modify();
     for (int i = 0; i < m_size; ++i)
     {
         int randi = (int)FanInt.random() % m_size;
         if (randi < 0)
         {
             randi = -randi;
         }
         object temp = m_values[i];
         m_values[i]     = m_values[randi];
         m_values[randi] = temp;
     }
     return(this);
 }
Ejemplo n.º 9
0
 static void checkId(string id)
 {
     if (id.Length == 0)
     {
         throw ParseErr.make("Invalid unit id length 0").val;
     }
     for (int i = 0; i < id.Length; ++i)
     {
         int c = id[i];
         if (FanInt.isAlpha(c) || c == '_' || c == '%' || c == '$' || c == '/' || c > 128)
         {
             continue;
         }
         throw ParseErr.make("Invalid unit id " + id + " (invalid char '" + (char)c + "')").val;
     }
 }
Ejemplo n.º 10
0
        public static Regex quote(string str)
        {
            StringBuilder s = new StringBuilder();

            for (int i = 0; i < str.Length; ++i)
            {
                int c = str[i];
                if (FanInt.isAlphaNum(c))
                {
                    s.Append((char)c);
                }
                else
                {
                    s.Append('\\').Append((char)c);
                }
            }
            return(new Regex(s.ToString()));
        }
Ejemplo n.º 11
0
        private void writePropStr(string s)
        {
            int len = s.Length;

            for (int i = 0; i < len; ++i)
            {
                int ch   = s[i];
                int peek = i + 1 < len ? s[i + 1] : -1;

                // escape special chars
                switch (ch)
                {
                case '\n': writeChar('\\').writeChar('n'); continue;

                case '\r': writeChar('\\').writeChar('r'); continue;

                case '\t': writeChar('\\').writeChar('t'); continue;

                case '\\': writeChar('\\').writeChar('\\'); continue;
                }

                // escape control chars, comments, and =
                if ((ch < ' ') || (ch == '/' && (peek == '/' || peek == '*')) || (ch == '='))
                {
                    long nib1 = FanInt.toDigit((ch >> 4) & 0xf, 16).longValue();
                    long nib2 = FanInt.toDigit((ch >> 0) & 0xf, 16).longValue();

                    this.writeChar('\\').writeChar('u')
                    .writeChar('0').writeChar('0')
                    .writeChar(nib1).writeChar(nib2);
                    continue;
                }

                // normal character
                writeChar(ch);
            }
        }
Ejemplo n.º 12
0
 public void verifyEq(object expected, object actual, string msg)
 {
     if (!OpUtil.compareEQ(expected, actual))
     {
         //if (msg == null) msg = s(expected) + " != " + s(actual);
         if (msg == null)
         {
             msg = s(expected) +
                   " [" + (expected != null ? expected.GetType().ToString() : "null") + "] != "
                   + s(actual) + " [" + (actual != null ? actual.GetType().ToString() : "null") + "]";
         }
         fail(msg);
     }
     if (expected != null && actual != null)
     {
         if (hash(expected) != hash(actual))
         {
             fail("Equal but different hash codes: " +
                  expected + " (0x" + FanInt.toHex(hash(expected)) + ") ?= " +
                  actual + " (0x" + FanInt.toHex(hash(actual)) + ")");
         }
     }
     verifyCount++;
 }
Ejemplo n.º 13
0
 public static Long toInt(string self)
 {
     return(FanInt.fromStr(self, 10, true));
 }
Ejemplo n.º 14
0
 private long resolveNodeAddr()
 {
     try { return(resolveMacAddr()); } catch (Exception e) { Err.dumpStack(e); }
     try { return(resolveIpAddr()); } catch (Exception e) { Err.dumpStack(e); }
     return(FanInt.random());
 }
Ejemplo n.º 15
0
 internal Factory()
 {
     nodeAddr = resolveNodeAddr();
     seq      = FanInt.random();
 }
Ejemplo n.º 16
0
 public long random()
 {
     return(FanInt.random(this));
 }
Ejemplo n.º 17
0
        private Map readProps(bool listVals) // listVals is Str:Str[]
        {
            Charset origCharset = charset();

            charset(Charset.utf8());
            try
            {
                Map props = new Map(Sys.StrType, listVals ? Sys.StrType.toListOf() : Sys.StrType);

                StringBuilder name = new StringBuilder();
                StringBuilder val = null;
                int           inBlockComment = 0;
                bool          inEndOfLineComment = false;
                int           c = ' ', last = ' ';
                int           lineNum = 1;

                while (true)
                {
                    last = c;
                    c    = rChar();
                    if (c < 0)
                    {
                        break;
                    }

                    // end of line
                    if (c == '\n' || c == '\r')
                    {
                        inEndOfLineComment = false;
                        if (last == '\r' && c == '\n')
                        {
                            continue;
                        }
                        string n = FanStr.makeTrim(name);
                        if (val != null)
                        {
                            addProp(props, n, FanStr.makeTrim(val), listVals);
                            name = new StringBuilder();
                            val  = null;
                        }
                        else if (n.Length > 0)
                        {
                            throw IOErr.make("Invalid name/value pair [Line " + lineNum + "]").val;
                        }
                        lineNum++;
                        continue;
                    }

                    // if in comment
                    if (inEndOfLineComment)
                    {
                        continue;
                    }

                    // block comment
                    if (inBlockComment > 0)
                    {
                        if (last == '/' && c == '*')
                        {
                            inBlockComment++;
                        }
                        if (last == '*' && c == '/')
                        {
                            inBlockComment--;
                        }
                        continue;
                    }

                    // equal
                    if (c == '=' && val == null)
                    {
                        val = new StringBuilder();
                        continue;
                    }

                    // comment
                    if (c == '/' && FanInt.isSpace(last))
                    {
                        int peek = rChar();
                        if (peek < 0)
                        {
                            break;
                        }
                        if (peek == '/')
                        {
                            inEndOfLineComment = true; continue;
                        }
                        if (peek == '*')
                        {
                            inBlockComment++; continue;
                        }
                        unreadChar(peek);
                    }

                    // escape or line continuation
                    if (c == '\\')
                    {
                        int peek = rChar();
                        if (peek < 0)
                        {
                            break;
                        }
                        else if (peek == 'n')
                        {
                            c = '\n';
                        }
                        else if (peek == 'r')
                        {
                            c = '\r';
                        }
                        else if (peek == 't')
                        {
                            c = '\t';
                        }
                        else if (peek == '\\')
                        {
                            c = '\\';
                        }
                        else if (peek == '\r' || peek == '\n')
                        {
                            // line continuation
                            lineNum++;
                            if (peek == '\r')
                            {
                                peek = rChar();
                                if (peek != '\n')
                                {
                                    unreadChar(peek);
                                }
                            }
                            while (true)
                            {
                                peek = rChar();
                                if (peek == ' ' || peek == '\t')
                                {
                                    continue;
                                }
                                unreadChar(peek);
                                break;
                            }
                            continue;
                        }
                        else if (peek == 'u')
                        {
                            int n3 = hex(rChar());
                            int n2 = hex(rChar());
                            int n1 = hex(rChar());
                            int n0 = hex(rChar());
                            if (n3 < 0 || n2 < 0 || n1 < 0 || n0 < 0)
                            {
                                throw IOErr.make("Invalid hex value for \\uxxxx [Line " + lineNum + "]").val;
                            }
                            c = ((n3 << 12) | (n2 << 8) | (n1 << 4) | n0);
                        }
                        else
                        {
                            throw IOErr.make("Invalid escape sequence [Line " + lineNum + "]").val;
                        }
                    }

                    // normal character
                    if (val == null)
                    {
                        name.Append((char)c);
                    }
                    else
                    {
                        val.Append((char)c);
                    }
                }

                string nm = FanStr.makeTrim(name);
                if (val != null)
                {
                    addProp(props, nm, FanStr.makeTrim(val), listVals);
                }
                else if (nm.Length > 0)
                {
                    throw IOErr.make("Invalid name/value pair [Line " + lineNum + "]").val;
                }

                return(props);
            }
            finally
            {
                try { close(); } catch (System.Exception e) { Err.dumpStack(e); }
                charset(origCharset);
            }
        }
Ejemplo n.º 18
0
 public static Long toInt(string self, long radix, bool check)
 {
     return(FanInt.fromStr(self, radix, check));
 }
Ejemplo n.º 19
0
        private static Map doParseParams(string s, int offset)
        {
            Map pars = new Map(Sys.StrType, Sys.StrType);

            pars.caseInsensitive(true);
            bool inQuotes = false;
            int  keyStart = offset;
            int  valStart = -1;
            int  valEnd   = -1;
            int  eq       = -1;
            bool hasEsc   = false;

            for (int i = keyStart; i < s.Length; ++i)
            {
                int c = s[i];

                // let parens slide since sometimes they occur in cookies
                // if (c == '(' && !inQuotes)
                //   throw ParseErr.make("MimeType", s, "comments not supported");

                if (c == '=' && !inQuotes && valStart < 0)
                {
                    eq = i++;
                    while (FanInt.isSpace(s[i]))
                    {
                        ++i;
                    }
                    if (s[i] == '"')
                    {
                        inQuotes = true; ++i; c = s[i];
                    }
                    else
                    {
                        inQuotes = false;
                    }
                    valStart = i;
                }

                if (eq < 0)
                {
                    continue;
                }

                if (c == '\\' && inQuotes)
                {
                    ++i;
                    hasEsc = true;
                    continue;
                }

                if (c == '"' && inQuotes)
                {
                    valEnd   = i - 1;
                    inQuotes = false;
                }

                if (c == ';' && !inQuotes)
                {
                    if (valEnd < 0)
                    {
                        valEnd = i - 1;
                    }
                    string key = s.Substring(keyStart, eq - keyStart).Trim();
                    string val = s.Substring(valStart, valEnd + 1 - valStart).Trim();
                    if (hasEsc)
                    {
                        val = unescape(val);
                    }
                    pars.set(key, val);
                    keyStart = i + 1;
                    eq       = valStart = valEnd = -1;
                    hasEsc   = false;
                }
            }

            if (keyStart < s.Length)
            {
                if (valEnd < 0)
                {
                    valEnd = s.Length - 1;
                }
                string key = s.Substring(keyStart, eq - keyStart).Trim();
                string val = s.Substring(valStart, valEnd + 1 - valStart).Trim();
                if (hasEsc)
                {
                    val = unescape(val);
                }
                pars.set(key, val);
            }

            return(pars);
        }
Ejemplo n.º 20
0
        //////////////////////////////////////////////////////////////////////////
        // Identity
        //////////////////////////////////////////////////////////////////////////

        public override sealed long compare(object obj)
        {
            return(FanInt.compare(m_ordinal, ((Enum)obj).m_ordinal));
        }
Ejemplo n.º 21
0
 public static Long toInt(string self, long radix)
 {
     return(FanInt.fromStr(self, radix, true));
 }
Ejemplo n.º 22
0
        //////////////////////////////////////////////////////////////////////////
        // Formatting
        //////////////////////////////////////////////////////////////////////////

        public string format()
        {
            StringBuilder s   = new StringBuilder();
            int           len = pattern.Length;

            for (int i = 0; i < len; ++i)
            {
                // character
                int c = pattern[i];

                // literals
                if (c == '\'')
                {
                    while (true)
                    {
                        ++i;
                        if (i >= len)
                        {
                            throw ArgErr.make("Invalid pattern: unterminated literal").val;
                        }
                        c = pattern[i];
                        if (c == '\'')
                        {
                            break;
                        }
                        s.Append((char)c);
                    }
                    continue;
                }

                // character count
                int n = 1;
                while (i + 1 < len && pattern[i + 1] == c)
                {
                    ++i; ++n;
                }

                // switch
                bool invalidNum = false;
                switch (c)
                {
                case 'Y':
                    int y = this.year;
                    switch (n)
                    {
                    case 2:  y %= 100; if (y < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(y); break;

                    case 4:  s.Append(y); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'M':
                    if (mon == null)
                    {
                        throw ArgErr.make("Month not available").val;
                    }
                    switch (n)
                    {
                    case 4:
                        s.Append(mon.full(locale()));
                        break;

                    case 3:
                        s.Append(mon.abbr(locale()));
                        break;

                    case 2:  if (mon.ordinal() + 1 < 10L)
                        {
                            s.Append('0');
                        }
                        s.Append(mon.ordinal() + 1); break;

                    case 1:  s.Append(mon.ordinal() + 1); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'D':
                    switch (n)
                    {
                    case 3:  s.Append(day).Append(daySuffix(day)); break;

                    case 2:  if (day < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(day); break;

                    case 1:  s.Append(day); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'W':
                    if (weekday == null)
                    {
                        throw ArgErr.make("Weekday not available").val;
                    }
                    switch (n)
                    {
                    case 4:
                        s.Append(weekday.full(locale()));
                        break;

                    case 3:
                        s.Append(weekday.abbr(locale()));
                        break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'V':
                    int woy = weekOfYear();
                    if (woy < 1)
                    {
                        throw ArgErr.make("Week of year not available").val;
                    }
                    switch (n)
                    {
                    case 3:  s.Append(woy).Append(daySuffix(woy)); break;

                    case 2:  if (woy < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(woy); break;

                    case 1:  s.Append(woy); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'h':
                case 'k':
                    int h = this.hour;
                    if (c == 'k')
                    {
                        if (h == 0)
                        {
                            h = 12;
                        }
                        else if (h > 12)
                        {
                            h -= 12;
                        }
                    }
                    switch (n)
                    {
                    case 2:  if (h < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(h); break;

                    case 1:  s.Append(h); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'm':
                    switch (n)
                    {
                    case 2:  if (min < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(min); break;

                    case 1:  s.Append(min); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 's':
                    switch (n)
                    {
                    case 2:  if (sec < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(sec); break;

                    case 1:  s.Append(sec); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'S':
                    if (sec != 0 || ns != 0)
                    {
                        switch (n)
                        {
                        case 2:  if (sec < 10)
                            {
                                s.Append('0');
                            }
                            s.Append(sec); break;

                        case 1:  s.Append(sec); break;

                        default: invalidNum = true; break;
                        }
                    }
                    break;

                case 'a':
                    switch (n)
                    {
                    case 1:  s.Append(hour < 12 ? "a"  : "p"); break;

                    case 2:  s.Append(hour < 12 ? "am" : "pm"); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'A':
                    switch (n)
                    {
                    case 1:  s.Append(hour < 12 ? "A"  : "P"); break;

                    case 2:  s.Append(hour < 12 ? "AM" : "PM"); break;

                    default: invalidNum = true; break;
                    }
                    break;

                case 'f':
                case 'F':
                    int req = 0, opt = 0; // required, optional
                    if (c == 'F')
                    {
                        opt = n;
                    }
                    else
                    {
                        req = n;
                        while (i + 1 < len && pattern[i + 1] == 'F')
                        {
                            ++i; ++opt;
                        }
                    }
                    int frac = ns;
                    for (int x = 0, tenth = 100000000; x < 9; ++x)
                    {
                        if (req > 0)
                        {
                            req--;
                        }
                        else
                        {
                            if (frac == 0 || opt <= 0)
                            {
                                break;
                            }
                            opt--;
                        }
                        s.Append(frac / tenth);
                        frac  %= tenth;
                        tenth /= 10;
                    }
                    break;

                case 'z':
                    TimeZone.Rule rule = tz.rule(year);
                    switch (n)
                    {
                    case 1:
                        int offset = rule.offset;
                        if (dst)
                        {
                            offset += rule.dstOffset;
                        }
                        if (offset == 0)
                        {
                            s.Append('Z'); break;
                        }
                        if (offset < 0)
                        {
                            s.Append('-'); offset = -offset;
                        }
                        else
                        {
                            s.Append('+');
                        }
                        int zh = offset / 3600;
                        int zm = (offset % 3600) / 60;
                        if (zh < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(zh).Append(':');
                        if (zm < 10)
                        {
                            s.Append('0');
                        }
                        s.Append(zm);
                        break;

                    case 3:
                        s.Append(dst ? rule.dstAbbr : rule.stdAbbr);
                        break;

                    case 4:
                        s.Append(tz.name());
                        break;

                    default:
                        invalidNum = true;
                        break;
                    }
                    break;

                default:
                    if (FanInt.isAlpha(c))
                    {
                        throw ArgErr.make("Invalid pattern: unsupported char '" + (char)c + "'").val;
                    }

                    // check for symbol skip
                    if (i + 1 < len)
                    {
                        int next = pattern[i + 1];

                        // don't display symbol between ss.FFF if fractions is zero
                        if (next == 'F' && ns == 0)
                        {
                            break;
                        }

                        // don't display symbol between mm:SS if secs is zero
                        if (next == 'S' && sec == 0 && ns == 0)
                        {
                            break;
                        }
                    }

                    s.Append((char)c);
                    break;
                }

                // if invalid number of characters
                if (invalidNum)
                {
                    throw ArgErr.make("Invalid pattern: unsupported num of '" + (char)c + "' (x" + n + ")").val;
                }
            }

            return(s.ToString());
        }