You've already forked opentelemetry-go
							
							
				mirror of
				https://github.com/open-telemetry/opentelemetry-go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	Use strings.Cut() instead of string.SplitN() (#4049)
strings.Cut() generates less garbage as it does not allocate the slice to hold parts.
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							8445f21305
						
					
				
				
					commit
					f95bee22b9
				
			| @@ -82,23 +82,23 @@ func constructOTResources(s string) (*Resource, error) { | ||||
| 		return Empty(), nil | ||||
| 	} | ||||
| 	pairs := strings.Split(s, ",") | ||||
| 	attrs := []attribute.KeyValue{} | ||||
| 	var attrs []attribute.KeyValue | ||||
| 	var invalid []string | ||||
| 	for _, p := range pairs { | ||||
| 		field := strings.SplitN(p, "=", 2) | ||||
| 		if len(field) != 2 { | ||||
| 		k, v, found := strings.Cut(p, "=") | ||||
| 		if !found { | ||||
| 			invalid = append(invalid, p) | ||||
| 			continue | ||||
| 		} | ||||
| 		k := strings.TrimSpace(field[0]) | ||||
| 		v, err := url.QueryUnescape(strings.TrimSpace(field[1])) | ||||
| 		key := strings.TrimSpace(k) | ||||
| 		val, err := url.QueryUnescape(strings.TrimSpace(v)) | ||||
| 		if err != nil { | ||||
| 			// Retain original value if decoding fails, otherwise it will be | ||||
| 			// an empty string. | ||||
| 			v = field[1] | ||||
| 			val = v | ||||
| 			otel.Handle(err) | ||||
| 		} | ||||
| 		attrs = append(attrs, attribute.String(k, v)) | ||||
| 		attrs = append(attrs, attribute.String(key, val)) | ||||
| 	} | ||||
| 	var err error | ||||
| 	if len(invalid) > 0 { | ||||
|   | ||||
| @@ -85,14 +85,14 @@ func skip(line string) bool { | ||||
| // parse attempts to split the provided line on the first '=' character, and then | ||||
| // sanitize each side of the split before returning them as a key-value pair. | ||||
| func parse(line string) (string, string, bool) { | ||||
| 	parts := strings.SplitN(line, "=", 2) | ||||
| 	k, v, found := strings.Cut(line, "=") | ||||
|  | ||||
| 	if len(parts) != 2 || len(parts[0]) == 0 { | ||||
| 	if !found || len(k) == 0 { | ||||
| 		return "", "", false | ||||
| 	} | ||||
|  | ||||
| 	key := strings.TrimSpace(parts[0]) | ||||
| 	value := unescape(unquote(strings.TrimSpace(parts[1]))) | ||||
| 	key := strings.TrimSpace(k) | ||||
| 	value := unescape(unquote(strings.TrimSpace(v))) | ||||
|  | ||||
| 	return key, value, true | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user