-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c41a1b7
commit ba174ce
Showing
5 changed files
with
79 additions
and
196 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
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
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
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 |
---|---|---|
@@ -1,79 +1,47 @@ | ||
package linecontent | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
lineContentMaxParseSize = 10000 | ||
contextLeftSizeLimit = 250 | ||
contextRightSizeLimit = 250 | ||
lineMaxParseSize = 10000 | ||
contextLeftSizeLimit = 250 | ||
contextRightSizeLimit = 250 | ||
) | ||
|
||
func GetLineContent(line, secret string) string { | ||
func GetLineContent(line, secret string) (string, error) { | ||
lineSize := len(line) | ||
if lineSize == 0 || len(secret) == 0 { | ||
return "" | ||
if lineSize == 0 { | ||
return "", fmt.Errorf("failed to get line content: line empty") | ||
} | ||
|
||
// Truncate line to max parse size before converting to runes | ||
shouldRemoveLastChars := false | ||
if lineSize > lineContentMaxParseSize { | ||
line = line[:lineContentMaxParseSize] | ||
shouldRemoveLastChars = true // to prevent issues when truncating a multibyte character in the middle | ||
if len(secret) == 0 { | ||
return "", fmt.Errorf("failed to get line content: secret empty") | ||
} | ||
|
||
// Convert line and secret to runes | ||
lineRunes, lineRunesSize := getLineRunes(line, shouldRemoveLastChars) | ||
secretRunes := []rune(secret) | ||
secretRunesSize := len(secretRunes) | ||
// Truncate lineContent to max size | ||
if lineSize > lineMaxParseSize { | ||
line = line[:lineMaxParseSize] | ||
lineSize = lineMaxParseSize | ||
} | ||
|
||
// Find the secret's position in the line (working with runes) | ||
secretStartIndex := indexOf(lineRunes, secretRunes, lineRunesSize, secretRunesSize) | ||
// Find the secret's position in the line | ||
secretStartIndex := strings.Index(line, secret) | ||
if secretStartIndex == -1 { | ||
// Secret not found, return truncated content based on context limits | ||
maxSize := contextLeftSizeLimit + contextRightSizeLimit | ||
if lineRunesSize < maxSize { | ||
return string(lineRunes) | ||
if lineSize < maxSize { | ||
return line, nil | ||
} | ||
return string(lineRunes[:maxSize]) | ||
return line[:maxSize], nil | ||
} | ||
|
||
// Calculate bounds for the result | ||
secretEndIndex := secretStartIndex + secretRunesSize | ||
secretEndIndex := secretStartIndex + len(secret) | ||
start := max(secretStartIndex-contextLeftSizeLimit, 0) | ||
end := min(secretEndIndex+contextRightSizeLimit, lineRunesSize) | ||
|
||
return string(lineRunes[start:end]) | ||
} | ||
|
||
func getLineRunes(line string, shouldRemoveLastChars bool) ([]rune, int) { | ||
lineRunes := []rune(line) | ||
lineRunesSize := len(lineRunes) | ||
if shouldRemoveLastChars { | ||
// A single rune can be up to 4 bytes in UTF-8 encoding. | ||
// If truncation occurs in the middle of a multibyte character, | ||
// it will leave a partial byte sequence, potentially consisting of | ||
// up to 3 bytes. Each of these remaining bytes will be treated | ||
// as an invalid character, displayed as a replacement character (�). | ||
// To prevent this, we adjust the rune count by removing the last | ||
// 3 runes, ensuring no partial characters are included. | ||
lineRunesSize -= 3 | ||
} | ||
return lineRunes[:lineRunesSize], lineRunesSize | ||
} | ||
end := min(secretEndIndex+contextRightSizeLimit, lineSize) | ||
|
||
func indexOf(line, secret []rune, lineSize, secretSize int) int { | ||
for i := 0; i <= lineSize-secretSize; i++ { | ||
if compareRunes(line[i:i+secretSize], secret) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func compareRunes(a, b []rune) bool { | ||
// a and b must have the same size. | ||
for i := range a { | ||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
return line[start:end], nil | ||
} |
Oops, something went wrong.