Skip to content

Commit

Permalink
feat: add function that escape \t to blank
Browse files Browse the repository at this point in the history
  • Loading branch information
lwpk110 committed May 14, 2024
1 parent 6c78cf4 commit 90f883d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/util/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package util

import (
"regexp"
"strings"
)

var reTab = regexp.MustCompile(`^\t+`)

func IndentTabToSpaces(code string, spaces int) string {
indentation := strings.Repeat(" ", spaces)
return reTab.ReplaceAllString(code, indentation)
}

func IndentTab4Spaces(code string) string {
return IndentTabToSpaces(code, 4)
}

func IndentTab2Spaces(code string) string {
return IndentTabToSpaces(code, 2)
}

var re2Spaces = regexp.MustCompile(`^` + strings.Repeat(" ", 2))
var re4Spaces = regexp.MustCompile(`^` + strings.Repeat(" ", 4))

func IndentSpacesToTab(code string, spaces int) string {
switch spaces {
case 2:
return re2Spaces.ReplaceAllString(code, "\t")
case 4:
return re4Spaces.ReplaceAllString(code, "\t")
default:
re := regexp.MustCompile(`^` + strings.Repeat(" ", spaces))
return re.ReplaceAllString(code, "\t")
}
}

func Indent4SpacesToTab(code string) string {
return IndentSpacesToTab(code, 4)
}

func Indent2SpacesToTab(code string) string {
return IndentSpacesToTab(code, 2)
}

0 comments on commit 90f883d

Please sign in to comment.