Git Product home page Git Product logo

Comments (2)

WeihanLi avatar WeihanLi commented on May 29, 2024

https://github.com/WeihanLi/WeihanLi.Npoi/blob/c1215adc1fe47554d9ef7f996117bb45ad067dc4/src/WeihanLi.Npoi/CsvHelper.cs#L493

public static IEnumerable<string> ParseLine(string line)
{
    if (string.IsNullOrEmpty(line))
    {
        yield break;
    }

    var tokenBuilder = new StringBuilder();

    var inToken= false;
    var inQuotes = false;

    // Iterate through every character in the line
    for (var i = 0; i < line.Length; i++)
    {
        var character = line[i];

        // If we are not currently inside a column
        if (!inToken)
        {
            // If the current character is a quote then the token value is contained within
            // quotes, otherwise append the next character
            inToken = true;
            if (character == '\'')
            {
                inQuotes = true;
                continue;
            }
        }

        // If we are in between quotes
        if (inQuotes)
        {
            if (i + 1 == line.Length)
            {
                break;
            }

            if (character == '\'' && line[i + 1] == ' ') // quotes end
            {
                inQuotes = false;
                inToken = false;
                i++; //skip next
            }
            else if (character == '\'' && line[i + 1] == '\'') // quotes
            {
                i++; //skip next
            }
            else if (character == '\'')
            {
                throw new ArgumentException($"unable to escape {line}");
            }
        }
        else if (character == ' ')
        {
            inToken = false;
        }

        // If we are no longer in the token clear the builder and add the columns to the list
        if (!inToken)
        {
            if (tokenBuilder.Length > 0)
            {
                yield return tokenBuilder.ToString();
                tokenBuilder.Clear();
            }
        }
        else
        {
            tokenBuilder.Append(character);
        }
    }

    if (tokenBuilder.Length > 0)
    {
        yield return tokenBuilder.ToString();
        tokenBuilder.Clear();
    }
}

from weihanli.common.

WeihanLi avatar WeihanLi commented on May 29, 2024

https://github.com/WeihanLi/WeihanLi.Common/tree/1.0.60/src/WeihanLi.Common/Helpers/LineParser.cs

from weihanli.common.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.