Beispiel #1
0
        private static string ComsumeQuoted(CsvSplitInfo info, StringEnumWithPeek readMe)
        {
            info.FieldExpected = false;

            var sb = new StringBuilder();

            sb.Append('"');

            while (readMe.MoveNext())
            {
                // null not possible with inside a MoveNext loop
                var item = readMe.Current.Value;
                if (item == '"')
                {
                    var nextItem = readMe.PeekNext();
                    if (nextItem == '"')
                    {
                        sb.Append('"');
                        readMe.MoveNext();
                        continue;
                    }
                    sb.Append('"');

                    return(sb.ToString());
                }

                sb.Append(item);
            }

            sb.Append('"');

            return(sb.ToString());
        }
Beispiel #2
0
        private static string ConsumeNoQuoted(CsvSplitInfo info, char starter, StringEnumWithPeek readMe)
        {
            info.FieldExpected = false;

            var sb = new StringBuilder();

            sb.Append(starter);

            while (readMe.MoveNext())
            {
                // null not possible with inside a MoveNext loop
                var item = readMe.Current.Value;
                if (item == ',')
                {
                    info.FieldExpected = true;

                    // Remove ending spaces on No Quoted Fields
                    return(sb.ToString().Trim());
                }

                sb.Append(item);
            }

            // Remove ending spaces on No Quoted Fields
            return(sb.ToString().Trim());
        }
Beispiel #3
0
 private static void GoToNextItem(CsvSplitInfo info, StringEnumWithPeek readMe)
 {
     while (readMe.MoveNext())
     {
         // null not possible with inside a MoveNext loop
         var item = readMe.Current.Value;
         if (item == ',')
         {
             info.FieldExpected = true;
             return;
         }
     }
 }
Beispiel #4
0
        public static List <string> Split(string text)
        {
            var result = new List <string>();

            var readMe = new StringEnumWithPeek(text);

            var info = new CsvSplitInfo();

            info.FieldExpected = false;

            while (readMe.MoveNext())
            {
                // null not possible with inside a MoveNext loop
                var item = readMe.Current.Value;

                if (item == ' ')
                {
                    continue;
                }

                if (item == ',')
                {
                    result.Add(string.Empty);
                    info.FieldExpected = true;
                    continue;
                }

                if (item == '"')
                {
                    var quotedItem = ComsumeQuoted(info, readMe);
                    result.Add(quotedItem);
                    GoToNextItem(info, readMe);
                    continue;
                }

                var unquotedItem = ConsumeNoQuoted(info, item, readMe);
                result.Add(unquotedItem);
            }

            // ending comma situation
            if (info.FieldExpected)
            {
                result.Add(string.Empty);
            }

            return(result);
        }