예제 #1
0
        //public static string[] Split(string path)
        //{
        //    if (path == null)
        //        return null;
        //    if (path.Length == 0)
        //        return new string[] { };
        //    Regex r = new Regex(Energy.Base.Expression.PathSplitCapture, RegexOptions.IgnorePatternWhitespace);
        //    Match m = r.Match(path);
        //    if (!m.Success)
        //        return new string[] { };
        //    List<string> l = new List<string>();
        //    for (int n = 1; n < m.Groups.Count; n++)
        //    {
        //        for (int i = 0; i < m.Groups[n].Captures.Count; i++)
        //        {
        //            l.Add(m.Groups[n].Captures[i].Value);
        //        }
        //    }
        //    return l.ToArray();
        //}

        /// <summary>
        /// Split path into parts.
        /// Each part will contain trailing directory separator characters.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string[] Split(string path, SplitFormat format)
        {
            if (null == path)
            {
                return(null);
            }
            if (0 == path.Length)
            {
                return(new string[] { });
            }
            if (format == null)
            {
                format = SplitFormat.Default;
            }

            string[] slashes        = format.Slashes ?? new string[] { };
            string[] quotes         = format.Quotes ?? new string[] { };
            bool     optionDoublets = (bool)(format.Doublets ?? false);
            bool     optionCStyle   = (bool)(format.CStyle ?? false);

            List <string> list = new List <string>();

            string pattern = BuildSplitPattern(slashes, quotes, optionDoublets, optionCStyle);

            Match match = Regex.Match(path, pattern);

            while (match.Success)
            {
                if (false)
                {
                }
                else if (0 < match.Groups["white"].Length)
                {
                    // ignore whitespace //
                }
                else if (0 < match.Groups["slash"].Length)
                {
                    if (0 == list.Count)
                    {
                        list.Add(match.Groups["slash"].Value);
                    }
                    else
                    {
                        list[-1 + list.Count] += match.Groups["slash"].Value;
                    }
                }
                else if (0 < match.Groups["item"].Length)
                {
                    list.Add(match.Groups["item"].Value);
                }
                match = match.NextMatch();
            }

            return(list.ToArray());
        }
예제 #2
0
 public static IEnumerable <string> Each(string path, SplitFormat format)
 {
     if (null == path)
     {
         yield break;
     }
     string[] array = Energy.Base.Path.Split(path, format);
     for (int i = 0, length = array.Length; i < length; i++)
     {
         yield return(array[i]);
     }
 }
예제 #3
0
            public static SplitFormat Create(string[] slashes, string[] quotes, bool?doublets, bool?cstyle)
            {
                SplitFormat o = new SplitFormat()
                {
                    Slashes  = slashes,
                    Quotes   = quotes,
                    Doublets = doublets,
                    CStyle   = cstyle
                };

                return(o);
            }
예제 #4
0
            public static SplitFormat Create(string slashes, string quotes, bool?doublets, bool?cstyle)
            {
                char[]   charsSlashes = slashes.ToCharArray();
                string[] arraySlashes = new List <char>(charsSlashes)
                                        .ConvertAll <string>(delegate(char c) { return(new string(c, 1)); })
                                        .ToArray();
                char[]   charsQuotes = quotes.ToCharArray();
                string[] arrayQuotes = new List <char>(charsQuotes)
                                       .ConvertAll <string>(delegate(char c) { return(new string(c, 1)); })
                                       .ToArray();
                SplitFormat o = new SplitFormat()
                {
                    Slashes  = arraySlashes,
                    Quotes   = arrayQuotes,
                    Doublets = doublets,
                    CStyle   = cstyle
                };

                return(o);
            }