Go package for working with the vCard 4.0 format
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

name.go 876B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package vcard
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. type Name struct {
  7. LastName string
  8. FirstName string
  9. AdditionalName string
  10. Prefix string
  11. Suffix string
  12. }
  13. func (n Name) ToVCard() string {
  14. return fmt.Sprintf(
  15. "%v;%v;%v;%v;%v",
  16. n.LastName,
  17. n.FirstName,
  18. n.AdditionalName,
  19. n.Prefix,
  20. n.Suffix,
  21. )
  22. }
  23. var nameRegexp *regexp.Regexp
  24. func init() {
  25. nameRegexp = regexp.MustCompile("(?P<LastName>.*);(?P<FirstName>.*);(?P<AdditionalName>.*);(?P<Prefix>.*);(?P<Suffix>.*)")
  26. }
  27. func nameFromString(s string) (*Name, error) {
  28. if !nameRegexp.MatchString(s) {
  29. return nil, ErrInvalidNameFormat
  30. }
  31. submatches := nameRegexp.FindStringSubmatch(s)
  32. name := Name{
  33. LastName: submatches[1],
  34. FirstName: submatches[2],
  35. AdditionalName: submatches[3],
  36. Prefix: submatches[4],
  37. Suffix: submatches[5],
  38. }
  39. return &name, nil
  40. }