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.

strings.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package irc
  2. import (
  3. "code.google.com/p/go.text/unicode/norm"
  4. "regexp"
  5. "strings"
  6. )
  7. var (
  8. // regexps
  9. ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`)
  10. NicknameExpr = regexp.MustCompile("^[\\pL\\pN\\pP\\pS]{1,32}$")
  11. )
  12. // Names are normalized and canonicalized to remove formatting marks
  13. // and simplify usage. They are things like hostnames and usermasks.
  14. type Name string
  15. func NewName(str string) Name {
  16. return Name(norm.NFKC.String(str))
  17. }
  18. func NewNames(strs []string) []Name {
  19. names := make([]Name, len(strs))
  20. for index, str := range strs {
  21. names[index] = NewName(str)
  22. }
  23. return names
  24. }
  25. // tests
  26. func (name Name) IsChannel() bool {
  27. return ChannelNameExpr.MatchString(name.String())
  28. }
  29. func (name Name) IsNickname() bool {
  30. return NicknameExpr.MatchString(name.String())
  31. }
  32. // conversions
  33. func (name Name) String() string {
  34. return string(name)
  35. }
  36. func (name Name) ToLower() Name {
  37. return Name(strings.ToLower(name.String()))
  38. }
  39. // It's safe to coerce a Name to Text. Name is a strict subset of Text.
  40. func (name Name) Text() Text {
  41. return Text(name)
  42. }
  43. // Text is PRIVMSG, NOTICE, or TOPIC data. It's canonicalized UTF8
  44. // data to simplify but keeps all formatting.
  45. type Text string
  46. func NewText(str string) Text {
  47. return Text(norm.NFC.String(str))
  48. }
  49. func (text Text) String() string {
  50. return string(text)
  51. }