FanStr defines the methods for sys::string. The actual class used for representation is F.lang.String.
Beispiel #1
0
        public static Locale fromStr(string s, bool check)
        {
            int len = s.Length;

            try
            {
                if (len == 2)
                {
                    if (FanStr.isLower(s))
                    {
                        return(new Locale(s, s, null));
                    }
                }

                if (len == 5)
                {
                    string lang    = s.Substring(0, 2);
                    string country = s.Substring(3, 2);
                    if (FanStr.isLower(lang) && FanStr.isUpper(country) && s[2] == '-')
                    {
                        return(new Locale(s, lang, country));
                    }
                }
            }
            catch (Exception e)
            {
                Err.dumpStack(e);
            }
            if (!check)
            {
                return(null);
            }
            throw ParseErr.make("Locale", s).val;
        }
Beispiel #2
0
        /**
         * Parse an dimension string and intern it:
         *   dim    := <ratio> ["*" <ratio>]*
         *   ratio  := <base> <exp>
         *   base   := "kg" | "m" | "sec" | "K" | "A" | "mol" | "cd"
         */
        private static Dimension parseDim(string s)
        {
            // handle empty string as dimensionless
            if (s.Length == 0)
            {
                return(m_dimensionless);
            }

            // parse dimension
            Dimension dim    = new Dimension();
            List      ratios = FanStr.split(s, Long.valueOf((long)'*'), true);

            for (int i = 0; i < ratios.sz(); ++i)
            {
                string r = (string)ratios.get(i);
                if (r.StartsWith("kg"))
                {
                    dim.kg = SByte.Parse(r.Substring(2).Trim()); continue;
                }
                if (r.StartsWith("sec"))
                {
                    dim.sec = SByte.Parse(r.Substring(3).Trim()); continue;
                }
                if (r.StartsWith("mol"))
                {
                    dim.mol = SByte.Parse(r.Substring(3).Trim()); continue;
                }
                if (r.StartsWith("m"))
                {
                    dim.m = SByte.Parse(r.Substring(1).Trim()); continue;
                }
                if (r.StartsWith("K"))
                {
                    dim.K = SByte.Parse(r.Substring(1).Trim()); continue;
                }
                if (r.StartsWith("A"))
                {
                    dim.A = SByte.Parse(r.Substring(1).Trim()); continue;
                }
                if (r.StartsWith("cd"))
                {
                    dim.cd = SByte.Parse(r.Substring(2).Trim()); continue;
                }
                throw new Exception("Bad ratio '" + r + "'");
            }

            // intern
            return(dim.intern());
        }
Beispiel #3
0
 /** Get a month by lowercase abbr or full name for this locale */
 internal Month monthByName(string name)
 {
     if (m_monthsByName == null)
     {
         Hashtable map = new Hashtable();
         for (int i = 0; i < Month.array.Length; ++i)
         {
             Month m = Month.array[i];
             map[FanStr.lower(m.abbr(this))] = m;
             map[FanStr.lower(m.full(this))] = m;
         }
         m_monthsByName = map;
     }
     return((Month)m_monthsByName[name]);
 }
Beispiel #4
0
 private static string s(object obj)
 {
     if (obj == null)
     {
         return("null");
     }
     if (obj is string)
     {
         return(FanStr.toCode((string)obj));
     }
     if (obj is List)
     {
         return(((List)obj).of().ToString() + obj);
     }
     return(toStr(obj));
 }
Beispiel #5
0
 public static long compare(object self, object x)
 {
     if (self is FanObj)
     {
         return(((FanObj)self).compare(x));
     }
     else if (self is string)
     {
         return(FanStr.compare((string)self, x));
     }
     else if (self is IComparable)
     {
         return(((IComparable)self).CompareTo(x));
     }
     else
     {
         return(FanStr.compare(toStr(self), toStr(x)));
     }
 }
Beispiel #6
0
        /**
         * Parse an un-interned unit:
         *   unit := <name> [";" <symbol> [";" <dim> [";" <scale> [";" <offset>]]]]
         */
        private static Unit parseUnit(string s)
        {
            string idStrs = s;
            int    c      = s.IndexOf(';');

            if (c > 0)
            {
                idStrs = s.Substring(0, c);
            }
            List ids = FanStr.split(idStrs, Long.valueOf(','));

            if (c < 0)
            {
                return(new Unit(ids, m_dimensionless, 1, 0));
            }

            string dim = s = s.Substring(c + 1).Trim();

            c = s.IndexOf(';');
            if (c < 0)
            {
                return(new Unit(ids, parseDim(dim), 1, 0));
            }

            dim = s.Substring(0, c).Trim();
            string scale = s = s.Substring(c + 1).Trim();

            c = s.IndexOf(';');
            if (c < 0)
            {
                return(new Unit(ids, parseDim(dim), Double.parseDouble(scale), 0));
            }

            scale = s.Substring(0, c).Trim();
            string offset = s.Substring(c + 1).Trim();

            return(new Unit(ids, parseDim(dim), Double.parseDouble(scale), Double.parseDouble(offset)));
        }
Beispiel #7
0
        private static MimeType parse(string s)
        {
            int    slash = s.IndexOf('/');
            string media = s.Substring(0, slash);
            string sub   = s.Substring(slash + 1);
            Map    pars  = emptyParams();

            int semi = sub.IndexOf(';');

            if (semi > 0)
            {
                pars = doParseParams(sub, semi + 1);
                sub  = sub.Substring(0, semi).Trim();
            }

            MimeType r = new MimeType();

            r.m_str       = s;
            r.m_mediaType = FanStr.lower(media);
            r.m_subType   = FanStr.lower(sub);
            r.m_params    = pars.ro();
            return(r);
        }
Beispiel #8
0
 public override long hash()
 {
     return(FanStr.hash(signature()));
 }
Beispiel #9
0
 public sealed override long hash()
 {
     return(FanStr.hash(m_source));
 }
Beispiel #10
0
 public virtual long compare(object obj)
 {
     return(FanStr.compare(toStr(), toStr(obj)));
 }
Beispiel #11
0
        static void doDumpStack(string msg, Exception err, int depth, StringWriter w)
        {
            // message
            for (int sp = 0; sp < depth; sp++)
            {
                w.Write(" ");
            }
            if (!(err is Err.Val) && msg == err.Message)
            {
                w.Write(err.GetType() + ": ");
            }
            w.WriteLine(msg);

            // stack
            string stack = err.StackTrace;

            if (err is Err.Val)
            {
                Err e = ((Err.Val)err).err();
                if (e.m_stack != null)
                {
                    stack = e.m_stack;
                }
            }
            if (stack != null)
            {
                string[] lines = stack.Split('\n');
                for (int i = 0; i < lines.Length; i++)
                {
                    // TODO - could be *way* more efficient

                    string s        = lines[i].Trim();
                    int    parOpen  = s.IndexOf('(');
                    int    parClose = s.IndexOf(')', parOpen);

                    string source = s.Substring(parClose + 1, s.Length - parClose - 1);
                    if (source == "")
                    {
                        source = "Unknown Source";
                    }
                    else
                    {
                        source = source.Substring(4);
                        int index = source.LastIndexOf("\\");
                        if (index != -1)
                        {
                            source = source.Substring(index + 1);
                        }
                        index  = source.LastIndexOf(":line");
                        source = source.Substring(0, index + 1) + source.Substring(index + 6);
                    }

                    string target = s.Substring(0, parOpen);
                    if (target.StartsWith("at Fan."))
                    {
                        int    a    = target.IndexOf(".", 7);
                        int    b    = target.IndexOf(".", a + 1);
                        string pod  = target.Substring(7, a - 7);
                        string type = target.Substring(a + 1, b - a - 1);
                        string meth = target.Substring(b + 1);

                        // check for closures
                        int dollar1 = type.IndexOf('$');
                        int dollar2 = dollar1 < 0 ? -1 : type.IndexOf('$', dollar1 + 1);
                        if (dollar2 > 0)
                        {
                            // don't print callX for closures
                            if (meth.StartsWith("call"))
                            {
                                continue;
                            }
                            // remap closure class back to original method
                            if (meth.StartsWith("doCall"))
                            {
                                meth = type.Substring(dollar1 + 1, dollar2 - dollar1 - 1);
                                type = type.Substring(0, dollar1);
                            }
                        }

                        target = FanStr.decapitalize(pod) + "::" + type + "." + meth;
                    }

                    for (int sp = 0; sp < depth; sp++)
                    {
                        w.Write(" ");
                    }
                    w.Write("  ");
                    w.Write(target);
                    w.Write(" (");
                    w.Write(source);
                    w.Write(")");
                    w.Write("\n");
                }
            }
            // inner exception
            Exception cause = err.InnerException;

            if (cause != null)
            {
                doDumpStack(msg, cause, depth + 1, w);
            }
        }
Beispiel #12
0
 public int GetHashCode(object obj)
 {
     return(FanStr.caseInsensitiveHash((string)obj));
 }
Beispiel #13
0
 public override long hash()
 {
     return(FanStr.hash(m_str));
 }
Beispiel #14
0
        //////////////////////////////////////////////////////////////////////////
        // Extension
        //////////////////////////////////////////////////////////////////////////

        public static MimeType forExt(string s)
        {
            if (s == null)
            {
                return(null);
            }
            try
            {
                string val = (string)Sys.m_sysPod.props(m_etcUri, Duration.m_oneMin).get(FanStr.lower(s));
                if (val == null)
                {
                    return(null);
                }
                return(MimeType.fromStr(val));
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("MimeType.forExt: " + s);
                Err.dumpStack(e);
                return(null);
            }
        }
Beispiel #15
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);
            }
        }
Beispiel #16
0
 public override long hash()
 {
     return(FanStr.hash(toStr()));
 }
Beispiel #17
0
 public new bool Equals(object x, object y)
 {
     return(FanStr.equalsIgnoreCase((string)x, (string)y));
 }