encodedText.go 654 B

123456789101112131415161718192021222324252627282930
  1. package weirdtext
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type EncodedText struct {
  7. Text string
  8. EncodedWords []string
  9. }
  10. func (e EncodedText) String() string {
  11. return fmt.Sprintf("\n---weird---\n%s\n---weird---\n%v", e.Text, strings.Join(e.EncodedWords, " "))
  12. }
  13. func (e *EncodedText) FromString(serialized string) error {
  14. if !strings.HasPrefix(serialized, "\n---weird---\n") {
  15. return fmt.Errorf("Invalid prefix: %s", serialized)
  16. }
  17. parts := strings.Split(serialized, "\n---weird---\n")
  18. if len(parts) != 3 {
  19. return fmt.Errorf("Invalid string: %s", serialized)
  20. }
  21. e.Text = parts[1]
  22. e.EncodedWords = strings.Split(parts[2], " ")
  23. return nil
  24. }