-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function that escape
\t
to blank
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |