From 03eddbde0c6e783cfdd2540201d4281b8ac4a03f Mon Sep 17 00:00:00 2001 From: Amy Boyd Date: Mon, 4 Jul 2016 13:49:43 +0100 Subject: [PATCH] Move reusable regexp constants to utils.go. --- editorconfig/full_file_checkers.go | 7 ------- editorconfig/line_checkers.go | 8 -------- editorconfig/utils.go | 13 +++++++++++++ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/editorconfig/full_file_checkers.go b/editorconfig/full_file_checkers.go index 8d28c6c..6f1e35c 100644 --- a/editorconfig/full_file_checkers.go +++ b/editorconfig/full_file_checkers.go @@ -2,7 +2,6 @@ package editorconfig import ( "github.com/saintfish/chardet" - "regexp" "strings" ) @@ -20,10 +19,6 @@ type FullFileCheckResult struct { messageIfNotOk string } -var lfRegexp = regexp.MustCompile(`\n`) -var crRegexp = regexp.MustCompile(`\r`) -var crlfRegexp = regexp.MustCompile(`\r\n`) - func CheckEndOfLineRule(ruleValue string, fileContent string) *FullFileCheckResult { // Valid rules values are "lf", "cr", or "crlf". The values are case insensitive. ruleValueLowercase := strings.ToLower(ruleValue) @@ -63,8 +58,6 @@ func CheckEndOfLineRule(ruleValue string, fileContent string) *FullFileCheckResu return &FullFileCheckResult{isOk: true} } -var endsWithFinalNewLineRegexp = regexp.MustCompile(`(\n|\r|\r\n)$`) - func CheckInsertFinalNewLineRule(ruleValue string, fileContent string) *FullFileCheckResult { // Valid rules values are "true" or "false". The values are case insensitive. ruleValueLowercase := strings.ToLower(ruleValue) diff --git a/editorconfig/line_checkers.go b/editorconfig/line_checkers.go index 3f5143e..b6a9c18 100644 --- a/editorconfig/line_checkers.go +++ b/editorconfig/line_checkers.go @@ -1,7 +1,6 @@ package editorconfig import ( - "regexp" "strconv" "strings" ) @@ -23,13 +22,6 @@ type LineCheckResult struct { messageIfNotOk string } -var hasIndentationRegexp = regexp.MustCompile(`^[\t ]`) -var hasNoIndentationRegexp = regexp.MustCompile(`^([^\t ]|$)`) -var indentedWithMixedTabsAndSpacesRegexp = regexp.MustCompile(`^(\t+ +| +\t+)`) -var indentedWithTabsRegexp = regexp.MustCompile(`^\t+`) -var indentedWithTabsThenCommentLineRegexp = regexp.MustCompile(`^\t+ \*`) -var indentedWithSpacesRegexp = regexp.MustCompile(`^ +`) - func HasIndentation(s string) bool { return hasIndentationRegexp.MatchString(s) } diff --git a/editorconfig/utils.go b/editorconfig/utils.go index 219b0e7..fefd1a2 100644 --- a/editorconfig/utils.go +++ b/editorconfig/utils.go @@ -11,6 +11,19 @@ var filePathSeparatorRegex = regexp.QuoteMeta(string(filepath.Separator)) var endOfPathRegex, _ = regexp.Compile(`[` + filePathSeparatorRegex + `][^` + filePathSeparatorRegex + `]+$`) +var lfRegexp = regexp.MustCompile(`\n`) +var crRegexp = regexp.MustCompile(`\r`) +var crlfRegexp = regexp.MustCompile(`\r\n`) + +var endsWithFinalNewLineRegexp = regexp.MustCompile(`(\n|\r|\r\n)$`) + +var hasIndentationRegexp = regexp.MustCompile(`^[\t ]`) +var hasNoIndentationRegexp = regexp.MustCompile(`^([^\t ]|$)`) +var indentedWithMixedTabsAndSpacesRegexp = regexp.MustCompile(`^(\t+ +| +\t+)`) +var indentedWithTabsRegexp = regexp.MustCompile(`^\t+`) +var indentedWithTabsThenCommentLineRegexp = regexp.MustCompile(`^\t+ \*`) +var indentedWithSpacesRegexp = regexp.MustCompile(`^ +`) + func GetParentDir(path string) string { return endOfPathRegex.ReplaceAllString(path, "") }