Gisle Aune
6 years ago
3 changed files with 90 additions and 12 deletions
@ -0,0 +1,23 @@ |
|||
package util |
|||
|
|||
import ( |
|||
"strings" |
|||
) |
|||
|
|||
// RemoveNPCAttribution removes NPC attribution.
|
|||
func RemoveNPCAttribution(str string) string { |
|||
lastOpen := strings.LastIndex(str, "(") |
|||
lastClose := strings.LastIndex(str, ")") |
|||
ending := len(str) - 1 |
|||
|
|||
if lastOpen > 0 && str[lastOpen-1] != '(' && (lastClose == -1 || lastClose == ending) { |
|||
cutoff := lastOpen |
|||
for cutoff > 1 && str[cutoff-1] == ' ' { |
|||
cutoff-- |
|||
} |
|||
|
|||
return str[:cutoff] |
|||
} |
|||
|
|||
return str |
|||
} |
@ -0,0 +1,44 @@ |
|||
package util_test |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
|
|||
"git.aiterp.net/rpdata/logbot3/internal/util" |
|||
) |
|||
|
|||
func TestRemoveNPCAttribution(t *testing.T) { |
|||
table := []struct { |
|||
Input string |
|||
Output string |
|||
}{ |
|||
{ |
|||
Input: "Character does stuff. (Player_Name)", |
|||
Output: "Character does stuff.", |
|||
}, |
|||
{ |
|||
Input: "Text (with some clarification) about stuff.", |
|||
Output: "Text (with some clarification) about stuff.", |
|||
}, |
|||
{ |
|||
Input: "Text without anything.", |
|||
Output: "Text without anything.", |
|||
}, |
|||
{ |
|||
Input: "((*Correction))", |
|||
Output: "((*Correction))", |
|||
}, |
|||
} |
|||
|
|||
for i, row := range table { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
result := util.RemoveNPCAttribution(row.Input) |
|||
|
|||
if result != row.Output { |
|||
t.Errorf("Row %d failed", i) |
|||
t.Errorf("Expected: %#+v", row.Output) |
|||
t.Errorf("Result: %#+v", result) |
|||
} |
|||
}) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue