mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(common): Improve string parser
This commit is contained in:
parent
41239654c8
commit
b7120f3a58
@ -1,13 +1,17 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
type ParseFeatures struct {
|
type ParseFeatures struct {
|
||||||
ParseDoubleQuotes bool
|
ParseDoubleQuotes bool
|
||||||
ParseEscapedCharacters bool
|
ParseEscapedCharacters bool
|
||||||
|
Replacements *map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
var FullFeatures = ParseFeatures{
|
var FullFeatures = ParseFeatures{
|
||||||
ParseDoubleQuotes: true,
|
ParseDoubleQuotes: true,
|
||||||
ParseEscapedCharacters: true,
|
ParseEscapedCharacters: true,
|
||||||
|
Replacements: &map[string]string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParsedString struct {
|
type ParsedString struct {
|
||||||
@ -21,6 +25,10 @@ func ParseRawString(
|
|||||||
) ParsedString {
|
) ParsedString {
|
||||||
value := raw
|
value := raw
|
||||||
|
|
||||||
|
if len(*features.Replacements) > 0 {
|
||||||
|
value = ParseReplacements(value, *features.Replacements)
|
||||||
|
}
|
||||||
|
|
||||||
// Parse double quotes
|
// Parse double quotes
|
||||||
if features.ParseDoubleQuotes {
|
if features.ParseDoubleQuotes {
|
||||||
value = ParseDoubleQuotes(value)
|
value = ParseDoubleQuotes(value)
|
||||||
@ -85,6 +93,19 @@ func ParseEscapedCharacters(
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseReplacements(
|
||||||
|
raw string,
|
||||||
|
replacements map[string]string,
|
||||||
|
) string {
|
||||||
|
value := raw
|
||||||
|
|
||||||
|
for key, replacement := range replacements {
|
||||||
|
value = strings.ReplaceAll(value, key, replacement)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
func modifyString(
|
func modifyString(
|
||||||
input string,
|
input string,
|
||||||
start int,
|
start int,
|
||||||
|
@ -165,3 +165,25 @@ func TestStringsIncompleteQuotes3FullFeatures(
|
|||||||
t.Errorf("Expected %v, got %v", expected, actual)
|
t.Errorf("Expected %v, got %v", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringsReplacements(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
input := `Hello\\040World`
|
||||||
|
expected := ParsedString{
|
||||||
|
Raw: input,
|
||||||
|
Value: `Hello World`,
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := ParseRawString(input, ParseFeatures{
|
||||||
|
ParseDoubleQuotes: true,
|
||||||
|
ParseEscapedCharacters: true,
|
||||||
|
Replacements: &map[string]string{
|
||||||
|
`\\040`: " ",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if !(cmp.Equal(expected, actual)) {
|
||||||
|
t.Errorf("Expected %v, got %v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user