Skip to content

Instantly share code, notes, and snippets.

@t-eckert
Last active November 21, 2022 16:11
Show Gist options
  • Save t-eckert/ff9f91c8245042556c31fd8f176ff7a5 to your computer and use it in GitHub Desktop.
Save t-eckert/ff9f91c8245042556c31fd8f176ff7a5 to your computer and use it in GitHub Desktop.

Revisions

  1. Thomas Eckert revised this gist Nov 21, 2022. No changes.
  2. Thomas Eckert created this gist Nov 21, 2022.
    32 changes: 32 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    package main

    import (
    "fmt"
    "os"
    "strings"
    )

    func main() {
    in := os.Args[1]
    formatted := format(in)
    fmt.Print(formatted)
    }

    func format(slashes string) string {
    formatted := ""

    indent := 0
    for _, slash := range slashes {
    if slash == '/' {
    if indent > 0 {
    indent--
    }
    }
    formatted += fmt.Sprintf("%s%c\n", strings.Repeat(" ", indent), slash)
    if slash == '\\' {
    indent++
    }
    }

    return formatted
    }
    28 changes: 28 additions & 0 deletions main_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    package main

    import "testing"

    func TestFormat(t *testing.T) {
    cases := []struct {
    in string
    expected string
    }{
    {
    in: `\\\//\/\\`,
    expected: "\\\n \\\n \\\n /\n /\n \\\n /\n \\\n \\\n",
    },
    {
    in: `\\\\`,
    expected: "\\\n \\\n \\\n \\\n",
    },
    }

    for _, tc := range cases {
    t.Run(tc.in, func(t *testing.T) {
    actual := format(tc.in)
    if tc.expected != actual {
    t.Fail()
    }
    })
    }
    }