-
Notifications
You must be signed in to change notification settings - Fork 0
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
1f8345a
commit 7f47aa0
Showing
3 changed files
with
143 additions
and
35 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package textdic | ||
|
||
import ( | ||
"backend/pkg/logger" | ||
"encoding/base64" | ||
"fmt" | ||
"golang.org/x/net/context" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// textDictionaryService struct definition | ||
type textDictionaryService struct{} | ||
|
||
// NewTextDictionaryService creates and returns a new instance of textDictionaryService | ||
func NewTextDictionaryService() *textDictionaryService { | ||
return &textDictionaryService{} | ||
} | ||
|
||
// Process processes a given dictionary string and returns the parsed Nodes or an error | ||
func (tds *textDictionaryService) Process(ctx context.Context, dic string) ([]Node, error) { | ||
|
||
// Use the new parser to parse the input | ||
l := newLexer(dic) | ||
|
||
// Parse the input using the new parser | ||
parsedNodes := ParseAndGetNodes(l) | ||
|
||
if len(parsedNodes) == 0 { | ||
err := fmt.Errorf("no nodes were parsed") | ||
logger.Logger.ErrorContext(ctx, err.Error()) | ||
return nil, err | ||
} | ||
|
||
return parsedNodes, nil | ||
} | ||
|
||
// isBase64 checks if the input string is a valid Base64 encoded string | ||
func (tds *textDictionaryService) isBase64(s string) bool { | ||
|
||
// Base64 strings are generally divisible by 4 | ||
if len(s)%4 != 0 { | ||
return false | ||
} | ||
|
||
// Check if the string contains only valid Base64 characters | ||
for _, r := range s { | ||
if !(unicode.IsLetter(r) || unicode.IsDigit(r) || strings.ContainsRune("+/=", r)) { | ||
return false | ||
} | ||
} | ||
|
||
// Decode to verify it is actually Base64 encoded | ||
_, err := base64.StdEncoding.DecodeString(s) | ||
return err == nil | ||
} | ||
|
||
// decodeBase64 decodes a Base64 encoded string | ||
func (tds *textDictionaryService) decodeBase64(s string) (string, error) { | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(s) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(decoded), nil | ||
} |
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