Quellcode durchsuchen

refactor(*): avoid using strings.lowercase

Use equalFold for better performance when comparing strings with case-insensitive

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang vor 3 Jahren
Ursprung
Commit
b317bcc0b5

+ 2 - 2
cmd/kuiper/main.go

@@ -110,7 +110,7 @@ func main() {
 					// convert CRLF to LF
 					// convert CRLF to LF
 					text = strings.Replace(text, "\n", "", -1)
 					text = strings.Replace(text, "\n", "", -1)
 
 
-					if strings.ToLower(text) == "quit" || strings.ToLower(text) == "exit" {
+					if strings.EqualFold(text, "quit") || strings.EqualFold(text, "exit") {
 						break
 						break
 					} else if strings.Trim(text, " ") == "" {
 					} else if strings.Trim(text, " ") == "" {
 						continue
 						continue
@@ -834,7 +834,7 @@ func main() {
 							return nil
 							return nil
 						}
 						}
 						ptype := c.Args()[0]
 						ptype := c.Args()[0]
-						if strings.ToLower(ptype) != "function" {
+						if !strings.EqualFold(ptype, "function") {
 							fmt.Printf("Plugin type must be function.\n")
 							fmt.Printf("Plugin type must be function.\n")
 							return nil
 							return nil
 						}
 						}

+ 1 - 1
extensions/sources/random/random.go

@@ -60,7 +60,7 @@ func (s *randomSource) Configure(topic string, props map[string]interface{}) err
 	if cfg.Seed <= 0 {
 	if cfg.Seed <= 0 {
 		return fmt.Errorf("source `random` property `seed` must be a positive integer but got %d", cfg.Seed)
 		return fmt.Errorf("source `random` property `seed` must be a positive integer but got %d", cfg.Seed)
 	}
 	}
-	if strings.ToLower(cfg.Format) != message.FormatJson {
+	if !strings.EqualFold(cfg.Format, message.FormatJson) {
 		return fmt.Errorf("random source only supports `json` format")
 		return fmt.Errorf("random source only supports `json` format")
 	}
 	}
 	s.conf = cfg
 	s.conf = cfg

+ 15 - 1
internal/conf/load.go

@@ -1,3 +1,17 @@
+// Copyright 2021 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 // INTECH Process Automation Ltd.
 // INTECH Process Automation Ltd.
 //
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // Licensed under the Apache License, Version 2.0 (the "License");
@@ -201,7 +215,7 @@ func applyKey(m map[string]interface{}, key string) {
 		if casted, ok := v.(map[string]interface{}); ok {
 		if casted, ok := v.(map[string]interface{}); ok {
 			applyKey(casted, key)
 			applyKey(casted, key)
 		}
 		}
-		if key != k && strings.ToLower(key) == k {
+		if key != k && strings.EqualFold(key, k) {
 			m[key] = v
 			m[key] = v
 			delete(m, k)
 			delete(m, k)
 		}
 		}

+ 15 - 1
internal/pkg/jwt/rsa_keys.go

@@ -1,3 +1,17 @@
+// Copyright 2021 EMQ Technologies Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 package jwt
 package jwt
 
 
 import (
 import (
@@ -58,7 +72,7 @@ func insensitiveGetFilePath(prikeyName string) (string, error) {
 
 
 	for _, info := range infos {
 	for _, info := range infos {
 		fileName := info.Name()
 		fileName := info.Name()
-		if strings.ToLower(fileName) == strings.ToLower(prikeyName) {
+		if strings.EqualFold(fileName, prikeyName) {
 			filePath := path.Join(dir, fileName)
 			filePath := path.Join(dir, fileName)
 			return filePath, nil
 			return filePath, nil
 		}
 		}

+ 1 - 1
internal/server/rest.go

@@ -631,7 +631,7 @@ func prebuildFuncsPlugins(w http.ResponseWriter, r *http.Request) {
 }
 }
 
 
 func isOffcialDockerImage() bool {
 func isOffcialDockerImage() bool {
-	if strings.ToLower(os.Getenv("MAINTAINER")) != "emqx.io" {
+	if !strings.EqualFold(os.Getenv("MAINTAINER"), "emqx.io") {
 		return false
 		return false
 	}
 	}
 	return true
 	return true

+ 2 - 2
internal/service/manager.go

@@ -375,11 +375,11 @@ func (m *Manager) unzip(name, src string) error {
 		return err
 		return err
 	}
 	}
 	defer r.Close()
 	defer r.Close()
-	baseName := strings.ToLower(name + ".json")
+	baseName := name + ".json"
 	// Try unzip
 	// Try unzip
 	found := false
 	found := false
 	for _, file := range r.File {
 	for _, file := range r.File {
-		if strings.ToLower(file.Name) == baseName {
+		if strings.EqualFold(file.Name, baseName) {
 			found = true
 			found = true
 			break
 			break
 		}
 		}

+ 9 - 6
internal/topo/planner/analyzer.go

@@ -177,21 +177,23 @@ func newFieldsMap(isSchemaless bool, defaultStream ast.StreamName) *fieldsMap {
 }
 }
 
 
 func (f *fieldsMap) reserve(fieldName string, streamName ast.StreamName) {
 func (f *fieldsMap) reserve(fieldName string, streamName ast.StreamName) {
-	if fm, ok := f.content[strings.ToLower(fieldName)]; ok {
+	lname := strings.ToLower(fieldName)
+	if fm, ok := f.content[lname]; ok {
 		fm.add(streamName)
 		fm.add(streamName)
 	} else {
 	} else {
 		fm := newStreamFieldStore(f.isSchemaless, f.defaultStream)
 		fm := newStreamFieldStore(f.isSchemaless, f.defaultStream)
 		fm.add(streamName)
 		fm.add(streamName)
-		f.content[strings.ToLower(fieldName)] = fm
+		f.content[lname] = fm
 	}
 	}
 }
 }
 
 
 func (f *fieldsMap) save(fieldName string, streamName ast.StreamName, field *ast.AliasRef) error {
 func (f *fieldsMap) save(fieldName string, streamName ast.StreamName, field *ast.AliasRef) error {
-	fm, ok := f.content[strings.ToLower(fieldName)]
+	lname := strings.ToLower(fieldName)
+	fm, ok := f.content[lname]
 	if !ok {
 	if !ok {
 		if streamName == ast.AliasStream || f.isSchemaless {
 		if streamName == ast.AliasStream || f.isSchemaless {
 			fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
 			fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
-			f.content[strings.ToLower(fieldName)] = fm
+			f.content[lname] = fm
 		} else {
 		} else {
 			return fmt.Errorf("unknown field %s", fieldName)
 			return fmt.Errorf("unknown field %s", fieldName)
 		}
 		}
@@ -204,11 +206,12 @@ func (f *fieldsMap) save(fieldName string, streamName ast.StreamName, field *ast
 }
 }
 
 
 func (f *fieldsMap) bind(fr *ast.FieldRef) error {
 func (f *fieldsMap) bind(fr *ast.FieldRef) error {
-	fm, ok := f.content[strings.ToLower(fr.Name)]
+	lname := strings.ToLower(fr.Name)
+	fm, ok := f.content[lname]
 	if !ok {
 	if !ok {
 		if f.isSchemaless && fr.Name != "" {
 		if f.isSchemaless && fr.Name != "" {
 			fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
 			fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
-			f.content[strings.ToLower(fr.Name)] = fm
+			f.content[lname] = fm
 		} else {
 		} else {
 			return fmt.Errorf("unknown field %s", fr.Name)
 			return fmt.Errorf("unknown field %s", fr.Name)
 		}
 		}

+ 1 - 1
internal/topo/planner/dataSourcePlan.go

@@ -205,7 +205,7 @@ func (p *DataSourcePlan) getProps() error {
 	if p.streamStmt.Options.TIMESTAMP_FORMAT != "" {
 	if p.streamStmt.Options.TIMESTAMP_FORMAT != "" {
 		p.timestampFormat = p.streamStmt.Options.TIMESTAMP_FORMAT
 		p.timestampFormat = p.streamStmt.Options.TIMESTAMP_FORMAT
 	}
 	}
-	if strings.ToLower(p.streamStmt.Options.FORMAT) == message.FormatBinary {
+	if strings.EqualFold(p.streamStmt.Options.FORMAT, message.FormatBinary) {
 		p.isBinary = true
 		p.isBinary = true
 	}
 	}
 	return nil
 	return nil

+ 2 - 2
internal/topo/source/edgex_source.go

@@ -105,7 +105,7 @@ func (es *EdgexSource) Configure(_ string, props map[string]interface{}) error {
 func printConf(mbconf types.MessageBusConfig) {
 func printConf(mbconf types.MessageBusConfig) {
 	var printableOptional = make(map[string]string)
 	var printableOptional = make(map[string]string)
 	for k, v := range mbconf.Optional {
 	for k, v := range mbconf.Optional {
-		if strings.ToLower(k) == "password" {
+		if strings.EqualFold(k, "password") {
 			printableOptional[k] = "*"
 			printableOptional[k] = "*"
 		} else {
 		} else {
 			printableOptional[k] = v
 			printableOptional[k] = v
@@ -166,7 +166,7 @@ func (es *EdgexSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTup
 				if !ok { // the source is closed
 				if !ok { // the source is closed
 					return
 					return
 				}
 				}
-				if strings.ToLower(env.ContentType) == "application/json" {
+				if strings.EqualFold(env.ContentType, "application/json") {
 					var r interface{}
 					var r interface{}
 					switch es.messageType {
 					switch es.messageType {
 					case MessageTypeEvent:
 					case MessageTypeEvent:

+ 2 - 2
internal/topo/source/mqtt_type.go

@@ -53,7 +53,7 @@ func (this *device) getName() string {
 }
 }
 func (this *device) findDataType(name string) string {
 func (this *device) findDataType(name string) string {
 	for _, v := range this.Properties {
 	for _, v := range this.Properties {
-		if strings.ToLower(v.getName()) == strings.ToLower(name) {
+		if strings.EqualFold(v.getName(), name) {
 			return v.getDataType()
 			return v.getDataType()
 		}
 		}
 	}
 	}
@@ -61,7 +61,7 @@ func (this *device) findDataType(name string) string {
 }
 }
 func (this *deviceModel) findDataType(deviceId, dataName string) string {
 func (this *deviceModel) findDataType(deviceId, dataName string) string {
 	for _, v := range this.Devices {
 	for _, v := range this.Devices {
-		if strings.ToLower(v.getName()) == strings.ToLower(deviceId) {
+		if strings.EqualFold(v.getName(), deviceId) {
 			return v.findDataType(dataName)
 			return v.findDataType(dataName)
 		}
 		}
 	}
 	}

+ 1 - 1
sdk/go/example/mirror/random_source.go

@@ -59,7 +59,7 @@ func (s *randomSource) Configure(_ string, props map[string]interface{}) error {
 	if cfg.Seed <= 0 {
 	if cfg.Seed <= 0 {
 		return fmt.Errorf("source `random` property `seed` must be a positive integer but got %d", cfg.Seed)
 		return fmt.Errorf("source `random` property `seed` must be a positive integer but got %d", cfg.Seed)
 	}
 	}
-	if strings.ToLower(cfg.Format) != "json" {
+	if !strings.EqualFold(cfg.Format, "json") {
 		return fmt.Errorf("random source only supports `json` format")
 		return fmt.Errorf("random source only supports `json` format")
 	}
 	}
 	s.conf = cfg
 	s.conf = cfg