Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change WriterTo to make a builder first #34

Open
Koeng101 opened this issue Dec 20, 2023 · 1 comment
Open

Change WriterTo to make a builder first #34

Koeng101 opened this issue Dec 20, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@Koeng101
Copy link
Owner

Here is an example written by ChatGPT:

func writeHD(w io.Writer, hd map[string]string) error {
    // Define the order of specific keys
    orderedKeys := []string{"VN", "SO", "GO", "SS"}

    // String builder to accumulate the output
    var sb strings.Builder

    // Write specific keys first if they exist
    for _, key := range orderedKeys {
        if value, exists := hd[key]; exists {
            sb.WriteString(fmt.Sprintf("%s:%s\t", key, value))
        }
    }

    // Write the remaining key-value pairs
    for key, value := range hd {
        // Skip if the key is one of the specific keys
        if key == "VN" || key == "SO" || key == "GO" || key == "SS" {
            continue
        }
        sb.WriteString(fmt.Sprintf("%s:%s\t", key, value))
    }

    // Write to the io.Writer
    _, err := w.Write([]byte(sb.String()))
    return err
}

Basically, accumulate the output in a builder before writing out to the io.Writer. This has advantages, since writing more bytes at once will likely be more efficient than writing super often. It also decreases the amount of tests needed.

This should be implemented across all parsers in a single PR.

@Koeng101 Koeng101 added the enhancement New feature or request label Dec 20, 2023
Copy link

This issue has had no activity in the past 2 months. Marking as stale.

@github-actions github-actions bot added the stale label Feb 18, 2024
@Koeng101 Koeng101 removed the stale label Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant