public static String[] Split(this string s, char[] separator, int count, StringSplitOptions options)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Count cannot be less than zero.");
            }
            if ((options != StringSplitOptions.None) && (options != StringSplitOptions.RemoveEmptyEntries))
            {
                throw new ArgumentException("Illegal enum value: " + options + ".");
            }

            if (s.Length == 0 && (options & StringSplitOptions.RemoveEmptyEntries) != 0)
            {
                return(ArrayEx.Empty <string> ());
            }

            if (count <= 1)
            {
                return(count == 0
                                        ? ArrayEx.Empty <string> ()
                                        : new[] { s });
            }

            return(s.SplitByCharacters(separator, count, options != 0));
        }
Example #2
0
        public static object CreateInstance(Type type, bool nonPublic)
        {
            CType ct = type;

            if (!nonPublic)
            {
                return(Crestron.SimplSharp.Reflection.Activator.CreateInstance(ct));
            }

            var ci = ct.GetConstructor(BfNonPublic, null, ArrayEx.Empty <CType> (), null);

            if (ci == null)
            {
                throw new MissingMethodException();
            }

            return(ci.Invoke(null));
        }
        public static String[] Split(this string s, string[] separator, int count, StringSplitOptions options)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Count cannot be less than zero.");
            }
            if ((options != StringSplitOptions.None) && (options != StringSplitOptions.RemoveEmptyEntries))
            {
                throw new ArgumentException("Illegal enum value: " + options + ".");
            }

            if (count <= 1)
            {
                return(count == 0
                                        ? ArrayEx.Empty <string> ()
                                        : new[] { s });
            }

            bool removeEmpty = (options & StringSplitOptions.RemoveEmptyEntries) != 0;

            if (separator == null || separator.Length == 0)
            {
                return(s.SplitByCharacters(null, count, removeEmpty));
            }

            if (s.Length == 0 && removeEmpty)
            {
                return(ArrayEx.Empty <string> ());
            }

            var arr = new List <String> ();

            int pos        = 0;
            int matchCount = 0;

            while (pos < s.Length)
            {
                int matchIndex = -1;
                int matchPos   = Int32.MaxValue;

                // Find the first position where any of the separators matches
                for (int i = 0; i < separator.Length; ++i)
                {
                    string sep = separator[i];
                    if (string.IsNullOrEmpty(sep))
                    {
                        continue;
                    }

                    int match = s.IndexOfOrdinalUnchecked(sep, pos, s.Length - pos);
                    if (match >= 0 && match < matchPos)
                    {
                        matchIndex = i;
                        matchPos   = match;
                    }
                }

                if (matchIndex == -1)
                {
                    break;
                }

                if (!(matchPos == pos && removeEmpty))
                {
                    if (arr.Count == count - 1)
                    {
                        break;
                    }
                    arr.Add(s.Substring(pos, matchPos - pos));
                }

                pos = matchPos + separator[matchIndex].Length;

                matchCount++;
            }

            if (matchCount == 0)
            {
                return new[] { s }
            }
            ;

            // string contained only separators
            if (removeEmpty && matchCount != 0 && pos == s.Length && arr.Count == 0)
            {
                return(ArrayEx.Empty <string> ());
            }

            if (!(removeEmpty && pos == s.Length))
            {
                arr.Add(s.Substring(pos));
            }

            return(arr.ToArray());
        }